blob: 1bdcf87a41208c003a727f36e408c13d758f5c83 [file] [log] [blame]
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +00001/*
2 * Copyright 2013 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.
6 */
7
8#include "GrGLProgramEffects.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +00009#include "gl/GrGLEffect.h"
kkinnunenec56e452014-08-25 22:21:16 -070010#include "gl/GrGLPathRendering.h"
joshualitt408d6122014-09-17 07:00:35 -070011#include "gl/builders/GrGLFullProgramBuilder.h"
12#include "gl/builders/GrGLFragmentOnlyProgramBuilder.h"
joshualitt249af152014-09-15 11:41:13 -070013#include "gl/GrGLGeometryProcessor.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000014#include "gl/GrGpuGL.h"
15
joshualitt23e280d2014-09-18 12:26:38 -070016typedef GrGLEffect::TransformedCoords TransformedCoords;
17typedef GrGLEffect::TransformedCoordsArray TransformedCoordsArray;
18typedef GrGLEffect::TextureSampler TextureSampler;
19typedef GrGLEffect::TextureSamplerArray TextureSamplerArray;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000020
21namespace {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000022/**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000023 * Retrieves the final matrix that a transform needs to apply to its source coords.
24 */
joshualitt49586be2014-09-16 08:21:41 -070025SkMatrix get_transform_matrix(const GrEffectStage& effectStage,
26 bool useExplicitLocalCoords,
27 int transformIdx) {
28 const GrCoordTransform& coordTransform = effectStage.getEffect()->coordTransform(transformIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000029 SkMatrix combined;
bsalomon06736762014-08-04 10:56:39 -070030
31 if (kLocal_GrCoordSet == coordTransform.sourceCoords()) {
32 // If we have explicit local coords then we shouldn't need a coord change.
joshualitt49586be2014-09-16 08:21:41 -070033 const SkMatrix& ccm =
34 useExplicitLocalCoords ? SkMatrix::I() : effectStage.getCoordChangeMatrix();
35 combined.setConcat(coordTransform.getMatrix(), ccm);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000036 } else {
37 combined = coordTransform.getMatrix();
38 }
39 if (coordTransform.reverseY()) {
40 // combined.postScale(1,-1);
41 // combined.postTranslate(0,1);
42 combined.set(SkMatrix::kMSkewY,
43 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
44 combined.set(SkMatrix::kMScaleY,
45 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
46 combined.set(SkMatrix::kMTransY,
47 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
48 }
49 return combined;
50}
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000051}
52
53////////////////////////////////////////////////////////////////////////////////
54
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000055GrGLProgramEffects::~GrGLProgramEffects() {
56 int numEffects = fGLEffects.count();
57 for (int e = 0; e < numEffects; ++e) {
58 SkDELETE(fGLEffects[e]);
59 }
60}
61
kkinnunen7510b222014-07-30 00:04:16 -070062void GrGLProgramEffects::initSamplers(const GrGLProgramDataManager& programResourceManager, int* texUnitIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000063 int numEffects = fGLEffects.count();
64 SkASSERT(numEffects == fSamplers.count());
65 for (int e = 0; e < numEffects; ++e) {
66 SkTArray<Sampler, true>& samplers = fSamplers[e];
67 int numSamplers = samplers.count();
68 for (int s = 0; s < numSamplers; ++s) {
69 SkASSERT(samplers[s].fUniform.isValid());
kkinnunen7510b222014-07-30 00:04:16 -070070 programResourceManager.setSampler(samplers[s].fUniform, *texUnitIdx);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000071 samplers[s].fTextureUnit = (*texUnitIdx)++;
72 }
73 }
74}
75
joshualitt49586be2014-09-16 08:21:41 -070076void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect& effect, int effectIdx) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000077 const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx];
78 int numSamplers = samplers.count();
joshualitt49586be2014-09-16 08:21:41 -070079 SkASSERT(numSamplers == effect.numTextures());
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000080 for (int s = 0; s < numSamplers; ++s) {
81 SkASSERT(samplers[s].fTextureUnit >= 0);
joshualitt49586be2014-09-16 08:21:41 -070082 const GrTextureAccess& textureAccess = effect.textureAccess(s);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000083 gpu->bindTexture(samplers[s].fTextureUnit,
84 textureAccess.getParams(),
85 static_cast<GrGLTexture*>(textureAccess.getTexture()));
86 }
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000091void GrGLVertexProgramEffects::setData(GrGpuGL* gpu,
kkinnunenec56e452014-08-25 22:21:16 -070092 GrGpu::DrawType drawType,
93 const GrGLProgramDataManager& programDataManager,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000094 const GrEffectStage* effectStages[]) {
95 int numEffects = fGLEffects.count();
96 SkASSERT(numEffects == fTransforms.count());
97 SkASSERT(numEffects == fSamplers.count());
98 for (int e = 0; e < numEffects; ++e) {
joshualitt49586be2014-09-16 08:21:41 -070099 const GrEffectStage& effectStage = *effectStages[e];
100 const GrEffect& effect = *effectStage.getEffect();
101 fGLEffects[e]->setData(programDataManager, effect);
kkinnunenec56e452014-08-25 22:21:16 -0700102 if (GrGpu::IsPathRenderingDrawType(drawType)) {
joshualitt49586be2014-09-16 08:21:41 -0700103 this->setPathTransformData(gpu, programDataManager, effectStage, e);
kkinnunenec56e452014-08-25 22:21:16 -0700104 } else {
joshualitt49586be2014-09-16 08:21:41 -0700105 this->setTransformData(gpu, programDataManager, effectStage, e);
kkinnunenec56e452014-08-25 22:21:16 -0700106 }
107
joshualitt49586be2014-09-16 08:21:41 -0700108 this->bindTextures(gpu, effect, e);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000109 }
110}
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000111
kkinnunenec56e452014-08-25 22:21:16 -0700112void GrGLVertexProgramEffects::setTransformData(GrGpuGL* gpu,
113 const GrGLProgramDataManager& pdman,
joshualitt49586be2014-09-16 08:21:41 -0700114 const GrEffectStage& effectStage,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000115 int effectIdx) {
116 SkTArray<Transform, true>& transforms = fTransforms[effectIdx];
117 int numTransforms = transforms.count();
joshualitt49586be2014-09-16 08:21:41 -0700118 SkASSERT(numTransforms == effectStage.getEffect()->numTransforms());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000119 for (int t = 0; t < numTransforms; ++t) {
egdaniel4c6443e2014-06-24 13:43:12 -0700120 SkASSERT(transforms[t].fHandle.isValid());
joshualitt49586be2014-09-16 08:21:41 -0700121 const SkMatrix& matrix = get_transform_matrix(effectStage, fHasExplicitLocalCoords, t);
egdaniel4c6443e2014-06-24 13:43:12 -0700122 if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) {
kkinnunenec56e452014-08-25 22:21:16 -0700123 pdman.setSkMatrix(transforms[t].fHandle, matrix);
egdaniel4c6443e2014-06-24 13:43:12 -0700124 transforms[t].fCurrentValue = matrix;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000125 }
126 }
127}
128
kkinnunenec56e452014-08-25 22:21:16 -0700129void GrGLVertexProgramEffects::setPathTransformData(GrGpuGL* gpu,
130 const GrGLProgramDataManager& pdman,
joshualitt49586be2014-09-16 08:21:41 -0700131 const GrEffectStage& effectStage,
kkinnunenec56e452014-08-25 22:21:16 -0700132 int effectIdx) {
133 SkTArray<PathTransform, true>& transforms = fPathTransforms[effectIdx];
134 int numTransforms = transforms.count();
joshualitt49586be2014-09-16 08:21:41 -0700135 SkASSERT(numTransforms == effectStage.getEffect()->numTransforms());
kkinnunenec56e452014-08-25 22:21:16 -0700136 for (int t = 0; t < numTransforms; ++t) {
137 SkASSERT(transforms[t].fHandle.isValid());
joshualitt49586be2014-09-16 08:21:41 -0700138 const SkMatrix& transform = get_transform_matrix(effectStage, fHasExplicitLocalCoords, t);
kkinnunenec56e452014-08-25 22:21:16 -0700139 if (transforms[t].fCurrentValue.cheapEqualTo(transform)) {
140 continue;
141 }
142 transforms[t].fCurrentValue = transform;
143 switch (transforms[t].fType) {
144 case kVec2f_GrSLType:
145 pdman.setProgramPathFragmentInputTransform(transforms[t].fHandle, 2, transform);
146 break;
147 case kVec3f_GrSLType:
148 pdman.setProgramPathFragmentInputTransform(transforms[t].fHandle, 3, transform);
149 break;
150 default:
151 SkFAIL("Unexpected matrix type.");
152 }
153 }
154}
155
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000156////////////////////////////////////////////////////////////////////////////////
157
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000158void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu,
kkinnunenec56e452014-08-25 22:21:16 -0700159 GrGpu::DrawType,
160 const GrGLProgramDataManager& pdman,
161 const GrEffectStage* effectStages[]) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000162 int numEffects = fGLEffects.count();
163 SkASSERT(numEffects == fTransforms.count());
164 SkASSERT(numEffects == fSamplers.count());
165 for (int e = 0; e < numEffects; ++e) {
joshualitt49586be2014-09-16 08:21:41 -0700166 const GrEffectStage& effectStage = *effectStages[e];
167 const GrEffect& effect = *effectStage.getEffect();
168 fGLEffects[e]->setData(pdman, effect);
169 this->setPathTexGenState(gpu, effectStage, e);
170 this->bindTextures(gpu, effect, e);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000171 }
172}
173
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000174void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu,
joshualitt49586be2014-09-16 08:21:41 -0700175 const GrEffectStage& effectStage,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000176 int effectIdx) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000177 int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex;
joshualitt49586be2014-09-16 08:21:41 -0700178 int numTransforms = effectStage.getEffect()->numTransforms();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000179 for (int t = 0; t < numTransforms; ++t) {
joshualitt23e280d2014-09-18 12:26:38 -0700180 const SkMatrix& transform = get_transform_matrix(effectStage, false, t);
181 GrGLPathRendering::PathTexGenComponents components =
182 GrGLPathRendering::kST_PathTexGenComponents;
183 if (effectStage.isPerspectiveCoordTransform(t, false)) {
184 components = GrGLPathRendering::kSTR_PathTexGenComponents;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000185 }
joshualitt23e280d2014-09-18 12:26:38 -0700186 gpu->glPathRendering()->enablePathTexGen(texCoordIndex++, components, transform);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000187 }
188}