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