Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/SkSLIRGenerator.h" |
| 11 | #include "src/sksl/ir/SkSLConstructor.h" |
| 12 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 13 | #include "src/sksl/ir/SkSLSetting.h" |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 14 | |
| 15 | namespace SkSL { |
| 16 | |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 17 | VariableReference::VariableReference(int offset, const Variable* variable, RefKind refKind) |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 18 | : INHERITED(offset, VariableReferenceData{variable, (int8_t)refKind}) { |
| 19 | SkASSERT(this->variable()); |
| 20 | this->variable()->referenceCreated(refKind); |
Brian Osman | 2683806 | 2020-09-30 13:03:33 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | VariableReference::~VariableReference() { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 24 | this->variable()->referenceDestroyed(this->refKind()); |
| 25 | } |
| 26 | |
| 27 | const Type& VariableReference::type() const { |
| 28 | return this->variableReferenceData().fVariable->type(); |
Brian Osman | 2683806 | 2020-09-30 13:03:33 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 31 | bool VariableReference::hasProperty(Property property) const { |
| 32 | switch (property) { |
| 33 | case Property::kSideEffects: return false; |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 34 | case Property::kContainsRTAdjust: return this->variable()->name() == "sk_RTAdjust"; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 35 | default: |
| 36 | SkASSERT(false); |
| 37 | return false; |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 41 | bool VariableReference::isConstantOrUniform() const { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 42 | return (this->variable()->modifiers().fFlags & Modifiers::kUniform_Flag) != 0; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | String VariableReference::description() const { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 46 | return this->variable()->name(); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void VariableReference::setRefKind(RefKind refKind) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 50 | this->variable()->referenceDestroyed(this->refKind()); |
| 51 | this->variableReferenceData().fRefKind = refKind; |
| 52 | this->variable()->referenceCreated(this->refKind()); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 55 | void VariableReference::setVariable(const Variable* variable) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 56 | this->variable()->referenceDestroyed(this->refKind()); |
| 57 | this->variableReferenceData().fVariable = variable; |
| 58 | this->variable()->referenceCreated(this->refKind()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 61 | std::unique_ptr<Expression> VariableReference::constantPropagate(const IRGenerator& irGenerator, |
| 62 | const DefinitionMap& definitions) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 63 | if (this->refKind() != kRead_RefKind) { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 64 | return nullptr; |
| 65 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 66 | const Expression* initialValue = this->variable()->initialValue(); |
| 67 | if ((this->variable()->modifiers().fFlags & Modifiers::kConst_Flag) && initialValue && |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 68 | initialValue->isCompileTimeConstant() && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 69 | this->type().typeKind() != Type::TypeKind::kArray) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 70 | return initialValue->clone(); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 71 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame^] | 72 | auto exprIter = definitions.find(this->variable()); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 73 | if (exprIter != definitions.end() && exprIter->second && |
Brian Osman | b6b9573 | 2020-06-30 11:44:27 -0400 | [diff] [blame] | 74 | (*exprIter->second)->isCompileTimeConstant()) { |
John Stiles | 8614e49 | 2020-09-21 09:28:48 -0400 | [diff] [blame] | 75 | return (*exprIter->second)->clone(); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 76 | } |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 80 | } // namespace SkSL |