blob: 5401d4d368afa81de4611635d44594203f8c623c [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
egdaniel5d8f69f2016-09-07 07:24:12 -07008#include "GrProgramDesc.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
egdaniel8dd688b2015-01-22 10:16:09 -080010#include "GrPipeline.h"
Brian Salomone7d30482017-03-29 12:09:15 -040011#include "GrPrimitiveProcessor.h"
12#include "GrProcessor.h"
cdalton28f45b92016-03-07 13:58:26 -080013#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050014#include "GrShaderCaps.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050015#include "GrTexturePriv.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016#include "SkChecksum.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040017#include "SkTo.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "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 Danielf259b8b2019-02-14 09:03:43 -050042static uint32_t sampler_key(GrTextureType textureType, GrPixelConfig config,
Brian Salomon94efbf52016-11-29 13:43:05 -050043 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 Danielf259b8b2019-02-14 09:03:43 -050046 GR_STATIC_ASSERT(2 == sizeof(caps.configTextureSwizzle(config).asKey()));
47 return SkToU32(samplerTypeKey |
Brian Salomonf9f45122016-11-29 11:59:17 -050048 caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
Greg Danielf259b8b2019-02-14 09:03:43 -050049 (GrSLSamplerPrecision(config) << (16 + kSamplerOrImageTypeKeyBits)));
Brian Salomonf9f45122016-11-29 11:59:17 -050050}
51
Brian Salomone782f842018-07-31 13:53:11 -040052static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050053 GrGpu* gpu, const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040054 int numTextureSamplers = fp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050055 if (!numTextureSamplers) {
bsalomoncdee0092016-01-08 13:20:12 -080056 return;
joshualitt23e280d2014-09-18 12:26:38 -070057 }
Greg Danielf259b8b2019-02-14 09:03:43 -050058 uint32_t* k32 = b->add32n(numTextureSamplers);
Brian Salomone782f842018-07-31 13:53:11 -040059 for (int i = 0; i < numTextureSamplers; ++i) {
60 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040061 const GrTexture* tex = sampler.peekTexture();
Greg Danielf259b8b2019-02-14 09:03:43 -050062 k32[i] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050063 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
64 sampler.samplerState(), sampler.proxy()->backendFormat());
65 if (extraSamplerKey) {
66 SkASSERT(sampler.proxy()->textureType() == GrTextureType::kExternal);
67 // We first mark the normal sampler key with last bit to flag that it has an extra
68 // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
Greg Danielf259b8b2019-02-14 09:03:43 -050069 SkASSERT((k32[i] & (1 << 31)) == 0);
70 k32[i] = k32[i] | (1 << 31);
Greg Daniel7a82edf2018-12-04 10:54:34 -050071 b->add32(extraSamplerKey);
72 }
Brian Salomone782f842018-07-31 13:53:11 -040073 }
Brian Salomone782f842018-07-31 13:53:11 -040074}
Robert Phillips9bee2e52017-05-29 12:37:20 -040075
Brian Salomone782f842018-07-31 13:53:11 -040076static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050077 const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040078 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050079 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040080 return;
81 }
Greg Danielf259b8b2019-02-14 09:03:43 -050082 uint32_t* k32 = b->add32n(numTextureSamplers);
Brian Salomone782f842018-07-31 13:53:11 -040083 for (int i = 0; i < numTextureSamplers; ++i) {
84 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Greg Danielf259b8b2019-02-14 09:03:43 -050085 k32[i] = sampler_key(sampler.textureType(), sampler.config(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050086 uint32_t extraSamplerKey = sampler.extraSamplerKey();
87 if (extraSamplerKey) {
88 SkASSERT(sampler.textureType() == GrTextureType::kExternal);
89 // We first mark the normal sampler key with last bit to flag that it has an extra
90 // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
Greg Danielf259b8b2019-02-14 09:03:43 -050091 SkASSERT((k32[i] & (1 << 15)) == 0);
92 k32[i] = k32[i] | (1 << 15);
Greg Daniel7a82edf2018-12-04 10:54:34 -050093 b->add32(extraSamplerKey);
94 }
bsalomoncdee0092016-01-08 13:20:12 -080095 }
joshualitt23e280d2014-09-18 12:26:38 -070096}
97
98/**
99 * A function which emits a meta key into the key builder. This is required because shader code may
100 * be dependent on properties of the effect that the effect itself doesn't use
101 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
102 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700103 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800104 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
105 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700106 */
Brian Salomone782f842018-07-31 13:53:11 -0400107static bool gen_meta_key(const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500108 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500109 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -0700110 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800111 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800112 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400113 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700114
bsalomon7ea33f52015-11-22 14:51:00 -0800115 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000116 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800117 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700118 return false;
119 }
120
Greg Daniel7a82edf2018-12-04 10:54:34 -0500121 add_sampler_keys(b, fp, gpu, shaderCaps);
Brian Salomone782f842018-07-31 13:53:11 -0400122
123 uint32_t* key = b->add32n(2);
124 key[0] = (classID << 16) | SkToU32(processorKeySize);
125 key[1] = transformKey;
126 return true;
127}
128
129static bool gen_meta_key(const GrPrimitiveProcessor& pp,
130 const GrShaderCaps& shaderCaps,
131 uint32_t transformKey,
132 GrProcessorKeyBuilder* b) {
133 size_t processorKeySize = b->size();
134 uint32_t classID = pp.classID();
135
136 // Currently we allow 16 bits for the class id and the overall processor key size.
137 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
138 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
139 return false;
140 }
141
142 add_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800143
144 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800145 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800146 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700147 return true;
148}
joshualittb0a8a372014-09-23 09:50:21 -0700149
Brian Salomonab015ef2017-04-04 10:15:51 -0400150static bool gen_meta_key(const GrXferProcessor& xp,
151 const GrShaderCaps& shaderCaps,
152 GrProcessorKeyBuilder* b) {
153 size_t processorKeySize = b->size();
154 uint32_t classID = xp.classID();
155
156 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000157 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400158 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
159 return false;
160 }
161
162 b->add32((classID << 16) | SkToU32(processorKeySize));
163 return true;
164}
165
bsalomon7ea33f52015-11-22 14:51:00 -0800166static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700167 const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500168 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500169 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700170 GrProcessorKeyBuilder* b) {
171 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500172 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700173 return false;
174 }
175 }
176
Brian Salomon1edc5b92016-11-29 13:43:46 -0500177 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700178
Greg Daniel7a82edf2018-12-04 10:54:34 -0500179 return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
180 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700181}
182
Chris Daltond7291ba2019-03-07 14:17:03 -0700183bool GrProgramDesc::Build(
184 GrProgramDesc* desc, const GrRenderTarget* renderTarget,
185 const GrPrimitiveProcessor& primProc, bool hasPointSize, const GrPipeline& pipeline,
186 GrGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000187 // The descriptor is used as a cache key. Thus when a field of the
188 // descriptor will not affect program generation (because of the attribute
189 // bindings in use or other descriptor field settings) it should be set
190 // to a canonical value to avoid duplicate programs with different keys.
191
Greg Daniel7a82edf2018-12-04 10:54:34 -0500192 const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
193
egdanielc67870c2014-11-26 08:50:50 -0800194 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
195 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700196 desc->key().reset();
197 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700198
egdaniel5d8f69f2016-09-07 07:24:12 -0700199 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800200
Brian Salomon1edc5b92016-11-29 13:43:46 -0500201 primProc.getGLSLProcessorKey(shaderCaps, &b);
Brian Osmand3e71302018-12-06 11:17:35 -0500202 primProc.getAttributeKey(&b);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500203 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700204 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800205 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000206 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700207 GrProcessor::CustomFeatures processorFeatures = primProc.requestedFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700208
bsalomonac856c92015-08-27 06:30:17 -0700209 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
210 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500211 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700212 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700213 return false;
214 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700215 processorFeatures |= fp.requestedFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000216 }
egdaniel170f90b2014-09-16 12:54:40 -0700217
bsalomon2047b782015-12-21 13:12:54 -0800218 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400219 const GrSurfaceOrigin* originIfDstTexture = nullptr;
220 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400221 if (pipeline.dstTextureProxy()) {
222 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400223 originIfDstTexture = &origin;
224 }
225 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400226 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700227 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800228 return false;
229 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700230 processorFeatures |= xp.requestedFeatures();
231
232 if (processorFeatures & GrProcessor::CustomFeatures::kSampleLocations) {
233 SkASSERT(pipeline.isHWAntialiasState());
234 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey(pipeline));
235 }
egdanielc2304142014-12-11 13:15:13 -0800236
joshualitt65171342014-10-09 07:25:36 -0700237 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700238 // Because header is a pointer into the dynamic array, we can't push any new data into the key
239 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700240 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000241
joshualitt65171342014-10-09 07:25:36 -0700242 // make sure any padding in the header is zeroed.
243 memset(header, 0, kHeaderSize);
Chris Daltond7291ba2019-03-07 14:17:03 -0700244 header->fOutputSwizzle = shaderCaps.configOutputSwizzle(renderTarget->config()).asKey();
bsalomon2eda5b32016-09-21 10:53:24 -0700245 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
246 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
247 // Fail if the client requested more processors than the key can fit.
248 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
249 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
250 return false;
251 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700252 header->fProcessorFeatures = (uint8_t)processorFeatures;
253 SkASSERT(header->processorFeatures() == processorFeatures); // Ensure enough bits.
254 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700255 header->fHasPointSize = hasPointSize ? 1 : 0;
Brian Osman65cdd612019-03-14 17:01:57 -0400256 header->fClampBlendInput = GrPixelConfigNeedsClamp(renderTarget->config()) ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700257 return true;
258}