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, GrGLShaderVar::kAttribute_TypeModifier, name); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool GrGLVertexShaderBuilder::addEffectAttribute(int attributeIndex, |
| 43 GrSLType type, |
| 44 const SkString& name) { |
| 45 if (!this->addAttribute(type, name.c_str())) { |
| 46 return false; |
| 47 } |
| 48 |
| 49 fEffectAttributes.push_back().set(attributeIndex, name); |
| 50 return true; |
| 51 } |
| 52 |
| 53 void GrGLVertexShaderBuilder::emitAttributes(const GrEffectStage& stage) { |
| 54 int numAttributes = stage.getVertexAttribIndexCount(); |
| 55 const int* attributeIndices = stage.getVertexAttribIndices(); |
| 56 for (int a = 0; a < numAttributes; ++a) { |
| 57 // TODO: Make addAttribute mangle the name. |
| 58 SkString attributeName("aAttr"); |
| 59 attributeName.appendS32(attributeIndices[a]); |
| 60 this->addEffectAttribute(attributeIndices[a], |
| 61 stage.getEffect()->vertexAttribType(a), |
| 62 attributeName); |
| 63 } |
| 64 } |
| 65 |
| 66 const SkString* GrGLVertexShaderBuilder::getEffectAttributeName(int attributeInd
ex) const { |
| 67 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 68 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 69 if (attrib->fIndex == attributeIndex) { |
| 70 return &attrib->fName; |
| 71 } |
| 72 } |
| 73 |
| 74 return NULL; |
| 75 } |
| 76 |
| 77 // private /////////////////////////////////////////////////////////////////////
//////////////////// |
| 78 void GrGLVertexShaderBuilder::addVarying(GrSLType type, const char* name, const
char** vsOutName) { |
| 79 fOutputs.push_back(); |
| 80 fOutputs.back().setType(type); |
| 81 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 82 fProgram->nameVariable(fOutputs.back().accessName(), 'v', name); |
| 83 |
| 84 if (vsOutName) { |
| 85 *vsOutName = fOutputs.back().getName().c_str(); |
| 86 } |
| 87 } |
| 88 |
| 89 |
| 90 void GrGLVertexShaderBuilder::bindProgramLocations(GrGLuint programId) { |
| 91 const GrGLProgramDesc::KeyHeader& header = fProgram->desc().getHeader(); |
| 92 GrGpuGL* gpu = fProgram->gpu(); |
| 93 |
| 94 // Bind the attrib locations to same values for all shaders |
| 95 SkASSERT(-1 != header.fPositionAttributeIndex); |
| 96 GL_CALL(BindAttribLocation(programId, |
| 97 header.fPositionAttributeIndex, |
| 98 fPositionVar->c_str())); |
| 99 if (-1 != header.fLocalCoordAttributeIndex) { |
| 100 GL_CALL(BindAttribLocation(programId, |
| 101 header.fLocalCoordAttributeIndex, |
| 102 fLocalCoordsVar->c_str())); |
| 103 } |
| 104 if (-1 != header.fColorAttributeIndex) { |
| 105 GL_CALL(BindAttribLocation(programId, |
| 106 header.fColorAttributeIndex, |
| 107 color_attribute_name())); |
| 108 } |
| 109 if (-1 != header.fCoverageAttributeIndex) { |
| 110 GL_CALL(BindAttribLocation(programId, |
| 111 header.fCoverageAttributeIndex, |
| 112 coverage_attribute_name())); |
| 113 } |
| 114 |
| 115 const AttributePair* attribEnd = fEffectAttributes.end(); |
| 116 for (const AttributePair* attrib = fEffectAttributes.begin(); attrib != attr
ibEnd; ++attrib) { |
| 117 GL_CALL(BindAttribLocation(programId, attrib->fIndex, attrib->fName.c_s
tr())); |
| 118 } |
| 119 } |
| 120 |
| 121 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, |
| 122 SkTDArray<GrGLuint>* shaderIds) const { |
| 123 GrGpuGL* gpu = fProgram->gpu(); |
| 124 const GrGLContext& glCtx = gpu->glContext(); |
| 125 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); |
| 126 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); |
| 127 fProgram->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility, &vertSh
aderSrc); |
| 128 fProgram->appendDecls(fInputs, &vertShaderSrc); |
| 129 fProgram->appendDecls(fOutputs, &vertShaderSrc); |
| 130 vertShaderSrc.append("void main() {\n"); |
| 131 vertShaderSrc.append(fCode); |
| 132 vertShaderSrc.append("}\n"); |
| 133 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, |
| 134 GR_GL_VERTEX_SHADER, vertShaderSrc); |
| 135 if (!vertShaderId) { |
| 136 return false; |
| 137 } |
| 138 *shaderIds->append() = vertShaderId; |
| 139 return true; |
| 140 } |
| 141 |
| 142 void GrGLVertexShaderBuilder::emitCodeAfterEffects() { |
| 143 const char* rtAdjustName; |
| 144 fProgram->fUniformHandles.fRTAdjustmentUni = |
| 145 fProgram->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 146 kVec4f_GrSLType, |
| 147 "rtAdjustment", |
| 148 &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, |
| 173 kMat33f_GrSLType, |
| 174 "ViewM", |
| 175 &viewMName); |
| 176 |
| 177 // Transform the position into Skia's device coords. |
| 178 this->codeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n", |
| 179 viewMName, fPositionVar->c_str()); |
| 180 |
| 181 // we output point size in the GS if present |
| 182 if (header.fEmitsPointSize |
| 183 #if GR_GL_EXPERIMENTAL_GS |
| 184 && !header.fExperimentalGS |
| 185 #endif |
| 186 ) { |
| 187 this->codeAppend("\tgl_PointSize = 1.0;\n"); |
| 188 } |
| 189 |
| 190 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 191 this->addAttribute(kVec4f_GrSLType, color_attribute_name()); |
| 192 const char *vsName, *fsName; |
| 193 fFullProgram->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 194 this->codeAppendf("\t%s = %s;\n", vsName, color_attribute_name()); |
| 195 *color = fsName; |
| 196 } |
| 197 |
| 198 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 199 this->addAttribute(kVec4f_GrSLType, coverage_attribute_name()); |
| 200 const char *vsName, *fsName; |
| 201 fFullProgram->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 202 this->codeAppendf("\t%s = %s;\n", vsName, coverage_attribute_name()); |
| 203 *coverage = fsName; |
| 204 } |
| 205 } |
OLD | NEW |