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 "GrGLVertexShaderBuilder.h" |
| 9 #include "GrGLProgramBuilder.h" |
| 10 #include "GrGLShaderStringBuilder.h" |
| 11 #include "../GrGpuGL.h" |
| 12 |
| 13 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) |
| 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) |
| 15 |
| 16 namespace { |
| 17 inline const char* color_attribute_name() { return "aColor"; } |
| 18 inline const char* coverage_attribute_name() { return "aCoverage"; } |
| 19 } |
| 20 |
| 21 GrGLVertexShaderBuilder::GrGLVertexShaderBuilder(GrGLFullProgramBuilder* program
) |
| 22 : INHERITED(program) |
| 23 , fPositionVar(NULL) |
| 24 , fLocalCoordsVar(NULL) { |
| 25 } |
| 26 bool GrGLVertexShaderBuilder::addAttribute(GrSLType type, const char* name) { |
| 27 for (int i = 0; i < fInputs.count(); ++i) { |
| 28 const GrGLShaderVar& attr = fInputs[i]; |
| 29 // if attribute already added, don't add it again |
| 30 if (attr.getName().equals(name)) { |
| 31 return false; |
| 32 } |
| 33 } |
| 34 fInputs.push_back().set(type, GrGLShaderVar::kAttribute_TypeModifier, name); |
| 35 return true; |
| 36 } |
| 37 |
| 38 bool GrGLVertexShaderBuilder::addEffectAttribute(int attributeIndex, |
| 39 GrSLType type, |
| 40 const SkString& name) { |
| 41 if (!this->addAttribute(type, name.c_str())) { |
| 42 return false; |
| 43 } |
| 44 |
| 45 fEffectAttributes.push_back().set(attributeIndex, name); |
| 46 return true; |
| 47 } |
| 48 |
| 49 void GrGLVertexShaderBuilder::emitAttributes(const GrEffectStage& stage) { |
| 50 int numAttributes = stage.getVertexAttribIndexCount(); |
| 51 const int* attributeIndices = stage.getVertexAttribIndices(); |
| 52 for (int a = 0; a < numAttributes; ++a) { |
| 53 // TODO: Make addAttribute mangle the name. |
| 54 SkString attributeName("aAttr"); |
| 55 attributeName.appendS32(attributeIndices[a]); |
| 56 this->addEffectAttribute(attributeIndices[a], |
| 57 stage.getEffect()->vertexAttribType(a), |
| 58 attributeName); |
| 59 } |
| 60 } |
| 61 |
| 62 const SkString* GrGLVertexShaderBuilder::getEffectAttributeName(int attributeInd
ex) const { |
| 63 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 64 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 65 if (attrib->fIndex == attributeIndex) { |
| 66 return &attrib->fName; |
| 67 } |
| 68 } |
| 69 |
| 70 return NULL; |
| 71 } |
| 72 |
| 73 void GrGLVertexShaderBuilder::addVarying(GrSLType type, const char* name, const
char** vsOutName) { |
| 74 fOutputs.push_back(); |
| 75 fOutputs.back().setType(type); |
| 76 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 77 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); |
| 78 |
| 79 if (vsOutName) { |
| 80 *vsOutName = fOutputs.back().getName().c_str(); |
| 81 } |
| 82 } |
| 83 |
| 84 |
| 85 void GrGLVertexShaderBuilder::bindProgramLocations(GrGLuint programId) { |
| 86 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader
(); |
| 87 GrGpuGL* gpu = fProgramBuilder->gpu(); |
| 88 |
| 89 // Bind the attrib locations to same values for all shaders |
| 90 SkASSERT(-1 != header.fPositionAttributeIndex); |
| 91 GL_CALL(BindAttribLocation(programId, |
| 92 header.fPositionAttributeIndex, |
| 93 fPositionVar->c_str())); |
| 94 if (-1 != header.fLocalCoordAttributeIndex) { |
| 95 GL_CALL(BindAttribLocation(programId, |
| 96 header.fLocalCoordAttributeIndex, |
| 97 fLocalCoordsVar->c_str())); |
| 98 } |
| 99 if (-1 != header.fColorAttributeIndex) { |
| 100 GL_CALL(BindAttribLocation(programId, |
| 101 header.fColorAttributeIndex, |
| 102 color_attribute_name())); |
| 103 } |
| 104 if (-1 != header.fCoverageAttributeIndex) { |
| 105 GL_CALL(BindAttribLocation(programId, |
| 106 header.fCoverageAttributeIndex, |
| 107 coverage_attribute_name())); |
| 108 } |
| 109 |
| 110 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 111 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 112 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); |
| 113 } |
| 114 } |
| 115 |
| 116 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, |
| 117 SkTDArray<GrGLuint>* shaderIds) const { |
| 118 GrGpuGL* gpu = fProgramBuilder->gpu(); |
| 119 const GrGLContext& glCtx = gpu->glContext(); |
| 120 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); |
| 121 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); |
| 122 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); |
| 123 fProgramBuilder->appendDecls(fInputs, &vertShaderSrc); |
| 124 fProgramBuilder->appendDecls(fOutputs, &vertShaderSrc); |
| 125 vertShaderSrc.append("void main() {\n"); |
| 126 vertShaderSrc.append(fCode); |
| 127 vertShaderSrc.append("}\n"); |
| 128 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, |
| 129 GR_GL_VERTEX_SHADER, vertShaderSrc); |
| 130 if (!vertShaderId) { |
| 131 return false; |
| 132 } |
| 133 *shaderIds->append() = vertShaderId; |
| 134 return true; |
| 135 } |
| 136 |
| 137 void GrGLVertexShaderBuilder::emitCodeAfterEffects() { |
| 138 const char* rtAdjustName; |
| 139 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = |
| 140 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 141 kVec4f_GrSLType, |
| 142 "rtAdjustment", |
| 143 &rtAdjustName); |
| 144 |
| 145 // Transform from Skia's device coords to GL's normalized device coords. |
| 146 this->codeAppendf( |
| 147 "\tgl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.
z);\n", |
| 148 rtAdjustName, rtAdjustName); |
| 149 } |
| 150 |
| 151 void GrGLVertexShaderBuilder::emitCodeBeforeEffects(GrGLSLExpr4* color, GrGLSLEx
pr4* coverage) { |
| 152 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader
(); |
| 153 |
| 154 fPositionVar = &fInputs.push_back(); |
| 155 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); |
| 156 if (-1 != header.fLocalCoordAttributeIndex) { |
| 157 fLocalCoordsVar = &fInputs.push_back(); |
| 158 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 159 GrGLShaderVar::kAttribute_TypeModifier, |
| 160 "aLocalCoords"); |
| 161 } else { |
| 162 fLocalCoordsVar = fPositionVar; |
| 163 } |
| 164 |
| 165 const char* viewMName; |
| 166 fProgramBuilder->fUniformHandles.fViewMatrixUni = |
| 167 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 168 kMat33f_GrSLType, |
| 169 "ViewM", |
| 170 &viewMName); |
| 171 |
| 172 // Transform the position into Skia's device coords. |
| 173 this->codeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n", |
| 174 viewMName, fPositionVar->c_str()); |
| 175 |
| 176 // we output point size in the GS if present |
| 177 if (header.fEmitsPointSize |
| 178 #if GR_GL_EXPERIMENTAL_GS |
| 179 && !header.fExperimentalGS |
| 180 #endif |
| 181 ) { |
| 182 this->codeAppend("\tgl_PointSize = 1.0;\n"); |
| 183 } |
| 184 |
| 185 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 186 this->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 187 const char *vsName, *fsName; |
| 188 fFullProgramBuilder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsNa
me); |
| 189 this->codeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 190 *color = fsName; |
| 191 } |
| 192 |
| 193 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 194 this->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 195 const char *vsName, *fsName; |
| 196 fFullProgramBuilder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &f
sName); |
| 197 this->codeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 198 *coverage = fsName; |
| 199 } |
| 200 } |
OLD | NEW |