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) |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 42 | , fUniformManager(gpu) |
| 43 | , fHasVertexShader(false) |
| 44 | , fNumTexCoordSets(0) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 45 | fDesc = desc; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 46 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 48 | fDstCopyTexUnit = -1; |
| 49 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 50 | fColor = GrColor_ILLEGAL; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 52 | if (fDesc.getHeader().fHasVertexCode || |
commit-bot@chromium.org | 8e919ad | 2013-10-21 14:48:23 +0000 | [diff] [blame] | 53 | !fGpu->shouldUseFixedFunctionTexturing()) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 54 | GrGLFullShaderBuilder fullBuilder(fGpu, fUniformManager, fDesc); |
| 55 | if (this->genProgram(&fullBuilder, colorStages, coverageStages)) { |
| 56 | fUniformHandles.fViewMatrixUni = fullBuilder.getViewMatrixUniform(); |
| 57 | fHasVertexShader = true; |
| 58 | } |
| 59 | } else { |
| 60 | GrGLFragmentOnlyShaderBuilder fragmentOnlyBuilder(fGpu, fUniformManager, fDesc); |
| 61 | if (this->genProgram(&fragmentOnlyBuilder, colorStages, coverageStages)) { |
| 62 | fNumTexCoordSets = fragmentOnlyBuilder.getNumTexCoordSets(); |
| 63 | } |
| 64 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 68 | if (fProgramID) { |
| 69 | GL_CALL(DeleteProgram(fProgramID)); |
| 70 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 73 | void GrGLProgram::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 74 | fProgramID = 0; |
| 75 | } |
| 76 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 77 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 78 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 79 | switch (fDesc.getHeader().fCoverageOutput) { |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 80 | case GrGLProgramDesc::kModulate_CoverageOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 81 | break; |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 82 | // The prog will write a coverage value to the secondary |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 83 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 84 | case GrGLProgramDesc::kSecondaryCoverage_CoverageOutput: |
| 85 | case GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput: |
| 86 | case GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput: |
| 87 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
| 88 | break; |
| 89 | case GrGLProgramDesc::kCombineWithDst_CoverageOutput: |
| 90 | // 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] | 91 | SkASSERT(kOne_GrBlendCoeff == *srcCoeff && kZero_GrBlendCoeff == *dstCoeff); |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 92 | break; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 93 | default: |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 94 | GrCrash("Unexpected coverage output"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 99 | bool GrGLProgram::genProgram(GrGLShaderBuilder* builder, |
| 100 | const GrEffectStage* colorStages[], |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 101 | const GrEffectStage* coverageStages[]) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 102 | SkASSERT(0 == fProgramID); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 103 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 104 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 105 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 106 | // incoming color to current stage being processed. |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 107 | GrGLSLExpr4 inColor = builder->getInputColor(); |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 109 | fColorEffects.reset( |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 110 | builder->createAndEmitEffects(colorStages, |
| 111 | fDesc.effectKeys(), |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 112 | fDesc.numColorEffects(), |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 113 | &inColor)); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 115 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 116 | // compute the partial coverage |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 117 | GrGLSLExpr4 inCoverage = builder->getInputCoverage(); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 119 | fCoverageEffects.reset( |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 120 | builder->createAndEmitEffects(coverageStages, |
| 121 | fDesc.getEffectKeys() + fDesc.numColorEffects(), |
| 122 | fDesc.numCoverageEffects(), |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 123 | &inCoverage)); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 125 | // discard if coverage is zero |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 126 | if (header.fDiscardIfZeroCoverage && !inCoverage.isOnes()) { |
| 127 | if (inCoverage.isZeros()) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 128 | // This is unfortunate. |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 129 | builder->fsCodeAppend("\tdiscard;\n"); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 130 | } else { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 131 | builder->fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", |
| 132 | inCoverage.c_str()); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 133 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 134 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame] | 136 | if (GrGLProgramDesc::CoverageOutputUsesSecondaryOutput(header.fCoverageOutput)) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 137 | const char* secondaryOutputName = builder->enableSecondaryOutput(); |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 138 | |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 139 | // default coeff to ones for kCoverage_DualSrcOutput |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 140 | GrGLSLExpr4 coeff(1); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 141 | if (GrGLProgramDesc::kSecondaryCoverageISA_CoverageOutput == header.fCoverageOutput) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 142 | // Get (1-A) into coeff |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 143 | coeff = GrGLSLExpr4::VectorCast(GrGLSLExpr1(1) - inColor.a()); |
commit-bot@chromium.org | 949eef0 | 2013-10-01 18:43:29 +0000 | [diff] [blame] | 144 | } else if (GrGLProgramDesc::kSecondaryCoverageISC_CoverageOutput == header.fCoverageOutput) { |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 145 | // Get (1-RGBA) into coeff |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 146 | coeff = GrGLSLExpr4(1) - inColor; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 147 | } |
| 148 | // Get coeff * coverage into modulate and then write that to the dual source output. |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 149 | builder->fsCodeAppendf("\t%s = %s;\n", secondaryOutputName, (coeff * inCoverage).c_str()); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////// |
| 153 | // combine color and coverage as frag color |
| 154 | |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 155 | // Get "color * coverage" into fragColor |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 156 | GrGLSLExpr4 fragColor = inColor * inCoverage; |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 157 | // 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] | 158 | if (GrGLProgramDesc::kCombineWithDst_CoverageOutput == header.fCoverageOutput) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 159 | GrGLSLExpr4 dstCoeff = GrGLSLExpr4(1) - inCoverage; |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 161 | GrGLSLExpr4 dstContribution = dstCoeff * GrGLSLExpr4(builder->dstColor()); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 162 | |
| 163 | fragColor = fragColor + dstContribution; |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 164 | } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 165 | builder->fsCodeAppendf("\t%s = %s;\n", builder->getColorOutputName(), fragColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 167 | if (!builder->finish(&fProgramID)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 171 | fUniformHandles.fRTHeightUni = builder->getRTHeightUniform(); |
| 172 | fUniformHandles.fDstCopyTopLeftUni = builder->getDstCopyTopLeftUniform(); |
| 173 | fUniformHandles.fDstCopyScaleUni = builder->getDstCopyScaleUniform(); |
| 174 | fUniformHandles.fColorUni = builder->getColorUniform(); |
| 175 | fUniformHandles.fCoverageUni = builder->getCoverageUniform(); |
| 176 | fUniformHandles.fDstCopySamplerUni = builder->getDstCopySamplerUniform(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 177 | // This must be called after we set fDstCopySamplerUni above. |
| 178 | this->initSamplerUniforms(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 183 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 184 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 185 | GrGLint texUnitIdx = 0; |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 186 | if (fUniformHandles.fDstCopySamplerUni.isValid()) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 187 | fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitIdx); |
bsalomon@google.com | 804e994 | 2013-06-06 18:04:38 +0000 | [diff] [blame] | 188 | fDstCopyTexUnit = texUnitIdx++; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 189 | } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 190 | fColorEffects->initSamplers(fUniformManager, &texUnitIdx); |
| 191 | fCoverageEffects->initSamplers(fUniformManager, &texUnitIdx); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 192 | } |
| 193 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 194 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 195 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 196 | void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 197 | const GrEffectStage* colorStages[], |
| 198 | const GrEffectStage* coverageStages[], |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 199 | const GrDeviceCoordTexture* dstCopy, |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 200 | SharedGLState* sharedState) { |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 201 | const GrDrawState& drawState = fGpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 203 | GrColor color; |
| 204 | GrColor coverage; |
| 205 | if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { |
| 206 | color = 0; |
| 207 | coverage = 0; |
| 208 | } else if (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) { |
| 209 | color = 0xffffffff; |
| 210 | coverage = drawState.getCoverage(); |
| 211 | } else { |
| 212 | color = drawState.getColor(); |
| 213 | coverage = drawState.getCoverage(); |
| 214 | } |
| 215 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 216 | this->setColor(drawState, color, sharedState); |
| 217 | this->setCoverage(drawState, coverage, sharedState); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 218 | this->setMatrixAndRenderTargetHeight(drawState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 219 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 220 | if (NULL != dstCopy) { |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 221 | if (fUniformHandles.fDstCopyTopLeftUni.isValid()) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 222 | fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, |
| 223 | static_cast<GrGLfloat>(dstCopy->offset().fX), |
| 224 | static_cast<GrGLfloat>(dstCopy->offset().fY)); |
| 225 | fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, |
| 226 | 1.f / dstCopy->texture()->width(), |
| 227 | 1.f / dstCopy->texture()->height()); |
| 228 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()); |
| 229 | static GrTextureParams kParams; // the default is clamp, nearest filtering. |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 230 | fGpu->bindTexture(fDstCopyTexUnit, kParams, texture); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 231 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 232 | SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 233 | SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 234 | } |
| 235 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 236 | SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid()); |
| 237 | SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 238 | SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 239 | } |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 240 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 241 | fColorEffects->setData(fGpu, fUniformManager, colorStages); |
| 242 | fCoverageEffects->setData(fGpu, fUniformManager, coverageStages); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 243 | |
| 244 | if (!fHasVertexShader) { |
| 245 | fGpu->disableUnusedTexGen(fNumTexCoordSets); |
| 246 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 247 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 248 | |
| 249 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 250 | GrColor color, |
| 251 | SharedGLState* sharedState) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 252 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 253 | if (!drawState.hasColorVertexAttribute()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 254 | switch (header.fColorInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 255 | case GrGLProgramDesc::kAttribute_ColorInput: |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 256 | SkASSERT(-1 != header.fColorAttributeIndex); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 257 | if (sharedState->fConstAttribColor != color || |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 258 | sharedState->fConstAttribColorIndex != header.fColorAttributeIndex) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 259 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 260 | GrGLfloat c[4]; |
| 261 | GrColorToRGBAFloat(color, c); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 262 | GL_CALL(VertexAttrib4fv(header.fColorAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 263 | sharedState->fConstAttribColor = color; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 264 | sharedState->fConstAttribColorIndex = header.fColorAttributeIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 265 | } |
| 266 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 267 | case GrGLProgramDesc::kUniform_ColorInput: |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 268 | if (fColor != color && fUniformHandles.fColorUni.isValid()) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 269 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 270 | GrGLfloat c[4]; |
| 271 | GrColorToRGBAFloat(color, c); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 272 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 273 | fColor = color; |
| 274 | } |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 275 | sharedState->fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 276 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 277 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 278 | case GrGLProgramDesc::kTransBlack_ColorInput: |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 279 | sharedState->fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 280 | break; |
| 281 | default: |
| 282 | GrCrash("Unknown color type."); |
| 283 | } |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 284 | } else { |
| 285 | sharedState->fConstAttribColorIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 290 | GrColor coverage, |
| 291 | SharedGLState* sharedState) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 292 | const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 293 | if (!drawState.hasCoverageVertexAttribute()) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 294 | switch (header.fCoverageInput) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 295 | case GrGLProgramDesc::kAttribute_ColorInput: |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 296 | if (sharedState->fConstAttribCoverage != coverage || |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 297 | sharedState->fConstAttribCoverageIndex != header.fCoverageAttributeIndex) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 298 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 299 | GrGLfloat c[4]; |
| 300 | GrColorToRGBAFloat(coverage, c); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 301 | GL_CALL(VertexAttrib4fv(header.fCoverageAttributeIndex, c)); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 302 | sharedState->fConstAttribCoverage = coverage; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 303 | sharedState->fConstAttribCoverageIndex = header.fCoverageAttributeIndex; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 304 | } |
| 305 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 306 | case GrGLProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 307 | if (fCoverage != coverage) { |
| 308 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 309 | GrGLfloat c[4]; |
| 310 | GrColorToRGBAFloat(coverage, c); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 311 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 312 | fCoverage = coverage; |
| 313 | } |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 314 | sharedState->fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 315 | break; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 316 | case GrGLProgramDesc::kSolidWhite_ColorInput: |
| 317 | case GrGLProgramDesc::kTransBlack_ColorInput: |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 318 | sharedState->fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 319 | break; |
| 320 | default: |
| 321 | GrCrash("Unknown coverage type."); |
| 322 | } |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 323 | } else { |
| 324 | sharedState->fConstAttribCoverageIndex = -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 327 | |
| 328 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 329 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 330 | SkISize size; |
| 331 | size.set(rt->width(), rt->height()); |
| 332 | |
| 333 | // 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] | 334 | if (fUniformHandles.fRTHeightUni.isValid() && |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 335 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 336 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 337 | } |
| 338 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 339 | if (!fHasVertexShader) { |
| 340 | SkASSERT(!fUniformHandles.fViewMatrixUni.isValid()); |
| 341 | fGpu->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin()); |
| 342 | } else if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 343 | fMatrixState.fRenderTargetSize != size || |
| 344 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix())) { |
| 345 | SkASSERT(fUniformHandles.fViewMatrixUni.isValid()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 346 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 347 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 348 | fMatrixState.fRenderTargetSize = size; |
| 349 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 350 | |
| 351 | GrGLfloat viewMatrix[3 * 3]; |
| 352 | fMatrixState.getGLMatrix<3>(viewMatrix); |
| 353 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 354 | } |
| 355 | } |