blob: e11d06acecab0bffb35a36d9fe7733e4d3347fea [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"
Brian Salomonf7f54332020-07-28 09:23:35 -040016#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#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 Salomon280fa3d2020-08-27 17:16:49 -040052 uint16_t swizzleKey = swizzle.asKey();
Brian Salomon67529b22019-08-13 15:31:04 -040053 return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
Brian Salomonf9f45122016-11-29 11:59:17 -050054}
55
Robert Phillipsf272bea2019-10-17 08:56:16 -040056static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -050057 const GrCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040058 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050059 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040060 return;
61 }
Brian Salomone782f842018-07-31 13:53:11 -040062 for (int i = 0; i < numTextureSamplers; ++i) {
63 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050064 const GrBackendFormat& backendFormat = sampler.backendFormat();
65
66 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
67 b->add32(samplerKey);
68
69 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080070 }
joshualitt23e280d2014-09-18 12:26:38 -070071}
72
73/**
74 * A function which emits a meta key into the key builder. This is required because shader code may
75 * be dependent on properties of the effect that the effect itself doesn't use
76 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
77 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070078 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080079 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
80 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070081 */
Robert Phillipsf272bea2019-10-17 08:56:16 -040082static bool gen_fp_meta_key(const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -050083 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -040084 uint32_t transformKey,
85 GrProcessorKeyBuilder* b) {
Brian Osmanacf26502021-03-03 13:35:04 +000086 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -040087 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070088
Brian Osmanacf26502021-03-03 13:35:04 +000089 // Currently we allow 16 bits for the class id and the overall processor key size.
90 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
91 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070092 return false;
93 }
94
Brian Salomond90b3d32020-07-09 12:04:31 -040095 fp.visitTextureEffects([&](const GrTextureEffect& te) {
96 const GrBackendFormat& backendFormat = te.view().proxy()->backendFormat();
97 uint32_t samplerKey = sampler_key(backendFormat.textureType(), te.view().swizzle(), caps);
98 b->add32(samplerKey);
99 caps.addExtraSamplerKey(b, te.samplerState(), backendFormat);
100 });
Brian Salomone782f842018-07-31 13:53:11 -0400101
Brian Osmanacf26502021-03-03 13:35:04 +0000102 uint32_t* key = b->add32n(2);
103 key[0] = (classID << 16) | SkToU32(processorKeySize);
104 key[1] = transformKey;
Brian Salomone782f842018-07-31 13:53:11 -0400105 return true;
106}
107
Robert Phillipsf272bea2019-10-17 08:56:16 -0400108static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -0500109 const GrCaps& caps,
Brian Osmanacf26502021-03-03 13:35:04 +0000110 uint32_t transformKey,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400111 GrProcessorKeyBuilder* b) {
Brian Osmanacf26502021-03-03 13:35:04 +0000112 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400113 uint32_t classID = pp.classID();
114
Brian Osmanacf26502021-03-03 13:35:04 +0000115 // Currently we allow 16 bits for the class id and the overall processor key size.
116 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
117 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
Brian Salomone782f842018-07-31 13:53:11 -0400118 return false;
119 }
120
Robert Phillips323471e2019-11-11 11:33:37 -0500121 add_pp_sampler_keys(b, pp, caps);
bsalomoncdee0092016-01-08 13:20:12 -0800122
Brian Osmanacf26502021-03-03 13:35:04 +0000123 uint32_t* key = b->add32n(2);
124 key[0] = (classID << 16) | SkToU32(processorKeySize);
125 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700126 return true;
127}
joshualittb0a8a372014-09-23 09:50:21 -0700128
Robert Phillips323471e2019-11-11 11:33:37 -0500129static bool gen_xp_meta_key(const GrXferProcessor& xp, GrProcessorKeyBuilder* b) {
Brian Osmanacf26502021-03-03 13:35:04 +0000130 size_t processorKeySize = b->size();
Brian Salomonab015ef2017-04-04 10:15:51 -0400131 uint32_t classID = xp.classID();
132
Brian Osmanacf26502021-03-03 13:35:04 +0000133 // Currently we allow 16 bits for the class id and the overall processor key size.
134 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
135 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400136 return false;
137 }
138
Brian Osmanacf26502021-03-03 13:35:04 +0000139 b->add32((classID << 16) | SkToU32(processorKeySize));
Brian Salomonab015ef2017-04-04 10:15:51 -0400140 return true;
141}
142
Brian Osmanacf26502021-03-03 13:35:04 +0000143static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
144 const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -0500145 const GrCaps& caps,
wangyixa7f4c432015-08-20 07:25:02 -0700146 GrProcessorKeyBuilder* b) {
147 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400148 if (auto child = fp.childProcessor(i)) {
Brian Osmanacf26502021-03-03 13:35:04 +0000149 if (!gen_frag_proc_and_meta_keys(primProc, *child, caps, b)) {
Brian Osman12c5d292020-07-13 16:11:35 -0400150 return false;
151 }
152 } else {
153 // Fold in a sentinel value as the "class ID" for any null children
154 b->add32(GrProcessor::ClassID::kNull_ClassID);
wangyixa7f4c432015-08-20 07:25:02 -0700155 }
156 }
157
Robert Phillips323471e2019-11-11 11:33:37 -0500158 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -0700159
Brian Osmanacf26502021-03-03 13:35:04 +0000160 return gen_fp_meta_key(fp, caps, primProc.computeCoordTransformsKey(fp), b);
wangyixa7f4c432015-08-20 07:25:02 -0700161}
162
Brian Salomonf7f54332020-07-28 09:23:35 -0400163bool GrProgramDesc::Build(GrProgramDesc* desc,
164 GrRenderTarget* renderTarget,
165 const GrProgramInfo& programInfo,
166 const GrCaps& caps) {
Robert Phillips933484f2019-11-26 09:38:55 -0500167#ifdef SK_DEBUG
168 if (renderTarget) {
169 SkASSERT(programInfo.backendFormat() == renderTarget->backendFormat());
170 }
171#endif
172
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000173 // The descriptor is used as a cache key. Thus when a field of the
174 // descriptor will not affect program generation (because of the attribute
175 // bindings in use or other descriptor field settings) it should be set
176 // to a canonical value to avoid duplicate programs with different keys.
177
egdaniel5d8f69f2016-09-07 07:24:12 -0700178 desc->key().reset();
egdaniel5d8f69f2016-09-07 07:24:12 -0700179 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800180
John Stilesd3feb6f2020-07-23 18:18:12 -0400181 const GrPrimitiveProcessor& primitiveProcessor = programInfo.primProc();
182 primitiveProcessor.getGLSLProcessorKey(*caps.shaderCaps(), &b);
183 primitiveProcessor.getAttributeKey(&b);
Brian Osmanacf26502021-03-03 13:35:04 +0000184 if (!gen_pp_meta_key(primitiveProcessor, caps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700185 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800186 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000187 }
bsalomon929f29a2014-07-17 07:55:11 -0700188
John Stilesd3feb6f2020-07-23 18:18:12 -0400189 const GrPipeline& pipeline = programInfo.pipeline();
190 int numColorFPs = 0, numCoverageFPs = 0;
191 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
192 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Brian Osmanacf26502021-03-03 13:35:04 +0000193 if (!gen_frag_proc_and_meta_keys(primitiveProcessor, fp, caps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700194 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700195 return false;
196 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400197 if (pipeline.isColorFragmentProcessor(i)) {
198 ++numColorFPs;
199 } else if (pipeline.isCoverageFragmentProcessor(i)) {
200 ++numCoverageFPs;
201 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000202 }
egdaniel170f90b2014-09-16 12:54:40 -0700203
John Stilesd3feb6f2020-07-23 18:18:12 -0400204 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400205 const GrSurfaceOrigin* originIfDstTexture = nullptr;
206 GrSurfaceOrigin origin;
John Stilesd3feb6f2020-07-23 18:18:12 -0400207 if (pipeline.dstProxyView().proxy()) {
208 origin = pipeline.dstProxyView().origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400209 originIfDstTexture = &origin;
210 }
Greg Daniel70dd6a92020-10-09 10:09:06 -0400211 xp.getGLSLProcessorKey(*caps.shaderCaps(), &b, originIfDstTexture, pipeline.dstSampleType());
Robert Phillips323471e2019-11-11 11:33:37 -0500212 if (!gen_xp_meta_key(xp, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700213 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800214 return false;
215 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700216
Robert Phillips7de13332019-10-09 15:44:54 -0400217 if (programInfo.requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
John Stilesd3feb6f2020-07-23 18:18:12 -0400218 SkASSERT(pipeline.isHWAntialiasState());
Brian Salomonf7f54332020-07-28 09:23:35 -0400219 b.add32(renderTarget->getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700220 }
egdanielc2304142014-12-11 13:15:13 -0800221
Brian Osman9b510a32021-02-26 14:29:39 -0500222 // Add "header" metadata
Brian Osmanacf26502021-03-03 13:35:04 +0000223 uint32_t header = 0;
224 SkDEBUGCODE(uint32_t header_bits = 0);
225 auto add_bits = [&](uint32_t nbits, uint32_t val) {
226 SkASSERT(val < (1u << nbits));
227 SkASSERT((header_bits += nbits) <= 32);
228 header = (header << nbits) | val;
229 };
230 add_bits(16, pipeline.writeSwizzle().asKey());
231 add_bits( 1, numColorFPs);
232 add_bits( 2, numCoverageFPs);
Robert Phillips2579de42019-10-09 09:51:59 -0400233 // If we knew the shader won't depend on origin, we could skip this (and use the same program
234 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
Brian Osmanacf26502021-03-03 13:35:04 +0000235 add_bits( 2, GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin()));
236 add_bits( 1, static_cast<uint32_t>(programInfo.requestedFeatures()));
237 add_bits( 1, pipeline.snapVerticesToPixelCenters());
Robert Phillipsfcaae482019-11-07 10:17:03 -0500238 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
239 // specific versions (e.g., Vulkan) require more detail
Brian Osmanacf26502021-03-03 13:35:04 +0000240 add_bits( 1, (programInfo.primitiveType() == GrPrimitiveType::kPoints));
Robert Phillipsc15e8902019-11-26 14:26:36 -0500241
Brian Osmanacf26502021-03-03 13:35:04 +0000242 b.add32(header);
243
Brian Osman9b510a32021-02-26 14:29:39 -0500244 desc->fInitialKeyLength = desc->keyLength();
Robert Phillipsc15e8902019-11-26 14:26:36 -0500245
bsalomon848faf02014-07-11 10:01:02 -0700246 return true;
247}