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