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