blob: 694473d0e2b28e336236d1ca316c4a9426d1074d [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"
Robert Phillips901aff02019-10-08 12:32:56 -040015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/GrShaderCaps.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040018#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
20#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000021
Brian Salomonf9f45122016-11-29 11:59:17 -050022enum {
23 kSamplerOrImageTypeKeyBits = 4
24};
Brian Salomonbe348822016-11-22 15:56:30 -050025
Brian Salomon60dd8c72018-07-30 10:24:13 -040026static inline uint16_t texture_type_key(GrTextureType type) {
Brian Salomonf9f45122016-11-29 11:59:17 -050027 int value = UINT16_MAX;
28 switch (type) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040029 case GrTextureType::k2D:
Brian Salomonf9f45122016-11-29 11:59:17 -050030 value = 0;
31 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040032 case GrTextureType::kExternal:
Brian Salomonf9f45122016-11-29 11:59:17 -050033 value = 1;
34 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040035 case GrTextureType::kRectangle:
Brian Salomonf9f45122016-11-29 11:59:17 -050036 value = 2;
37 break;
Robert Phillipsf209e882019-06-25 15:59:50 -040038 default:
39 SK_ABORT("Unexpected texture type");
40 value = 3;
41 break;
Brian Salomonf9f45122016-11-29 11:59:17 -050042 }
43 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
Brian Salomon60dd8c72018-07-30 10:24:13 -040044 return SkToU16(value);
Brian Salomonbe348822016-11-22 15:56:30 -050045}
46
Greg Daniel2c3398d2019-06-19 11:58:01 -040047static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle,
Robert Phillips323471e2019-11-11 11:33:37 -050048 const GrCaps& caps) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040049 int samplerTypeKey = texture_type_key(textureType);
Brian Salomonf9f45122016-11-29 11:59:17 -050050
Brian Salomon4dea72a2019-12-18 10:43:10 -050051 static_assert(2 == sizeof(swizzle.asKey()));
Brian Salomon68ba1172019-06-05 11:15:08 -040052 uint16_t swizzleKey = 0;
Robert Phillips323471e2019-11-11 11:33:37 -050053 if (caps.shaderCaps()->textureSwizzleAppliedInShader()) {
Greg Daniel2c3398d2019-06-19 11:58:01 -040054 swizzleKey = swizzle.asKey();
Brian Salomon68ba1172019-06-05 11:15:08 -040055 }
Brian Salomon67529b22019-08-13 15:31:04 -040056 return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
Brian Salomonf9f45122016-11-29 11:59:17 -050057}
58
Robert Phillipsf272bea2019-10-17 08:56:16 -040059static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -050060 const GrCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040061 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050062 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040063 return;
64 }
Brian Salomone782f842018-07-31 13:53:11 -040065 for (int i = 0; i < numTextureSamplers; ++i) {
66 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050067 const GrBackendFormat& backendFormat = sampler.backendFormat();
68
69 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
70 b->add32(samplerKey);
71
72 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080073 }
joshualitt23e280d2014-09-18 12:26:38 -070074}
75
76/**
77 * A function which emits a meta key into the key builder. This is required because shader code may
78 * be dependent on properties of the effect that the effect itself doesn't use
79 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
80 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070081 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080082 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
83 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070084 */
Robert Phillipsf272bea2019-10-17 08:56:16 -040085static bool gen_fp_meta_key(const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -050086 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -040087 uint32_t transformKey,
88 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080089 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -040090 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070091
bsalomon7ea33f52015-11-22 14:51:00 -080092 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +000093 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -080094 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070095 return false;
96 }
97
Brian Salomond90b3d32020-07-09 12:04:31 -040098 fp.visitTextureEffects([&](const GrTextureEffect& te) {
99 const GrBackendFormat& backendFormat = te.view().proxy()->backendFormat();
100 uint32_t samplerKey = sampler_key(backendFormat.textureType(), te.view().swizzle(), caps);
101 b->add32(samplerKey);
102 caps.addExtraSamplerKey(b, te.samplerState(), backendFormat);
103 });
Brian Salomone782f842018-07-31 13:53:11 -0400104
105 uint32_t* key = b->add32n(2);
106 key[0] = (classID << 16) | SkToU32(processorKeySize);
107 key[1] = transformKey;
108 return true;
109}
110
Robert Phillipsf272bea2019-10-17 08:56:16 -0400111static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -0500112 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400113 uint32_t transformKey,
114 GrProcessorKeyBuilder* b) {
Brian Salomone782f842018-07-31 13:53:11 -0400115 size_t processorKeySize = b->size();
116 uint32_t classID = pp.classID();
117
118 // Currently we allow 16 bits for the class id and the overall processor key size.
119 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
120 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
121 return false;
122 }
123
Robert Phillips323471e2019-11-11 11:33:37 -0500124 add_pp_sampler_keys(b, pp, caps);
bsalomoncdee0092016-01-08 13:20:12 -0800125
126 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800127 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800128 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700129 return true;
130}
joshualittb0a8a372014-09-23 09:50:21 -0700131
Robert Phillips323471e2019-11-11 11:33:37 -0500132static bool gen_xp_meta_key(const GrXferProcessor& xp, GrProcessorKeyBuilder* b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400133 size_t processorKeySize = b->size();
134 uint32_t classID = xp.classID();
135
136 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000137 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400138 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
139 return false;
140 }
141
142 b->add32((classID << 16) | SkToU32(processorKeySize));
143 return true;
144}
145
bsalomon7ea33f52015-11-22 14:51:00 -0800146static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700147 const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -0500148 const GrCaps& caps,
wangyixa7f4c432015-08-20 07:25:02 -0700149 GrProcessorKeyBuilder* b) {
150 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400151 if (auto child = fp.childProcessor(i)) {
152 if (!gen_frag_proc_and_meta_keys(primProc, *child, caps, b)) {
153 return false;
154 }
155 } else {
156 // Fold in a sentinel value as the "class ID" for any null children
157 b->add32(GrProcessor::ClassID::kNull_ClassID);
wangyixa7f4c432015-08-20 07:25:02 -0700158 }
159 }
160
Robert Phillips323471e2019-11-11 11:33:37 -0500161 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -0700162
Brian Salomon7eabfe82019-12-02 14:20:20 -0500163 return gen_fp_meta_key(fp, caps, primProc.computeCoordTransformsKey(fp), b);
wangyixa7f4c432015-08-20 07:25:02 -0700164}
165
Robert Phillips901aff02019-10-08 12:32:56 -0400166bool GrProgramDesc::Build(GrProgramDesc* desc, const GrRenderTarget* renderTarget,
Robert Phillips323471e2019-11-11 11:33:37 -0500167 const GrProgramInfo& programInfo, const GrCaps& caps) {
168
Robert Phillips933484f2019-11-26 09:38:55 -0500169#ifdef SK_DEBUG
170 if (renderTarget) {
171 SkASSERT(programInfo.backendFormat() == renderTarget->backendFormat());
172 }
173#endif
174
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000175 // The descriptor is used as a cache key. Thus when a field of the
176 // descriptor will not affect program generation (because of the attribute
177 // bindings in use or other descriptor field settings) it should be set
178 // to a canonical value to avoid duplicate programs with different keys.
179
Brian Salomon4dea72a2019-12-18 10:43:10 -0500180 static_assert(0 == kProcessorKeysOffset % sizeof(uint32_t));
egdanielc67870c2014-11-26 08:50:50 -0800181 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700182 desc->key().reset();
183 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700184
egdaniel5d8f69f2016-09-07 07:24:12 -0700185 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800186
Robert Phillips323471e2019-11-11 11:33:37 -0500187 programInfo.primProc().getGLSLProcessorKey(*caps.shaderCaps(), &b);
Robert Phillips901aff02019-10-08 12:32:56 -0400188 programInfo.primProc().getAttributeKey(&b);
Robert Phillips323471e2019-11-11 11:33:37 -0500189 if (!gen_pp_meta_key(programInfo.primProc(), caps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700190 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800191 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000192 }
bsalomon929f29a2014-07-17 07:55:11 -0700193
Robert Phillips901aff02019-10-08 12:32:56 -0400194 for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
195 const GrFragmentProcessor& fp = programInfo.pipeline().getFragmentProcessor(i);
Robert Phillips323471e2019-11-11 11:33:37 -0500196 if (!gen_frag_proc_and_meta_keys(programInfo.primProc(), fp, caps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700197 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700198 return false;
199 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000200 }
egdaniel170f90b2014-09-16 12:54:40 -0700201
Robert Phillips901aff02019-10-08 12:32:56 -0400202 const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400203 const GrSurfaceOrigin* originIfDstTexture = nullptr;
204 GrSurfaceOrigin origin;
Greg Daniel524e28b2019-11-01 11:48:53 -0400205 if (programInfo.pipeline().dstProxyView().proxy()) {
206 origin = programInfo.pipeline().dstProxyView().origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400207 originIfDstTexture = &origin;
208 }
Robert Phillips323471e2019-11-11 11:33:37 -0500209 xp.getGLSLProcessorKey(*caps.shaderCaps(), &b, originIfDstTexture);
210 if (!gen_xp_meta_key(xp, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700211 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800212 return false;
213 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700214
Robert Phillips7de13332019-10-09 15:44:54 -0400215 if (programInfo.requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
Robert Phillips901aff02019-10-08 12:32:56 -0400216 SkASSERT(programInfo.pipeline().isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600217 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700218 }
egdanielc2304142014-12-11 13:15:13 -0800219
joshualitt65171342014-10-09 07:25:36 -0700220 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700221 // Because header is a pointer into the dynamic array, we can't push any new data into the key
222 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700223 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000224
joshualitt65171342014-10-09 07:25:36 -0700225 // make sure any padding in the header is zeroed.
226 memset(header, 0, kHeaderSize);
Brian Salomon982f5462020-03-30 12:52:33 -0400227 header->fWriteSwizzle = programInfo.pipeline().writeSwizzle().asKey();
Robert Phillips901aff02019-10-08 12:32:56 -0400228 header->fColorFragmentProcessorCnt = programInfo.pipeline().numColorFragmentProcessors();
229 header->fCoverageFragmentProcessorCnt = programInfo.pipeline().numCoverageFragmentProcessors();
bsalomon2eda5b32016-09-21 10:53:24 -0700230 // Fail if the client requested more processors than the key can fit.
Robert Phillips901aff02019-10-08 12:32:56 -0400231 if (header->fColorFragmentProcessorCnt != programInfo.pipeline().numColorFragmentProcessors() ||
232 header->fCoverageFragmentProcessorCnt !=
233 programInfo.pipeline().numCoverageFragmentProcessors()) {
Robert Phillipsc15e8902019-11-26 14:26:36 -0500234 desc->key().reset();
bsalomon2eda5b32016-09-21 10:53:24 -0700235 return false;
236 }
Robert Phillips2579de42019-10-09 09:51:59 -0400237 // If we knew the shader won't depend on origin, we could skip this (and use the same program
238 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
239 header->fSurfaceOriginKey =
Robert Phillips7de13332019-10-09 15:44:54 -0400240 GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin());
241 header->fProcessorFeatures = (uint8_t)programInfo.requestedFeatures();
242 // Ensure enough bits.
243 SkASSERT(header->fProcessorFeatures == (int) programInfo.requestedFeatures());
Robert Phillips901aff02019-10-08 12:32:56 -0400244 header->fSnapVerticesToPixelCenters = programInfo.pipeline().snapVerticesToPixelCenters();
Robert Phillipsfcaae482019-11-07 10:17:03 -0500245 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
246 // specific versions (e.g., Vulkan) require more detail
247 header->fHasPointSize = (programInfo.primitiveType() == GrPrimitiveType::kPoints);
Robert Phillipsc15e8902019-11-26 14:26:36 -0500248
249 header->fInitialKeyLength = desc->keyLength();
250 // Fail if the initial key length won't fit in 27 bits.
251 if (header->fInitialKeyLength != desc->keyLength()) {
252 desc->key().reset();
253 return false;
254 }
255
bsalomon848faf02014-07-11 10:01:02 -0700256 return true;
257}