blob: 0ea5417b5022ca2b791fcda6914e84e6d9b55183 [file] [log] [blame]
bsalomon@google.com798c8c42013-03-27 19:50:27 +00001/*
egdaniel0d9990f2016-07-29 07:36:52 -07002 * Copyright 2016 Google Inc.
bsalomon@google.com798c8c42013-03-27 19:50:27 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Hal Canaryc640d0d2018-06-13 09:59:02 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProgramDesc.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkChecksum.h"
11#include "include/private/SkTo.h"
12#include "src/gpu/GrPipeline.h"
13#include "src/gpu/GrPrimitiveProcessor.h"
14#include "src/gpu/GrProcessor.h"
15#include "src/gpu/GrRenderTargetPriv.h"
16#include "src/gpu/GrShaderCaps.h"
17#include "src/gpu/GrTexturePriv.h"
18#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
19#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000020
Brian Salomonf9f45122016-11-29 11:59:17 -050021enum {
22 kSamplerOrImageTypeKeyBits = 4
23};
Brian Salomonbe348822016-11-22 15:56:30 -050024
Brian Salomon60dd8c72018-07-30 10:24:13 -040025static inline uint16_t texture_type_key(GrTextureType type) {
Brian Salomonf9f45122016-11-29 11:59:17 -050026 int value = UINT16_MAX;
27 switch (type) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040028 case GrTextureType::k2D:
Brian Salomonf9f45122016-11-29 11:59:17 -050029 value = 0;
30 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040031 case GrTextureType::kExternal:
Brian Salomonf9f45122016-11-29 11:59:17 -050032 value = 1;
33 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040034 case GrTextureType::kRectangle:
Brian Salomonf9f45122016-11-29 11:59:17 -050035 value = 2;
36 break;
Brian Salomonf9f45122016-11-29 11:59:17 -050037 }
38 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
Brian Salomon60dd8c72018-07-30 10:24:13 -040039 return SkToU16(value);
Brian Salomonbe348822016-11-22 15:56:30 -050040}
41
Greg Daniel2c3398d2019-06-19 11:58:01 -040042static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle,
43 GrPixelConfig config, const GrShaderCaps& caps) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040044 int samplerTypeKey = texture_type_key(textureType);
Brian Salomonf9f45122016-11-29 11:59:17 -050045
Greg Daniel2c3398d2019-06-19 11:58:01 -040046 GR_STATIC_ASSERT(2 == sizeof(swizzle.asKey()));
Brian Salomon68ba1172019-06-05 11:15:08 -040047 uint16_t swizzleKey = 0;
48 if (caps.textureSwizzleAppliedInShader()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -040049 swizzleKey = swizzle.asKey();
Brian Salomon68ba1172019-06-05 11:15:08 -040050 }
Greg Danielf259b8b2019-02-14 09:03:43 -050051 return SkToU32(samplerTypeKey |
Brian Salomon68ba1172019-06-05 11:15:08 -040052 swizzleKey << kSamplerOrImageTypeKeyBits |
Greg Danielf259b8b2019-02-14 09:03:43 -050053 (GrSLSamplerPrecision(config) << (16 + kSamplerOrImageTypeKeyBits)));
Brian Salomonf9f45122016-11-29 11:59:17 -050054}
55
Brian Salomone782f842018-07-31 13:53:11 -040056static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050057 GrGpu* gpu, const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040058 int numTextureSamplers = fp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050059 if (!numTextureSamplers) {
bsalomoncdee0092016-01-08 13:20:12 -080060 return;
joshualitt23e280d2014-09-18 12:26:38 -070061 }
Greg Danielf259b8b2019-02-14 09:03:43 -050062 uint32_t* k32 = b->add32n(numTextureSamplers);
Brian Salomone782f842018-07-31 13:53:11 -040063 for (int i = 0; i < numTextureSamplers; ++i) {
64 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040065 const GrTexture* tex = sampler.peekTexture();
Greg Daniel2c3398d2019-06-19 11:58:01 -040066 k32[i] = sampler_key(tex->texturePriv().textureType(), sampler.swizzle(), tex->config(),
67 caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050068 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
69 sampler.samplerState(), sampler.proxy()->backendFormat());
70 if (extraSamplerKey) {
71 SkASSERT(sampler.proxy()->textureType() == GrTextureType::kExternal);
72 // We first mark the normal sampler key with last bit to flag that it has an extra
73 // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
Greg Danielf259b8b2019-02-14 09:03:43 -050074 SkASSERT((k32[i] & (1 << 31)) == 0);
75 k32[i] = k32[i] | (1 << 31);
Greg Daniel7a82edf2018-12-04 10:54:34 -050076 b->add32(extraSamplerKey);
77 }
Brian Salomone782f842018-07-31 13:53:11 -040078 }
Brian Salomone782f842018-07-31 13:53:11 -040079}
Robert Phillips9bee2e52017-05-29 12:37:20 -040080
Brian Salomone782f842018-07-31 13:53:11 -040081static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050082 const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040083 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050084 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040085 return;
86 }
Greg Danielf259b8b2019-02-14 09:03:43 -050087 uint32_t* k32 = b->add32n(numTextureSamplers);
Brian Salomone782f842018-07-31 13:53:11 -040088 for (int i = 0; i < numTextureSamplers; ++i) {
89 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Greg Daniel2c3398d2019-06-19 11:58:01 -040090 k32[i] = sampler_key(sampler.textureType(), sampler.swizzle(), sampler.config(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050091 uint32_t extraSamplerKey = sampler.extraSamplerKey();
92 if (extraSamplerKey) {
93 SkASSERT(sampler.textureType() == GrTextureType::kExternal);
94 // We first mark the normal sampler key with last bit to flag that it has an extra
95 // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
Greg Danielf259b8b2019-02-14 09:03:43 -050096 SkASSERT((k32[i] & (1 << 15)) == 0);
97 k32[i] = k32[i] | (1 << 15);
Greg Daniel7a82edf2018-12-04 10:54:34 -050098 b->add32(extraSamplerKey);
99 }
bsalomoncdee0092016-01-08 13:20:12 -0800100 }
joshualitt23e280d2014-09-18 12:26:38 -0700101}
102
103/**
104 * A function which emits a meta key into the key builder. This is required because shader code may
105 * be dependent on properties of the effect that the effect itself doesn't use
106 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
107 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700108 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800109 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
110 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700111 */
Brian Salomone782f842018-07-31 13:53:11 -0400112static bool gen_meta_key(const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500113 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500114 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -0700115 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800116 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800117 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400118 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700119
bsalomon7ea33f52015-11-22 14:51:00 -0800120 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000121 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800122 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700123 return false;
124 }
125
Greg Daniel7a82edf2018-12-04 10:54:34 -0500126 add_sampler_keys(b, fp, gpu, shaderCaps);
Brian Salomone782f842018-07-31 13:53:11 -0400127
128 uint32_t* key = b->add32n(2);
129 key[0] = (classID << 16) | SkToU32(processorKeySize);
130 key[1] = transformKey;
131 return true;
132}
133
134static bool gen_meta_key(const GrPrimitiveProcessor& pp,
135 const GrShaderCaps& shaderCaps,
136 uint32_t transformKey,
137 GrProcessorKeyBuilder* b) {
138 size_t processorKeySize = b->size();
139 uint32_t classID = pp.classID();
140
141 // Currently we allow 16 bits for the class id and the overall processor key size.
142 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
143 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
144 return false;
145 }
146
147 add_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800148
149 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800150 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800151 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700152 return true;
153}
joshualittb0a8a372014-09-23 09:50:21 -0700154
Brian Salomonab015ef2017-04-04 10:15:51 -0400155static bool gen_meta_key(const GrXferProcessor& xp,
156 const GrShaderCaps& shaderCaps,
157 GrProcessorKeyBuilder* b) {
158 size_t processorKeySize = b->size();
159 uint32_t classID = xp.classID();
160
161 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000162 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400163 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
164 return false;
165 }
166
167 b->add32((classID << 16) | SkToU32(processorKeySize));
168 return true;
169}
170
bsalomon7ea33f52015-11-22 14:51:00 -0800171static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700172 const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500173 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500174 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700175 GrProcessorKeyBuilder* b) {
176 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500177 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700178 return false;
179 }
180 }
181
Brian Salomon1edc5b92016-11-29 13:43:46 -0500182 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700183
Greg Daniel7a82edf2018-12-04 10:54:34 -0500184 return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
185 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700186}
187
Chris Daltond7291ba2019-03-07 14:17:03 -0700188bool GrProgramDesc::Build(
189 GrProgramDesc* desc, const GrRenderTarget* renderTarget,
190 const GrPrimitiveProcessor& primProc, bool hasPointSize, const GrPipeline& pipeline,
191 GrGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000192 // The descriptor is used as a cache key. Thus when a field of the
193 // descriptor will not affect program generation (because of the attribute
194 // bindings in use or other descriptor field settings) it should be set
195 // to a canonical value to avoid duplicate programs with different keys.
196
Greg Daniel7a82edf2018-12-04 10:54:34 -0500197 const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
198
egdanielc67870c2014-11-26 08:50:50 -0800199 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
200 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700201 desc->key().reset();
202 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700203
egdaniel5d8f69f2016-09-07 07:24:12 -0700204 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800205
Brian Salomon1edc5b92016-11-29 13:43:46 -0500206 primProc.getGLSLProcessorKey(shaderCaps, &b);
Brian Osmand3e71302018-12-06 11:17:35 -0500207 primProc.getAttributeKey(&b);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500208 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700209 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800210 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000211 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700212 GrProcessor::CustomFeatures processorFeatures = primProc.requestedFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700213
bsalomonac856c92015-08-27 06:30:17 -0700214 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
215 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500216 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700217 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700218 return false;
219 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700220 processorFeatures |= fp.requestedFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000221 }
egdaniel170f90b2014-09-16 12:54:40 -0700222
bsalomon2047b782015-12-21 13:12:54 -0800223 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400224 const GrSurfaceOrigin* originIfDstTexture = nullptr;
225 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400226 if (pipeline.dstTextureProxy()) {
227 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400228 originIfDstTexture = &origin;
229 }
230 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400231 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700232 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800233 return false;
234 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700235 processorFeatures |= xp.requestedFeatures();
236
237 if (processorFeatures & GrProcessor::CustomFeatures::kSampleLocations) {
238 SkASSERT(pipeline.isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600239 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700240 }
egdanielc2304142014-12-11 13:15:13 -0800241
joshualitt65171342014-10-09 07:25:36 -0700242 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700243 // Because header is a pointer into the dynamic array, we can't push any new data into the key
244 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700245 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000246
joshualitt65171342014-10-09 07:25:36 -0700247 // make sure any padding in the header is zeroed.
248 memset(header, 0, kHeaderSize);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400249 header->fOutputSwizzle = pipeline.outputSwizzle().asKey();
bsalomon2eda5b32016-09-21 10:53:24 -0700250 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
251 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
252 // Fail if the client requested more processors than the key can fit.
253 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
254 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
255 return false;
256 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700257 header->fProcessorFeatures = (uint8_t)processorFeatures;
258 SkASSERT(header->processorFeatures() == processorFeatures); // Ensure enough bits.
259 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700260 header->fHasPointSize = hasPointSize ? 1 : 0;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400261 header->fClampBlendInput =
262 GrClampType::kManual == GrPixelConfigClampType(renderTarget->config()) ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700263 return true;
264}