Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(681)

Side by Side Diff: src/gpu/gl/GrGLShaderBuilder.cpp

Issue 306663003: Move all code generation from GrGLProgram to GrGLShaderBuilder (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: line wraps Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/gpu/gl/GrGLProgram.cpp ('K') | « src/gpu/gl/GrGLShaderBuilder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 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 "gl/GrGLShaderBuilder.h" 8 #include "gl/GrGLShaderBuilder.h"
9 #include "gl/GrGLProgram.h" 9 #include "gl/GrGLProgram.h"
10 #include "gl/GrGLUniformHandle.h" 10 #include "gl/GrGLUniformHandle.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 out->appendf(".%s", swizzle); 82 out->appendf(".%s", swizzle);
83 } 83 }
84 } 84 }
85 85
86 } 86 }
87 87
88 static const char kDstCopyColorName[] = "_dstColor"; 88 static const char kDstCopyColorName[] = "_dstColor";
89 89
90 /////////////////////////////////////////////////////////////////////////////// 90 ///////////////////////////////////////////////////////////////////////////////
91 91
92 bool GrGLShaderBuilder::GenProgram(GrGpuGL* gpu,
93 GrGLUniformManager& uman,
94 const GrGLProgramDesc& desc,
95 const GrEffectStage* inColorStages[],
96 const GrEffectStage* inCoverageStages[],
97 GenProgramOutput* output) {
98 if (desc.getHeader().fHasVertexCode ||!gpu->shouldUseFixedFunctionTexturing( )) {
99 GrGLFullShaderBuilder fullBuilder(gpu, uman, desc);
100 if (fullBuilder.genProgram(inColorStages, inCoverageStages, output)) {
101 output->fHasVS = true;
102 output->fUniformHandles.fViewMatrixUni = fullBuilder.getViewMatrixUn iform();
103 output->fUniformHandles.fRTAdjustmentUni = fullBuilder.getRTAdjustme ntVecUniform();
104 return true;
105 }
106 } else {
107 GrGLFragmentOnlyShaderBuilder fragmentOnlyBuilder(gpu, uman, desc);
108 if (fragmentOnlyBuilder.genProgram(inColorStages, inCoverageStages, outp ut)) {
109 output->fHasVS = false;
110 output->fNumTexCoordSets = fragmentOnlyBuilder.getNumTexCoordSets();
111 return true;
112 }
113 }
114 return false;
115 }
116
117 bool GrGLShaderBuilder::genProgram(const GrEffectStage* colorStages[],
118 const GrEffectStage* coverageStages[],
119 GenProgramOutput* output) {
120 const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader();
121
122 // incoming color to current stage being processed.
123 GrGLSLExpr4 inColor = this->getInputColor();
124
125 output->fColorEffects =
126 this->createAndEmitEffects(colorStages,
127 this->desc().getEffectKeys(),
128 this->desc().numColorEffects(),
129 &inColor);
130
131 ///////////////////////////////////////////////////////////////////////////
132 // compute the partial coverage
133 GrGLSLExpr4 inCoverage = this->getInputCoverage();
134
135 output->fCoverageEffects =
136 this->createAndEmitEffects(coverageStages,
137 this->desc().getEffectKeys() + this->desc(). numColorEffects(),
138 this->desc().numCoverageEffects(),
139 &inCoverage);
140
141 if (GrGLProgramDesc::CoverageOutputUsesSecondaryOutput(header.fCoverageOutpu t)) {
142 const char* secondaryOutputName = this->enableSecondaryOutput();
143
144 // default coeff to ones for kCoverage_DualSrcOutput
145 GrGLSLExpr4 coeff(1);
146 if (GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput == header.fCov erageOutput) {
147 // Get (1-A) into coeff
148 coeff = GrGLSLExpr4::VectorCast(GrGLSLExpr1(1) - inColor.a());
149 } else if (GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput ==
150 header.fCoverageOutput){
151 // Get (1-RGBA) into coeff
152 coeff = GrGLSLExpr4(1) - inColor;
153 }
154 // Get coeff * coverage into modulate and then write that to the dual so urce output.
155 this->fsCodeAppendf("\t%s = %s;\n", secondaryOutputName, (coeff * inCove rage).c_str());
156 }
157
158 ///////////////////////////////////////////////////////////////////////////
159 // combine color and coverage as frag color
160
161 // Get "color * coverage" into fragColor
162 GrGLSLExpr4 fragColor = inColor * inCoverage;
163 // Now tack on "+(1-coverage)dst onto the frag color if we were asked to do so.
164 if (GrGLProgramDesc::kCombineWithDst_CoverageOutput == header.fCoverageOutpu t) {
165 GrGLSLExpr4 dstCoeff = GrGLSLExpr4(1) - inCoverage;
166
167 GrGLSLExpr4 dstContribution = dstCoeff * GrGLSLExpr4(this->dstColor());
168
169 fragColor = fragColor + dstContribution;
170 }
171 this->fsCodeAppendf("\t%s = %s;\n", this->getColorOutputName(), fragColor.c_ str());
172
173 if (!this->finish(&output->fProgramID)) {
174 return false;
175 }
176
177 output->fUniformHandles.fRTHeightUni = this->getRTHeightUniform();
178 output->fUniformHandles.fDstCopyTopLeftUni = this->getDstCopyTopLeftUniform( );
179 output->fUniformHandles.fDstCopyScaleUni = this->getDstCopyScaleUniform();
180 output->fUniformHandles.fColorUni = this->getColorUniform();
181 output->fUniformHandles.fCoverageUni = this->getCoverageUniform();
182 output->fUniformHandles.fDstCopySamplerUni = this->getDstCopySamplerUniform( );
183
184 return true;
185 }
186
187 //////////////////////////////////////////////////////////////////////////////
188
92 GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu, 189 GrGLShaderBuilder::GrGLShaderBuilder(GrGpuGL* gpu,
93 GrGLUniformManager& uniformManager, 190 GrGLUniformManager& uniformManager,
94 const GrGLProgramDesc& desc) 191 const GrGLProgramDesc& desc)
95 : fGpu(gpu) 192 : fDesc(desc)
193 , fGpu(gpu)
96 , fUniformManager(uniformManager) 194 , fUniformManager(uniformManager)
97 , fFSFeaturesAddedMask(0) 195 , fFSFeaturesAddedMask(0)
98 , fFSInputs(kVarsPerBlock) 196 , fFSInputs(kVarsPerBlock)
99 , fFSOutputs(kMaxFSOutputs) 197 , fFSOutputs(kMaxFSOutputs)
100 , fUniforms(kVarsPerBlock) 198 , fUniforms(kVarsPerBlock)
101 , fSetupFragPosition(false) 199 , fSetupFragPosition(false)
102 , fHasCustomColorOutput(false) 200 , fHasCustomColorOutput(false)
103 , fHasSecondaryOutput(false) 201 , fHasSecondaryOutput(false)
104 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr agPosKey) { 202 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == desc.getHeader().fFr agPosKey) {
105 203
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const { 826 const GrGLContextInfo& GrGLShaderBuilder::ctxInfo() const {
729 return fGpu->ctxInfo(); 827 return fGpu->ctxInfo();
730 } 828 }
731 829
732 //////////////////////////////////////////////////////////////////////////////// 830 ////////////////////////////////////////////////////////////////////////////////
733 831
734 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, 832 GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu,
735 GrGLUniformManager& uniformManager, 833 GrGLUniformManager& uniformManager,
736 const GrGLProgramDesc& desc) 834 const GrGLProgramDesc& desc)
737 : INHERITED(gpu, uniformManager, desc) 835 : INHERITED(gpu, uniformManager, desc)
738 , fDesc(desc)
739 , fVSAttrs(kVarsPerBlock) 836 , fVSAttrs(kVarsPerBlock)
740 , fVSOutputs(kVarsPerBlock) 837 , fVSOutputs(kVarsPerBlock)
741 , fGSInputs(kVarsPerBlock) 838 , fGSInputs(kVarsPerBlock)
742 , fGSOutputs(kVarsPerBlock) { 839 , fGSOutputs(kVarsPerBlock) {
743 840
744 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); 841 const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader();
745 842
746 fPositionVar = &fVSAttrs.push_back(); 843 fPositionVar = &fVSAttrs.push_back();
747 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " aPosition"); 844 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " aPosition");
748 if (-1 != header.fLocalCoordAttributeIndex) { 845 if (-1 != header.fLocalCoordAttributeIndex) {
749 fLocalCoordsVar = &fVSAttrs.push_back(); 846 fLocalCoordsVar = &fVSAttrs.push_back();
750 fLocalCoordsVar->set(kVec2f_GrSLType, 847 fLocalCoordsVar->set(kVec2f_GrSLType,
751 GrGLShaderVar::kAttribute_TypeModifier, 848 GrGLShaderVar::kAttribute_TypeModifier,
752 "aLocalCoords"); 849 "aLocalCoords");
753 } else { 850 } else {
754 fLocalCoordsVar = fPositionVar; 851 fLocalCoordsVar = fPositionVar;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 fVSOutputs.back().setType(type); 927 fVSOutputs.back().setType(type);
831 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); 928 fVSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier);
832 this->nameVariable(fVSOutputs.back().accessName(), 'v', name); 929 this->nameVariable(fVSOutputs.back().accessName(), 'v', name);
833 930
834 if (vsOutName) { 931 if (vsOutName) {
835 *vsOutName = fVSOutputs.back().getName().c_str(); 932 *vsOutName = fVSOutputs.back().getName().c_str();
836 } 933 }
837 // input to FS comes either from VS or GS 934 // input to FS comes either from VS or GS
838 const SkString* fsName; 935 const SkString* fsName;
839 #if GR_GL_EXPERIMENTAL_GS 936 #if GR_GL_EXPERIMENTAL_GS
840 if (fDesc.getHeader().fExperimentalGS) { 937 if (this->desc().getHeader().fExperimentalGS) {
841 // if we have a GS take each varying in as an array 938 // if we have a GS take each varying in as an array
842 // and output as non-array. 939 // and output as non-array.
843 fGSInputs.push_back(); 940 fGSInputs.push_back();
844 fGSInputs.back().setType(type); 941 fGSInputs.back().setType(type);
845 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier) ; 942 fGSInputs.back().setTypeModifier(GrGLShaderVar::kVaryingIn_TypeModifier) ;
846 fGSInputs.back().setUnsizedArray(); 943 fGSInputs.back().setUnsizedArray();
847 *fGSInputs.back().accessName() = fVSOutputs.back().getName(); 944 *fGSInputs.back().accessName() = fVSOutputs.back().getName();
848 fGSOutputs.push_back(); 945 fGSOutputs.push_back();
849 fGSOutputs.back().setType(type); 946 fGSOutputs.back().setType(type);
850 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie r); 947 fGSOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifie r);
(...skipping 29 matching lines...) Expand all
880 977
881 GrGLVertexProgramEffectsBuilder programEffectsBuilder(this, effectCnt); 978 GrGLVertexProgramEffectsBuilder programEffectsBuilder(this, effectCnt);
882 this->INHERITED::createAndEmitEffects(&programEffectsBuilder, 979 this->INHERITED::createAndEmitEffects(&programEffectsBuilder,
883 effectStages, 980 effectStages,
884 effectKeys, 981 effectKeys,
885 effectCnt, 982 effectCnt,
886 inOutFSColor); 983 inOutFSColor);
887 return programEffectsBuilder.finish(); 984 return programEffectsBuilder.finish();
888 } 985 }
889 986
890 bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId, SkTDArra y<GrGLuint>* shaderIds) const { 987 bool GrGLFullShaderBuilder::compileAndAttachShaders(GrGLuint programId,
988 SkTDArray<GrGLuint>* shaderI ds) const {
891 const GrGLContext& glCtx = this->gpu()->glContext(); 989 const GrGLContext& glCtx = this->gpu()->glContext();
892 SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); 990 SkString vertShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo()));
893 this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc); 991 this->appendUniformDecls(kVertex_Visibility, &vertShaderSrc);
894 this->appendDecls(fVSAttrs, &vertShaderSrc); 992 this->appendDecls(fVSAttrs, &vertShaderSrc);
895 this->appendDecls(fVSOutputs, &vertShaderSrc); 993 this->appendDecls(fVSOutputs, &vertShaderSrc);
896 vertShaderSrc.append("void main() {\n"); 994 vertShaderSrc.append("void main() {\n");
897 vertShaderSrc.append(fVSCode); 995 vertShaderSrc.append(fVSCode);
898 vertShaderSrc.append("}\n"); 996 vertShaderSrc.append("}\n");
899 GrGLuint vertShaderId = attach_shader(glCtx, programId, GR_GL_VERTEX_SHADER, vertShaderSrc); 997 GrGLuint vertShaderId = attach_shader(glCtx, programId, GR_GL_VERTEX_SHADER, vertShaderSrc);
900 if (!vertShaderId) { 998 if (!vertShaderId) {
901 return false; 999 return false;
902 } 1000 }
903 *shaderIds->append() = vertShaderId; 1001 *shaderIds->append() = vertShaderId;
904 1002
905 #if GR_GL_EXPERIMENTAL_GS 1003 #if GR_GL_EXPERIMENTAL_GS
906 if (fDesc.getHeader().fExperimentalGS) { 1004 if (this->desc().getHeader().fExperimentalGS) {
907 SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration); 1005 SkASSERT(this->ctxInfo().glslGeneration() >= k150_GrGLSLGeneration);
908 SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo())); 1006 SkString geomShaderSrc(GrGetGLSLVersionDecl(this->ctxInfo()));
909 geomShaderSrc.append("layout(triangles) in;\n" 1007 geomShaderSrc.append("layout(triangles) in;\n"
910 "layout(triangle_strip, max_vertices = 6) out;\n"); 1008 "layout(triangle_strip, max_vertices = 6) out;\n");
911 this->appendDecls(fGSInputs, &geomShaderSrc); 1009 this->appendDecls(fGSInputs, &geomShaderSrc);
912 this->appendDecls(fGSOutputs, &geomShaderSrc); 1010 this->appendDecls(fGSOutputs, &geomShaderSrc);
913 geomShaderSrc.append("void main() {\n"); 1011 geomShaderSrc.append("void main() {\n");
914 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n" 1012 geomShaderSrc.append("\tfor (int i = 0; i < 3; ++i) {\n"
915 "\t\tgl_Position = gl_in[i].gl_Position;\n"); 1013 "\t\tgl_Position = gl_in[i].gl_Position;\n");
916 if (fDesc.getHeader().fEmitsPointSize) { 1014 if (this->desc().getHeader().fEmitsPointSize) {
917 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n"); 1015 geomShaderSrc.append("\t\tgl_PointSize = 1.0;\n");
918 } 1016 }
919 SkASSERT(fGSInputs.count() == fGSOutputs.count()); 1017 SkASSERT(fGSInputs.count() == fGSOutputs.count());
920 for (int i = 0; i < fGSInputs.count(); ++i) { 1018 for (int i = 0; i < fGSInputs.count(); ++i) {
921 geomShaderSrc.appendf("\t\t%s = %s[i];\n", 1019 geomShaderSrc.appendf("\t\t%s = %s[i];\n",
922 fGSOutputs[i].getName().c_str(), 1020 fGSOutputs[i].getName().c_str(),
923 fGSInputs[i].getName().c_str()); 1021 fGSInputs[i].getName().c_str());
924 } 1022 }
925 geomShaderSrc.append("\t\tEmitVertex();\n" 1023 geomShaderSrc.append("\t\tEmitVertex();\n"
926 "\t}\n" 1024 "\t}\n"
927 "\tEndPrimitive();\n"); 1025 "\tEndPrimitive();\n");
928 geomShaderSrc.append("}\n"); 1026 geomShaderSrc.append("}\n");
929 GrGLuint geomShaderId = attach_shader(glCtx, programId, GR_GL_GEOMETRY_S HADER, geomShaderSrc); 1027 GrGLuint geomShaderId = attach_shader(glCtx, programId, GR_GL_GEOMETRY_S HADER, geomShaderSrc);
930 if (!geomShaderId) { 1028 if (!geomShaderId) {
931 return false; 1029 return false;
932 } 1030 }
933 *shaderIds->append() = geomShaderId; 1031 *shaderIds->append() = geomShaderId;
934 } 1032 }
935 #endif 1033 #endif
936 1034
937 return this->INHERITED::compileAndAttachShaders(programId, shaderIds); 1035 return this->INHERITED::compileAndAttachShaders(programId, shaderIds);
938 } 1036 }
939 1037
940 void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const { 1038 void GrGLFullShaderBuilder::bindProgramLocations(GrGLuint programId) const {
941 this->INHERITED::bindProgramLocations(programId); 1039 this->INHERITED::bindProgramLocations(programId);
942 1040
943 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); 1041 const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader();
944 1042
945 // Bind the attrib locations to same values for all shaders 1043 // Bind the attrib locations to same values for all shaders
946 SkASSERT(-1 != header.fPositionAttributeIndex); 1044 SkASSERT(-1 != header.fPositionAttributeIndex);
947 GL_CALL(BindAttribLocation(programId, 1045 GL_CALL(BindAttribLocation(programId,
948 header.fPositionAttributeIndex, 1046 header.fPositionAttributeIndex,
949 fPositionVar->c_str())); 1047 fPositionVar->c_str()));
950 if (-1 != header.fLocalCoordAttributeIndex) { 1048 if (-1 != header.fLocalCoordAttributeIndex) {
951 GL_CALL(BindAttribLocation(programId, 1049 GL_CALL(BindAttribLocation(programId,
952 header.fLocalCoordAttributeIndex, 1050 header.fLocalCoordAttributeIndex,
953 fLocalCoordsVar->c_str())); 1051 fLocalCoordsVar->c_str()));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 1096
999 GrGLPathTexGenProgramEffectsBuilder pathTexGenEffectsBuilder(this, 1097 GrGLPathTexGenProgramEffectsBuilder pathTexGenEffectsBuilder(this,
1000 effectCnt); 1098 effectCnt);
1001 this->INHERITED::createAndEmitEffects(&pathTexGenEffectsBuilder, 1099 this->INHERITED::createAndEmitEffects(&pathTexGenEffectsBuilder,
1002 effectStages, 1100 effectStages,
1003 effectKeys, 1101 effectKeys,
1004 effectCnt, 1102 effectCnt,
1005 inOutFSColor); 1103 inOutFSColor);
1006 return pathTexGenEffectsBuilder.finish(); 1104 return pathTexGenEffectsBuilder.finish();
1007 } 1105 }
OLDNEW
« src/gpu/gl/GrGLProgram.cpp ('K') | « src/gpu/gl/GrGLShaderBuilder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698