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 "GrGLVertexShaderBuilder.h" | 8 #include "GrGLVertexShaderBuilder.h" |
9 #include "GrGLProgramBuilder.h" | 9 #include "GrGLProgramBuilder.h" |
10 #include "GrGLShaderStringBuilder.h" | 10 #include "GrGLShaderStringBuilder.h" |
11 #include "../GrGpuGL.h" | 11 #include "../GrGpuGL.h" |
12 | 12 |
13 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) | 13 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) |
14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) | 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) |
15 | 15 |
16 static const char* color_attribute_name() { return "inColor"; } | 16 static const char* color_attribute_name() { return "inColor"; } |
17 static const char* coverage_attribute_name() { return "inCoverage"; } | 17 static const char* coverage_attribute_name() { return "inCoverage"; } |
18 | 18 |
19 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) | 19 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) |
20 : INHERITED(program) | 20 : INHERITED(program) |
21 , fPositionVar(NULL) | 21 , fPositionVar(NULL) |
22 , fLocalCoordsVar(NULL) | 22 , fLocalCoordsVar(NULL) |
| 23 , fRtAdjustName(NULL) |
23 , fEffectAttribOffset(0) { | 24 , fEffectAttribOffset(0) { |
24 } | 25 } |
25 | 26 |
26 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { | 27 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { |
27 fOutputs.push_back(); | 28 fOutputs.push_back(); |
28 fOutputs.back().setType(v->fType); | 29 fOutputs.back().setType(v->fType); |
29 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 30 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
30 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); | 31 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); |
31 v->fVsOut = fOutputs.back().getName().c_str(); | 32 v->fVsOut = fOutputs.back().getName().c_str(); |
32 } | 33 } |
33 | 34 |
34 void GrGLVertexBuilder::setupLocalCoords() { | 35 void GrGLVertexBuilder::setupLocalCoords() { |
35 fPositionVar = &fInputs.push_back(); | 36 fPositionVar = &fInputs.push_back(); |
36 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
inPosition"); | 37 fPositionVar->set(kVec2f_GrSLType, |
| 38 GrGLShaderVar::kAttribute_TypeModifier, |
| 39 fProgramBuilder->fOptState.getGeometryProcessor()->inPosit
ion()); |
37 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { | 40 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { |
38 fLocalCoordsVar = &fInputs.push_back(); | 41 fLocalCoordsVar = &fInputs.push_back(); |
39 fLocalCoordsVar->set(kVec2f_GrSLType, | 42 fLocalCoordsVar->set(kVec2f_GrSLType, |
40 GrGLShaderVar::kAttribute_TypeModifier, | 43 GrGLShaderVar::kAttribute_TypeModifier, |
41 "inLocalCoords"); | 44 "inLocalCoords"); |
42 } else { | 45 } else { |
43 fLocalCoordsVar = fPositionVar; | 46 fLocalCoordsVar = fPositionVar; |
44 } | 47 } |
45 fEffectAttribOffset = fInputs.count(); | 48 fEffectAttribOffset = fInputs.count(); |
46 } | 49 } |
47 | 50 |
48 void GrGLVertexBuilder::transformGLToSkiaCoords() { | |
49 const char* viewMName; | |
50 fProgramBuilder->fUniformHandles.fViewMatrixUni = | |
51 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | |
52 kMat33f_GrSLType, | |
53 "ViewM", | |
54 &viewMName); | |
55 | |
56 // Transform the position into Skia's device coords. | |
57 this->codeAppendf("vec3 pos3 = %s * vec3(%s, 1);", viewMName, fPositionVar->
c_str()); | |
58 } | |
59 | |
60 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr4* out) { | 51 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr4* out) { |
61 GrGLVertToFrag v(kVec4f_GrSLType); | 52 GrGLVertToFrag v(kVec4f_GrSLType); |
62 fProgramBuilder->addVarying(inName, &v); | 53 fProgramBuilder->addVarying(inName, &v); |
63 SkString name(inName); | 54 SkString name(inName); |
64 name.prepend("in"); | 55 name.prepend("in"); |
65 this->addAttribute(GrShaderVar(name.c_str(), | 56 this->addAttribute(GrShaderVar(name.c_str(), |
66 kVec4f_GrSLType, | 57 kVec4f_GrSLType, |
67 GrShaderVar::kAttribute_TypeModifier)); | 58 GrShaderVar::kAttribute_TypeModifier)); |
68 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); | 59 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); |
69 *out = v.fsIn(); | 60 *out = v.fsIn(); |
70 fEffectAttribOffset++; | 61 fEffectAttribOffset++; |
71 } | 62 } |
72 | 63 |
73 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { | 64 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { |
74 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); | 65 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); |
75 int numAttributes = vars.count(); | 66 int numAttributes = vars.count(); |
76 for (int a = 0; a < numAttributes; ++a) { | 67 for (int a = 0; a < numAttributes; ++a) { |
77 this->addAttribute(vars[a]); | 68 this->addAttribute(vars[a]); |
78 } | 69 } |
79 } | 70 } |
80 | 71 |
81 void GrGLVertexBuilder::transformSkiaToGLCoords() { | 72 void GrGLVertexBuilder::transformPositionToDeviceSpace(const char* pos3) { |
82 const char* rtAdjustName; | 73 // setup RT Uniform |
83 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = | 74 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = |
84 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 75 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
85 kVec4f_GrSLType, | 76 kVec4f_GrSLType, |
86 "rtAdjustment", | 77 fProgramBuilder->rtAdjustment(), |
87 &rtAdjustName); | 78 &fRtAdjustName); |
| 79 // Wire transforms |
| 80 SkTArray<GrGLProgramBuilder::TransformVarying, true>& transVs = fProgramBuil
der->fCoordVaryings; |
| 81 int transformCount = transVs.count(); |
| 82 for (int i = 0; i < transformCount; i++) { |
| 83 const char* coords = transVs[i].fSourceCoords.c_str(); |
| 84 |
| 85 // varying = matrix * coords (logically) |
| 86 const GrGLVarying& v = transVs[i].fV; |
| 87 if (kVec2f_GrSLType == v.fType) { |
| 88 this->codeAppendf("%s = (%s * vec3(%s, 1)).xy;", v.fVsOut, transVs[i
].fUniName.c_str(), |
| 89 coords); |
| 90 } else { |
| 91 this->codeAppendf("%s = %s * vec3(%s, 1);", v.fVsOut, transVs[i].fUn
iName.c_str(), |
| 92 coords); |
| 93 } |
| 94 } |
88 | 95 |
89 // Transform from Skia's device coords to GL's normalized device coords. | 96 // Transform from Skia's device coords to GL's normalized device coords. |
90 this->codeAppendf("gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.z
w), 0, pos3.z);", | 97 this->codeAppendf("gl_Position = vec4(dot(%s.xz, %s.xy), dot(%s.yz, %s.zw),
0, %s.z);", |
91 rtAdjustName, rtAdjustName); | 98 pos3, fRtAdjustName, pos3, fRtAdjustName, pos3); |
92 } | 99 } |
93 | 100 |
94 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { | 101 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { |
95 // Bind the attrib locations to same values for all shaders | 102 // Bind the attrib locations to same values for all shaders |
96 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->header(); | 103 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->header(); |
97 SkASSERT(-1 != header.fPositionAttributeIndex); | 104 SkASSERT(-1 != header.fPositionAttributeIndex); |
98 GL_CALL(BindAttribLocation(programID, | 105 GL_CALL(BindAttribLocation(programID, |
99 header.fPositionAttributeIndex, | 106 header.fPositionAttributeIndex, |
100 fPositionVar->c_str())); | 107 fPositionVar->c_str())); |
101 if (-1 != header.fLocalCoordAttributeIndex) { | 108 if (-1 != header.fLocalCoordAttributeIndex) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 for (int i = 0; i < fInputs.count(); ++i) { | 171 for (int i = 0; i < fInputs.count(); ++i) { |
165 const GrGLShaderVar& attr = fInputs[i]; | 172 const GrGLShaderVar& attr = fInputs[i]; |
166 // if attribute already added, don't add it again | 173 // if attribute already added, don't add it again |
167 if (attr.getName().equals(var.getName())) { | 174 if (attr.getName().equals(var.getName())) { |
168 return false; | 175 return false; |
169 } | 176 } |
170 } | 177 } |
171 fInputs.push_back(var); | 178 fInputs.push_back(var); |
172 return true; | 179 return true; |
173 } | 180 } |
OLD | NEW |