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