OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 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 #include "GrGLShaderBuilder.h" |
| 9 #include "GrGLProgramBuilder.h" |
| 10 #include "../GrGpuGL.h" |
| 11 #include "../GrGLShaderVar.h" |
| 12 |
| 13 ////////////////////////////////////////////////////////////////////////////////
//////////////////// |
| 14 // local ///////////////////////////////////////////////////////////////////////
//////////////////// |
| 15 namespace { |
| 16 inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen)
{ |
| 17 if (kVec2f_GrSLType == type) { |
| 18 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
| 19 } else { |
| 20 SkASSERT(kVec3f_GrSLType == type); |
| 21 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj
"; |
| 22 } |
| 23 } |
| 24 void append_texture_lookup(SkString* out, |
| 25 GrGpuGL* gpu, |
| 26 const char* samplerName, |
| 27 const char* coordName, |
| 28 uint32_t configComponentMask, |
| 29 const char* swizzle, |
| 30 GrSLType varyingType = kVec2f_GrSLType) { |
| 31 SkASSERT(NULL != coordName); |
| 32 |
| 33 out->appendf("%s(%s, %s)", |
| 34 sample_function_name(varyingType, gpu->glslGeneration()), |
| 35 samplerName, |
| 36 coordName); |
| 37 |
| 38 char mangledSwizzle[5]; |
| 39 |
| 40 // The swizzling occurs using texture params instead of shader-mangling if A
RB_texture_swizzle |
| 41 // is available. |
| 42 if (!gpu->glCaps().textureSwizzleSupport() && |
| 43 (kA_GrColorComponentFlag == configComponentMask)) { |
| 44 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a'; |
| 45 int i; |
| 46 for (i = 0; '\0' != swizzle[i]; ++i) { |
| 47 mangledSwizzle[i] = alphaChar; |
| 48 } |
| 49 mangledSwizzle[i] ='\0'; |
| 50 swizzle = mangledSwizzle; |
| 51 } |
| 52 // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 53 if (memcmp(swizzle, "rgba", 4)) { |
| 54 out->appendf(".%s", swizzle); |
| 55 } |
| 56 } |
| 57 static const int kVarsPerBlock = 8; |
| 58 } |
| 59 |
| 60 ////////////////////////////////////////////////////////////////////////////////
//////////////////// |
| 61 // GrGLShaderBuilder public ////////////////////////////////////////////////////
//////////////////// |
| 62 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program) |
| 63 : fProgram(program) |
| 64 , fInputs(kVarsPerBlock) |
| 65 , fOutputs(kVarsPerBlock) |
| 66 , fFeaturesAddedMask(0) { |
| 67 } |
| 68 |
| 69 void GrGLShaderBuilder::emitFunction(GrSLType returnType, |
| 70 const char* name, |
| 71 int argCnt, |
| 72 const GrGLShaderVar* args, |
| 73 const char* body, |
| 74 SkString* outName) { |
| 75 fFunctions.append(GrGLSLTypeString(returnType)); |
| 76 fProgram->nameVariable(outName, '\0', name); |
| 77 fFunctions.appendf(" %s", outName->c_str()); |
| 78 fFunctions.append("("); |
| 79 const GrGLContextInfo& ctxInfo = fProgram->gpu()->ctxInfo(); |
| 80 for (int i = 0; i < argCnt; ++i) { |
| 81 args[i].appendDecl(ctxInfo, &fFunctions); |
| 82 if (i < argCnt - 1) { |
| 83 fFunctions.append(", "); |
| 84 } |
| 85 } |
| 86 fFunctions.append(") {\n"); |
| 87 fFunctions.append(body); |
| 88 fFunctions.append("}\n\n"); |
| 89 } |
| 90 |
| 91 void GrGLShaderBuilder::appendTextureLookup(SkString* out, |
| 92 const TextureSampler& sampler, |
| 93 const char* coordName, |
| 94 GrSLType varyingType) const { |
| 95 append_texture_lookup(out, |
| 96 fProgram->gpu(), |
| 97 fProgram->getUniformCStr(sampler.samplerUniform()), |
| 98 coordName, |
| 99 sampler.configComponentMask(), |
| 100 sampler.swizzle(), |
| 101 varyingType); |
| 102 } |
| 103 |
| 104 void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler, |
| 105 const char* coordName, |
| 106 GrSLType varyingType) { |
| 107 this->appendTextureLookup(&fCode, sampler, coordName, varyingType); |
| 108 } |
| 109 |
| 110 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
| 111 const TextureSampler& sam
pler, |
| 112 const char* coordName, |
| 113 GrSLType varyingType) { |
| 114 SkString lookup; |
| 115 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 116 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
| 117 } |
| 118 |
| 119 |
| 120 const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, cons
t GrGLCaps& caps) { |
| 121 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 122 if (caps.textureRedSupport()) { |
| 123 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RE
D, GR_GL_RED }; |
| 124 return gRedSmear; |
| 125 } else { |
| 126 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 127 GR_GL_ALPHA, GR_GL_ALPHA }; |
| 128 return gAlphaSmear; |
| 129 } |
| 130 } else { |
| 131 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE
, GR_GL_ALPHA }; |
| 132 return gStraight; |
| 133 } |
| 134 } |
| 135 |
| 136 // GrGLShaderBuilder private ///////////////////////////////////////////////////
/////////////////// |
| 137 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionNam
e) { |
| 138 if (!(featureBit & fFeaturesAddedMask)) { |
| 139 fExtensions.appendf("#extension %s: require\n", extensionName); |
| 140 fFeaturesAddedMask |= featureBit; |
| 141 } |
| 142 } |
| 143 |
| 144 void GrGLShaderBuilder::appendTextureLookup(const char* samplerName, |
| 145 const char* coordName, |
| 146 uint32_t configComponentMask, |
| 147 const char* swizzle) { |
| 148 append_texture_lookup(&fCode, |
| 149 fProgram->gpu(), |
| 150 samplerName, |
| 151 coordName, |
| 152 configComponentMask, |
| 153 swizzle, |
| 154 kVec2f_GrSLType); |
| 155 } |
| 156 |
| 157 ////////////////////////////////////////////////////////////////////////////////
//////////////////// |
| 158 // GrGLFullShaderBuilder public ////////////////////////////////////////////////
//////////////////// |
| 159 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGLFullProgramBuilder* program) |
| 160 : INHERITED(program) |
| 161 , fFullProgram(program) {} |
OLD | NEW |