blob: 1821790277677b2e6101cf194359a80d393c26ab [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"
18#include "src/gpu/GrTexturePriv.h"
19#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
Greg Daniel2c3398d2019-06-19 11:58:01 -040051 GR_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_fp_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -050060 const GrCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040061 int numTextureSamplers = fp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050062 if (!numTextureSamplers) {
bsalomoncdee0092016-01-08 13:20:12 -080063 return;
joshualitt23e280d2014-09-18 12:26:38 -070064 }
Brian Salomone782f842018-07-31 13:53:11 -040065 for (int i = 0; i < numTextureSamplers; ++i) {
66 const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050067 const GrBackendFormat& backendFormat = sampler.proxy()->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);
Brian Salomone782f842018-07-31 13:53:11 -040073 }
Brian Salomone782f842018-07-31 13:53:11 -040074}
Robert Phillips9bee2e52017-05-29 12:37:20 -040075
Robert Phillipsf272bea2019-10-17 08:56:16 -040076static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -050077 const GrCaps& 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 }
Brian Salomone782f842018-07-31 13:53:11 -040082 for (int i = 0; i < numTextureSamplers; ++i) {
83 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050084 const GrBackendFormat& backendFormat = sampler.backendFormat();
85
86 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
87 b->add32(samplerKey);
88
89 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080090 }
joshualitt23e280d2014-09-18 12:26:38 -070091}
92
93/**
94 * A function which emits a meta key into the key builder. This is required because shader code may
95 * be dependent on properties of the effect that the effect itself doesn't use
96 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
97 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070098 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080099 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
100 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700101 */
Robert Phillipsf272bea2019-10-17 08:56:16 -0400102static bool gen_fp_meta_key(const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -0500103 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400104 uint32_t transformKey,
105 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800106 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400107 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700108
bsalomon7ea33f52015-11-22 14:51:00 -0800109 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000110 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800111 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700112 return false;
113 }
114
Robert Phillips323471e2019-11-11 11:33:37 -0500115 add_fp_sampler_keys(b, fp, caps);
Brian Salomone782f842018-07-31 13:53:11 -0400116
117 uint32_t* key = b->add32n(2);
118 key[0] = (classID << 16) | SkToU32(processorKeySize);
119 key[1] = transformKey;
120 return true;
121}
122
Robert Phillipsf272bea2019-10-17 08:56:16 -0400123static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -0500124 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400125 uint32_t transformKey,
126 GrProcessorKeyBuilder* b) {
Brian Salomone782f842018-07-31 13:53:11 -0400127 size_t processorKeySize = b->size();
128 uint32_t classID = pp.classID();
129
130 // Currently we allow 16 bits for the class id and the overall processor key size.
131 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
132 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
133 return false;
134 }
135
Robert Phillips323471e2019-11-11 11:33:37 -0500136 add_pp_sampler_keys(b, pp, caps);
bsalomoncdee0092016-01-08 13:20:12 -0800137
138 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800139 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800140 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700141 return true;
142}
joshualittb0a8a372014-09-23 09:50:21 -0700143
Robert Phillips323471e2019-11-11 11:33:37 -0500144static bool gen_xp_meta_key(const GrXferProcessor& xp, GrProcessorKeyBuilder* b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400145 size_t processorKeySize = b->size();
146 uint32_t classID = xp.classID();
147
148 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000149 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400150 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
151 return false;
152 }
153
154 b->add32((classID << 16) | SkToU32(processorKeySize));
155 return true;
156}
157
bsalomon7ea33f52015-11-22 14:51:00 -0800158static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700159 const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -0500160 const GrCaps& caps,
wangyixa7f4c432015-08-20 07:25:02 -0700161 GrProcessorKeyBuilder* b) {
162 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Robert Phillips323471e2019-11-11 11:33:37 -0500163 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700164 return false;
165 }
166 }
167
Robert Phillips323471e2019-11-11 11:33:37 -0500168 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -0700169
Robert Phillips323471e2019-11-11 11:33:37 -0500170 return gen_fp_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(),
171 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700172}
173
Robert Phillips901aff02019-10-08 12:32:56 -0400174bool GrProgramDesc::Build(GrProgramDesc* desc, const GrRenderTarget* renderTarget,
Robert Phillips323471e2019-11-11 11:33:37 -0500175 const GrProgramInfo& programInfo, const GrCaps& caps) {
176
Robert Phillips933484f2019-11-26 09:38:55 -0500177#ifdef SK_DEBUG
178 if (renderTarget) {
179 SkASSERT(programInfo.backendFormat() == renderTarget->backendFormat());
180 }
181#endif
182
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000183 // The descriptor is used as a cache key. Thus when a field of the
184 // descriptor will not affect program generation (because of the attribute
185 // bindings in use or other descriptor field settings) it should be set
186 // to a canonical value to avoid duplicate programs with different keys.
187
egdanielc67870c2014-11-26 08:50:50 -0800188 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
189 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700190 desc->key().reset();
191 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700192
egdaniel5d8f69f2016-09-07 07:24:12 -0700193 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800194
Robert Phillips323471e2019-11-11 11:33:37 -0500195 programInfo.primProc().getGLSLProcessorKey(*caps.shaderCaps(), &b);
Robert Phillips901aff02019-10-08 12:32:56 -0400196 programInfo.primProc().getAttributeKey(&b);
Robert Phillips323471e2019-11-11 11:33:37 -0500197 if (!gen_pp_meta_key(programInfo.primProc(), caps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700198 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800199 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000200 }
bsalomon929f29a2014-07-17 07:55:11 -0700201
Robert Phillips901aff02019-10-08 12:32:56 -0400202 for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
203 const GrFragmentProcessor& fp = programInfo.pipeline().getFragmentProcessor(i);
Robert Phillips323471e2019-11-11 11:33:37 -0500204 if (!gen_frag_proc_and_meta_keys(programInfo.primProc(), fp, caps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700205 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700206 return false;
207 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000208 }
egdaniel170f90b2014-09-16 12:54:40 -0700209
Robert Phillips901aff02019-10-08 12:32:56 -0400210 const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400211 const GrSurfaceOrigin* originIfDstTexture = nullptr;
212 GrSurfaceOrigin origin;
Greg Daniel524e28b2019-11-01 11:48:53 -0400213 if (programInfo.pipeline().dstProxyView().proxy()) {
214 origin = programInfo.pipeline().dstProxyView().origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400215 originIfDstTexture = &origin;
216 }
Robert Phillips323471e2019-11-11 11:33:37 -0500217 xp.getGLSLProcessorKey(*caps.shaderCaps(), &b, originIfDstTexture);
218 if (!gen_xp_meta_key(xp, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700219 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800220 return false;
221 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700222
Robert Phillips7de13332019-10-09 15:44:54 -0400223 if (programInfo.requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
Robert Phillips901aff02019-10-08 12:32:56 -0400224 SkASSERT(programInfo.pipeline().isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600225 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700226 }
egdanielc2304142014-12-11 13:15:13 -0800227
joshualitt65171342014-10-09 07:25:36 -0700228 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700229 // Because header is a pointer into the dynamic array, we can't push any new data into the key
230 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700231 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000232
joshualitt65171342014-10-09 07:25:36 -0700233 // make sure any padding in the header is zeroed.
234 memset(header, 0, kHeaderSize);
Robert Phillips901aff02019-10-08 12:32:56 -0400235 header->fOutputSwizzle = programInfo.pipeline().outputSwizzle().asKey();
236 header->fColorFragmentProcessorCnt = programInfo.pipeline().numColorFragmentProcessors();
237 header->fCoverageFragmentProcessorCnt = programInfo.pipeline().numCoverageFragmentProcessors();
bsalomon2eda5b32016-09-21 10:53:24 -0700238 // Fail if the client requested more processors than the key can fit.
Robert Phillips901aff02019-10-08 12:32:56 -0400239 if (header->fColorFragmentProcessorCnt != programInfo.pipeline().numColorFragmentProcessors() ||
240 header->fCoverageFragmentProcessorCnt !=
241 programInfo.pipeline().numCoverageFragmentProcessors()) {
bsalomon2eda5b32016-09-21 10:53:24 -0700242 return false;
243 }
Robert Phillips2579de42019-10-09 09:51:59 -0400244 // If we knew the shader won't depend on origin, we could skip this (and use the same program
245 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
246 header->fSurfaceOriginKey =
Robert Phillips7de13332019-10-09 15:44:54 -0400247 GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin());
248 header->fProcessorFeatures = (uint8_t)programInfo.requestedFeatures();
249 // Ensure enough bits.
250 SkASSERT(header->fProcessorFeatures == (int) programInfo.requestedFeatures());
Robert Phillips901aff02019-10-08 12:32:56 -0400251 header->fSnapVerticesToPixelCenters = programInfo.pipeline().snapVerticesToPixelCenters();
Robert Phillipsfcaae482019-11-07 10:17:03 -0500252 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
253 // specific versions (e.g., Vulkan) require more detail
254 header->fHasPointSize = (programInfo.primitiveType() == GrPrimitiveType::kPoints);
bsalomon848faf02014-07-11 10:01:02 -0700255 return true;
256}