blob: 6867a1329b048b59384dfded65f122eee414d05b [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 "GrProcessor.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000011#include "GrGpuGL.h"
egdaniel170f90b2014-09-16 12:54:40 -070012#include "GrOptDrawState.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000013#include "SkChecksum.h"
joshualitt79f8fae2014-10-28 17:59:26 -070014#include "gl/builders/GrGLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015
joshualitt23e280d2014-09-18 12:26:38 -070016/**
17 * The key for an individual coord transform is made up of a matrix type and a bit that
18 * indicates the source of the input coords.
19 */
20enum {
21 kMatrixTypeKeyBits = 1,
22 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
23 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
24 kTransformKeyBits = kMatrixTypeKeyBits + 1,
25};
26
27/**
28 * We specialize the vertex code for each of these matrix types.
29 */
30enum MatrixType {
31 kNoPersp_MatrixType = 0,
32 kGeneral_MatrixType = 1,
33};
34
35/**
36 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
37 * present in the texture's config. swizzleComponentMask indicates the channels present in the
38 * shader swizzle.
39 */
40static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
41 uint32_t configComponentMask,
42 uint32_t swizzleComponentMask) {
43 if (caps.textureSwizzleSupport()) {
44 // Any remapping is handled using texture swizzling not shader modifications.
45 return false;
46 }
47 // check if the texture is alpha-only
48 if (kA_GrColorComponentFlag == configComponentMask) {
49 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
50 // we must map the swizzle 'a's to 'r'.
51 return true;
52 }
53 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
54 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
55 // alpha-only textures smear alpha across all four channels when read.
56 return true;
57 }
58 }
59 return false;
60}
61
joshualitta5305a12014-10-10 17:47:00 -070062static uint32_t gen_attrib_key(const GrGeometryProcessor& proc) {
joshualitt23e280d2014-09-18 12:26:38 -070063 uint32_t key = 0;
64
joshualitt2dd1ae02014-12-03 06:24:10 -080065 const GrGeometryProcessor::VertexAttribArray& vars = proc.getAttribs();
joshualitt23e280d2014-09-18 12:26:38 -070066 int numAttributes = vars.count();
joshualitt2dd1ae02014-12-03 06:24:10 -080067 SkASSERT(numAttributes <= GrGeometryProcessor::kMaxVertexAttribs);
joshualitt23e280d2014-09-18 12:26:38 -070068 for (int a = 0; a < numAttributes; ++a) {
69 uint32_t value = 1 << a;
70 key |= value;
71 }
72 return key;
73}
74
bsalomonae59b772014-11-19 08:23:49 -080075static uint32_t gen_transform_key(const GrPendingFragmentStage& stage,
joshualitt23e280d2014-09-18 12:26:38 -070076 bool useExplicitLocalCoords) {
77 uint32_t totalKey = 0;
bsalomonae59b772014-11-19 08:23:49 -080078 int numTransforms = stage.getProcessor()->numTransforms();
joshualitt23e280d2014-09-18 12:26:38 -070079 for (int t = 0; t < numTransforms; ++t) {
80 uint32_t key = 0;
bsalomonae59b772014-11-19 08:23:49 -080081 if (stage.isPerspectiveCoordTransform(t)) {
joshualitt23e280d2014-09-18 12:26:38 -070082 key |= kGeneral_MatrixType;
83 } else {
84 key |= kNoPersp_MatrixType;
85 }
86
bsalomonae59b772014-11-19 08:23:49 -080087 const GrCoordTransform& coordTransform = stage.getProcessor()->coordTransform(t);
joshualitt23e280d2014-09-18 12:26:38 -070088 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLocalCoords) {
89 key |= kPositionCoords_Flag;
90 }
91 key <<= kTransformKeyBits * t;
92 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
93 totalKey |= key;
94 }
95 return totalKey;
96}
97
joshualitta5305a12014-10-10 17:47:00 -070098static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -070099 uint32_t key = 0;
joshualitta5305a12014-10-10 17:47:00 -0700100 int numTextures = proc.numTextures();
joshualitt23e280d2014-09-18 12:26:38 -0700101 for (int t = 0; t < numTextures; ++t) {
joshualitta5305a12014-10-10 17:47:00 -0700102 const GrTextureAccess& access = proc.textureAccess(t);
joshualitt23e280d2014-09-18 12:26:38 -0700103 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
104 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
105 key |= 1 << t;
106 }
107 }
108 return key;
109}
110
111/**
112 * A function which emits a meta key into the key builder. This is required because shader code may
113 * be dependent on properties of the effect that the effect itself doesn't use
114 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
115 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700116 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
joshualitta5305a12014-10-10 17:47:00 -0700117 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
118 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700119 */
joshualitta5305a12014-10-10 17:47:00 -0700120static bool get_meta_key(const GrProcessor& proc,
121 const GrGLCaps& caps,
122 uint32_t transformKey,
123 uint32_t attribKey,
egdanielc67870c2014-11-26 08:50:50 -0800124 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800125 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -0700126 uint32_t textureKey = gen_texture_key(proc, caps);
joshualitteb2a6762014-12-04 11:35:33 -0800127 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700128
129 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
130 // don't fit.
131 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
132 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -0700133 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -0700134 }
egdanielc67870c2014-11-26 08:50:50 -0800135 if (processorKeySize > SK_MaxU16) {
joshualitt65171342014-10-09 07:25:36 -0700136 return false;
137 }
138
egdanielc67870c2014-11-26 08:50:50 -0800139 uint32_t* key = b->add32n(2);
140 key[0] = (textureKey << 16 | transformKey);
141 key[1] = (classID << 16 | SkToU16(processorKeySize));
joshualitt65171342014-10-09 07:25:36 -0700142 return true;
143}
joshualittb0a8a372014-09-23 09:50:21 -0700144
joshualitt79f8fae2014-10-28 17:59:26 -0700145bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState,
joshualitt4dd99882014-11-11 08:51:30 -0800146 const GrProgramDesc::DescInfo& descInfo,
147 GrGpu::DrawType drawType,
148 GrGpuGL* gpu,
joshualitt4dd99882014-11-11 08:51:30 -0800149 GrProgramDesc* desc) {
joshualitt79f8fae2014-10-28 17:59:26 -0700150 bool inputColorIsUsed = descInfo.fInputColorIsUsed;
151 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000152
153 // The descriptor is used as a cache key. Thus when a field of the
154 // descriptor will not affect program generation (because of the attribute
155 // bindings in use or other descriptor field settings) it should be set
156 // to a canonical value to avoid duplicate programs with different keys.
157
joshualitt79f8fae2014-10-28 17:59:26 -0700158 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib;
kkinnunenec56e452014-08-25 22:21:16 -0700159
egdanielc67870c2014-11-26 08:50:50 -0800160 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
161 // Make room for everything up to the effect keys.
bsalomon848faf02014-07-11 10:01:02 -0700162 desc->fKey.reset();
egdanielc67870c2014-11-26 08:50:50 -0800163 desc->fKey.push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700164
joshualittbd769d02014-09-04 08:56:46 -0700165 // We can only have one effect which touches the vertex shader
egdaniel170f90b2014-09-16 12:54:40 -0700166 if (optState.hasGeometryProcessor()) {
egdanielc67870c2014-11-26 08:50:50 -0800167 const GrGeometryProcessor& gp = *optState.getGeometryProcessor();
168 GrProcessorKeyBuilder b(&desc->fKey);
joshualitteb2a6762014-12-04 11:35:33 -0800169 gp.getGLProcessorKey(optState.getBatchTracker(), gpu->glCaps(), &b);
egdanielc67870c2014-11-26 08:50:50 -0800170 if (!get_meta_key(gp, gpu->glCaps(), 0, gen_attrib_key(gp), &b)) {
171 desc->fKey.reset();
joshualittb0a8a372014-09-23 09:50:21 -0700172 return false;
173 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000174 }
bsalomon929f29a2014-07-17 07:55:11 -0700175
joshualitta5305a12014-10-10 17:47:00 -0700176 for (int s = 0; s < optState.numFragmentStages(); ++s) {
egdanielc67870c2014-11-26 08:50:50 -0800177 const GrPendingFragmentStage& fps = optState.getFragmentStage(s);
joshualitt87f48d92014-12-04 10:41:40 -0800178 const GrFragmentProcessor& fp = *fps.getProcessor();
egdanielc67870c2014-11-26 08:50:50 -0800179 GrProcessorKeyBuilder b(&desc->fKey);
joshualitteb2a6762014-12-04 11:35:33 -0800180 fp.getGLProcessorKey(gpu->glCaps(), &b);
egdanielc67870c2014-11-26 08:50:50 -0800181 if (!get_meta_key(*fps.getProcessor(), gpu->glCaps(),
182 gen_transform_key(fps, requiresLocalCoordAttrib), 0, &b)) {
183 desc->fKey.reset();
joshualittb0a8a372014-09-23 09:50:21 -0700184 return false;
185 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000186 }
egdaniel170f90b2014-09-16 12:54:40 -0700187
joshualitt65171342014-10-09 07:25:36 -0700188 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700189 // Because header is a pointer into the dynamic array, we can't push any new data into the key
190 // below here.
joshualitt79f8fae2014-10-28 17:59:26 -0700191 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000192
joshualitt65171342014-10-09 07:25:36 -0700193 // make sure any padding in the header is zeroed.
194 memset(header, 0, kHeaderSize);
195
196 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
egdanielae444962014-09-22 12:29:52 -0700197
joshualitt0e602822014-10-28 10:27:44 -0700198 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
199 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
200 header->fUseNvpr = true;
bsalomoncd523eb2014-09-23 08:19:00 -0700201 SkASSERT(!optState.hasGeometryProcessor());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000202 } else {
joshualitt0e602822014-10-28 10:27:44 -0700203 header->fUseNvpr = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000204 }
205
joshualitt2dd1ae02014-12-03 06:24:10 -0800206 bool hasUniformColor = inputColorIsUsed && (isPathRendering || !descInfo.fHasVertexColor);
bsalomoncd523eb2014-09-23 08:19:00 -0700207
208 if (!inputColorIsUsed) {
joshualitt79f8fae2014-10-28 17:59:26 -0700209 header->fColorInput = GrProgramDesc::kAllOnes_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700210 } else if (hasUniformColor) {
joshualitt79f8fae2014-10-28 17:59:26 -0700211 header->fColorInput = GrProgramDesc::kUniform_ColorInput;
bsalomoncd523eb2014-09-23 08:19:00 -0700212 } else {
joshualitt79f8fae2014-10-28 17:59:26 -0700213 header->fColorInput = GrProgramDesc::kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700214 SkASSERT(!header->fUseNvpr);
bsalomoncd523eb2014-09-23 08:19:00 -0700215 }
216
joshualitt2dd1ae02014-12-03 06:24:10 -0800217 bool hasVertexCoverage = !isPathRendering && descInfo.fHasVertexCoverage;
218
219 bool covIsSolidWhite = !hasVertexCoverage && 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000220
egdaniel170f90b2014-09-16 12:54:40 -0700221 if (covIsSolidWhite || !inputCoverageIsUsed) {
joshualitt79f8fae2014-10-28 17:59:26 -0700222 header->fCoverageInput = GrProgramDesc::kAllOnes_ColorInput;
joshualitt2dd1ae02014-12-03 06:24:10 -0800223 } else if (!hasVertexCoverage) {
joshualitt79f8fae2014-10-28 17:59:26 -0700224 header->fCoverageInput = GrProgramDesc::kUniform_ColorInput;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000225 } else {
joshualitt79f8fae2014-10-28 17:59:26 -0700226 header->fCoverageInput = GrProgramDesc::kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700227 SkASSERT(!header->fUseNvpr);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000228 }
229
joshualitt79f8fae2014-10-28 17:59:26 -0700230 if (descInfo.fReadsDst) {
joshualitt9176e2c2014-11-20 07:28:52 -0800231 const GrDeviceCoordTexture* dstCopy = optState.getDstCopy();
bsalomon49f085d2014-09-05 13:34:00 -0700232 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000233 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700234 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000235 dstCopyTexture = dstCopy->texture();
236 }
joshualitt30ba4362014-08-21 20:18:45 -0700237 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
joshualitt47bb3822014-10-07 16:43:25 -0700238 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000239 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000240 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000241 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000242 }
243
joshualitt79f8fae2014-10-28 17:59:26 -0700244 if (descInfo.fReadsFragPosition) {
joshualitt47bb3822014-10-07 16:43:25 -0700245 header->fFragPosKey =
246 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
247 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000248 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000249 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000250 }
251
joshualitt79f8fae2014-10-28 17:59:26 -0700252 header->fPrimaryOutputType = descInfo.fPrimaryOutputType;
253 header->fSecondaryOutputType = descInfo.fSecondaryOutputType;
joshualittbd769d02014-09-04 08:56:46 -0700254
joshualitta5305a12014-10-10 17:47:00 -0700255 header->fColorEffectCnt = optState.numColorStages();
256 header->fCoverageEffectCnt = optState.numCoverageStages();
bsalomon848faf02014-07-11 10:01:02 -0700257 desc->finalize();
258 return true;
259}