blob: c5c31d33aec90b3c12faa317533318fb1b26faeb [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/**
joshualitt23e280d2014-09-18 12:26:38 -070017 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
18 * present in the texture's config. swizzleComponentMask indicates the channels present in the
19 * shader swizzle.
20 */
21static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
22 uint32_t configComponentMask,
23 uint32_t swizzleComponentMask) {
24 if (caps.textureSwizzleSupport()) {
25 // Any remapping is handled using texture swizzling not shader modifications.
26 return false;
27 }
28 // check if the texture is alpha-only
29 if (kA_GrColorComponentFlag == configComponentMask) {
30 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
31 // we must map the swizzle 'a's to 'r'.
32 return true;
33 }
34 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
35 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
36 // alpha-only textures smear alpha across all four channels when read.
37 return true;
38 }
39 }
40 return false;
41}
42
bsalomon17168df2014-12-09 09:00:49 -080043/**
44 * The key for an individual coord transform is made up of a matrix type, a precision, and a bit
45 * that indicates the source of the input coords.
46 */
47enum {
48 kMatrixTypeKeyBits = 1,
49 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
50
51 kPrecisionBits = 2,
52 kPrecisionShift = kMatrixTypeKeyBits,
53
54 kPositionCoords_Flag = (1 << (kPrecisionShift + kPrecisionBits)),
55
56 kTransformKeyBits = kMatrixTypeKeyBits + kPrecisionBits + 1,
57};
58
bsalomonc0bd6482014-12-09 10:04:14 -080059GR_STATIC_ASSERT(kHigh_GrSLPrecision < (1 << kPrecisionBits));
bsalomon17168df2014-12-09 09:00:49 -080060
61/**
62 * We specialize the vertex code for each of these matrix types.
63 */
64enum MatrixType {
65 kNoPersp_MatrixType = 0,
66 kGeneral_MatrixType = 1,
67};
68
69static uint32_t gen_transform_key(const GrPendingFragmentStage& stage, bool useExplicitLocalCoords) {
joshualitt23e280d2014-09-18 12:26:38 -070070 uint32_t totalKey = 0;
bsalomonae59b772014-11-19 08:23:49 -080071 int numTransforms = stage.getProcessor()->numTransforms();
joshualitt23e280d2014-09-18 12:26:38 -070072 for (int t = 0; t < numTransforms; ++t) {
73 uint32_t key = 0;
bsalomonae59b772014-11-19 08:23:49 -080074 if (stage.isPerspectiveCoordTransform(t)) {
joshualitt23e280d2014-09-18 12:26:38 -070075 key |= kGeneral_MatrixType;
76 } else {
77 key |= kNoPersp_MatrixType;
78 }
79
bsalomonae59b772014-11-19 08:23:49 -080080 const GrCoordTransform& coordTransform = stage.getProcessor()->coordTransform(t);
joshualitt23e280d2014-09-18 12:26:38 -070081 if (kLocal_GrCoordSet != coordTransform.sourceCoords() && useExplicitLocalCoords) {
82 key |= kPositionCoords_Flag;
83 }
bsalomon17168df2014-12-09 09:00:49 -080084
bsalomonc0bd6482014-12-09 10:04:14 -080085 GR_STATIC_ASSERT(kGrSLPrecisionCount <= (1 << kPrecisionBits));
bsalomon17168df2014-12-09 09:00:49 -080086 key |= (coordTransform.precision() << kPrecisionShift);
87
joshualitt23e280d2014-09-18 12:26:38 -070088 key <<= kTransformKeyBits * t;
bsalomon17168df2014-12-09 09:00:49 -080089
joshualitt23e280d2014-09-18 12:26:38 -070090 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
91 totalKey |= key;
92 }
93 return totalKey;
94}
95
joshualitta5305a12014-10-10 17:47:00 -070096static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -070097 uint32_t key = 0;
joshualitta5305a12014-10-10 17:47:00 -070098 int numTextures = proc.numTextures();
joshualitt23e280d2014-09-18 12:26:38 -070099 for (int t = 0; t < numTextures; ++t) {
joshualitta5305a12014-10-10 17:47:00 -0700100 const GrTextureAccess& access = proc.textureAccess(t);
joshualitt23e280d2014-09-18 12:26:38 -0700101 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
102 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
103 key |= 1 << t;
104 }
105 }
106 return key;
107}
108
109/**
110 * A function which emits a meta key into the key builder. This is required because shader code may
111 * be dependent on properties of the effect that the effect itself doesn't use
112 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
113 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700114 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
joshualitta5305a12014-10-10 17:47:00 -0700115 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
116 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700117 */
joshualitta5305a12014-10-10 17:47:00 -0700118static bool get_meta_key(const GrProcessor& proc,
119 const GrGLCaps& caps,
120 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800121 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800122 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -0700123 uint32_t textureKey = gen_texture_key(proc, caps);
joshualitteb2a6762014-12-04 11:35:33 -0800124 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700125
126 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
127 // don't fit.
128 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
129 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -0700130 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -0700131 }
egdanielc67870c2014-11-26 08:50:50 -0800132 if (processorKeySize > SK_MaxU16) {
joshualitt65171342014-10-09 07:25:36 -0700133 return false;
134 }
135
egdanielc67870c2014-11-26 08:50:50 -0800136 uint32_t* key = b->add32n(2);
137 key[0] = (textureKey << 16 | transformKey);
138 key[1] = (classID << 16 | SkToU16(processorKeySize));
joshualitt65171342014-10-09 07:25:36 -0700139 return true;
140}
joshualittb0a8a372014-09-23 09:50:21 -0700141
joshualitt79f8fae2014-10-28 17:59:26 -0700142bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState,
joshualitt4dd99882014-11-11 08:51:30 -0800143 const GrProgramDesc::DescInfo& descInfo,
144 GrGpu::DrawType drawType,
145 GrGpuGL* gpu,
joshualitt4dd99882014-11-11 08:51:30 -0800146 GrProgramDesc* desc) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000147 // The descriptor is used as a cache key. Thus when a field of the
148 // descriptor will not affect program generation (because of the attribute
149 // bindings in use or other descriptor field settings) it should be set
150 // to a canonical value to avoid duplicate programs with different keys.
151
joshualitt79f8fae2014-10-28 17:59:26 -0700152 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib;
kkinnunenec56e452014-08-25 22:21:16 -0700153
egdanielc67870c2014-11-26 08:50:50 -0800154 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
155 // Make room for everything up to the effect keys.
bsalomon848faf02014-07-11 10:01:02 -0700156 desc->fKey.reset();
egdanielc67870c2014-11-26 08:50:50 -0800157 desc->fKey.push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700158
joshualitt9b989322014-12-15 14:16:27 -0800159 GrProcessorKeyBuilder b(&desc->fKey);
160
161 const GrPrimitiveProcessor& primProc = *optState.getPrimitiveProcessor();
162 primProc.getGLProcessorKey(optState.getBatchTracker(), gpu->glCaps(), &b);
163 if (!get_meta_key(primProc, gpu->glCaps(), 0, &b)) {
164 desc->fKey.reset();
165 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000166 }
bsalomon929f29a2014-07-17 07:55:11 -0700167
joshualitta5305a12014-10-10 17:47:00 -0700168 for (int s = 0; s < optState.numFragmentStages(); ++s) {
egdanielc67870c2014-11-26 08:50:50 -0800169 const GrPendingFragmentStage& fps = optState.getFragmentStage(s);
joshualitt87f48d92014-12-04 10:41:40 -0800170 const GrFragmentProcessor& fp = *fps.getProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800171 fp.getGLProcessorKey(gpu->glCaps(), &b);
egdanielc2304142014-12-11 13:15:13 -0800172 if (!get_meta_key(fp, gpu->glCaps(),
joshualitt9b989322014-12-15 14:16:27 -0800173 gen_transform_key(fps, requiresLocalCoordAttrib), &b)) {
egdanielc67870c2014-11-26 08:50:50 -0800174 desc->fKey.reset();
joshualittb0a8a372014-09-23 09:50:21 -0700175 return false;
176 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000177 }
egdaniel170f90b2014-09-16 12:54:40 -0700178
egdanielc2304142014-12-11 13:15:13 -0800179 const GrXferProcessor& xp = *optState.getXferProcessor();
egdanielc2304142014-12-11 13:15:13 -0800180 xp.getGLProcessorKey(gpu->glCaps(), &b);
joshualitt9b989322014-12-15 14:16:27 -0800181 if (!get_meta_key(xp, gpu->glCaps(), 0, &b)) {
egdanielc2304142014-12-11 13:15:13 -0800182 desc->fKey.reset();
183 return false;
184 }
185
joshualitt65171342014-10-09 07:25:36 -0700186 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700187 // Because header is a pointer into the dynamic array, we can't push any new data into the key
188 // below here.
joshualitt79f8fae2014-10-28 17:59:26 -0700189 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000190
joshualitt65171342014-10-09 07:25:36 -0700191 // make sure any padding in the header is zeroed.
192 memset(header, 0, kHeaderSize);
193
joshualitt0e602822014-10-28 10:27:44 -0700194 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
195 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
196 header->fUseNvpr = true;
bsalomoncd523eb2014-09-23 08:19:00 -0700197 SkASSERT(!optState.hasGeometryProcessor());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000198 } else {
joshualitt0e602822014-10-28 10:27:44 -0700199 header->fUseNvpr = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000200 }
201
joshualitt79f8fae2014-10-28 17:59:26 -0700202 if (descInfo.fReadsDst) {
joshualitt9176e2c2014-11-20 07:28:52 -0800203 const GrDeviceCoordTexture* dstCopy = optState.getDstCopy();
bsalomon49f085d2014-09-05 13:34:00 -0700204 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000205 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000207 dstCopyTexture = dstCopy->texture();
208 }
joshualitt30ba4362014-08-21 20:18:45 -0700209 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
joshualitt47bb3822014-10-07 16:43:25 -0700210 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000211 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000212 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000213 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000214 }
215
joshualitt79f8fae2014-10-28 17:59:26 -0700216 if (descInfo.fReadsFragPosition) {
joshualitt47bb3822014-10-07 16:43:25 -0700217 header->fFragPosKey =
218 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
219 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000220 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000221 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000222 }
223
joshualitta5305a12014-10-10 17:47:00 -0700224 header->fColorEffectCnt = optState.numColorStages();
225 header->fCoverageEffectCnt = optState.numCoverageStages();
bsalomon848faf02014-07-11 10:01:02 -0700226 desc->finalize();
227 return true;
228}