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