blob: d20d6e17c01c34b16d6421ae25ab60c8bd51f922 [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;
Robert Phillipsf209e882019-06-25 15:59:50 -040037 default:
38 SK_ABORT("Unexpected texture type");
39 value = 3;
40 break;
Brian Salomonf9f45122016-11-29 11:59:17 -050041 }
42 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
Brian Salomon60dd8c72018-07-30 10:24:13 -040043 return SkToU16(value);
Brian Salomonbe348822016-11-22 15:56:30 -050044}
45
Greg Daniel2c3398d2019-06-19 11:58:01 -040046static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle,
47 GrPixelConfig config, const GrShaderCaps& caps) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040048 int samplerTypeKey = texture_type_key(textureType);
Brian Salomonf9f45122016-11-29 11:59:17 -050049
Greg Daniel2c3398d2019-06-19 11:58:01 -040050 GR_STATIC_ASSERT(2 == sizeof(swizzle.asKey()));
Brian Salomon68ba1172019-06-05 11:15:08 -040051 uint16_t swizzleKey = 0;
52 if (caps.textureSwizzleAppliedInShader()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -040053 swizzleKey = swizzle.asKey();
Brian Salomon68ba1172019-06-05 11:15:08 -040054 }
Greg Danielf259b8b2019-02-14 09:03:43 -050055 return SkToU32(samplerTypeKey |
Brian Salomon68ba1172019-06-05 11:15:08 -040056 swizzleKey << kSamplerOrImageTypeKeyBits |
Greg Danielf259b8b2019-02-14 09:03:43 -050057 (GrSLSamplerPrecision(config) << (16 + kSamplerOrImageTypeKeyBits)));
Brian Salomonf9f45122016-11-29 11:59:17 -050058}
59
Brian Salomone782f842018-07-31 13:53:11 -040060static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050061 GrGpu* gpu, const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040062 int numTextureSamplers = fp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050063 if (!numTextureSamplers) {
bsalomoncdee0092016-01-08 13:20:12 -080064 return;
joshualitt23e280d2014-09-18 12:26:38 -070065 }
Brian Salomone782f842018-07-31 13:53:11 -040066 for (int i = 0; i < numTextureSamplers; ++i) {
67 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040068 const GrTexture* tex = sampler.peekTexture();
Brian Osman1a22b7f2019-07-23 09:32:08 -040069 uint32_t samplerKey = sampler_key(
70 tex->texturePriv().textureType(), sampler.swizzle(), tex->config(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050071 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
72 sampler.samplerState(), sampler.proxy()->backendFormat());
73 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050074 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040075 // sampler key. We then add both keys.
76 SkASSERT((samplerKey & (1 << 31)) == 0);
77 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -050078 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -040079 } else {
80 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -050081 }
Brian Salomone782f842018-07-31 13:53:11 -040082 }
Brian Salomone782f842018-07-31 13:53:11 -040083}
Robert Phillips9bee2e52017-05-29 12:37:20 -040084
Brian Salomone782f842018-07-31 13:53:11 -040085static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050086 const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040087 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050088 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040089 return;
90 }
Brian Salomone782f842018-07-31 13:53:11 -040091 for (int i = 0; i < numTextureSamplers; ++i) {
92 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Brian Osman1a22b7f2019-07-23 09:32:08 -040093 uint32_t samplerKey = sampler_key(
94 sampler.textureType(), sampler.swizzle(), sampler.config(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050095 uint32_t extraSamplerKey = sampler.extraSamplerKey();
96 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050097 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040098 // sampler key. We then add both keys.
99 SkASSERT((samplerKey & (1 << 31)) == 0);
100 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -0500101 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -0400102 } else {
103 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500104 }
bsalomoncdee0092016-01-08 13:20:12 -0800105 }
joshualitt23e280d2014-09-18 12:26:38 -0700106}
107
108/**
109 * A function which emits a meta key into the key builder. This is required because shader code may
110 * be dependent on properties of the effect that the effect itself doesn't use
111 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
112 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700113 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800114 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
115 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700116 */
Brian Salomone782f842018-07-31 13:53:11 -0400117static bool gen_meta_key(const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500118 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500119 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -0700120 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800121 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800122 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400123 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700124
bsalomon7ea33f52015-11-22 14:51:00 -0800125 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000126 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800127 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700128 return false;
129 }
130
Greg Daniel7a82edf2018-12-04 10:54:34 -0500131 add_sampler_keys(b, fp, gpu, shaderCaps);
Brian Salomone782f842018-07-31 13:53:11 -0400132
133 uint32_t* key = b->add32n(2);
134 key[0] = (classID << 16) | SkToU32(processorKeySize);
135 key[1] = transformKey;
136 return true;
137}
138
139static bool gen_meta_key(const GrPrimitiveProcessor& pp,
140 const GrShaderCaps& shaderCaps,
141 uint32_t transformKey,
142 GrProcessorKeyBuilder* b) {
143 size_t processorKeySize = b->size();
144 uint32_t classID = pp.classID();
145
146 // Currently we allow 16 bits for the class id and the overall processor key size.
147 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
148 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
149 return false;
150 }
151
152 add_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800153
154 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800155 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800156 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700157 return true;
158}
joshualittb0a8a372014-09-23 09:50:21 -0700159
Brian Salomonab015ef2017-04-04 10:15:51 -0400160static bool gen_meta_key(const GrXferProcessor& xp,
161 const GrShaderCaps& shaderCaps,
162 GrProcessorKeyBuilder* b) {
163 size_t processorKeySize = b->size();
164 uint32_t classID = xp.classID();
165
166 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000167 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400168 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
169 return false;
170 }
171
172 b->add32((classID << 16) | SkToU32(processorKeySize));
173 return true;
174}
175
bsalomon7ea33f52015-11-22 14:51:00 -0800176static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700177 const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500178 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500179 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700180 GrProcessorKeyBuilder* b) {
181 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500182 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700183 return false;
184 }
185 }
186
Brian Salomon1edc5b92016-11-29 13:43:46 -0500187 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700188
Greg Daniel7a82edf2018-12-04 10:54:34 -0500189 return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
190 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700191}
192
Chris Daltond7291ba2019-03-07 14:17:03 -0700193bool GrProgramDesc::Build(
194 GrProgramDesc* desc, const GrRenderTarget* renderTarget,
195 const GrPrimitiveProcessor& primProc, bool hasPointSize, const GrPipeline& pipeline,
196 GrGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000197 // The descriptor is used as a cache key. Thus when a field of the
198 // descriptor will not affect program generation (because of the attribute
199 // bindings in use or other descriptor field settings) it should be set
200 // to a canonical value to avoid duplicate programs with different keys.
201
Greg Daniel7a82edf2018-12-04 10:54:34 -0500202 const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
203
egdanielc67870c2014-11-26 08:50:50 -0800204 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
205 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700206 desc->key().reset();
207 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700208
egdaniel5d8f69f2016-09-07 07:24:12 -0700209 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800210
Brian Salomon1edc5b92016-11-29 13:43:46 -0500211 primProc.getGLSLProcessorKey(shaderCaps, &b);
Brian Osmand3e71302018-12-06 11:17:35 -0500212 primProc.getAttributeKey(&b);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500213 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700214 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800215 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000216 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700217 GrProcessor::CustomFeatures processorFeatures = primProc.requestedFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700218
bsalomonac856c92015-08-27 06:30:17 -0700219 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
220 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500221 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700222 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700223 return false;
224 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700225 processorFeatures |= fp.requestedFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000226 }
egdaniel170f90b2014-09-16 12:54:40 -0700227
bsalomon2047b782015-12-21 13:12:54 -0800228 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400229 const GrSurfaceOrigin* originIfDstTexture = nullptr;
230 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400231 if (pipeline.dstTextureProxy()) {
232 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400233 originIfDstTexture = &origin;
234 }
235 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400236 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700237 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800238 return false;
239 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700240 processorFeatures |= xp.requestedFeatures();
241
242 if (processorFeatures & GrProcessor::CustomFeatures::kSampleLocations) {
243 SkASSERT(pipeline.isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600244 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700245 }
egdanielc2304142014-12-11 13:15:13 -0800246
joshualitt65171342014-10-09 07:25:36 -0700247 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700248 // Because header is a pointer into the dynamic array, we can't push any new data into the key
249 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700250 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000251
joshualitt65171342014-10-09 07:25:36 -0700252 // make sure any padding in the header is zeroed.
253 memset(header, 0, kHeaderSize);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400254 header->fOutputSwizzle = pipeline.outputSwizzle().asKey();
bsalomon2eda5b32016-09-21 10:53:24 -0700255 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
256 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
257 // Fail if the client requested more processors than the key can fit.
258 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
259 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
260 return false;
261 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700262 header->fProcessorFeatures = (uint8_t)processorFeatures;
263 SkASSERT(header->processorFeatures() == processorFeatures); // Ensure enough bits.
264 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700265 header->fHasPointSize = hasPointSize ? 1 : 0;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400266 header->fClampBlendInput =
267 GrClampType::kManual == GrPixelConfigClampType(renderTarget->config()) ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700268 return true;
269}