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