blob: c63914893d2950a0be069a25f1526d24efc0cc84 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comf93e7172011-03-31 21:26:24 +00006 */
7
8#include "GrGLProgram.h"
9
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "GrAllocator.h"
bsalomon@google.coma469c282012-10-24 18:28:34 +000011#include "GrEffect.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000012#include "GrCoordTransform.h"
bsalomon@google.comc7818882013-03-20 19:19:53 +000013#include "GrDrawEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000014#include "GrGLEffect.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000015#include "GrGpuGL.h"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000016#include "GrGLShaderVar.h"
bsalomon@google.com018f1792013-04-18 19:36:09 +000017#include "GrGLSL.h"
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkXfermode.h"
19
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000020#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.comecb60aa2012-07-18 13:20:29 +000022
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000023GrGLProgram* GrGLProgram::Create(GrGpuGL* gpu,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000024 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000025 const GrEffectStage* colorStages[],
26 const GrEffectStage* coverageStages[]) {
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000027 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gpu, desc, colorStages, coverageStages));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000028 if (!program->succeeded()) {
29 delete program;
30 program = NULL;
31 }
32 return program;
33}
34
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000035GrGLProgram::GrGLProgram(GrGpuGL* gpu,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000036 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000037 const GrEffectStage* colorStages[],
38 const GrEffectStage* coverageStages[])
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000039: fGpu(gpu)
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000040, fUniformManager(gpu)
41, fHasVertexShader(false)
42, fNumTexCoordSets(0) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000043 fDesc = desc;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000044 fProgramID = 0;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000045
bsalomon@google.com804e9942013-06-06 18:04:38 +000046 fDstCopyTexUnit = -1;
47
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000048 fColor = GrColor_ILLEGAL;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000049
commit-bot@chromium.org03652612014-05-29 19:09:52 +000050 GrGLShaderBuilder::GenProgramOutput output;
51
52 if (GrGLShaderBuilder::GenProgram(gpu, fUniformManager, desc, colorStages, coverageStages,
53 &output)) {
54 fProgramID = output.fProgramID;
55 fUniformHandles = output.fUniformHandles;
56 fColorEffects.reset(output.fColorEffects);
57 fCoverageEffects.reset(output.fCoverageEffects);
58 fHasVertexShader = output.fHasVS;
59 fNumTexCoordSets = output.fNumTexCoordSets;
60 fGpu = gpu;
61 this->initSamplerUniforms();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000062 }
junov@google.comf93e7172011-03-31 21:26:24 +000063}
64
65GrGLProgram::~GrGLProgram() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000066 if (fProgramID) {
67 GL_CALL(DeleteProgram(fProgramID));
68 }
junov@google.comf93e7172011-03-31 21:26:24 +000069}
70
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000071void GrGLProgram::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000072 fProgramID = 0;
73}
74
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +000075void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076 GrBlendCoeff* dstCoeff) const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000077 switch (fDesc.getHeader().fCoverageOutput) {
bsalomon@google.com5920ac22013-04-19 13:14:45 +000078 case GrGLProgramDesc::kModulate_CoverageOutput:
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079 break;
bsalomon@google.com5920ac22013-04-19 13:14:45 +000080 // The prog will write a coverage value to the secondary
bsalomon@google.com271cffc2011-05-20 14:13:56 +000081 // output and the dst is blended by one minus that value.
bsalomon@google.com5920ac22013-04-19 13:14:45 +000082 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.orgf6de4752013-08-17 00:02:59 +000089 SkASSERT(kOne_GrBlendCoeff == *srcCoeff && kZero_GrBlendCoeff == *dstCoeff);
bsalomon@google.com5920ac22013-04-19 13:14:45 +000090 break;
bsalomon@google.com271cffc2011-05-20 14:13:56 +000091 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000092 SkFAIL("Unexpected coverage output");
bsalomon@google.com271cffc2011-05-20 14:13:56 +000093 break;
94 }
95}
96
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000097void GrGLProgram::initSamplerUniforms() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000098 GL_CALL(UseProgram(fProgramID));
bsalomon@google.com34cccde2013-01-04 18:34:30 +000099 GrGLint texUnitIdx = 0;
commit-bot@chromium.org7425c122013-08-14 18:14:19 +0000100 if (fUniformHandles.fDstCopySamplerUni.isValid()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000101 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitIdx);
bsalomon@google.com804e9942013-06-06 18:04:38 +0000102 fDstCopyTexUnit = texUnitIdx++;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000103 }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000104 fColorEffects->initSamplers(fUniformManager, &texUnitIdx);
105 fCoverageEffects->initSamplers(fUniformManager, &texUnitIdx);
bsalomon@google.com91961302011-05-09 18:39:58 +0000106}
107
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000108///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +0000109
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000110void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000111 const GrEffectStage* colorStages[],
112 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000113 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com91207482013-02-12 21:45:24 +0000114 SharedGLState* sharedState) {
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000115 const GrDrawState& drawState = fGpu->getDrawState();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000116
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000117 GrColor color;
118 GrColor coverage;
119 if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) {
120 color = 0;
121 coverage = 0;
122 } else if (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) {
123 color = 0xffffffff;
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000124 coverage = drawState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000125 } else {
126 color = drawState.getColor();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000127 coverage = drawState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000128 }
129
bsalomon@google.com91207482013-02-12 21:45:24 +0000130 this->setColor(drawState, color, sharedState);
131 this->setCoverage(drawState, coverage, sharedState);
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000132 this->setMatrixAndRenderTargetHeight(drawState);
bsalomon@google.com91207482013-02-12 21:45:24 +0000133
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000134 if (NULL != dstCopy) {
commit-bot@chromium.org7425c122013-08-14 18:14:19 +0000135 if (fUniformHandles.fDstCopyTopLeftUni.isValid()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000136 fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni,
137 static_cast<GrGLfloat>(dstCopy->offset().fX),
138 static_cast<GrGLfloat>(dstCopy->offset().fY));
139 fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni,
140 1.f / dstCopy->texture()->width(),
141 1.f / dstCopy->texture()->height());
142 GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture());
143 static GrTextureParams kParams; // the default is clamp, nearest filtering.
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000144 fGpu->bindTexture(fDstCopyTexUnit, kParams, texture);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000145 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000146 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid());
147 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid());
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000148 }
149 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000150 SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid());
151 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid());
152 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid());
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000153 }
bsalomon@google.comc7818882013-03-20 19:19:53 +0000154
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000155 fColorEffects->setData(fGpu, fUniformManager, colorStages);
156 fCoverageEffects->setData(fGpu, fUniformManager, coverageStages);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000157
commit-bot@chromium.org20807222013-11-01 11:54:54 +0000158
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000159 // PathTexGen state applies to the the fixed function vertex shader. For
160 // custom shaders, it's ignored, so we don't need to change the texgen
161 // settings in that case.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000162 if (!fHasVertexShader) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000163 fGpu->flushPathTexGenSettings(fNumTexCoordSets);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000164 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000165}
bsalomon@google.com91207482013-02-12 21:45:24 +0000166
167void GrGLProgram::setColor(const GrDrawState& drawState,
168 GrColor color,
169 SharedGLState* sharedState) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000170 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader();
jvanverth@google.com054ae992013-04-01 20:06:51 +0000171 if (!drawState.hasColorVertexAttribute()) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000172 switch (header.fColorInput) {
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000173 case GrGLProgramDesc::kAttribute_ColorInput:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000174 SkASSERT(-1 != header.fColorAttributeIndex);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000175 if (sharedState->fConstAttribColor != color ||
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000176 sharedState->fConstAttribColorIndex != header.fColorAttributeIndex) {
bsalomon@google.com91207482013-02-12 21:45:24 +0000177 // OpenGL ES only supports the float varieties of glVertexAttrib
178 GrGLfloat c[4];
179 GrColorToRGBAFloat(color, c);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000180 GL_CALL(VertexAttrib4fv(header.fColorAttributeIndex, c));
bsalomon@google.com91207482013-02-12 21:45:24 +0000181 sharedState->fConstAttribColor = color;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000182 sharedState->fConstAttribColorIndex = header.fColorAttributeIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +0000183 }
184 break;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000185 case GrGLProgramDesc::kUniform_ColorInput:
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000186 if (fColor != color && fUniformHandles.fColorUni.isValid()) {
bsalomon@google.com91207482013-02-12 21:45:24 +0000187 // OpenGL ES doesn't support unsigned byte varieties of glUniform
188 GrGLfloat c[4];
189 GrColorToRGBAFloat(color, c);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000190 fUniformManager.set4fv(fUniformHandles.fColorUni, 1, c);
bsalomon@google.com91207482013-02-12 21:45:24 +0000191 fColor = color;
192 }
jvanverth@google.com054ae992013-04-01 20:06:51 +0000193 sharedState->fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000194 break;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000195 case GrGLProgramDesc::kSolidWhite_ColorInput:
196 case GrGLProgramDesc::kTransBlack_ColorInput:
jvanverth@google.com054ae992013-04-01 20:06:51 +0000197 sharedState->fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000198 break;
199 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000200 SkFAIL("Unknown color type.");
bsalomon@google.com91207482013-02-12 21:45:24 +0000201 }
jvanverth@google.com054ae992013-04-01 20:06:51 +0000202 } else {
203 sharedState->fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000204 }
205}
206
207void GrGLProgram::setCoverage(const GrDrawState& drawState,
208 GrColor coverage,
209 SharedGLState* sharedState) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader();
jvanverth@google.com054ae992013-04-01 20:06:51 +0000211 if (!drawState.hasCoverageVertexAttribute()) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000212 switch (header.fCoverageInput) {
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000213 case GrGLProgramDesc::kAttribute_ColorInput:
jvanverth@google.com054ae992013-04-01 20:06:51 +0000214 if (sharedState->fConstAttribCoverage != coverage ||
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000215 sharedState->fConstAttribCoverageIndex != header.fCoverageAttributeIndex) {
bsalomon@google.com91207482013-02-12 21:45:24 +0000216 // OpenGL ES only supports the float varieties of glVertexAttrib
217 GrGLfloat c[4];
218 GrColorToRGBAFloat(coverage, c);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000219 GL_CALL(VertexAttrib4fv(header.fCoverageAttributeIndex, c));
bsalomon@google.com91207482013-02-12 21:45:24 +0000220 sharedState->fConstAttribCoverage = coverage;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000221 sharedState->fConstAttribCoverageIndex = header.fCoverageAttributeIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +0000222 }
223 break;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000224 case GrGLProgramDesc::kUniform_ColorInput:
bsalomon@google.com91207482013-02-12 21:45:24 +0000225 if (fCoverage != coverage) {
226 // OpenGL ES doesn't support unsigned byte varieties of glUniform
227 GrGLfloat c[4];
228 GrColorToRGBAFloat(coverage, c);
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +0000229 fUniformManager.set4fv(fUniformHandles.fCoverageUni, 1, c);
bsalomon@google.com91207482013-02-12 21:45:24 +0000230 fCoverage = coverage;
231 }
jvanverth@google.com054ae992013-04-01 20:06:51 +0000232 sharedState->fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000233 break;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000234 case GrGLProgramDesc::kSolidWhite_ColorInput:
235 case GrGLProgramDesc::kTransBlack_ColorInput:
jvanverth@google.com054ae992013-04-01 20:06:51 +0000236 sharedState->fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000237 break;
238 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000239 SkFAIL("Unknown coverage type.");
bsalomon@google.com91207482013-02-12 21:45:24 +0000240 }
jvanverth@google.com054ae992013-04-01 20:06:51 +0000241 } else {
242 sharedState->fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000243 }
244}
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000245
246void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) {
247 const GrRenderTarget* rt = drawState.getRenderTarget();
248 SkISize size;
249 size.set(rt->width(), rt->height());
250
251 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
commit-bot@chromium.org7425c122013-08-14 18:14:19 +0000252 if (fUniformHandles.fRTHeightUni.isValid() &&
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000253 fMatrixState.fRenderTargetSize.fHeight != size.fHeight) {
254 fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight));
255 }
256
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000257 if (!fHasVertexShader) {
258 SkASSERT(!fUniformHandles.fViewMatrixUni.isValid());
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000259 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000260 fGpu->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin());
261 } else if (fMatrixState.fRenderTargetOrigin != rt->origin() ||
262 fMatrixState.fRenderTargetSize != size ||
263 !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix())) {
264 SkASSERT(fUniformHandles.fViewMatrixUni.isValid());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000265
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000266 fMatrixState.fViewMatrix = drawState.getViewMatrix();
267 fMatrixState.fRenderTargetSize = size;
268 fMatrixState.fRenderTargetOrigin = rt->origin();
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000269
270 GrGLfloat viewMatrix[3 * 3];
271 fMatrixState.getGLMatrix<3>(viewMatrix);
272 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix);
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000273
274 GrGLfloat rtAdjustmentVec[4];
275 fMatrixState.getRTAdjustmentVec(rtAdjustmentVec);
276 fUniformManager.set4fv(fUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000277 }
278}