blob: e99a947d8047c9f6a832eb632c53066b4a893ec4 [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
Brian Salomon60dd8c72018-07-30 10:24:13 -040042static uint16_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
46 GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey()));
47 return SkToU16(samplerTypeKey |
48 caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
Chris Dalton47c8ed32017-11-15 18:27:09 -070049 (GrSLSamplerPrecision(config) << (8 + 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,
53 const GrShaderCaps& caps) {
54 int numTextureSamplers = fp.numTextureSamplers();
Brian Salomonf9f45122016-11-29 11:59:17 -050055 // Need two bytes per key.
Brian Salomon662ea4b2018-07-12 14:53:49 -040056 int word32Count = (numTextureSamplers + 1) / 2;
bsalomoncdee0092016-01-08 13:20:12 -080057 if (0 == word32Count) {
58 return;
joshualitt23e280d2014-09-18 12:26:38 -070059 }
Mike Kleine72e7732018-06-14 14:41:22 -040060 uint16_t* k16 = reinterpret_cast<uint16_t*>(b->add32n(word32Count));
Brian Salomone782f842018-07-31 13:53:11 -040061 for (int i = 0; i < numTextureSamplers; ++i) {
62 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040063 const GrTexture* tex = sampler.peekTexture();
Brian Salomone782f842018-07-31 13:53:11 -040064 k16[i] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps);
65 }
66 // zero the last 16 bits if the number of uniforms for samplers is odd.
67 if (numTextureSamplers & 0x1) {
68 k16[numTextureSamplers] = 0;
69 }
70}
Robert Phillips9bee2e52017-05-29 12:37:20 -040071
Brian Salomone782f842018-07-31 13:53:11 -040072static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
73 const GrShaderCaps& caps) {
74 int numTextureSamplers = pp.numTextureSamplers();
75 // Need two bytes per key.
76 int word32Count = (numTextureSamplers + 1) / 2;
77 if (0 == word32Count) {
78 return;
79 }
80 uint16_t* k16 = reinterpret_cast<uint16_t*>(b->add32n(word32Count));
81 for (int i = 0; i < numTextureSamplers; ++i) {
82 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
83 const GrTexture* tex = sampler.peekTexture();
84 k16[i] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps);
bsalomoncdee0092016-01-08 13:20:12 -080085 }
Brian Salomon662ea4b2018-07-12 14:53:49 -040086 // zero the last 16 bits if the number of uniforms for samplers is odd.
87 if (numTextureSamplers & 0x1) {
88 k16[numTextureSamplers] = 0;
bsalomon65859ec2016-01-11 09:17:52 -080089 }
joshualitt23e280d2014-09-18 12:26:38 -070090}
91
92/**
93 * A function which emits a meta key into the key builder. This is required because shader code may
94 * be dependent on properties of the effect that the effect itself doesn't use
95 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
96 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070097 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080098 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
99 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700100 */
Brian Salomone782f842018-07-31 13:53:11 -0400101static bool gen_meta_key(const GrFragmentProcessor& fp,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500102 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -0700103 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800104 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800105 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400106 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700107
bsalomon7ea33f52015-11-22 14:51:00 -0800108 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000109 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800110 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700111 return false;
112 }
113
Brian Salomone782f842018-07-31 13:53:11 -0400114 add_sampler_keys(b, fp, shaderCaps);
115
116 uint32_t* key = b->add32n(2);
117 key[0] = (classID << 16) | SkToU32(processorKeySize);
118 key[1] = transformKey;
119 return true;
120}
121
122static bool gen_meta_key(const GrPrimitiveProcessor& pp,
123 const GrShaderCaps& shaderCaps,
124 uint32_t transformKey,
125 GrProcessorKeyBuilder* b) {
126 size_t processorKeySize = b->size();
127 uint32_t classID = pp.classID();
128
129 // Currently we allow 16 bits for the class id and the overall processor key size.
130 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
131 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
132 return false;
133 }
134
135 add_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800136
137 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800138 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800139 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700140 return true;
141}
joshualittb0a8a372014-09-23 09:50:21 -0700142
Brian Salomonab015ef2017-04-04 10:15:51 -0400143static bool gen_meta_key(const GrXferProcessor& xp,
144 const GrShaderCaps& shaderCaps,
145 GrProcessorKeyBuilder* b) {
146 size_t processorKeySize = b->size();
147 uint32_t classID = xp.classID();
148
149 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000150 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400151 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
152 return false;
153 }
154
155 b->add32((classID << 16) | SkToU32(processorKeySize));
156 return true;
157}
158
bsalomon7ea33f52015-11-22 14:51:00 -0800159static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700160 const GrFragmentProcessor& fp,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500161 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700162 GrProcessorKeyBuilder* b) {
163 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500164 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700165 return false;
166 }
167 }
168
Brian Salomon1edc5b92016-11-29 13:43:46 -0500169 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700170
Brian Salomon1edc5b92016-11-29 13:43:46 -0500171 return gen_meta_key(fp, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
172 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700173}
174
egdaniel5d8f69f2016-09-07 07:24:12 -0700175bool GrProgramDesc::Build(GrProgramDesc* desc,
176 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -0700177 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -0700178 const GrPipeline& pipeline,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500179 const GrShaderCaps& shaderCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000180 // The descriptor is used as a cache key. Thus when a field of the
181 // descriptor will not affect program generation (because of the attribute
182 // bindings in use or other descriptor field settings) it should be set
183 // to a canonical value to avoid duplicate programs with different keys.
184
egdanielc67870c2014-11-26 08:50:50 -0800185 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
186 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700187 desc->key().reset();
188 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700189
egdaniel5d8f69f2016-09-07 07:24:12 -0700190 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800191
Brian Salomon1edc5b92016-11-29 13:43:46 -0500192 primProc.getGLSLProcessorKey(shaderCaps, &b);
193 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700194 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800195 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000196 }
bsalomon929f29a2014-07-17 07:55:11 -0700197
bsalomonac856c92015-08-27 06:30:17 -0700198 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
199 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500200 if (!gen_frag_proc_and_meta_keys(primProc, fp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700201 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700202 return false;
203 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000204 }
egdaniel170f90b2014-09-16 12:54:40 -0700205
bsalomon2047b782015-12-21 13:12:54 -0800206 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400207 const GrSurfaceOrigin* originIfDstTexture = nullptr;
208 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400209 if (pipeline.dstTextureProxy()) {
210 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400211 originIfDstTexture = &origin;
212 }
213 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400214 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700215 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800216 return false;
217 }
218
joshualitt65171342014-10-09 07:25:36 -0700219 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700220 // Because header is a pointer into the dynamic array, we can't push any new data into the key
221 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700222 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000223
joshualitt65171342014-10-09 07:25:36 -0700224 // make sure any padding in the header is zeroed.
225 memset(header, 0, kHeaderSize);
Chris Dalton535ba8d2018-02-20 09:51:59 -0700226 header->fOutputSwizzle = shaderCaps.configOutputSwizzle(pipeline.proxy()->config()).asKey();
bsalomond79c5492015-04-27 10:07:04 -0700227 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700228 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
229 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
230 // Fail if the client requested more processors than the key can fit.
231 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
232 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
233 return false;
234 }
235 header->fHasPointSize = hasPointSize ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700236 return true;
237}