| 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 | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 16 | #include "GrGLSL.h" |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkXfermode.h" |
| 18 | |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 19 | SK_DEFINE_INST_COUNT(GrGLProgram) |
| 20 | |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 21 | #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) |
| 22 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X) |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 23 | |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 24 | GrGLProgram* GrGLProgram::Create(GrGpuGL* gpu, |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 25 | const GrGLProgramDesc& desc, |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 26 | const GrEffectStage* colorStages[], |
| 27 | const GrEffectStage* coverageStages[]) { |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 28 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gpu, desc, colorStages, coverageStages)); |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 29 | if (!program->succeeded()) { |
| 30 | delete program; |
| 31 | program = NULL; |
| 32 | } |
| 33 | return program; |
| 34 | } |
| 35 | |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 36 | GrGLProgram::GrGLProgram(GrGpuGL* gpu, |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 37 | const GrGLProgramDesc& desc, |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 38 | const GrEffectStage* colorStages[], |
| 39 | const GrEffectStage* coverageStages[]) |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 40 | : fGpu(gpu) |
| 41 | , fUniformManager(gpu) { |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 42 | fDesc = desc; |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 43 | fProgramID = 0; |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 44 | |
| bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 45 | fDstCopyTexUnit = -1; |
| 46 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 47 | fColor = GrColor_ILLEGAL; |
| 48 | fColorFilterColor = GrColor_ILLEGAL; |
| 49 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 50 | fColorEffects.reset(desc.numColorEffects()); |
| 51 | fCoverageEffects.reset(desc.numCoverageEffects()); |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 52 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 53 | this->genProgram(colorStages, coverageStages); |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | GrGLProgram::~GrGLProgram() { |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 57 | if (fProgramID) { |
| 58 | GL_CALL(DeleteProgram(fProgramID)); |
| 59 | } |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 62 | void GrGLProgram::abandon() { |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 63 | fProgramID = 0; |
| 64 | } |
| 65 | |
| tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 66 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 67 | GrBlendCoeff* dstCoeff) const { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 68 | switch (fDesc.getHeader().fCoverageOutput) { |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 69 | case GrGLProgramDesc::kModulate_CoverageOutput: |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 70 | break; |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 71 | // The prog will write a coverage value to the secondary |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 72 | // output and the dst is blended by one minus that value. |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 73 | case GrGLProgramDesc::kSecondaryCoverage_CoverageOutput: |
| 74 | case GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput: |
| 75 | case GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput: |
| 76 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 77 | break; |
| 78 | case GrGLProgramDesc::kCombineWithDst_CoverageOutput: |
| 79 | // We should only have set this if the blend was specified as (1, 0) |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 80 | SkASSERT(kOne_GrBlendCoeff == *srcCoeff && kZero_GrBlendCoeff == *dstCoeff); |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 81 | break; |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 82 | default: |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 83 | GrCrash("Unexpected coverage output"); |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 88 | namespace { |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 89 | // given two blend coefficients determine whether the src |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 90 | // and/or dst computation can be omitted. |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 91 | inline void need_blend_inputs(SkXfermode::Coeff srcCoeff, |
| 92 | SkXfermode::Coeff dstCoeff, |
| 93 | bool* needSrcValue, |
| 94 | bool* needDstValue) { |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 95 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 96 | switch (dstCoeff) { |
| 97 | // these all read the src |
| 98 | case SkXfermode::kSC_Coeff: |
| 99 | case SkXfermode::kISC_Coeff: |
| 100 | case SkXfermode::kSA_Coeff: |
| 101 | case SkXfermode::kISA_Coeff: |
| 102 | *needSrcValue = true; |
| 103 | break; |
| 104 | default: |
| 105 | *needSrcValue = false; |
| 106 | break; |
| 107 | } |
| 108 | } else { |
| 109 | *needSrcValue = true; |
| 110 | } |
| 111 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 112 | switch (srcCoeff) { |
| 113 | // these all read the dst |
| 114 | case SkXfermode::kDC_Coeff: |
| 115 | case SkXfermode::kIDC_Coeff: |
| 116 | case SkXfermode::kDA_Coeff: |
| 117 | case SkXfermode::kIDA_Coeff: |
| 118 | *needDstValue = true; |
| 119 | break; |
| 120 | default: |
| 121 | *needDstValue = false; |
| 122 | break; |
| 123 | } |
| 124 | } else { |
| 125 | *needDstValue = true; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 130 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 131 | * string if result is trivially zero. |
| 132 | */ |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 133 | inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff, |
| 134 | const char* src, const char* dst, |
| 135 | const char* value) { |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 136 | switch (coeff) { |
| 137 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 138 | *str = ""; |
| 139 | break; |
| 140 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 141 | *str = value; |
| 142 | break; |
| 143 | case SkXfermode::kSC_Coeff: |
| 144 | str->printf("(%s * %s)", src, value); |
| 145 | break; |
| 146 | case SkXfermode::kISC_Coeff: |
| bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 147 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 148 | break; |
| 149 | case SkXfermode::kDC_Coeff: |
| 150 | str->printf("(%s * %s)", dst, value); |
| 151 | break; |
| 152 | case SkXfermode::kIDC_Coeff: |
| bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 153 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 154 | break; |
| 155 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 156 | str->printf("(%s.a * %s)", src, value); |
| 157 | break; |
| 158 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 159 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 160 | break; |
| 161 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 162 | str->printf("(%s.a * %s)", dst, value); |
| 163 | break; |
| 164 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 165 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 166 | break; |
| 167 | default: |
| 168 | GrCrash("Unexpected xfer coeff."); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | /** |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 173 | * Adds a line to the fragment shader code which modifies the color by |
| 174 | * the specified color filter. |
| 175 | */ |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 176 | void add_color_filter(GrGLShaderBuilder* builder, |
| 177 | const char * outputVar, |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 178 | SkXfermode::Coeff uniformCoeff, |
| 179 | SkXfermode::Coeff colorCoeff, |
| 180 | const char* filterColor, |
| 181 | const char* inColor) { |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 182 | SkString colorStr, constStr; |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 183 | blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor); |
| 184 | blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 185 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 186 | SkString sum; |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 187 | GrGLSLAddf<4>(&sum, colorStr.c_str(), constStr.c_str()); |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 188 | builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str()); |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 189 | } |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 190 | } |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 191 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 192 | namespace { |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 193 | |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 194 | void expand_known_value4f(SkString* string, GrSLConstantVec vec) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 195 | SkASSERT(string->isEmpty() == (vec != kNone_GrSLConstantVec)); |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 196 | switch (vec) { |
| 197 | case kNone_GrSLConstantVec: |
| 198 | break; |
| 199 | case kZeros_GrSLConstantVec: |
| 200 | *string = GrGLSLZerosVecf(4); |
| 201 | break; |
| 202 | case kOnes_GrSLConstantVec: |
| 203 | *string = GrGLSLOnesVecf(4); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 210 | bool GrGLProgram::genProgram(const GrEffectStage* colorStages[], |
| 211 | const GrEffectStage* coverageStages[]) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 212 | SkASSERT(0 == fProgramID); |
| bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 213 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 214 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 215 | |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 216 | bool needsVertexShader = true; |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 217 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 218 | GrGLShaderBuilder builder(fGpu, fUniformManager, fDesc, needsVertexShader); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 219 | if (GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder.getVertexBuilder()) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 220 | fUniformHandles.fViewMatrixUni = vertexBuilder->getViewMatrixUniform(); |
| bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 221 | } |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 222 | |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 223 | // incoming color to current stage being processed. |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 224 | SkString inColor = builder.getInputColor(); |
| 225 | GrSLConstantVec knownColorValue = builder.getKnownColorValue(); |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 226 | |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 227 | // Get the coeffs for the Mode-based color filter, determine if color is needed. |
| 228 | SkXfermode::Coeff colorCoeff; |
| 229 | SkXfermode::Coeff filterColorCoeff; |
| 230 | SkAssertResult( |
| commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame^] | 231 | SkXfermode::ModeAsCoeff(header.fColorFilterXfermode, |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 232 | &filterColorCoeff, |
| 233 | &colorCoeff)); |
| 234 | bool needColor, needFilterColor; |
| 235 | need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor); |
| 236 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 237 | // used in order for builder to return the per-stage uniform handles. |
| bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 238 | typedef SkTArray<GrGLUniformManager::UniformHandle, true>* UniHandleArrayPtr; |
| 239 | int maxColorOrCovEffectCnt = GrMax(fDesc.numColorEffects(), fDesc.numCoverageEffects()); |
| 240 | SkAutoTArray<UniHandleArrayPtr> effectUniformArrays(maxColorOrCovEffectCnt); |
| 241 | SkAutoTArray<GrGLEffect*> glEffects(maxColorOrCovEffectCnt); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 242 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 243 | if (needColor) { |
| bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 244 | for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 245 | effectUniformArrays[e] = &fColorEffects[e].fSamplerUnis; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 246 | } |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 247 | |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 248 | builder.emitEffects(colorStages, |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 249 | fDesc.effectKeys(), |
| 250 | fDesc.numColorEffects(), |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 251 | &inColor, |
| 252 | &knownColorValue, |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 253 | effectUniformArrays.get(), |
| 254 | glEffects.get()); |
| 255 | |
| 256 | for (int e = 0; e < fDesc.numColorEffects(); ++e) { |
| 257 | fColorEffects[e].fGLEffect = glEffects[e]; |
| 258 | } |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 259 | } |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 260 | |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 261 | // Insert the color filter. This will soon be replaced by a color effect. |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 262 | if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 263 | const char* colorFilterColorUniName = NULL; |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 264 | fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 265 | kVec4f_GrSLType, "FilterColor", |
| 266 | &colorFilterColorUniName); |
| 267 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 268 | builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 269 | const char* color; |
| 270 | // add_color_filter requires a real input string. |
| 271 | if (knownColorValue == kOnes_GrSLConstantVec) { |
| 272 | color = GrGLSLOnesVecf(4); |
| 273 | } else if (knownColorValue == kZeros_GrSLConstantVec) { |
| 274 | color = GrGLSLZerosVecf(4); |
| 275 | } else { |
| 276 | color = inColor.c_str(); |
| 277 | } |
| 278 | add_color_filter(&builder, "filteredColor", filterColorCoeff, |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 279 | colorCoeff, colorFilterColorUniName, color); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 280 | inColor = "filteredColor"; |
| 281 | } |
| 282 | |
| 283 | /////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 284 | // compute the partial coverage |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 285 | SkString inCoverage = builder.getInputCoverage(); |
| 286 | GrSLConstantVec knownCoverageValue = builder.getKnownCoverageValue(); |
| skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 287 | |
| bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 288 | for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 289 | effectUniformArrays[e] = &fCoverageEffects[e].fSamplerUnis; |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 290 | } |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 291 | |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 292 | builder.emitEffects(coverageStages, |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 293 | fDesc.getEffectKeys() + fDesc.numColorEffects(), |
| 294 | fDesc.numCoverageEffects(), |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 295 | &inCoverage, |
| 296 | &knownCoverageValue, |
| commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 297 | effectUniformArrays.get(), |
| 298 | glEffects.get()); |
| 299 | for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { |
| 300 | fCoverageEffects[e].fGLEffect = glEffects[e]; |
| 301 | } |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 302 | |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 303 | // discard if coverage is zero |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 304 | if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageValue) { |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 305 | if (kZeros_GrSLConstantVec == knownCoverageValue) { |
| 306 | // This is unfortunate. |
| 307 | builder.fsCodeAppend("\tdiscard;\n"); |
| 308 | } else { |
| 309 | builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", |
| 310 | inCoverage.c_str()); |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 311 | } |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 312 | } |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 313 | |
| commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame^] | 314 | if (GrGLProgramDesc::CoverageOutputUsesSecondaryOutput(header.fCoverageOutput)) { |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 315 | const char* secondaryOutputName = builder.enableSecondaryOutput(); |
| 316 | |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 317 | // default coeff to ones for kCoverage_DualSrcOutput |
| 318 | SkString coeff; |
| 319 | GrSLConstantVec knownCoeffValue = kOnes_GrSLConstantVec; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 320 | if (GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput == header.fCoverageOutput) { |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 321 | // Get (1-A) into coeff |
| 322 | SkString inColorAlpha; |
| 323 | GrGLSLGetComponent4f(&inColorAlpha, |
| 324 | inColor.c_str(), |
| 325 | kA_GrColorComponentFlag, |
| 326 | knownColorValue, |
| 327 | true); |
| 328 | knownCoeffValue = GrGLSLSubtractf<1>(&coeff, |
| 329 | NULL, |
| 330 | inColorAlpha.c_str(), |
| 331 | kOnes_GrSLConstantVec, |
| 332 | knownColorValue, |
| 333 | true); |
| commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame^] | 334 | } else if (GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput == header.fCoverageOutput) { |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 335 | // Get (1-RGBA) into coeff |
| 336 | knownCoeffValue = GrGLSLSubtractf<4>(&coeff, |
| 337 | NULL, |
| 338 | inColor.c_str(), |
| 339 | kOnes_GrSLConstantVec, |
| 340 | knownColorValue, |
| 341 | true); |
| 342 | } |
| 343 | // Get coeff * coverage into modulate and then write that to the dual source output. |
| 344 | SkString modulate; |
| 345 | GrGLSLModulatef<4>(&modulate, |
| 346 | coeff.c_str(), |
| 347 | inCoverage.c_str(), |
| 348 | knownCoeffValue, |
| 349 | knownCoverageValue, |
| 350 | false); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 351 | builder.fsCodeAppendf("\t%s = %s;\n", secondaryOutputName, modulate.c_str()); |
| bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 354 | /////////////////////////////////////////////////////////////////////////// |
| 355 | // combine color and coverage as frag color |
| 356 | |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 357 | // Get "color * coverage" into fragColor |
| 358 | SkString fragColor; |
| 359 | GrSLConstantVec knownFragColorValue = GrGLSLModulatef<4>(&fragColor, |
| 360 | inColor.c_str(), |
| 361 | inCoverage.c_str(), |
| 362 | knownColorValue, |
| 363 | knownCoverageValue, |
| 364 | true); |
| 365 | // Now tack on "+(1-coverage)dst onto the frag color if we were asked to do so. |
| commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame^] | 366 | if (GrGLProgramDesc::kCombineWithDst_CoverageOutput == header.fCoverageOutput) { |
| bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 367 | SkString dstCoeff; |
| 368 | GrSLConstantVec knownDstCoeffValue = GrGLSLSubtractf<4>(&dstCoeff, |
| 369 | NULL, |
| 370 | inCoverage.c_str(), |
| 371 | kOnes_GrSLConstantVec, |
| 372 | knownCoverageValue, |
| 373 | true); |
| 374 | SkString dstContribution; |
| 375 | GrSLConstantVec knownDstContributionValue = GrGLSLModulatef<4>(&dstContribution, |
| 376 | dstCoeff.c_str(), |
| 377 | builder.dstColor(), |
| 378 | knownDstCoeffValue, |
| 379 | kNone_GrSLConstantVec, |
| 380 | true); |
| 381 | SkString oldFragColor = fragColor; |
| 382 | fragColor.reset(); |
| 383 | GrGLSLAddf<4>(&fragColor, |
| 384 | oldFragColor.c_str(), |
| 385 | dstContribution.c_str(), |
| 386 | knownFragColorValue, |
| 387 | knownDstContributionValue, |
| 388 | false); |
| 389 | } else { |
| 390 | expand_known_value4f(&fragColor, knownFragColorValue); |
| 391 | } |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 392 | builder.fsCodeAppendf("\t%s = %s;\n", builder.getColorOutputName(), fragColor.c_str()); |
| bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 393 | |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 394 | if (!builder.finish(&fProgramID)) { |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 398 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 399 | fUniformHandles.fDstCopyTopLeftUni = builder.getDstCopyTopLeftUniform(); |
| 400 | fUniformHandles.fDstCopyScaleUni = builder.getDstCopyScaleUniform(); |
| commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 401 | fUniformHandles.fColorUni = builder.getColorUniform(); |
| 402 | fUniformHandles.fCoverageUni = builder.getCoverageUniform(); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 403 | fUniformHandles.fDstCopySamplerUni = builder.getDstCopySamplerUniform(); |
| 404 | // This must be called after we set fDstCopySamplerUni above. |
| 405 | this->initSamplerUniforms(); |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 410 | void GrGLProgram::initSamplerUniforms() { |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 411 | GL_CALL(UseProgram(fProgramID)); |
| bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 412 | GrGLint texUnitIdx = 0; |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 413 | if (fUniformHandles.fDstCopySamplerUni.isValid()) { |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 414 | fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitIdx); |
| bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 415 | fDstCopyTexUnit = texUnitIdx++; |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 418 | for (int e = 0; e < fColorEffects.count(); ++e) { |
| 419 | this->initEffectSamplerUniforms(&fColorEffects[e], &texUnitIdx); |
| 420 | } |
| 421 | |
| 422 | for (int e = 0; e < fCoverageEffects.count(); ++e) { |
| 423 | this->initEffectSamplerUniforms(&fCoverageEffects[e], &texUnitIdx); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void GrGLProgram::initEffectSamplerUniforms(EffectAndSamplers* effect, int* texUnitIdx) { |
| 428 | int numSamplers = effect->fSamplerUnis.count(); |
| 429 | effect->fTextureUnits.reset(numSamplers); |
| 430 | for (int s = 0; s < numSamplers; ++s) { |
| 431 | UniformHandle handle = effect->fSamplerUnis[s]; |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 432 | if (handle.isValid()) { |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 433 | fUniformManager.setSampler(handle, *texUnitIdx); |
| 434 | effect->fTextureUnits[s] = (*texUnitIdx)++; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 435 | } |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 436 | } |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 439 | /////////////////////////////////////////////////////////////////////////////// |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 440 | |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 441 | void GrGLProgram::setEffectData(const GrEffectStage& stage, |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 442 | const EffectAndSamplers& effect) { |
| 443 | |
| 444 | // Let the GrGLEffect set its data. |
| 445 | bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttributeIndex; |
| 446 | GrDrawEffect drawEffect(stage, explicitLocalCoords); |
| 447 | effect.fGLEffect->setData(fUniformManager, drawEffect); |
| 448 | |
| 449 | // Bind the texures for the effect. |
| 450 | int numSamplers = effect.fSamplerUnis.count(); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 451 | SkASSERT((*stage.getEffect())->numTextures() == numSamplers); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 452 | for (int s = 0; s < numSamplers; ++s) { |
| 453 | UniformHandle handle = effect.fSamplerUnis[s]; |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 454 | if (handle.isValid()) { |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 455 | const GrTextureAccess& access = (*stage.getEffect())->textureAccess(s); |
| 456 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 457 | int unit = effect.fTextureUnits[s]; |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 458 | fGpu->bindTexture(unit, access.getParams(), texture); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 463 | void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts, |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 464 | const GrEffectStage* colorStages[], |
| 465 | const GrEffectStage* coverageStages[], |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 466 | const GrDeviceCoordTexture* dstCopy, |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 467 | SharedGLState* sharedState) { |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 468 | const GrDrawState& drawState = fGpu->getDrawState(); |
| bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 469 | |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 470 | GrColor color; |
| 471 | GrColor coverage; |
| 472 | if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { |
| 473 | color = 0; |
| 474 | coverage = 0; |
| 475 | } else if (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) { |
| 476 | color = 0xffffffff; |
| 477 | coverage = drawState.getCoverage(); |
| 478 | } else { |
| 479 | color = drawState.getColor(); |
| 480 | coverage = drawState.getCoverage(); |
| 481 | } |
| 482 | |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 483 | this->setColor(drawState, color, sharedState); |
| 484 | this->setCoverage(drawState, coverage, sharedState); |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 485 | this->setMatrixAndRenderTargetHeight(drawState); |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 486 | |
| 487 | // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 488 | if (fUniformHandles.fColorFilterUni.isValid() && |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 489 | fColorFilterColor != drawState.getColorFilterColor()) { |
| 490 | GrGLfloat c[4]; |
| 491 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
| 492 | fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
| 493 | fColorFilterColor = drawState.getColorFilterColor(); |
| 494 | } |
| 495 | |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 496 | if (NULL != dstCopy) { |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 497 | if (fUniformHandles.fDstCopyTopLeftUni.isValid()) { |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 498 | fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, |
| 499 | static_cast<GrGLfloat>(dstCopy->offset().fX), |
| 500 | static_cast<GrGLfloat>(dstCopy->offset().fY)); |
| 501 | fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, |
| 502 | 1.f / dstCopy->texture()->width(), |
| 503 | 1.f / dstCopy->texture()->height()); |
| 504 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()); |
| 505 | static GrTextureParams kParams; // the default is clamp, nearest filtering. |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 506 | fGpu->bindTexture(fDstCopyTexUnit, kParams, texture); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 507 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 508 | SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 509 | SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 510 | } |
| 511 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 512 | SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid()); |
| 513 | SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 514 | SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 515 | } |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 516 | |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 517 | for (int e = 0; e < fColorEffects.count(); ++e) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 518 | // We may have omitted the GrGLEffect because of the color filter logic in genProgram. |
| 519 | // This can be removed when the color filter is an effect. |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 520 | if (NULL != fColorEffects[e].fGLEffect) { |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 521 | this->setEffectData(*colorStages[e], fColorEffects[e]); |
| bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | for (int e = 0; e < fCoverageEffects.count(); ++e) { |
| 526 | if (NULL != fCoverageEffects[e].fGLEffect) { |
| commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 527 | this->setEffectData(*coverageStages[e], fCoverageEffects[e]); |
| bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 530 | } |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 531 | |
| 532 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 533 | GrColor color, |
| 534 | SharedGLState* sharedState) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 535 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 536 | if (!drawState.hasColorVertexAttribute()) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 537 | switch (header.fColorInput) { |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 538 | case GrGLProgramDesc::kAttribute_ColorInput: |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 539 | SkASSERT(-1 != header.fColorAttributeIndex); |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 540 | if (sharedState->fConstAttribColor != color || |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 541 | sharedState->fConstAttribColorIndex != header.fColorAttributeIndex) { |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 542 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 543 | GrGLfloat c[4]; |
| 544 | GrColorToRGBAFloat(color, c); |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 545 | GL_CALL(VertexAttrib4fv(header.fColorAttributeIndex, c)); |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 546 | sharedState->fConstAttribColor = color; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 547 | sharedState->fConstAttribColorIndex = header.fColorAttributeIndex; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 548 | } |
| 549 | break; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 550 | case GrGLProgramDesc::kUniform_ColorInput: |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 551 | if (fColor != color) { |
| 552 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 553 | GrGLfloat c[4]; |
| 554 | GrColorToRGBAFloat(color, c); |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 555 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 556 | fColor = color; |
| 557 | } |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 558 | sharedState->fConstAttribColorIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 559 | break; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 560 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 561 | case GrGLProgramDesc::kTransBlack_ColorInput: |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 562 | sharedState->fConstAttribColorIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 563 | break; |
| 564 | default: |
| 565 | GrCrash("Unknown color type."); |
| 566 | } |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 567 | } else { |
| 568 | sharedState->fConstAttribColorIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 573 | GrColor coverage, |
| 574 | SharedGLState* sharedState) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 575 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 576 | if (!drawState.hasCoverageVertexAttribute()) { |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 577 | switch (header.fCoverageInput) { |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 578 | case GrGLProgramDesc::kAttribute_ColorInput: |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 579 | if (sharedState->fConstAttribCoverage != coverage || |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 580 | sharedState->fConstAttribCoverageIndex != header.fCoverageAttributeIndex) { |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 581 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 582 | GrGLfloat c[4]; |
| 583 | GrColorToRGBAFloat(coverage, c); |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 584 | GL_CALL(VertexAttrib4fv(header.fCoverageAttributeIndex, c)); |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 585 | sharedState->fConstAttribCoverage = coverage; |
| bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 586 | sharedState->fConstAttribCoverageIndex = header.fCoverageAttributeIndex; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 587 | } |
| 588 | break; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 589 | case GrGLProgramDesc::kUniform_ColorInput: |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 590 | if (fCoverage != coverage) { |
| 591 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 592 | GrGLfloat c[4]; |
| 593 | GrColorToRGBAFloat(coverage, c); |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 594 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 595 | fCoverage = coverage; |
| 596 | } |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 597 | sharedState->fConstAttribCoverageIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 598 | break; |
| bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 599 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 600 | case GrGLProgramDesc::kTransBlack_ColorInput: |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 601 | sharedState->fConstAttribCoverageIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 602 | break; |
| 603 | default: |
| 604 | GrCrash("Unknown coverage type."); |
| 605 | } |
| jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 606 | } else { |
| 607 | sharedState->fConstAttribCoverageIndex = -1; |
| bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 610 | |
| 611 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 612 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 613 | SkISize size; |
| 614 | size.set(rt->width(), rt->height()); |
| 615 | |
| 616 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 617 | if (fUniformHandles.fRTHeightUni.isValid() && |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 618 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 619 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 620 | } |
| 621 | |
| 622 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 623 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) || |
| 624 | fMatrixState.fRenderTargetSize != size) { |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 625 | |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 626 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 627 | fMatrixState.fRenderTargetSize = size; |
| 628 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
| commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 629 | |
| 630 | GrGLfloat viewMatrix[3 * 3]; |
| 631 | fMatrixState.getGLMatrix<3>(viewMatrix); |
| 632 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); |
| bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 633 | } |
| 634 | } |