1 /* 2 * Copyright 2016 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 8 #ifndef GrShaderVar_DEFINED 9 #define GrShaderVar_DEFINED 10 11 #include "include/core/SkString.h" 12 #include "include/private/gpu/ganesh/GrTypesPriv.h" 13 #include "src/core/SkSLTypeShared.h" 14 15 struct GrShaderCaps; 16 17 /** 18 * Represents a variable in a shader 19 */ 20 class GrShaderVar { 21 public: 22 enum class TypeModifier { 23 None, 24 Out, 25 In, 26 InOut, 27 Uniform, 28 }; 29 30 /** Values for array count that have special meaning. We allow 1-sized arrays. */ 31 enum { 32 kNonArray = 0, // not an array 33 }; 34 35 /** Defaults to a void with no type modifier or layout qualifier. */ GrShaderVar()36 GrShaderVar() 37 : fType(SkSLType::kVoid) 38 , fTypeModifier(TypeModifier::None) 39 , fCount(kNonArray) {} 40 41 GrShaderVar(SkString name, SkSLType type, int arrayCount = kNonArray) fType(type)42 : fType(type) 43 , fTypeModifier(TypeModifier::None) 44 , fCount(arrayCount) 45 , fName(std::move(name)) {} 46 GrShaderVar(const char* name, SkSLType type, int arrayCount = kNonArray) GrShaderVar(SkString (name),type,arrayCount)47 : GrShaderVar(SkString(name), type, arrayCount) {} 48 GrShaderVar(SkString name,SkSLType type,TypeModifier typeModifier)49 GrShaderVar(SkString name, SkSLType type, TypeModifier typeModifier) 50 : fType(type) 51 , fTypeModifier(typeModifier) 52 , fCount(kNonArray) 53 , fName(std::move(name)) {} GrShaderVar(const char * name,SkSLType type,TypeModifier typeModifier)54 GrShaderVar(const char* name, SkSLType type, TypeModifier typeModifier) 55 : GrShaderVar(SkString(name), type, typeModifier) {} 56 GrShaderVar(SkString name,SkSLType type,TypeModifier typeModifier,int arrayCount)57 GrShaderVar(SkString name, SkSLType type, TypeModifier typeModifier, int arrayCount) 58 : fType(type) 59 , fTypeModifier(typeModifier) 60 , fCount(arrayCount) 61 , fName(std::move(name)) {} 62 GrShaderVar(SkString name,SkSLType type,TypeModifier typeModifier,int arrayCount,SkString layoutQualifier,SkString extraModifier)63 GrShaderVar(SkString name, SkSLType type, TypeModifier typeModifier, int arrayCount, 64 SkString layoutQualifier, SkString extraModifier) 65 : fType(type) 66 , fTypeModifier(typeModifier) 67 , fCount(arrayCount) 68 , fName(std::move(name)) 69 , fLayoutQualifier(std::move(layoutQualifier)) 70 , fExtraModifiers(std::move(extraModifier)) {} 71 72 GrShaderVar(const GrShaderVar&) = default; 73 GrShaderVar& operator=(const GrShaderVar&) = default; 74 GrShaderVar(GrShaderVar&&) = default; 75 GrShaderVar& operator=(GrShaderVar&&) = default; 76 77 /** Sets as a non-array. */ set(SkSLType type,const char * name)78 void set(SkSLType type, const char* name) { 79 SkASSERT(SkSLType::kVoid != type); 80 fType = type; 81 fName = name; 82 } 83 84 /** Is the var an array. */ isArray()85 bool isArray() const { return kNonArray != fCount; } 86 87 /** Get the array length. */ getArrayCount()88 int getArrayCount() const { return fCount; } 89 90 /** Get the name. */ getName()91 const SkString& getName() const { return fName; } 92 93 /** Shortcut for this->getName().c_str(); */ c_str()94 const char* c_str() const { return this->getName().c_str(); } 95 96 /** Get the type. */ getType()97 SkSLType getType() const { return fType; } 98 getTypeModifier()99 TypeModifier getTypeModifier() const { return fTypeModifier; } setTypeModifier(TypeModifier type)100 void setTypeModifier(TypeModifier type) { fTypeModifier = type; } 101 102 /** Appends to the layout qualifier. */ addLayoutQualifier(const char * layoutQualifier)103 void addLayoutQualifier(const char* layoutQualifier) { 104 if (!layoutQualifier || !strlen(layoutQualifier)) { 105 return; 106 } 107 if (fLayoutQualifier.isEmpty()) { 108 fLayoutQualifier = layoutQualifier; 109 } else { 110 fLayoutQualifier.appendf(", %s", layoutQualifier); 111 } 112 } 113 114 /** Appends to the modifiers. */ addModifier(const char * modifier)115 void addModifier(const char* modifier) { 116 if (!modifier || !strlen(modifier)) { 117 return; 118 } 119 if (fExtraModifiers.isEmpty()) { 120 fExtraModifiers = modifier; 121 } else { 122 fExtraModifiers.appendf(" %s", modifier); 123 } 124 } 125 126 /** Write a declaration of this variable to out. */ 127 void appendDecl(const GrShaderCaps*, SkString* out) const; 128 129 private: 130 SkSLType fType; 131 TypeModifier fTypeModifier; 132 int fCount; 133 134 SkString fName; 135 SkString fLayoutQualifier; 136 SkString fExtraModifiers; 137 }; 138 139 #endif 140