blob: 4a274c6bb1e8806b98043328031b18d12df8ac16 [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 */
7
joshualitt47bb3822014-10-07 16:43:25 -07008#include "gl/builders/GrGLFragmentShaderBuilder.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +00009#include "GrGLProgramDesc.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.com798c8c42013-03-27 19:50:27 +000014
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015#include "SkChecksum.h"
16
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
joshualitta5305a12014-10-10 17:47:00 -070076static uint32_t gen_transform_key(const GrFragmentStage& effectStage,
joshualitt23e280d2014-09-18 12:26:38 -070077 bool useExplicitLocalCoords) {
78 uint32_t totalKey = 0;
joshualittb0a8a372014-09-23 09:50:21 -070079 int numTransforms = effectStage.getProcessor()->numTransforms();
joshualitt23e280d2014-09-18 12:26:38 -070080 for (int t = 0; t < numTransforms; ++t) {
81 uint32_t key = 0;
82 if (effectStage.isPerspectiveCoordTransform(t, useExplicitLocalCoords)) {
83 key |= kGeneral_MatrixType;
84 } else {
85 key |= kNoPersp_MatrixType;
86 }
87
joshualittb0a8a372014-09-23 09:50:21 -070088 const GrCoordTransform& coordTransform = effectStage.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,
125 GrProcessorKeyBuilder* b,
126 uint16_t* processorKeySize) {
127 const GrBackendProcessorFactory& factory = proc.getFactory();
128 factory.getGLProcessorKey(proc, caps, b);
129 size_t size = b->size();
130 if (size > SK_MaxU16) {
131 *processorKeySize = 0; // suppresses a warning.
132 return false;
133 }
134 *processorKeySize = SkToU16(size);
135 uint32_t textureKey = gen_texture_key(proc, caps);
bsalomonf2765412014-10-15 18:34:46 -0700136 uint32_t classID = proc.getFactory().classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700137
138 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
139 // don't fit.
140 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
141 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -0700142 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -0700143 }
144
145 uint32_t* key = b->add32n(2);
146 key[0] = (textureKey << 16 | transformKey);
147 key[1] = (classID << 16);
bsalomon929f29a2014-07-17 07:55:11 -0700148 return true;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000149}
bsalomon848faf02014-07-11 10:01:02 -0700150
joshualitt65171342014-10-09 07:25:36 -0700151struct GeometryProcessorKeyBuilder {
joshualitta5305a12014-10-10 17:47:00 -0700152 typedef GrGeometryProcessor StagedProcessor;
153 static bool GetProcessorKey(const GrGeometryProcessor& gp,
joshualitt65171342014-10-09 07:25:36 -0700154 const GrGLCaps& caps,
joshualitta5305a12014-10-10 17:47:00 -0700155 bool,
joshualitt65171342014-10-09 07:25:36 -0700156 GrProcessorKeyBuilder* b,
joshualitta5305a12014-10-10 17:47:00 -0700157 uint16_t* keySize) {
158 /* 0 because no transforms on a GP */
159 return get_meta_key(gp, caps, 0, gen_attrib_key(gp), b, keySize);
joshualitt65171342014-10-09 07:25:36 -0700160 }
161};
162
163struct FragmentProcessorKeyBuilder {
164 typedef GrFragmentStage StagedProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700165 static bool GetProcessorKey(const GrFragmentStage& fps,
joshualitt65171342014-10-09 07:25:36 -0700166 const GrGLCaps& caps,
joshualitta5305a12014-10-10 17:47:00 -0700167 bool useLocalCoords,
joshualitt65171342014-10-09 07:25:36 -0700168 GrProcessorKeyBuilder* b,
joshualitta5305a12014-10-10 17:47:00 -0700169 uint16_t* keySize) {
170 /* 0 because no attribs on a fP */
171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, useLocalCoords), 0,
172 b, keySize);
joshualitt65171342014-10-09 07:25:36 -0700173 }
174};
175
176
177template <class ProcessorKeyBuilder>
178bool
179GrGLProgramDesc::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::StagedProcessor& stage,
180 const GrGLCaps& caps,
181 bool requiresLocalCoordAttrib,
182 GrGLProgramDesc* desc,
183 int* offsetAndSizeIndex) {
184 GrProcessorKeyBuilder b(&desc->fKey);
185 uint16_t processorKeySize;
186 uint32_t processorOffset = desc->fKey.count();
187 if (processorOffset > SK_MaxU16 ||
188 !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoordAttrib, &b,
189 &processorKeySize)){
190 desc->fKey.reset();
191 return false;
192 }
193
194 uint16_t* offsetAndSize =
195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset +
196 *offsetAndSizeIndex * 2 * sizeof(uint16_t));
197 offsetAndSize[0] = SkToU16(processorOffset);
198 offsetAndSize[1] = processorKeySize;
199 ++(*offsetAndSizeIndex);
200 return true;
201}
joshualittb0a8a372014-09-23 09:50:21 -0700202
egdaniel170f90b2014-09-16 12:54:40 -0700203bool GrGLProgramDesc::Build(const GrOptDrawState& optState,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000204 GrGpu::DrawType drawType,
egdanielae444962014-09-22 12:29:52 -0700205 GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000206 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000207 GrGLProgramDesc* desc) {
egdaniel170f90b2014-09-16 12:54:40 -0700208 bool inputColorIsUsed = optState.inputColorIsUsed();
egdaniel033ea7f2014-09-23 08:14:13 -0700209 bool inputCoverageIsUsed = optState.inputCoverageIsUsed();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000210
211 // The descriptor is used as a cache key. Thus when a field of the
212 // descriptor will not affect program generation (because of the attribute
213 // bindings in use or other descriptor field settings) it should be set
214 // to a canonical value to avoid duplicate programs with different keys.
215
egdaniela7dc0a82014-09-17 08:25:05 -0700216 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib();
kkinnunenec56e452014-08-25 22:21:16 -0700217
egdaniel170f90b2014-09-16 12:54:40 -0700218 int numStages = optState.numTotalStages();
219
bsalomon929f29a2014-07-17 07:55:11 -0700220 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700221 // Make room for everything up to and including the array of offsets to effect keys.
222 desc->fKey.reset();
bsalomon929f29a2014-07-17 07:55:11 -0700223 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000224
bsalomon929f29a2014-07-17 07:55:11 -0700225 int offsetAndSizeIndex = 0;
joshualittbd769d02014-09-04 08:56:46 -0700226
joshualittbd769d02014-09-04 08:56:46 -0700227 // We can only have one effect which touches the vertex shader
egdaniel170f90b2014-09-16 12:54:40 -0700228 if (optState.hasGeometryProcessor()) {
joshualitta5305a12014-10-10 17:47:00 -0700229 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getGeometryProcessor(),
joshualitt65171342014-10-09 07:25:36 -0700230 gpu->glCaps(),
joshualitta5305a12014-10-10 17:47:00 -0700231 false,
joshualitt65171342014-10-09 07:25:36 -0700232 desc,
233 &offsetAndSizeIndex)) {
joshualittb0a8a372014-09-23 09:50:21 -0700234 return false;
235 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000236 }
bsalomon929f29a2014-07-17 07:55:11 -0700237
joshualitta5305a12014-10-10 17:47:00 -0700238 for (int s = 0; s < optState.numFragmentStages(); ++s) {
239 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFragmentStage(s),
joshualitt65171342014-10-09 07:25:36 -0700240 gpu->glCaps(),
241 requiresLocalCoordAttrib,
242 desc,
243 &offsetAndSizeIndex)) {
joshualittb0a8a372014-09-23 09:50:21 -0700244 return false;
245 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000246 }
egdaniel170f90b2014-09-16 12:54:40 -0700247
joshualitt65171342014-10-09 07:25:36 -0700248 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700249 // Because header is a pointer into the dynamic array, we can't push any new data into the key
250 // below here.
joshualitt65171342014-10-09 07:25:36 -0700251 KeyHeader* header = desc->header();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000252
joshualitt65171342014-10-09 07:25:36 -0700253 // make sure any padding in the header is zeroed.
254 memset(header, 0, kHeaderSize);
255
256 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
egdanielae444962014-09-22 12:29:52 -0700257
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000258 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000259
joshualitt0e602822014-10-28 10:27:44 -0700260 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
261 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
262 header->fUseNvpr = true;
bsalomoncd523eb2014-09-23 08:19:00 -0700263 SkASSERT(!optState.hasGeometryProcessor());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000264 } else {
joshualitt0e602822014-10-28 10:27:44 -0700265 header->fUseNvpr = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000266 }
267
joshualitt0e602822014-10-28 10:27:44 -0700268 bool hasUniformColor = inputColorIsUsed &&
269 (isPathRendering || !optState.hasColorVertexAttribute());
270
271 bool hasUniformCoverage = inputCoverageIsUsed &&
272 (isPathRendering || !optState.hasCoverageVertexAttribute());
bsalomoncd523eb2014-09-23 08:19:00 -0700273
274 if (!inputColorIsUsed) {
275 header->fColorInput = kAllOnes_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700276 } else if (hasUniformColor) {
bsalomoncd523eb2014-09-23 08:19:00 -0700277 header->fColorInput = kUniform_ColorInput;
278 } else {
279 header->fColorInput = kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700280 SkASSERT(!header->fUseNvpr);
bsalomoncd523eb2014-09-23 08:19:00 -0700281 }
282
283 bool covIsSolidWhite = !optState.hasCoverageVertexAttribute() &&
284 0xffffffff == optState.getCoverageColor();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000285
egdaniel170f90b2014-09-16 12:54:40 -0700286 if (covIsSolidWhite || !inputCoverageIsUsed) {
egdaniel842b0862014-09-02 10:01:30 -0700287 header->fCoverageInput = kAllOnes_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700288 } else if (hasUniformCoverage) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000289 header->fCoverageInput = kUniform_ColorInput;
290 } else {
291 header->fCoverageInput = kAttribute_ColorInput;
joshualitt0e602822014-10-28 10:27:44 -0700292 SkASSERT(!header->fUseNvpr);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000293 }
294
egdaniela7dc0a82014-09-17 08:25:05 -0700295 if (optState.readsDst()) {
bsalomon49f085d2014-09-05 13:34:00 -0700296 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000297 const GrTexture* dstCopyTexture = NULL;
bsalomon49f085d2014-09-05 13:34:00 -0700298 if (dstCopy) {
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000299 dstCopyTexture = dstCopy->texture();
300 }
joshualitt30ba4362014-08-21 20:18:45 -0700301 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
joshualitt47bb3822014-10-07 16:43:25 -0700302 gpu->glCaps());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000303 SkASSERT(0 != header->fDstReadKey);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000304 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000305 header->fDstReadKey = 0;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000306 }
307
egdaniela7dc0a82014-09-17 08:25:05 -0700308 if (optState.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700309 header->fFragPosKey =
310 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRenderTarget(),
311 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000312 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000313 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000314 }
315
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000316 // Record attribute indices
egdaniel170f90b2014-09-16 12:54:40 -0700317 header->fPositionAttributeIndex = optState.positionAttributeIndex();
318 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex();
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000319
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000320 // For constant color and coverage we need an attribute with an index beyond those already set
egdaniel170f90b2014-09-16 12:54:40 -0700321 int availableAttributeIndex = optState.getVertexAttribCount();
bsalomoncd523eb2014-09-23 08:19:00 -0700322 if (optState.hasColorVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700323 header->fColorAttributeIndex = optState.colorVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000324 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000325 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000326 header->fColorAttributeIndex = availableAttributeIndex;
327 availableAttributeIndex++;
328 } else {
329 header->fColorAttributeIndex = -1;
330 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000331
bsalomoncd523eb2014-09-23 08:19:00 -0700332 if (optState.hasCoverageVertexAttribute()) {
egdaniel170f90b2014-09-16 12:54:40 -0700333 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000334 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000335 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000336 header->fCoverageAttributeIndex = availableAttributeIndex;
337 } else {
338 header->fCoverageAttributeIndex = -1;
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000339 }
340
egdanielc0648242014-09-22 13:17:02 -0700341 header->fPrimaryOutputType = optState.getPrimaryOutputType();
342 header->fSecondaryOutputType = optState.getSecondaryOutputType();
joshualittbd769d02014-09-04 08:56:46 -0700343
joshualitta5305a12014-10-10 17:47:00 -0700344 header->fColorEffectCnt = optState.numColorStages();
345 header->fCoverageEffectCnt = optState.numCoverageStages();
bsalomon848faf02014-07-11 10:01:02 -0700346 desc->finalize();
347 return true;
348}
349
350void GrGLProgramDesc::finalize() {
351 int keyLength = fKey.count();
352 SkASSERT(0 == (keyLength % 4));
353 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
354
355 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
356 *checksum = 0;
357 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000358}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000359
360GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
bsalomon848faf02014-07-11 10:01:02 -0700361 size_t keyLength = other.keyLength();
362 fKey.reset(keyLength);
363 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000364 return *this;
365}