blob: 6a2d38869219649a3754a840b2bd117fcf02979c [file] [log] [blame]
bsalomon@google.com798c8c42013-03-27 19:50:27 +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 */
bsalomon@google.com798c8c42013-03-27 19:50:27 +00007#include "GrGLProgramDesc.h"
joshualitt79f8fae2014-10-28 17:59:26 -07008
9#include "GrGLProcessor.h"
joshualittb0a8a372014-09-23 09:50:21 -070010#include "GrBackendProcessorFactory.h"
11#include "GrProcessor.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000012#include "GrGpuGL.h"
egdaniel170f90b2014-09-16 12:54:40 -070013#include "GrOptDrawState.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000014#include "SkChecksum.h"
joshualitt79f8fae2014-10-28 17:59:26 -070015#include "gl/builders/GrGLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016
joshualitt23e280d2014-09-18 12:26:38 -070017/**
18 * The key for an individual coord transform is made up of a matrix type and a bit that
19 * indicates the source of the input coords.
20 */
21enum {
22 kMatrixTypeKeyBits = 1,
23 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
24 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
25 kTransformKeyBits = kMatrixTypeKeyBits + 1,
26};
27
28/**
29 * We specialize the vertex code for each of these matrix types.
30 */
31enum MatrixType {
32 kNoPersp_MatrixType = 0,
33 kGeneral_MatrixType = 1,
34};
35
36/**
37 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
38 * present in the texture's config. swizzleComponentMask indicates the channels present in the
39 * shader swizzle.
40 */
41static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
42 uint32_t configComponentMask,
43 uint32_t swizzleComponentMask) {
44 if (caps.textureSwizzleSupport()) {
45 // Any remapping is handled using texture swizzling not shader modifications.
46 return false;
47 }
48 // check if the texture is alpha-only
49 if (kA_GrColorComponentFlag == configComponentMask) {
50 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
51 // we must map the swizzle 'a's to 'r'.
52 return true;
53 }
54 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
55 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
56 // alpha-only textures smear alpha across all four channels when read.
57 return true;
58 }
59 }
60 return false;
61}
62
joshualitta5305a12014-10-10 17:47:00 -070063static uint32_t gen_attrib_key(const GrGeometryProcessor& proc) {
joshualitt23e280d2014-09-18 12:26:38 -070064 uint32_t key = 0;
65
joshualitta5305a12014-10-10 17:47:00 -070066 const GrGeometryProcessor::VertexAttribArray& vars = proc.getVertexAttribs();
joshualitt23e280d2014-09-18 12:26:38 -070067 int numAttributes = vars.count();
68 SkASSERT(numAttributes <= 2);
69 for (int a = 0; a < numAttributes; ++a) {
70 uint32_t value = 1 << a;
71 key |= value;
72 }
73 return key;
74}
75
bsalomonae59b772014-11-19 08:23:49 -080076static uint32_t gen_transform_key(const GrPendingFragmentStage& stage,
joshualitt23e280d2014-09-18 12:26:38 -070077 bool useExplicitLocalCoords) {
78 uint32_t totalKey = 0;
bsalomonae59b772014-11-19 08:23:49 -080079 int numTransforms = stage.getProcessor()->numTransforms();
joshualitt23e280d2014-09-18 12:26:38 -070080 for (int t = 0; t < numTransforms; ++t) {
81 uint32_t key = 0;
bsalomonae59b772014-11-19 08:23:49 -080082 if (stage.isPerspectiveCoordTransform(t)) {
joshualitt23e280d2014-09-18 12:26:38 -070083 key |= kGeneral_MatrixType;
84 } else {
85 key |= kNoPersp_MatrixType;
86 }
87
bsalomonae59b772014-11-19 08:23:49 -080088 const GrCoordTransform& coordTransform = stage.getProcessor()->coordTransform(t);
joshualitt23e280d2014-09-18 12:26:38 -070089 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLocalCoords) {
90 key |= kPositionCoords_Flag;
91 }
92 key <<= kTransformKeyBits * t;
93 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
94 totalKey |= key;
95 }
96 return totalKey;
97}
98
joshualitta5305a12014-10-10 17:47:00 -070099static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -0700100 uint32_t key = 0;
joshualitta5305a12014-10-10 17:47:00 -0700101 int numTextures = proc.numTextures();
joshualitt23e280d2014-09-18 12:26:38 -0700102 for (int t = 0; t < numTextures; ++t) {
joshualitta5305a12014-10-10 17:47:00 -0700103 const GrTextureAccess& access = proc.textureAccess(t);
joshualitt23e280d2014-09-18 12:26:38 -0700104 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
105 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
106 key |= 1 << t;
107 }
108 }
109 return key;
110}
111
112/**
113 * A function which emits a meta key into the key builder. This is required because shader code may
114 * be dependent on properties of the effect that the effect itself doesn't use
115 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
116 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700117 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
joshualitta5305a12014-10-10 17:47:00 -0700118 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
119 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700120 */
joshualitta5305a12014-10-10 17:47:00 -0700121static bool get_meta_key(const GrProcessor& proc,
122 const GrGLCaps& caps,
123 uint32_t transformKey,
124 uint32_t attribKey,
egdanielc67870c2014-11-26 08:50:50 -0800125 GrProcessorKeyBuilder* b) {
joshualitta5305a12014-10-10 17:47:00 -0700126 const GrBackendProcessorFactory& factory = proc.getFactory();
127 factory.getGLProcessorKey(proc, caps, b);
egdanielc67870c2014-11-26 08:50:50 -0800128
129 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -0700130 uint32_t textureKey = gen_texture_key(proc, caps);
bsalomonf2765412014-10-15 18:34:46 -0700131 uint32_t classID = proc.getFactory().classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700132
133 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
134 // don't fit.
135 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
136 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -0700137 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -0700138 }
egdanielc67870c2014-11-26 08:50:50 -0800139 if (processorKeySize > SK_MaxU16) {
joshualitt65171342014-10-09 07:25:36 -0700140 return false;
141 }
142
egdanielc67870c2014-11-26 08:50:50 -0800143 uint32_t* key = b->add32n(2);
144 key[0] = (textureKey << 16 | transformKey);
145 key[1] = (classID << 16 | SkToU16(processorKeySize));
joshualitt65171342014-10-09 07:25:36 -0700146 return true;
147}
joshualittb0a8a372014-09-23 09:50:21 -0700148
joshualitt79f8fae2014-10-28 17:59:26 -0700149bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState,
joshualitt4dd99882014-11-11 08:51:30 -0800150 const GrProgramDesc::DescInfo& descInfo,
151 GrGpu::DrawType drawType,
152 GrGpuGL* gpu,
joshualitt4dd99882014-11-11 08:51:30 -0800153 GrProgramDesc* desc) {
joshualitt79f8fae2014-10-28 17:59:26 -0700154 bool inputColorIsUsed = descInfo.fInputColorIsUsed;
155 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000156
157 // The descriptor is used as a cache key. Thus when a field of the
158 // descriptor will not affect program generation (because of the attribute
159 // bindings in use or other descriptor field settings) it should be set
160 // to a canonical value to avoid duplicate programs with different keys.
161
joshualitt79f8fae2014-10-28 17:59:26 -0700162 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib;
kkinnunenec56e452014-08-25 22:21:16 -0700163
egdanielc67870c2014-11-26 08:50:50 -0800164 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
165 // Make room for everything up to the effect keys.
bsalomon848faf02014-07-11 10:01:02 -0700166 desc->fKey.reset();
egdanielc67870c2014-11-26 08:50:50 -0800167 desc->fKey.push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700168
joshualittbd769d02014-09-04 08:56:46 -0700169 // We can only have one effect which touches the vertex shader
egdaniel170f90b2014-09-16 12:54:40 -0700170 if (optState.hasGeometryProcessor()) {
egdanielc67870c2014-11-26 08:50:50 -0800171 const GrGeometryProcessor& gp = *optState.getGeometryProcessor();
172 GrProcessorKeyBuilder b(&desc->fKey);
173 if (!get_meta_key(gp, gpu->glCaps(), 0, gen_attrib_key(gp), &b)) {
174 desc->fKey.reset();
joshualittb0a8a372014-09-23 09:50:21 -0700175 return false;
176 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000177 }
bsalomon929f29a2014-07-17 07:55:11 -0700178
joshualitta5305a12014-10-10 17:47:00 -0700179 for (int s = 0; s < optState.numFragmentStages(); ++s) {
egdanielc67870c2014-11-26 08:50:50 -0800180 const GrPendingFragmentStage& fps = optState.getFragmentStage(s);
181 GrProcessorKeyBuilder b(&desc->fKey);
182 if (!get_meta_key(*fps.getProcessor(), gpu->glCaps(),
183 gen_transform_key(fps, requiresLocalCoordAttrib), 0, &b)) {
184 desc->fKey.reset();
joshualittb0a8a372014-09-23 09:50:21 -0700185 return false;
186 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000187 }
egdaniel170f90b2014-09-16 12:54:40 -0700188
joshualitt65171342014-10-09 07:25:36 -0700189 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700190 // Because header is a pointer into the dynamic array, we can't push any new data into the key
191 // below here.
joshualitt79f8fae2014-10-28 17:59:26 -0700192 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000193
joshualitt65171342014-10-09 07:25:36 -0700194 // make sure any padding in the header is zeroed.
195 memset(header, 0, kHeaderSize);
196
197 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
egdanielae444962014-09-22 12:29:52 -0700198
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000199 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000200
joshualitt0e602822014-10-28 10:27:44 -0700201 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
202 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
203 header->fUseNvpr = true;
bsalomoncd523eb2014-09-23 08:19:00 -0700204 SkASSERT(!optState.hasGeometryProcessor());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000205 } else {
joshualitt0e602822014-10-28 10:27:44 -0700206 header->fUseNvpr = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000207 }
208
joshualitt0e602822014-10-28 10:27:44 -0700209 bool hasUniformColor = inputColorIsUsed &&
joshualitt79f8fae2014-10-28 17:59:26 -0700210 (isPathRendering || !descInfo.hasColorVertexAttribute());
joshualitt0e602822014-10-28 10:27:44 -0700211
212 bool hasUniformCoverage = inputCoverageIsUsed &&
joshualitt79f8fae2014-10-28 17:59:26 -0700213 (isPathRendering || !descInfo.hasCoverageVertexAttribute());
bsalomoncd523eb2014-09-23 08:19:00 -0700214
215 if (!inputColorIsUsed) {
joshualitt79f8fae2014-10-28 17:59:26 -0700216 header->fColorInput = GrProgramDesc::kAllOnes_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700217 } else if (hasUniformColor) {
joshualitt79f8fae2014-10-28 17:59:26 -0700218 header->fColorInput = GrProgramDesc::kUniform_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700219 } else {
joshualitt79f8fae2014-10-28 17:59:26 -0700220 header->fColorInput = GrProgramDesc::kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700221 SkASSERT(!header->fUseNvpr);
bsalomoncd523eb2014-09-23 08:19:00 -0700222 }
223
joshualitt79f8fae2014-10-28 17:59:26 -0700224 bool covIsSolidWhite = !descInfo.hasCoverageVertexAttribute() &&
bsalomoncd523eb2014-09-23 08:19:00 -0700225 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000226
egdaniel170f90b2014-09-16 12:54:40 -0700227 if (covIsSolidWhite || !inputCoverageIsUsed) {
joshualitt79f8fae2014-10-28 17:59:26 -0700228 header->fCoverageInput = GrProgramDesc::kAllOnes_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700229 } else if (hasUniformCoverage) {
joshualitt79f8fae2014-10-28 17:59:26 -0700230 header->fCoverageInput = GrProgramDesc::kUniform_ColorInput;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000231 } else {
joshualitt79f8fae2014-10-28 17:59:26 -0700232 header->fCoverageInput = GrProgramDesc::kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700233 SkASSERT(!header->fUseNvpr);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000234 }
235
joshualitt79f8fae2014-10-28 17:59:26 -0700236 if (descInfo.fReadsDst) {
joshualitt9176e2c2014-11-20 07:28:52 -0800237 const GrDeviceCoordTexture* dstCopy = optState.getDstCopy();
bsalomon49f085d2014-09-05 13:34:00 -0700238 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000239 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700240 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000241 dstCopyTexture = dstCopy->texture();
242 }
joshualitt30ba4362014-08-21 20:18:45 -0700243 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
joshualitt47bb3822014-10-07 16:43:25 -0700244 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000245 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000246 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000247 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000248 }
249
joshualitt79f8fae2014-10-28 17:59:26 -0700250 if (descInfo.fReadsFragPosition) {
joshualitt47bb3822014-10-07 16:43:25 -0700251 header->fFragPosKey =
252 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
253 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000254 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000255 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000256 }
257
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000258 // Record attribute indices
joshualitt79f8fae2014-10-28 17:59:26 -0700259 header->fPositionAttributeIndex = descInfo.positionAttributeIndex();
260 header->fLocalCoordAttributeIndex = descInfo.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000261
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000262 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700263 int availableAttributeIndex = optState.getVertexAttribCount();
joshualitt79f8fae2014-10-28 17:59:26 -0700264 if (descInfo.hasColorVertexAttribute()) {
265 header->fColorAttributeIndex = descInfo.colorVertexAttributeIndex();
266 } else if (GrProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000267 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000268 header->fColorAttributeIndex = availableAttributeIndex;
269 availableAttributeIndex++;
270 } else {
271 header->fColorAttributeIndex = -1;
272 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000273
joshualitt79f8fae2014-10-28 17:59:26 -0700274 if (descInfo.hasCoverageVertexAttribute()) {
275 header->fCoverageAttributeIndex = descInfo.coverageVertexAttributeIndex();
276 } else if (GrProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000277 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000278 header->fCoverageAttributeIndex = availableAttributeIndex;
279 } else {
280 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000281 }
282
joshualitt79f8fae2014-10-28 17:59:26 -0700283 header->fPrimaryOutputType = descInfo.fPrimaryOutputType;
284 header->fSecondaryOutputType = descInfo.fSecondaryOutputType;
joshualittbd769d02014-09-04 08:56:46 -0700285
joshualitta5305a12014-10-10 17:47:00 -0700286 header->fColorEffectCnt = optState.numColorStages();
287 header->fCoverageEffectCnt = optState.numCoverageStages();
bsalomon848faf02014-07-11 10:01:02 -0700288 desc->finalize();
289 return true;
290}