1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include "src/sksl/SkSLConstantFolder.h" 11cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLConstructor.h" 12cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLType.h" 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_cinamespace SkSL { 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_cistd::unique_ptr<Expression> ConstructorDiagonalMatrix::Make(const Context& context, 17cb93a386Sopenharmony_ci int line, 18cb93a386Sopenharmony_ci const Type& type, 19cb93a386Sopenharmony_ci std::unique_ptr<Expression> arg) { 20cb93a386Sopenharmony_ci SkASSERT(type.isMatrix()); 21cb93a386Sopenharmony_ci SkASSERT(type.isAllowedInES2(context)); 22cb93a386Sopenharmony_ci SkASSERT(arg->type().isScalar()); 23cb93a386Sopenharmony_ci SkASSERT(arg->type() == type.componentType()); 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci // Look up the value of constant variables. This allows constant-expressions like `mat4(five)` 26cb93a386Sopenharmony_ci // to be replaced with `mat4(5.0)`. 27cb93a386Sopenharmony_ci arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg)); 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci return std::make_unique<ConstructorDiagonalMatrix>(line, type, std::move(arg)); 30cb93a386Sopenharmony_ci} 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ciskstd::optional<double> ConstructorDiagonalMatrix::getConstantValue(int n) const { 33cb93a386Sopenharmony_ci int rows = this->type().rows(); 34cb93a386Sopenharmony_ci int row = n % rows; 35cb93a386Sopenharmony_ci int col = n / rows; 36cb93a386Sopenharmony_ci 37cb93a386Sopenharmony_ci SkASSERT(col >= 0); 38cb93a386Sopenharmony_ci SkASSERT(row >= 0); 39cb93a386Sopenharmony_ci SkASSERT(col < this->type().columns()); 40cb93a386Sopenharmony_ci SkASSERT(row < this->type().rows()); 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci return (col == row) ? this->argument()->getConstantValue(0) : 0.0; 43cb93a386Sopenharmony_ci} 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci} // namespace SkSL 46