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