OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrGLShaderBuilder.h" | 8 #include "glsl/GrGLSLShaderBuilder.h" |
9 #include "glsl/GrGLSLCaps.h" | 9 #include "glsl/GrGLSLCaps.h" |
10 #include "glsl/GrGLSLShaderVar.h" | 10 #include "glsl/GrGLSLShaderVar.h" |
11 #include "glsl/GrGLSLTextureSampler.h" | 11 #include "glsl/GrGLSLTextureSampler.h" |
12 #include "glsl/GrGLSLProgramBuilder.h" | 12 #include "glsl/GrGLSLProgramBuilder.h" |
13 | 13 |
14 static void map_swizzle(const char* swizzleMap, const char* swizzle, char* mangl
edSwizzle) { | 14 static void map_swizzle(const char* swizzleMap, const char* swizzle, char* mangl
edSwizzle) { |
15 int i; | 15 int i; |
16 for (i = 0; '\0' != swizzle[i]; ++i) { | 16 for (i = 0; '\0' != swizzle[i]; ++i) { |
17 switch (swizzle[i]) { | 17 switch (swizzle[i]) { |
18 case 'r': | 18 case 'r': |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 swizzle = mangledSwizzle; | 62 swizzle = mangledSwizzle; |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 // For shader prettiness we omit the swizzle rather than appending ".rgba". | 66 // For shader prettiness we omit the swizzle rather than appending ".rgba". |
67 if (memcmp(swizzle, "rgba", 4)) { | 67 if (memcmp(swizzle, "rgba", 4)) { |
68 out->appendf(".%s", swizzle); | 68 out->appendf(".%s", swizzle); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 GrGLShaderBuilder::GrGLShaderBuilder(GrGLSLProgramBuilder* program) | 72 GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program) |
73 : fProgramBuilder(program) | 73 : fProgramBuilder(program) |
74 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) | 74 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) |
75 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) | 75 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) |
76 , fFeaturesAddedMask(0) | 76 , fFeaturesAddedMask(0) |
77 , fCodeIndex(kCode) | 77 , fCodeIndex(kCode) |
78 , fFinalized(false) { | 78 , fFinalized(false) { |
79 // We push back some dummy pointers which will later become our header | 79 // We push back some dummy pointers which will later become our header |
80 for (int i = 0; i <= kCode; i++) { | 80 for (int i = 0; i <= kCode; i++) { |
81 fShaderStrings.push_back(); | 81 fShaderStrings.push_back(); |
82 fCompilerStrings.push_back(nullptr); | 82 fCompilerStrings.push_back(nullptr); |
83 fCompilerStringLengths.push_back(0); | 83 fCompilerStringLengths.push_back(0); |
84 } | 84 } |
85 | 85 |
86 this->main() = "void main() {"; | 86 this->main() = "void main() {"; |
87 } | 87 } |
88 | 88 |
89 void GrGLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { | 89 void GrGLSLShaderBuilder::declAppend(const GrGLSLShaderVar& var) { |
90 SkString tempDecl; | 90 SkString tempDecl; |
91 var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); | 91 var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl); |
92 this->codeAppendf("%s;", tempDecl.c_str()); | 92 this->codeAppendf("%s;", tempDecl.c_str()); |
93 } | 93 } |
94 | 94 |
95 void GrGLShaderBuilder::emitFunction(GrSLType returnType, | 95 void GrGLSLShaderBuilder::emitFunction(GrSLType returnType, |
96 const char* name, | 96 const char* name, |
97 int argCnt, | 97 int argCnt, |
98 const GrGLSLShaderVar* args, | 98 const GrGLSLShaderVar* args, |
99 const char* body, | 99 const char* body, |
100 SkString* outName) { | 100 SkString* outName) { |
101 this->functions().append(GrGLSLTypeString(returnType)); | 101 this->functions().append(GrGLSLTypeString(returnType)); |
102 fProgramBuilder->nameVariable(outName, '\0', name); | 102 fProgramBuilder->nameVariable(outName, '\0', name); |
103 this->functions().appendf(" %s", outName->c_str()); | 103 this->functions().appendf(" %s", outName->c_str()); |
104 this->functions().append("("); | 104 this->functions().append("("); |
105 for (int i = 0; i < argCnt; ++i) { | 105 for (int i = 0; i < argCnt; ++i) { |
106 args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); | 106 args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions()); |
107 if (i < argCnt - 1) { | 107 if (i < argCnt - 1) { |
108 this->functions().append(", "); | 108 this->functions().append(", "); |
109 } | 109 } |
110 } | 110 } |
111 this->functions().append(") {\n"); | 111 this->functions().append(") {\n"); |
112 this->functions().append(body); | 112 this->functions().append(body); |
113 this->functions().append("}\n\n"); | 113 this->functions().append("}\n\n"); |
114 } | 114 } |
115 | 115 |
116 void GrGLShaderBuilder::appendTextureLookup(SkString* out, | 116 void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
117 const GrGLSLTextureSampler& sampler, | 117 const GrGLSLTextureSampler& sample
r, |
118 const char* coordName, | 118 const char* coordName, |
119 GrSLType varyingType) const { | 119 GrSLType varyingType) const { |
120 append_texture_lookup(out, | 120 append_texture_lookup(out, |
121 fProgramBuilder->glslCaps(), | 121 fProgramBuilder->glslCaps(), |
122 fProgramBuilder->getUniformCStr(sampler.fSamplerUnifor
m), | 122 fProgramBuilder->getUniformCStr(sampler.fSamplerUnifor
m), |
123 coordName, | 123 coordName, |
124 sampler.config(), | 124 sampler.config(), |
125 sampler.swizzle(), | 125 sampler.swizzle(), |
126 varyingType); | 126 varyingType); |
127 } | 127 } |
128 | 128 |
129 void GrGLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, | 129 void GrGLSLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sample
r, |
130 const char* coordName, | 130 const char* coordName, |
131 GrSLType varyingType) { | 131 GrSLType varyingType) { |
132 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); | 132 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
133 } | 133 } |
134 | 134 |
135 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, | 135 void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
136 const GrGLSLTextureSample
r& sampler, | 136 const GrGLSLTextureSamp
ler& sampler, |
137 const char* coordName, | 137 const char* coordName, |
138 GrSLType varyingType) { | 138 GrSLType varyingType) { |
139 SkString lookup; | 139 SkString lookup; |
140 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); | 140 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
141 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); | 141 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
142 } | 142 } |
143 | 143 |
144 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionNam
e) { | 144 void GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionN
ame) { |
145 if (!(featureBit & fFeaturesAddedMask)) { | 145 if (!(featureBit & fFeaturesAddedMask)) { |
146 this->extensions().appendf("#extension %s: require\n", extensionName); | 146 this->extensions().appendf("#extension %s: require\n", extensionName); |
147 fFeaturesAddedMask |= featureBit; | 147 fFeaturesAddedMask |= featureBit; |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { | 151 void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const
{ |
152 for (int i = 0; i < vars.count(); ++i) { | 152 for (int i = 0; i < vars.count(); ++i) { |
153 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); | 153 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
154 out->append(";\n"); | 154 out->append(";\n"); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier
interface) { | 158 void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifi
er interface) { |
159 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration
|| | 159 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration
|| |
160 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); | 160 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
161 fLayoutParams[interface].push_back() = param; | 161 fLayoutParams[interface].push_back() = param; |
162 } | 162 } |
163 | 163 |
164 void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() { | 164 void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() { |
165 static const char* interfaceQualifierNames[] = { | 165 static const char* interfaceQualifierNames[] = { |
166 "out" | 166 "out" |
167 }; | 167 }; |
168 | 168 |
169 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { | 169 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) { |
170 const SkTArray<SkString>& params = fLayoutParams[interface]; | 170 const SkTArray<SkString>& params = fLayoutParams[interface]; |
171 if (params.empty()) { | 171 if (params.empty()) { |
172 continue; | 172 continue; |
173 } | 173 } |
174 this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); | 174 this->layoutQualifiers().appendf("layout(%s", params[0].c_str()); |
175 for (int i = 1; i < params.count(); ++i) { | 175 for (int i = 1; i < params.count(); ++i) { |
176 this->layoutQualifiers().appendf(", %s", params[i].c_str()); | 176 this->layoutQualifiers().appendf(", %s", params[i].c_str()); |
177 } | 177 } |
178 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[inte
rface]); | 178 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[inte
rface]); |
179 } | 179 } |
180 | 180 |
181 GR_STATIC_ASSERT(0 == GrGLShaderBuilder::kOut_InterfaceQualifier); | 181 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier); |
182 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQu
alifier + 1); | 182 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQu
alifier + 1); |
183 } | 183 } |
184 | 184 |
185 void GrGLShaderBuilder::finalize(uint32_t visibility) { | 185 void GrGLSLShaderBuilder::finalize(uint32_t visibility) { |
186 SkASSERT(!fFinalized); | 186 SkASSERT(!fFinalized); |
187 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); | 187 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); |
188 this->compileAndAppendLayoutQualifiers(); | 188 this->compileAndAppendLayoutQualifiers(); |
189 SkASSERT(visibility); | 189 SkASSERT(visibility); |
190 fProgramBuilder->appendUniformDecls((GrGLSLProgramBuilder::ShaderVisibility)
visibility, | 190 fProgramBuilder->appendUniformDecls((GrGLSLProgramBuilder::ShaderVisibility)
visibility, |
191 &this->uniforms()); | 191 &this->uniforms()); |
192 this->appendDecls(fInputs, &this->inputs()); | 192 this->appendDecls(fInputs, &this->inputs()); |
193 // We should not have any outputs in the fragment shader when using version
1.10 | 193 // We should not have any outputs in the fragment shader when using version
1.10 |
194 SkASSERT(GrGLSLProgramBuilder::kFragment_Visibility != visibility || | 194 SkASSERT(GrGLSLProgramBuilder::kFragment_Visibility != visibility || |
195 k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation()
|| | 195 k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation()
|| |
196 fOutputs.empty()); | 196 fOutputs.empty()); |
197 this->appendDecls(fOutputs, &this->outputs()); | 197 this->appendDecls(fOutputs, &this->outputs()); |
198 this->onFinalize(); | 198 this->onFinalize(); |
199 // append the 'footer' to code | 199 // append the 'footer' to code |
200 this->code().append("}"); | 200 this->code().append("}"); |
201 | 201 |
202 for (int i = 0; i <= fCodeIndex; i++) { | 202 for (int i = 0; i <= fCodeIndex; i++) { |
203 fCompilerStrings[i] = fShaderStrings[i].c_str(); | 203 fCompilerStrings[i] = fShaderStrings[i].c_str(); |
204 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); | 204 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
205 } | 205 } |
206 | 206 |
207 fFinalized = true; | 207 fFinalized = true; |
208 } | 208 } |
209 | 209 |
OLD | NEW |