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 "GrGLShaderBuilder.h" |
9 #include "gl/GrGLGpu.h" | |
10 #include "gl/builders/GrGLProgramBuilder.h" | 9 #include "gl/builders/GrGLProgramBuilder.h" |
11 #include "glsl/GrGLSLCaps.h" | 10 #include "glsl/GrGLSLCaps.h" |
12 #include "glsl/GrGLSLShaderVar.h" | 11 #include "glsl/GrGLSLShaderVar.h" |
13 #include "glsl/GrGLSLTextureSampler.h" | 12 #include "glsl/GrGLSLTextureSampler.h" |
14 | 13 |
15 namespace { | 14 static void map_swizzle(const char* swizzleMap, const char* swizzle, char* mangl
edSwizzle) { |
16 void append_texture_lookup(SkString* out, | 15 int i; |
17 GrGLGpu* gpu, | 16 for (i = 0; '\0' != swizzle[i]; ++i) { |
18 const char* samplerName, | 17 switch (swizzle[i]) { |
19 const char* coordName, | 18 case 'r': |
20 uint32_t configComponentMask, | 19 mangledSwizzle[i] = swizzleMap[0]; |
21 const char* swizzle, | 20 break; |
22 GrSLType varyingType = kVec2f_GrSLType) { | 21 case 'g': |
| 22 mangledSwizzle[i] = swizzleMap[1]; |
| 23 break; |
| 24 case 'b': |
| 25 mangledSwizzle[i] = swizzleMap[2]; |
| 26 break; |
| 27 case 'a': |
| 28 mangledSwizzle[i] = swizzleMap[3]; |
| 29 break; |
| 30 default: |
| 31 SkFAIL("Unsupported swizzle"); |
| 32 } |
| 33 } |
| 34 mangledSwizzle[i] ='\0'; |
| 35 } |
| 36 |
| 37 static void append_texture_lookup(SkString* out, |
| 38 const GrGLSLCaps* glslCaps, |
| 39 const char* samplerName, |
| 40 const char* coordName, |
| 41 GrPixelConfig config, |
| 42 const char* swizzle, |
| 43 GrSLType varyingType = kVec2f_GrSLType) { |
23 SkASSERT(coordName); | 44 SkASSERT(coordName); |
24 | 45 |
25 out->appendf("%s(%s, %s)", | 46 out->appendf("%s(%s, %s)", |
26 GrGLSLTexture2DFunctionName(varyingType, gpu->glslGeneration())
, | 47 GrGLSLTexture2DFunctionName(varyingType, glslCaps->generation()
), |
27 samplerName, | 48 samplerName, |
28 coordName); | 49 coordName); |
29 | 50 |
30 char mangledSwizzle[5]; | 51 char mangledSwizzle[5]; |
31 | 52 |
32 // The swizzling occurs using texture params instead of shader-mangling if A
RB_texture_swizzle | 53 // This refers to any swizzling we may need to get from some backend interna
l format to the |
33 // is available. | 54 // format used in GrPixelConfig. Some backends will automatically do the siz
zling for us. |
34 if (!gpu->glCaps().textureSwizzleSupport() && | 55 if (glslCaps->mustSwizzleInShader()) { |
35 (kA_GrColorComponentFlag == configComponentMask)) { | 56 const char* swizzleMap = glslCaps->getSwizzleMap(config); |
36 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; | 57 // if the map is simply 'rgba' then we don't need to do any manual swizz
ling to get us to |
37 int i; | 58 // a GrPixelConfig format. |
38 for (i = 0; '\0' != swizzle[i]; ++i) { | 59 if (memcmp(swizzleMap, "rgba", 4)) { |
39 mangledSwizzle[i] = alphaChar; | 60 // Manually 'swizzle' the swizzle using our mapping |
| 61 map_swizzle(swizzleMap, swizzle, mangledSwizzle); |
| 62 swizzle = mangledSwizzle; |
40 } | 63 } |
41 mangledSwizzle[i] ='\0'; | |
42 swizzle = mangledSwizzle; | |
43 } | 64 } |
| 65 |
44 // For shader prettiness we omit the swizzle rather than appending ".rgba". | 66 // For shader prettiness we omit the swizzle rather than appending ".rgba". |
45 if (memcmp(swizzle, "rgba", 4)) { | 67 if (memcmp(swizzle, "rgba", 4)) { |
46 out->appendf(".%s", swizzle); | 68 out->appendf(".%s", swizzle); |
47 } | 69 } |
48 } | 70 } |
49 } | |
50 | 71 |
51 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) | 72 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) |
52 : fProgramBuilder(program) | 73 : fProgramBuilder(program) |
53 , fInputs(GrGLProgramBuilder::kVarsPerBlock) | 74 , fInputs(GrGLProgramBuilder::kVarsPerBlock) |
54 , fOutputs(GrGLProgramBuilder::kVarsPerBlock) | 75 , fOutputs(GrGLProgramBuilder::kVarsPerBlock) |
55 , fFeaturesAddedMask(0) | 76 , fFeaturesAddedMask(0) |
56 , fCodeIndex(kCode) | 77 , fCodeIndex(kCode) |
57 , fFinalized(false) { | 78 , fFinalized(false) { |
58 // 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 |
59 for (int i = 0; i <= kCode; i++) { | 80 for (int i = 0; i <= kCode; i++) { |
(...skipping 30 matching lines...) Expand all Loading... |
90 this->functions().append(") {\n"); | 111 this->functions().append(") {\n"); |
91 this->functions().append(body); | 112 this->functions().append(body); |
92 this->functions().append("}\n\n"); | 113 this->functions().append("}\n\n"); |
93 } | 114 } |
94 | 115 |
95 void GrGLShaderBuilder::appendTextureLookup(SkString* out, | 116 void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
96 const GrGLSLTextureSampler& sampler, | 117 const GrGLSLTextureSampler& sampler, |
97 const char* coordName, | 118 const char* coordName, |
98 GrSLType varyingType) const { | 119 GrSLType varyingType) const { |
99 append_texture_lookup(out, | 120 append_texture_lookup(out, |
100 fProgramBuilder->gpu(), | 121 fProgramBuilder->glslCaps(), |
101 fProgramBuilder->getUniformCStr(sampler.fSamplerUnifor
m), | 122 fProgramBuilder->getUniformCStr(sampler.fSamplerUnifor
m), |
102 coordName, | 123 coordName, |
103 sampler.configComponentMask(), | 124 sampler.config(), |
104 sampler.swizzle(), | 125 sampler.swizzle(), |
105 varyingType); | 126 varyingType); |
106 } | 127 } |
107 | 128 |
108 void GrGLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, | 129 void GrGLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler, |
109 const char* coordName, | 130 const char* coordName, |
110 GrSLType varyingType) { | 131 GrSLType varyingType) { |
111 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); | 132 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType); |
112 } | 133 } |
113 | 134 |
(...skipping 13 matching lines...) Expand all Loading... |
127 } | 148 } |
128 } | 149 } |
129 | 150 |
130 void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { | 151 void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const { |
131 for (int i = 0; i < vars.count(); ++i) { | 152 for (int i = 0; i < vars.count(); ++i) { |
132 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); | 153 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
133 out->append(";\n"); | 154 out->append(";\n"); |
134 } | 155 } |
135 } | 156 } |
136 | 157 |
137 void GrGLShaderBuilder::appendTextureLookup(const char* samplerName, | |
138 const char* coordName, | |
139 uint32_t configComponentMask, | |
140 const char* swizzle) { | |
141 append_texture_lookup(&this->code(), | |
142 fProgramBuilder->gpu(), | |
143 samplerName, | |
144 coordName, | |
145 configComponentMask, | |
146 swizzle, | |
147 kVec2f_GrSLType); | |
148 } | |
149 | |
150 void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier
interface) { | 158 void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier
interface) { |
151 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration
|| | 159 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration
|| |
152 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); | 160 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs()); |
153 fLayoutParams[interface].push_back() = param; | 161 fLayoutParams[interface].push_back() = param; |
154 } | 162 } |
155 | 163 |
156 void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() { | 164 void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() { |
157 static const char* interfaceQualifierNames[] = { | 165 static const char* interfaceQualifierNames[] = { |
158 "out" | 166 "out" |
159 }; | 167 }; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 this->code().append("}"); | 200 this->code().append("}"); |
193 | 201 |
194 for (int i = 0; i <= fCodeIndex; i++) { | 202 for (int i = 0; i <= fCodeIndex; i++) { |
195 fCompilerStrings[i] = fShaderStrings[i].c_str(); | 203 fCompilerStrings[i] = fShaderStrings[i].c_str(); |
196 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); | 204 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
197 } | 205 } |
198 | 206 |
199 fFinalized = true; | 207 fFinalized = true; |
200 } | 208 } |
201 | 209 |
OLD | NEW |