junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "GrGLProgram.h" |
| 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "GrAllocator.h" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 11 | #include "GrEffect.h" |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 12 | #include "GrDrawEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 13 | #include "GrGLEffect.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 14 | #include "GrGpuGL.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 15 | #include "GrGLShaderVar.h" |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 16 | #include "GrBackendEffectFactory.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 17 | #include "SkTrace.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 18 | #include "SkXfermode.h" |
| 19 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 20 | #include "SkRTConf.h" |
| 21 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 22 | SK_DEFINE_INST_COUNT(GrGLProgram) |
| 23 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 24 | #define GL_CALL(X) GR_GL_CALL(fContext.interface(), X) |
| 25 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContext.interface(), R, X) |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 26 | |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 27 | SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, |
| 28 | "Print the source code for all shaders generated."); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 29 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 30 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 31 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 32 | #define EDGE_ATTR_NAME "aEdge" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 34 | namespace { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 35 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 36 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 37 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 38 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 39 | const GrGLProgram::AttribLayout GrGLProgram::kAttribLayouts[kGrVertexAttribTypeCount] = { |
| 40 | {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType |
| 41 | {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType |
| 42 | {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType |
| 43 | {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType |
| 44 | {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType |
| 45 | }; |
| 46 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 47 | void GrGLProgram::BuildDesc(const GrDrawState& drawState, |
| 48 | bool isPoints, |
| 49 | GrDrawState::BlendOptFlags blendOpts, |
| 50 | GrBlendCoeff srcCoeff, |
| 51 | GrBlendCoeff dstCoeff, |
| 52 | const GrGpuGL* gpu, |
| 53 | Desc* desc) { |
| 54 | |
| 55 | // This should already have been caught |
| 56 | GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
| 57 | |
| 58 | bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 59 | |
| 60 | bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag | |
| 61 | GrDrawState::kEmitCoverage_BlendOptFlag)); |
| 62 | |
| 63 | // The descriptor is used as a cache key. Thus when a field of the |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 64 | // descriptor will not affect program generation (because of the attribute |
| 65 | // bindings in use or other descriptor field settings) it should be set |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 66 | // to a canonical value to avoid duplicate programs with different keys. |
| 67 | |
| 68 | // Must initialize all fields or cache will have false negatives! |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 69 | desc->fAttribBindings = drawState.getAttribBindings(); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 70 | |
| 71 | desc->fEmitsPointSize = isPoints; |
| 72 | |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 73 | bool requiresAttributeColors = |
| 74 | !skipColor && SkToBool(desc->fAttribBindings & GrDrawState::kColor_AttribBindingsBit); |
| 75 | bool requiresAttributeCoverage = |
| 76 | !skipCoverage && SkToBool(desc->fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 77 | |
| 78 | // fColorInput/fCoverageInput records how colors are specified for the program So we strip the |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 79 | // bits from the bindings to avoid false negatives when searching for an existing program in the |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 80 | // cache. |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 81 | desc->fAttribBindings &= |
| 82 | ~(GrDrawState::kColor_AttribBindingsBit | GrDrawState::kCoverage_AttribBindingsBit); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 83 | |
| 84 | desc->fColorFilterXfermode = skipColor ? |
| 85 | SkXfermode::kDst_Mode : |
| 86 | drawState.getColorFilterMode(); |
| 87 | |
| 88 | // no reason to do edge aa or look at per-vertex coverage if coverage is ignored |
| 89 | if (skipCoverage) { |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame^] | 90 | desc->fAttribBindings &= ~(GrDrawState::kCoverage_AttribBindingsBit); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 94 | bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) || |
| 95 | (!requiresAttributeColors && 0xffffffff == drawState.getColor()); |
| 96 | if (colorIsTransBlack) { |
| 97 | desc->fColorInput = Desc::kTransBlack_ColorInput; |
| 98 | } else if (colorIsSolidWhite) { |
| 99 | desc->fColorInput = Desc::kSolidWhite_ColorInput; |
| 100 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
| 101 | desc->fColorInput = Desc::kUniform_ColorInput; |
| 102 | } else { |
| 103 | desc->fColorInput = Desc::kAttribute_ColorInput; |
| 104 | } |
| 105 | |
| 106 | bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage(); |
| 107 | |
| 108 | if (skipCoverage) { |
| 109 | desc->fCoverageInput = Desc::kTransBlack_ColorInput; |
| 110 | } else if (covIsSolidWhite) { |
| 111 | desc->fCoverageInput = Desc::kSolidWhite_ColorInput; |
| 112 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 113 | desc->fCoverageInput = Desc::kUniform_ColorInput; |
| 114 | } else { |
| 115 | desc->fCoverageInput = Desc::kAttribute_ColorInput; |
| 116 | } |
| 117 | |
| 118 | int lastEnabledStage = -1; |
| 119 | |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame^] | 120 | if (!skipCoverage) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 121 | desc->fDiscardIfOutsideEdge = drawState.getStencil().doesWrite(); |
| 122 | } else { |
| 123 | // Use canonical values when edge-aa is not enabled to avoid program cache misses. |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 124 | desc->fDiscardIfOutsideEdge = false; |
| 125 | } |
| 126 | |
| 127 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 128 | |
| 129 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage; |
| 130 | if (!skip && drawState.isStageEnabled(s)) { |
| 131 | lastEnabledStage = s; |
| 132 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
| 133 | const GrBackendEffectFactory& factory = effect->getFactory(); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 134 | bool explicitLocalCoords = (drawState.getAttribBindings() & |
| 135 | GrDrawState::kLocalCoords_AttribBindingsBit); |
| 136 | GrDrawEffect drawEffect(drawState.getStage(s), explicitLocalCoords); |
| 137 | desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps()); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 138 | } else { |
| 139 | desc->fEffectKeys[s] = 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | desc->fDualSrcOutput = Desc::kNone_DualSrcOutput; |
| 144 | |
| 145 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 146 | // other than pass through values from the VS to the FS anyway). |
| 147 | #if GR_GL_EXPERIMENTAL_GS |
| 148 | #if 0 |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 149 | desc->fExperimentalGS = gpu->caps().geometryShaderSupport(); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 150 | #else |
| 151 | desc->fExperimentalGS = false; |
| 152 | #endif |
| 153 | #endif |
| 154 | |
| 155 | // We want to avoid generating programs with different "first cov stage" values when they would |
| 156 | // compute the same result. We set field in the desc to kNumStages when either there are no |
| 157 | // coverage stages or the distinction between coverage and color is immaterial. |
| 158 | int firstCoverageStage = GrDrawState::kNumStages; |
| 159 | desc->fFirstCoverageStage = GrDrawState::kNumStages; |
| 160 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
| 161 | if (hasCoverage) { |
| 162 | firstCoverageStage = drawState.getFirstCoverageStage(); |
| 163 | } |
| 164 | |
| 165 | // other coverage inputs |
| 166 | if (!hasCoverage) { |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame^] | 167 | hasCoverage = requiresAttributeCoverage; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (hasCoverage) { |
| 171 | // color filter is applied between color/coverage computation |
| 172 | if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { |
| 173 | desc->fFirstCoverageStage = firstCoverageStage; |
| 174 | } |
| 175 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 176 | if (gpu->caps()->dualSourceBlendingSupport() && |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 177 | !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
| 178 | GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
| 179 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 180 | // write the coverage value to second color |
| 181 | desc->fDualSrcOutput = Desc::kCoverage_DualSrcOutput; |
| 182 | desc->fFirstCoverageStage = firstCoverageStage; |
| 183 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 184 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 185 | desc->fDualSrcOutput = Desc::kCoverageISA_DualSrcOutput; |
| 186 | desc->fFirstCoverageStage = firstCoverageStage; |
| 187 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 188 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 189 | desc->fDualSrcOutput = Desc::kCoverageISC_DualSrcOutput; |
| 190 | desc->fFirstCoverageStage = firstCoverageStage; |
| 191 | } |
| 192 | } |
| 193 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 194 | |
| 195 | desc->fPositionAttributeIndex = drawState.getAttribIndex(GrDrawState::kPosition_AttribIndex); |
| 196 | if (requiresAttributeColors) { |
| 197 | desc->fColorAttributeIndex = drawState.getAttribIndex(GrDrawState::kColor_AttribIndex); |
| 198 | } else { |
| 199 | desc->fColorAttributeIndex = GrDrawState::kColorOverrideAttribIndexValue; |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 200 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 201 | if (requiresAttributeCoverage) { |
| 202 | desc->fCoverageAttributeIndex = drawState.getAttribIndex(GrDrawState::kCoverage_AttribIndex); |
| 203 | } else { |
| 204 | desc->fCoverageAttributeIndex = GrDrawState::kCoverageOverrideAttribIndexValue; |
| 205 | } |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 206 | if (desc->fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
| 207 | desc->fLocalCoordsAttributeIndex = drawState.getAttribIndex(GrDrawState::kLocalCoords_AttribIndex); |
robertphillips@google.com | 37ebe3f | 2013-03-04 20:03:44 +0000 | [diff] [blame] | 208 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 209 | |
| 210 | #if GR_DEBUG |
| 211 | // verify valid vertex attribute state |
| 212 | const GrVertexAttrib* vertexAttribs = drawState.getVertexAttribs(); |
| 213 | GrAssert(desc->fPositionAttributeIndex < GrDrawState::kVertexAttribCnt); |
| 214 | GrAssert(kAttribLayouts[vertexAttribs[desc->fPositionAttributeIndex].fType].fCount == 2); |
| 215 | if (requiresAttributeColors) { |
| 216 | GrAssert(desc->fColorAttributeIndex < GrDrawState::kVertexAttribCnt); |
| 217 | GrAssert(kAttribLayouts[vertexAttribs[desc->fColorAttributeIndex].fType].fCount == 4); |
| 218 | } |
| 219 | if (requiresAttributeCoverage) { |
| 220 | GrAssert(desc->fCoverageAttributeIndex < GrDrawState::kVertexAttribCnt); |
| 221 | GrAssert(kAttribLayouts[vertexAttribs[desc->fCoverageAttributeIndex].fType].fCount == 4); |
| 222 | } |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 223 | if (desc->fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
| 224 | GrAssert(desc->fLocalCoordsAttributeIndex < GrDrawState::kVertexAttribCnt); |
| 225 | GrAssert(kAttribLayouts[vertexAttribs[desc->fLocalCoordsAttributeIndex].fType].fCount == 2); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 226 | } |
| 227 | #endif |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 230 | GrGLProgram* GrGLProgram::Create(const GrGLContext& gl, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 231 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 232 | const GrEffectStage* stages[]) { |
| 233 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 234 | if (!program->succeeded()) { |
| 235 | delete program; |
| 236 | program = NULL; |
| 237 | } |
| 238 | return program; |
| 239 | } |
| 240 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 241 | GrGLProgram::GrGLProgram(const GrGLContext& gl, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 242 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 243 | const GrEffectStage* stages[]) |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 244 | : fContext(gl) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 245 | , fUniformManager(gl) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 246 | fDesc = desc; |
| 247 | fVShaderID = 0; |
| 248 | fGShaderID = 0; |
| 249 | fFShaderID = 0; |
| 250 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 251 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 252 | fColor = GrColor_ILLEGAL; |
| 253 | fColorFilterColor = GrColor_ILLEGAL; |
| 254 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 255 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 256 | fEffects[s] = NULL; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 257 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 258 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 259 | this->genProgram(stages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 263 | if (fVShaderID) { |
| 264 | GL_CALL(DeleteShader(fVShaderID)); |
| 265 | } |
| 266 | if (fGShaderID) { |
| 267 | GL_CALL(DeleteShader(fGShaderID)); |
| 268 | } |
| 269 | if (fFShaderID) { |
| 270 | GL_CALL(DeleteShader(fFShaderID)); |
| 271 | } |
| 272 | if (fProgramID) { |
| 273 | GL_CALL(DeleteProgram(fProgramID)); |
| 274 | } |
| 275 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 276 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 277 | delete fEffects[i]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 278 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 279 | } |
| 280 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 281 | void GrGLProgram::abandon() { |
| 282 | fVShaderID = 0; |
| 283 | fGShaderID = 0; |
| 284 | fFShaderID = 0; |
| 285 | fProgramID = 0; |
| 286 | } |
| 287 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 288 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 289 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 290 | switch (fDesc.fDualSrcOutput) { |
| 291 | case Desc::kNone_DualSrcOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 292 | break; |
| 293 | // the prog will write a coverage value to the secondary |
| 294 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 295 | case Desc::kCoverage_DualSrcOutput: |
| 296 | case Desc::kCoverageISA_DualSrcOutput: |
| 297 | case Desc::kCoverageISC_DualSrcOutput: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 298 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 299 | break; |
| 300 | default: |
| 301 | GrCrash("Unexpected dual source blend output"); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 306 | namespace { |
| 307 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 308 | // given two blend coeffecients determine whether the src |
| 309 | // and/or dst computation can be omitted. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 310 | inline void need_blend_inputs(SkXfermode::Coeff srcCoeff, |
| 311 | SkXfermode::Coeff dstCoeff, |
| 312 | bool* needSrcValue, |
| 313 | bool* needDstValue) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 314 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 315 | switch (dstCoeff) { |
| 316 | // these all read the src |
| 317 | case SkXfermode::kSC_Coeff: |
| 318 | case SkXfermode::kISC_Coeff: |
| 319 | case SkXfermode::kSA_Coeff: |
| 320 | case SkXfermode::kISA_Coeff: |
| 321 | *needSrcValue = true; |
| 322 | break; |
| 323 | default: |
| 324 | *needSrcValue = false; |
| 325 | break; |
| 326 | } |
| 327 | } else { |
| 328 | *needSrcValue = true; |
| 329 | } |
| 330 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 331 | switch (srcCoeff) { |
| 332 | // these all read the dst |
| 333 | case SkXfermode::kDC_Coeff: |
| 334 | case SkXfermode::kIDC_Coeff: |
| 335 | case SkXfermode::kDA_Coeff: |
| 336 | case SkXfermode::kIDA_Coeff: |
| 337 | *needDstValue = true; |
| 338 | break; |
| 339 | default: |
| 340 | *needDstValue = false; |
| 341 | break; |
| 342 | } |
| 343 | } else { |
| 344 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 349 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 350 | * string if result is trivially zero. |
| 351 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 352 | inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff, |
| 353 | const char* src, const char* dst, |
| 354 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 355 | switch (coeff) { |
| 356 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 357 | *str = ""; |
| 358 | break; |
| 359 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 360 | *str = value; |
| 361 | break; |
| 362 | case SkXfermode::kSC_Coeff: |
| 363 | str->printf("(%s * %s)", src, value); |
| 364 | break; |
| 365 | case SkXfermode::kISC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 366 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 367 | break; |
| 368 | case SkXfermode::kDC_Coeff: |
| 369 | str->printf("(%s * %s)", dst, value); |
| 370 | break; |
| 371 | case SkXfermode::kIDC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 372 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 373 | break; |
| 374 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 375 | str->printf("(%s.a * %s)", src, value); |
| 376 | break; |
| 377 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 378 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 379 | break; |
| 380 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 381 | str->printf("(%s.a * %s)", dst, value); |
| 382 | break; |
| 383 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 384 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 385 | break; |
| 386 | default: |
| 387 | GrCrash("Unexpected xfer coeff."); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 392 | * Adds a line to the fragment shader code which modifies the color by |
| 393 | * the specified color filter. |
| 394 | */ |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 395 | void add_color_filter(GrGLShaderBuilder* builder, |
| 396 | const char * outputVar, |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 397 | SkXfermode::Coeff uniformCoeff, |
| 398 | SkXfermode::Coeff colorCoeff, |
| 399 | const char* filterColor, |
| 400 | const char* inColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 401 | SkString colorStr, constStr; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 402 | blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor); |
| 403 | blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 404 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 405 | SkString sum; |
| 406 | GrGLSLAdd4f(&sum, colorStr.c_str(), constStr.c_str()); |
| 407 | builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str()); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 408 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 409 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 410 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 411 | void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { |
| 412 | switch (fDesc.fColorInput) { |
| 413 | case GrGLProgram::Desc::kAttribute_ColorInput: { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 414 | builder->addAttribute(kVec4f_GrSLType, COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 415 | const char *vsName, *fsName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 416 | builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 417 | builder->vsCodeAppendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 418 | *inColor = fsName; |
| 419 | } break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 420 | case GrGLProgram::Desc::kUniform_ColorInput: { |
| 421 | const char* name; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 422 | fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 423 | kVec4f_GrSLType, "Color", &name); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 424 | *inColor = name; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 425 | break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 426 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 427 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 428 | GrAssert(!"needComputedColor should be false."); |
| 429 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 430 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 431 | break; |
| 432 | default: |
| 433 | GrCrash("Unknown color type."); |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 438 | void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 439 | const char* covUniName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 440 | fUniformHandles.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 441 | kVec4f_GrSLType, "Coverage", &covUniName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 442 | if (inOutCoverage->size()) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 443 | builder->fsCodeAppendf("\tvec4 uniCoverage = %s * %s;\n", |
| 444 | covUniName, inOutCoverage->c_str()); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 445 | *inOutCoverage = "uniCoverage"; |
| 446 | } else { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 447 | *inOutCoverage = covUniName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | namespace { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 452 | void gen_attribute_coverage(GrGLShaderBuilder* builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 453 | SkString* inOutCoverage) { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 454 | builder->addAttribute(kVec4f_GrSLType, COV_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 455 | const char *vsName, *fsName; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 456 | builder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
| 457 | builder->vsCodeAppendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 458 | if (inOutCoverage->size()) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 459 | builder->fsCodeAppendf("\tvec4 attrCoverage = %s * %s;\n", fsName, inOutCoverage->c_str()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 460 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 461 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 462 | *inOutCoverage = fsName; |
| 463 | } |
| 464 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 465 | } |
| 466 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 467 | void GrGLProgram::genGeometryShader(GrGLShaderBuilder* builder) const { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 468 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 469 | // TODO: The builder should add all this glue code. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 470 | if (fDesc.fExperimentalGS) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 471 | GrAssert(fContext.info().glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 472 | builder->fGSHeader.append("layout(triangles) in;\n" |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 473 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 474 | builder->gsCodeAppend("\tfor (int i = 0; i < 3; ++i) {\n" |
| 475 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 476 | if (fDesc.fEmitsPointSize) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 477 | builder->gsCodeAppend("\t\tgl_PointSize = 1.0;\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 478 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 479 | GrAssert(builder->fGSInputs.count() == builder->fGSOutputs.count()); |
| 480 | int count = builder->fGSInputs.count(); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 481 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 482 | builder->gsCodeAppendf("\t\t%s = %s[i];\n", |
| 483 | builder->fGSOutputs[i].getName().c_str(), |
| 484 | builder->fGSInputs[i].getName().c_str()); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 485 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 486 | builder->gsCodeAppend("\t\tEmitVertex();\n" |
| 487 | "\t}\n" |
| 488 | "\tEndPrimitive();\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 489 | } |
| 490 | #endif |
| 491 | } |
| 492 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 493 | const char* GrGLProgram::adjustInColor(const SkString& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 494 | if (inColor.size()) { |
| 495 | return inColor.c_str(); |
| 496 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 497 | if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 498 | return GrGLSLOnesVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 499 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 500 | return GrGLSLZerosVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 505 | namespace { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 506 | // prints a shader using params similar to glShaderSource |
| 507 | void print_shader(GrGLint stringCnt, |
| 508 | const GrGLchar** strings, |
| 509 | GrGLint* stringLengths) { |
| 510 | for (int i = 0; i < stringCnt; ++i) { |
| 511 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 512 | GrPrintf(strings[i]); |
| 513 | } else { |
| 514 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 520 | GrGLuint compile_shader(const GrGLContext& gl, |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 521 | GrGLenum type, |
| 522 | int stringCnt, |
| 523 | const char** strings, |
| 524 | int* stringLengths) { |
| 525 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 526 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 527 | |
| 528 | GrGLuint shader; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 529 | GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 530 | if (0 == shader) { |
| 531 | return 0; |
| 532 | } |
| 533 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 534 | const GrGLInterface* gli = gl.interface(); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 535 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 536 | GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 537 | GR_GL_CALL(gli, CompileShader(shader)); |
| 538 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 539 | |
| 540 | if (!compiled) { |
| 541 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 542 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 543 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 544 | if (infoLen > 0) { |
| 545 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 546 | // param validation. |
| 547 | GrGLsizei length = GR_GL_INIT_ZERO; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 548 | GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 549 | &length, (char*)log.get())); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 550 | print_shader(stringCnt, strings, stringLengths); |
| 551 | GrPrintf("\n%s", log.get()); |
| 552 | } |
| 553 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 554 | GR_GL_CALL(gli, DeleteShader(shader)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | return shader; |
| 558 | } |
| 559 | |
| 560 | // helper version of above for when shader is already flattened into a single SkString |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 561 | GrGLuint compile_shader(const GrGLContext& gl, GrGLenum type, const SkString& shader) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 562 | const GrGLchar* str = shader.c_str(); |
| 563 | int length = shader.size(); |
| 564 | return compile_shader(gl, type, 1, &str, &length); |
| 565 | } |
| 566 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // compiles all the shaders from builder and stores the shader IDs |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 570 | bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 571 | |
| 572 | SkString shader; |
| 573 | |
| 574 | builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 575 | if (c_PrintShaders) { |
| 576 | GrPrintf(shader.c_str()); |
| 577 | GrPrintf("\n"); |
| 578 | } |
skia.committer@gmail.com | 7fc0e0a | 2013-01-15 02:01:40 +0000 | [diff] [blame] | 579 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 580 | if (!(fVShaderID = compile_shader(fContext, GR_GL_VERTEX_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
| 583 | |
| 584 | if (builder.fUsesGS) { |
| 585 | builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 586 | if (c_PrintShaders) { |
| 587 | GrPrintf(shader.c_str()); |
| 588 | GrPrintf("\n"); |
| 589 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 590 | if (!(fGShaderID = compile_shader(fContext, GR_GL_GEOMETRY_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 594 | fGShaderID = 0; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 598 | if (c_PrintShaders) { |
| 599 | GrPrintf(shader.c_str()); |
| 600 | GrPrintf("\n"); |
| 601 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 602 | if (!(fFShaderID = compile_shader(fContext, GR_GL_FRAGMENT_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
| 606 | return true; |
| 607 | } |
| 608 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 609 | bool GrGLProgram::genProgram(const GrEffectStage* stages[]) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 610 | GrAssert(0 == fProgramID); |
| 611 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 612 | const GrAttribBindings& attribBindings = fDesc.fAttribBindings; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 613 | bool hasExplicitLocalCoords = |
| 614 | SkToBool(attribBindings & GrDrawState::kLocalCoords_AttribBindingsBit); |
| 615 | GrGLShaderBuilder builder(fContext.info(), fUniformManager, hasExplicitLocalCoords); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 616 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 617 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 618 | builder.fUsesGS = fDesc.fExperimentalGS; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 619 | #endif |
| 620 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 621 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
| 622 | // The rest of transfer mode color filters have not been implemented |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 623 | if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 624 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 625 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 626 | (fDesc.fColorFilterXfermode), |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 627 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 628 | GR_DEBUGASSERT(success); |
| 629 | } else { |
| 630 | colorCoeff = SkXfermode::kOne_Coeff; |
| 631 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 632 | } |
| 633 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 634 | // no need to do the color filter if coverage is 0. The output color is scaled by the coverage. |
| 635 | // All the dual source outputs are scaled by the coverage as well. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 636 | if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 637 | colorCoeff = SkXfermode::kZero_Coeff; |
| 638 | uniformCoeff = SkXfermode::kZero_Coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 639 | } |
| 640 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 641 | // If we know the final color is going to be all zeros then we can |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 642 | // simplify the color filter coefficients. needComputedColor will then |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 643 | // come out false below. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 644 | if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 645 | colorCoeff = SkXfermode::kZero_Coeff; |
| 646 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 647 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 648 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 649 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 650 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 651 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 652 | } |
| 653 | } |
| 654 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 655 | bool needColorFilterUniform; |
| 656 | bool needComputedColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 657 | need_blend_inputs(uniformCoeff, colorCoeff, |
| 658 | &needColorFilterUniform, &needComputedColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 659 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 660 | // the dual source output has no canonical var name, have to |
| 661 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 662 | bool dualSourceOutputWritten = false; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 663 | builder.fHeader.append(GrGetGLSLVersionDecl(fContext.info().binding(), |
| 664 | fContext.info().glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 665 | |
| 666 | GrGLShaderVar colorOutput; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 667 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContext.info().glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 668 | declared_color_output_name(), |
| 669 | &colorOutput); |
| 670 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 671 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 672 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 673 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 674 | const char* viewMName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 675 | fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 676 | kMat33f_GrSLType, "ViewM", &viewMName); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 677 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 678 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 679 | builder.vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
| 680 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
| 681 | viewMName, builder.positionAttribute().getName().c_str()); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 682 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 683 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 684 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 685 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 686 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 687 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 688 | } |
| 689 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 690 | // we output point size in the GS if present |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 691 | if (fDesc.fEmitsPointSize && !builder.fUsesGS){ |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 692 | builder.vsCodeAppend("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 693 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 694 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 695 | /////////////////////////////////////////////////////////////////////////// |
| 696 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 697 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 698 | // if we have color stages string them together, feeding the output color |
| 699 | // of each to the next and generating code for each stage. |
| 700 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 701 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 702 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 703 | if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 704 | // create var to hold stage result |
| 705 | outColor = "color"; |
| 706 | outColor.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 707 | builder.fsCodeAppendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 708 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 709 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 710 | fEffects[s] = builder.createAndEmitGLEffect(*stages[s], |
| 711 | fDesc.fEffectKeys[s], |
| 712 | inColor.size() ? inColor.c_str() : NULL, |
| 713 | outColor.c_str(), |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 714 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 715 | builder.setNonStage(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 719 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 720 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 721 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 722 | // may be able to make additional optimizations. |
| 723 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 724 | GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 725 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 726 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 727 | if (uniformCoeffIsZero) { |
| 728 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 729 | bool bogus; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 730 | need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 731 | &needColorFilterUniform, &bogus); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 732 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 733 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 734 | const char* colorFilterColorUniName = NULL; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 735 | if (needColorFilterUniform) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 736 | fUniformHandles.fColorFilterUni = builder.addUniform( |
| 737 | GrGLShaderBuilder::kFragment_ShaderType, |
| 738 | kVec4f_GrSLType, "FilterColor", |
| 739 | &colorFilterColorUniName); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 740 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 741 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 742 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 743 | SkXfermode::kZero_Coeff == colorCoeff) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 744 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 745 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 746 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 747 | builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 748 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 749 | add_color_filter(&builder, "filteredColor", uniformCoeff, |
| 750 | colorCoeff, colorFilterColorUniName, color); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 751 | inColor = "filteredColor"; |
| 752 | } |
| 753 | |
| 754 | /////////////////////////////////////////////////////////////////////////// |
| 755 | // compute the partial coverage (coverage stages and edge aa) |
| 756 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 757 | SkString inCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 758 | bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 759 | // we don't need to compute coverage at all if we know the final shader |
| 760 | // output will be zero and we don't have a dual src blend output. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 761 | if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 762 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 763 | if (!coverageIsZero) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 764 | switch (fDesc.fCoverageInput) { |
| 765 | case Desc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 766 | // empty string implies solid white |
| 767 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 768 | case Desc::kAttribute_ColorInput: |
| 769 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 770 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 771 | case Desc::kUniform_ColorInput: |
| 772 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 773 | break; |
| 774 | default: |
| 775 | GrCrash("Unexpected input coverage."); |
| 776 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 777 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 778 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 779 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 780 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 781 | if (fDesc.fEffectKeys[s]) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 782 | // create var to hold stage output |
| 783 | outCoverage = "coverage"; |
| 784 | outCoverage.appendS32(s); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 785 | builder.fsCodeAppendf("\tvec4 %s;\n", outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 786 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 787 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 788 | fEffects[s] = builder.createAndEmitGLEffect( |
| 789 | *stages[s], |
| 790 | fDesc.fEffectKeys[s], |
| 791 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 792 | outCoverage.c_str(), |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 793 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 794 | builder.setNonStage(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 795 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 796 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 797 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 798 | |
| 799 | // discard if coverage is zero |
| 800 | if (fDesc.fDiscardIfOutsideEdge && !outCoverage.isEmpty()) { |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 801 | builder.fsCodeAppendf( |
| 802 | "\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", |
| 803 | outCoverage.c_str()); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 804 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 805 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 806 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 807 | if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 808 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 809 | GrGLShaderVar::kOut_TypeModifier, |
| 810 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 811 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 812 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 813 | if (!outputIsZero && |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 814 | Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 815 | if (!inColor.size()) { |
| 816 | outputIsZero = true; |
| 817 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 818 | if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 819 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 820 | } else { |
| 821 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | if (outputIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 826 | builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 827 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 828 | SkString modulate; |
| 829 | GrGLSLModulate4f(&modulate, coeff.c_str(), inCoverage.c_str()); |
| 830 | builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), modulate.c_str()); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 831 | } |
| 832 | dualSourceOutputWritten = true; |
| 833 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 834 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 835 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 836 | /////////////////////////////////////////////////////////////////////////// |
| 837 | // combine color and coverage as frag color |
| 838 | |
| 839 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 840 | if (coverageIsZero) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 841 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 842 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 843 | SkString modulate; |
| 844 | GrGLSLModulate4f(&modulate, inColor.c_str(), inCoverage.c_str()); |
| 845 | builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), modulate.c_str()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 846 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 847 | } |
| 848 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 849 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 850 | // insert GS |
| 851 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 852 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 853 | #endif |
| 854 | |
| 855 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 856 | // compile and setup attribs and unis |
| 857 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 858 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 862 | if (!this->bindOutputsAttribsAndLinkProgram(builder, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 863 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 864 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 868 | builder.finished(fProgramID); |
| 869 | this->initSamplerUniforms(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 870 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 871 | |
| 872 | return true; |
| 873 | } |
| 874 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 875 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 876 | bool bindColorOut, |
| 877 | bool bindDualSrcOut) { |
| 878 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 879 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 880 | return false; |
| 881 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 882 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 883 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 884 | if (fGShaderID) { |
| 885 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 886 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 887 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 888 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 889 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 890 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 891 | } |
| 892 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 893 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 894 | } |
| 895 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 896 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 897 | GL_CALL(BindAttribLocation(fProgramID, |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 898 | fDesc.fPositionAttributeIndex, |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 899 | builder.positionAttribute().c_str())); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 900 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_NAME)); |
| 901 | GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_ATTR_NAME)); |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 902 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 903 | if (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
| 904 | GL_CALL(BindAttribLocation(fProgramID, |
| 905 | fDesc.fLocalCoordsAttributeIndex, |
| 906 | builder.localCoordsAttribute().c_str())); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 907 | } |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 908 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 909 | const GrGLShaderBuilder::AttributePair* attribEnd = builder.getEffectAttributes().end(); |
skia.committer@gmail.com | 91274b9 | 2013-03-13 07:01:04 +0000 | [diff] [blame] | 910 | for (const GrGLShaderBuilder::AttributePair* attrib = builder.getEffectAttributes().begin(); |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 911 | attrib != attribEnd; |
| 912 | ++attrib) { |
| 913 | GL_CALL(BindAttribLocation(fProgramID, attrib->fIndex, attrib->fName.c_str())); |
| 914 | } |
| 915 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 916 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 917 | |
| 918 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 919 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 920 | if (!linked) { |
| 921 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 922 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 923 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 924 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 925 | // retrieve length even though we don't need it to workaround |
| 926 | // bug in chrome cmd buffer param validation. |
| 927 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 928 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 929 | infoLen+1, |
| 930 | &length, |
| 931 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 932 | GrPrintf((char*)log.get()); |
| 933 | } |
| 934 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 935 | GL_CALL(DeleteProgram(fProgramID)); |
| 936 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | return true; |
| 940 | } |
| 941 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 942 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 943 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 944 | // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes this |
| 945 | // behavior. |
| 946 | GrGLint texUnitIdx = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 947 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 948 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 949 | for (int u = 0; u < numSamplers; ++u) { |
| 950 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 951 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 952 | fUniformManager.setSampler(handle, texUnitIdx); |
| 953 | ++texUnitIdx; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 954 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 955 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 956 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 957 | } |
| 958 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 959 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 960 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 961 | void GrGLProgram::setData(GrGpuGL* gpu, |
| 962 | GrColor color, |
| 963 | GrColor coverage, |
| 964 | SharedGLState* sharedState) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 965 | const GrDrawState& drawState = gpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 966 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 967 | this->setColor(drawState, color, sharedState); |
| 968 | this->setCoverage(drawState, coverage, sharedState); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 969 | this->setMatrixAndRenderTargetHeight(drawState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 970 | |
| 971 | // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
| 972 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilterUni && |
| 973 | fColorFilterColor != drawState.getColorFilterColor()) { |
| 974 | GrGLfloat c[4]; |
| 975 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
| 976 | fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
| 977 | fColorFilterColor = drawState.getColorFilterColor(); |
| 978 | } |
| 979 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 980 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 981 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 982 | if (NULL != fEffects[s]) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 983 | const GrEffectStage& stage = drawState.getStage(s); |
| 984 | GrAssert(NULL != stage.getEffect()); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 985 | |
| 986 | bool explicitLocalCoords = |
| 987 | (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit); |
| 988 | GrDrawEffect drawEffect(stage, explicitLocalCoords); |
| 989 | fEffects[s]->setData(fUniformManager, drawEffect); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 990 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 991 | for (int u = 0; u < numSamplers; ++u) { |
| 992 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 993 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 994 | const GrTextureAccess& access = (*stage.getEffect())->textureAccess(u); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 995 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 996 | gpu->bindTexture(texUnitIdx, access.getParams(), texture); |
| 997 | ++texUnitIdx; |
| 998 | } |
| 999 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1002 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1003 | |
| 1004 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 1005 | GrColor color, |
| 1006 | SharedGLState* sharedState) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1007 | if (!(drawState.getAttribBindings() & GrDrawState::kColor_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1008 | switch (fDesc.fColorInput) { |
| 1009 | case GrGLProgram::Desc::kAttribute_ColorInput: |
| 1010 | if (sharedState->fConstAttribColor != color) { |
| 1011 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 1012 | GrGLfloat c[4]; |
| 1013 | GrColorToRGBAFloat(color, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1014 | GL_CALL(VertexAttrib4fv(fDesc.fColorAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1015 | sharedState->fConstAttribColor = color; |
| 1016 | } |
| 1017 | break; |
| 1018 | case GrGLProgram::Desc::kUniform_ColorInput: |
| 1019 | if (fColor != color) { |
| 1020 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 1021 | GrGLfloat c[4]; |
| 1022 | GrColorToRGBAFloat(color, c); |
| 1023 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 1024 | fUniformHandles.fColorUni); |
| 1025 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 1026 | fColor = color; |
| 1027 | } |
| 1028 | break; |
| 1029 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
| 1030 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
| 1031 | break; |
| 1032 | default: |
| 1033 | GrCrash("Unknown color type."); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 1039 | GrColor coverage, |
| 1040 | SharedGLState* sharedState) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1041 | if (!(drawState.getAttribBindings() & GrDrawState::kCoverage_AttribBindingsBit)) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1042 | switch (fDesc.fCoverageInput) { |
| 1043 | case Desc::kAttribute_ColorInput: |
| 1044 | if (sharedState->fConstAttribCoverage != coverage) { |
| 1045 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 1046 | GrGLfloat c[4]; |
| 1047 | GrColorToRGBAFloat(coverage, c); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1048 | GL_CALL(VertexAttrib4fv(fDesc.fCoverageAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1049 | sharedState->fConstAttribCoverage = coverage; |
| 1050 | } |
| 1051 | break; |
| 1052 | case Desc::kUniform_ColorInput: |
| 1053 | if (fCoverage != coverage) { |
| 1054 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 1055 | GrGLfloat c[4]; |
| 1056 | GrColorToRGBAFloat(coverage, c); |
| 1057 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 1058 | fUniformHandles.fCoverageUni); |
| 1059 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 1060 | fCoverage = coverage; |
| 1061 | } |
| 1062 | break; |
| 1063 | case Desc::kSolidWhite_ColorInput: |
| 1064 | case Desc::kTransBlack_ColorInput: |
| 1065 | break; |
| 1066 | default: |
| 1067 | GrCrash("Unknown coverage type."); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 1071 | |
| 1072 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 1073 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 1074 | SkISize size; |
| 1075 | size.set(rt->width(), rt->height()); |
| 1076 | |
| 1077 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 1078 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni && |
| 1079 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 1080 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 1081 | } |
| 1082 | |
| 1083 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 1084 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) || |
| 1085 | fMatrixState.fRenderTargetSize != size) { |
| 1086 | SkMatrix m; |
| 1087 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 1088 | m.setAll( |
| 1089 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 1090 | 0,-SkIntToScalar(2) / size.fHeight, SK_Scalar1, |
| 1091 | 0, 0, SkMatrix::I()[8]); |
| 1092 | } else { |
| 1093 | m.setAll( |
| 1094 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 1095 | 0, SkIntToScalar(2) / size.fHeight,-SK_Scalar1, |
| 1096 | 0, 0, SkMatrix::I()[8]); |
| 1097 | } |
| 1098 | m.setConcat(m, drawState.getViewMatrix()); |
| 1099 | |
| 1100 | // ES doesn't allow you to pass true to the transpose param so we do our own transpose. |
| 1101 | GrGLfloat mt[] = { |
| 1102 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 1103 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 1104 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
| 1105 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 1106 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 1107 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
| 1108 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 1109 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
| 1110 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
| 1111 | }; |
| 1112 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
| 1113 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 1114 | fMatrixState.fRenderTargetSize = size; |
| 1115 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
| 1116 | } |
| 1117 | } |