blob: acc0fd100c5633e881348668f239edc02c60e564 [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,
Brian Salomon67529b22019-08-13 15:31:04 -040047 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 }
Brian Salomon67529b22019-08-13 15:31:04 -040055 return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
Brian Salomonf9f45122016-11-29 11:59:17 -050056}
57
Brian Salomone782f842018-07-31 13:53:11 -040058static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050059 GrGpu* gpu, const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040060 int numTextureSamplers = fp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050061 if (!numTextureSamplers) {
bsalomoncdee0092016-01-08 13:20:12 -080062 return;
joshualitt23e280d2014-09-18 12:26:38 -070063 }
Brian Salomone782f842018-07-31 13:53:11 -040064 for (int i = 0; i < numTextureSamplers; ++i) {
65 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040066 const GrTexture* tex = sampler.peekTexture();
Brian Osman1a22b7f2019-07-23 09:32:08 -040067 uint32_t samplerKey = sampler_key(
Brian Salomon67529b22019-08-13 15:31:04 -040068 tex->texturePriv().textureType(), sampler.swizzle(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050069 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
70 sampler.samplerState(), sampler.proxy()->backendFormat());
71 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050072 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040073 // sampler key. We then add both keys.
74 SkASSERT((samplerKey & (1 << 31)) == 0);
75 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -050076 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -040077 } else {
78 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -050079 }
Brian Salomone782f842018-07-31 13:53:11 -040080 }
Brian Salomone782f842018-07-31 13:53:11 -040081}
Robert Phillips9bee2e52017-05-29 12:37:20 -040082
Brian Salomone782f842018-07-31 13:53:11 -040083static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Greg Daniel7a82edf2018-12-04 10:54:34 -050084 const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040085 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050086 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040087 return;
88 }
Brian Salomone782f842018-07-31 13:53:11 -040089 for (int i = 0; i < numTextureSamplers; ++i) {
90 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Brian Osman1a22b7f2019-07-23 09:32:08 -040091 uint32_t samplerKey = sampler_key(
Brian Salomon67529b22019-08-13 15:31:04 -040092 sampler.textureType(), sampler.swizzle(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050093 uint32_t extraSamplerKey = sampler.extraSamplerKey();
94 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050095 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040096 // sampler key. We then add both keys.
97 SkASSERT((samplerKey & (1 << 31)) == 0);
98 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -050099 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -0400100 } else {
101 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500102 }
bsalomoncdee0092016-01-08 13:20:12 -0800103 }
joshualitt23e280d2014-09-18 12:26:38 -0700104}
105
106/**
107 * A function which emits a meta key into the key builder. This is required because shader code may
108 * be dependent on properties of the effect that the effect itself doesn't use
109 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
110 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700111 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800112 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
113 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700114 */
Brian Salomone782f842018-07-31 13:53:11 -0400115static bool gen_meta_key(const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500116 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500117 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -0700118 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800119 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800120 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400121 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700122
bsalomon7ea33f52015-11-22 14:51:00 -0800123 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000124 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800125 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700126 return false;
127 }
128
Greg Daniel7a82edf2018-12-04 10:54:34 -0500129 add_sampler_keys(b, fp, gpu, shaderCaps);
Brian Salomone782f842018-07-31 13:53:11 -0400130
131 uint32_t* key = b->add32n(2);
132 key[0] = (classID << 16) | SkToU32(processorKeySize);
133 key[1] = transformKey;
134 return true;
135}
136
137static bool gen_meta_key(const GrPrimitiveProcessor& pp,
138 const GrShaderCaps& shaderCaps,
139 uint32_t transformKey,
140 GrProcessorKeyBuilder* b) {
141 size_t processorKeySize = b->size();
142 uint32_t classID = pp.classID();
143
144 // Currently we allow 16 bits for the class id and the overall processor key size.
145 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
146 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
147 return false;
148 }
149
150 add_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800151
152 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800153 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800154 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700155 return true;
156}
joshualittb0a8a372014-09-23 09:50:21 -0700157
Brian Salomonab015ef2017-04-04 10:15:51 -0400158static bool gen_meta_key(const GrXferProcessor& xp,
159 const GrShaderCaps& shaderCaps,
160 GrProcessorKeyBuilder* b) {
161 size_t processorKeySize = b->size();
162 uint32_t classID = xp.classID();
163
164 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000165 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400166 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
167 return false;
168 }
169
170 b->add32((classID << 16) | SkToU32(processorKeySize));
171 return true;
172}
173
bsalomon7ea33f52015-11-22 14:51:00 -0800174static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700175 const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500176 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500177 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700178 GrProcessorKeyBuilder* b) {
179 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500180 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700181 return false;
182 }
183 }
184
Brian Salomon1edc5b92016-11-29 13:43:46 -0500185 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700186
Greg Daniel7a82edf2018-12-04 10:54:34 -0500187 return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
188 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700189}
190
Chris Daltond7291ba2019-03-07 14:17:03 -0700191bool GrProgramDesc::Build(
192 GrProgramDesc* desc, const GrRenderTarget* renderTarget,
193 const GrPrimitiveProcessor& primProc, bool hasPointSize, const GrPipeline& pipeline,
194 GrGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000195 // The descriptor is used as a cache key. Thus when a field of the
196 // descriptor will not affect program generation (because of the attribute
197 // bindings in use or other descriptor field settings) it should be set
198 // to a canonical value to avoid duplicate programs with different keys.
199
Greg Daniel7a82edf2018-12-04 10:54:34 -0500200 const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
201
egdanielc67870c2014-11-26 08:50:50 -0800202 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
203 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700204 desc->key().reset();
205 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700206
egdaniel5d8f69f2016-09-07 07:24:12 -0700207 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800208
Brian Salomon1edc5b92016-11-29 13:43:46 -0500209 primProc.getGLSLProcessorKey(shaderCaps, &b);
Brian Osmand3e71302018-12-06 11:17:35 -0500210 primProc.getAttributeKey(&b);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500211 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700212 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800213 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000214 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700215 GrProcessor::CustomFeatures processorFeatures = primProc.requestedFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700216
bsalomonac856c92015-08-27 06:30:17 -0700217 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
218 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500219 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700220 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700221 return false;
222 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700223 processorFeatures |= fp.requestedFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000224 }
egdaniel170f90b2014-09-16 12:54:40 -0700225
bsalomon2047b782015-12-21 13:12:54 -0800226 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400227 const GrSurfaceOrigin* originIfDstTexture = nullptr;
228 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400229 if (pipeline.dstTextureProxy()) {
230 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400231 originIfDstTexture = &origin;
232 }
233 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400234 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700235 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800236 return false;
237 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700238 processorFeatures |= xp.requestedFeatures();
239
240 if (processorFeatures & GrProcessor::CustomFeatures::kSampleLocations) {
241 SkASSERT(pipeline.isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600242 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700243 }
egdanielc2304142014-12-11 13:15:13 -0800244
joshualitt65171342014-10-09 07:25:36 -0700245 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700246 // Because header is a pointer into the dynamic array, we can't push any new data into the key
247 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700248 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000249
joshualitt65171342014-10-09 07:25:36 -0700250 // make sure any padding in the header is zeroed.
251 memset(header, 0, kHeaderSize);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400252 header->fOutputSwizzle = pipeline.outputSwizzle().asKey();
bsalomon2eda5b32016-09-21 10:53:24 -0700253 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
254 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
255 // Fail if the client requested more processors than the key can fit.
256 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
257 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
258 return false;
259 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700260 header->fProcessorFeatures = (uint8_t)processorFeatures;
261 SkASSERT(header->processorFeatures() == processorFeatures); // Ensure enough bits.
262 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700263 header->fHasPointSize = hasPointSize ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700264 return true;
265}