1 /* 2 * Copyright 2020 Google LLC 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 8 #ifndef SKSL_DSL_VAR 9 #define SKSL_DSL_VAR 10 11 #include "include/sksl/DSLExpression.h" 12 #include "include/sksl/DSLModifiers.h" 13 #include "include/sksl/DSLType.h" 14 15 namespace SkSL { 16 17 class Variable; 18 enum class VariableStorage : int8_t; 19 20 namespace dsl { 21 22 class DSLVar { 23 public: 24 /** 25 * Creates an empty, unpopulated DSLVar. Can be replaced with a real DSLVar later via `swap`. 26 */ DSLVar()27 DSLVar() : fType(kVoid_Type), fDeclared(true) {} 28 29 /** 30 * Constructs a new variable with the specified type and name. The name is used (in mangled 31 * form) in the resulting shader code; it is not otherwise important. Since mangling prevents 32 * name conflicts and the variable's name is only important when debugging shaders, the name 33 * parameter is optional. 34 */ 35 DSLVar(DSLType type, const char* name = "var", DSLExpression initialValue = DSLExpression()); 36 37 DSLVar(DSLType type, DSLExpression initialValue); 38 39 DSLVar(DSLModifiers modifiers, DSLType type, const char* name = "var", 40 DSLExpression initialValue = DSLExpression()); 41 42 DSLVar(DSLModifiers modifiers, DSLType type, DSLExpression initialValue); 43 44 DSLVar(DSLVar&&) = default; 45 46 ~DSLVar(); 47 name()48 const char* name() const { 49 return fName; 50 } 51 52 void swap(DSLVar& other); 53 x()54 DSLExpression x() { 55 return DSLExpression(*this).x(); 56 } 57 y()58 DSLExpression y() { 59 return DSLExpression(*this).y(); 60 } 61 z()62 DSLExpression z() { 63 return DSLExpression(*this).z(); 64 } 65 w()66 DSLExpression w() { 67 return DSLExpression(*this).w(); 68 } 69 r()70 DSLExpression r() { 71 return DSLExpression(*this).r(); 72 } 73 g()74 DSLExpression g() { 75 return DSLExpression(*this).g(); 76 } 77 b()78 DSLExpression b() { 79 return DSLExpression(*this).b(); 80 } 81 a()82 DSLExpression a() { 83 return DSLExpression(*this).a(); 84 } 85 field(const char * name)86 DSLExpression field(const char* name) { 87 return DSLExpression(*this).field(name); 88 } 89 90 DSLPossibleExpression operator=(DSLVar& var) { 91 return this->operator=(DSLExpression(var)); 92 } 93 94 DSLPossibleExpression operator=(DSLExpression expr); 95 96 DSLPossibleExpression operator=(int expr) { 97 return this->operator=(DSLExpression(expr)); 98 } 99 100 DSLPossibleExpression operator=(float expr) { 101 return this->operator=(DSLExpression(expr)); 102 } 103 104 DSLPossibleExpression operator=(double expr) { 105 return this->operator=(DSLExpression(expr)); 106 } 107 108 DSLPossibleExpression operator[](DSLExpression&& index); 109 110 DSLPossibleExpression operator++() { 111 return ++DSLExpression(*this); 112 } 113 114 DSLPossibleExpression operator++(int) { 115 return DSLExpression(*this)++; 116 } 117 118 DSLPossibleExpression operator--() { 119 return --DSLExpression(*this); 120 } 121 122 DSLPossibleExpression operator--(int) { 123 return DSLExpression(*this)--; 124 } 125 126 private: 127 /** 128 * Constructs a reference to a variable that already exists in the symbol table. This is used 129 * internally to reference built-in vars. 130 */ 131 DSLVar(const char* name); 132 133 DSLModifiers fModifiers; 134 // We only need to keep track of the type here so that we can create the SkSL::Variable. For 135 // predefined variables this field is unnecessary, so we don't bother tracking it and just set 136 // it to kVoid; in other words, you shouldn't generally be relying on this field to be correct. 137 // If you need to determine the variable's type, look at DSLWriter::Var(...).type() instead. 138 DSLType fType; 139 int fUniformHandle = -1; 140 std::unique_ptr<SkSL::Statement> fDeclaration; 141 const SkSL::Variable* fVar = nullptr; 142 const char* fRawName = nullptr; // for error reporting 143 const char* fName = nullptr; 144 DSLExpression fInitialValue; 145 VariableStorage fStorage; 146 bool fDeclared = false; 147 148 friend DSLVar sk_SampleCoord(); 149 150 friend class DSLCore; 151 friend class DSLExpression; 152 friend class DSLFunction; 153 friend class DSLWriter; 154 }; 155 156 } // namespace dsl 157 158 } // namespace SkSL 159 160 161 #endif 162