blob: c1e5cd3b115d4feb52d6e18264422813c456a244 [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
Brian Salomonb0047b52019-12-05 19:52:25 +000051 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 Phillipsbd99c0c2019-12-12 13:26:58 +000067 const GrBackendFormat& backendFormat = sampler.view().proxy()->backendFormat();
Robert Phillips323471e2019-11-11 11:33:37 -050068
Robert Phillipsbd99c0c2019-12-12 13:26:58 +000069 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.view().swizzle(),
70 caps);
Robert Phillips323471e2019-11-11 11:33:37 -050071 b->add32(samplerKey);
72
73 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
Brian Salomone782f842018-07-31 13:53:11 -040074 }
Brian Salomone782f842018-07-31 13:53:11 -040075}
Robert Phillips9bee2e52017-05-29 12:37:20 -040076
Robert Phillipsf272bea2019-10-17 08:56:16 -040077static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -050078 const GrCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040079 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050080 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040081 return;
82 }
Brian Salomone782f842018-07-31 13:53:11 -040083 for (int i = 0; i < numTextureSamplers; ++i) {
84 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050085 const GrBackendFormat& backendFormat = sampler.backendFormat();
86
87 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
88 b->add32(samplerKey);
89
90 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080091 }
joshualitt23e280d2014-09-18 12:26:38 -070092}
93
94/**
95 * A function which emits a meta key into the key builder. This is required because shader code may
96 * be dependent on properties of the effect that the effect itself doesn't use
97 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
98 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070099 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800100 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
101 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700102 */
Robert Phillipsf272bea2019-10-17 08:56:16 -0400103static bool gen_fp_meta_key(const GrFragmentProcessor& fp,
Robert Phillips323471e2019-11-11 11:33:37 -0500104 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400105 uint32_t transformKey,
106 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800107 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400108 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700109
bsalomon7ea33f52015-11-22 14:51:00 -0800110 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000111 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800112 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700113 return false;
114 }
115
Robert Phillips323471e2019-11-11 11:33:37 -0500116 add_fp_sampler_keys(b, fp, caps);
Brian Salomone782f842018-07-31 13:53:11 -0400117
118 uint32_t* key = b->add32n(2);
119 key[0] = (classID << 16) | SkToU32(processorKeySize);
120 key[1] = transformKey;
121 return true;
122}
123
Robert Phillipsf272bea2019-10-17 08:56:16 -0400124static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp,
Robert Phillips323471e2019-11-11 11:33:37 -0500125 const GrCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400126 uint32_t transformKey,
127 GrProcessorKeyBuilder* b) {
Brian Salomone782f842018-07-31 13:53:11 -0400128 size_t processorKeySize = b->size();
129 uint32_t classID = pp.classID();
130
131 // Currently we allow 16 bits for the class id and the overall processor key size.
132 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
133 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
134 return false;
135 }
136
Robert Phillips323471e2019-11-11 11:33:37 -0500137 add_pp_sampler_keys(b, pp, caps);
bsalomoncdee0092016-01-08 13:20:12 -0800138
139 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800140 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800141 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700142 return true;
143}
joshualittb0a8a372014-09-23 09:50:21 -0700144
Robert Phillips323471e2019-11-11 11:33:37 -0500145static bool gen_xp_meta_key(const GrXferProcessor& xp, GrProcessorKeyBuilder* b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400146 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,
Robert Phillips323471e2019-11-11 11:33:37 -0500161 const GrCaps& caps,
wangyixa7f4c432015-08-20 07:25:02 -0700162 GrProcessorKeyBuilder* b) {
163 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Robert Phillips323471e2019-11-11 11:33:37 -0500164 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700165 return false;
166 }
167 }
168
Robert Phillips323471e2019-11-11 11:33:37 -0500169 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -0700170
Brian Salomon7eabfe82019-12-02 14:20:20 -0500171 return gen_fp_meta_key(fp, caps, primProc.computeCoordTransformsKey(fp), 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
Brian Salomonb0047b52019-12-05 19:52:25 +0000188 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
egdanielc67870c2014-11-26 08:50:50 -0800189 // 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()) {
Robert Phillipsc15e8902019-11-26 14:26:36 -0500242 desc->key().reset();
bsalomon2eda5b32016-09-21 10:53:24 -0700243 return false;
244 }
Robert Phillips2579de42019-10-09 09:51:59 -0400245 // If we knew the shader won't depend on origin, we could skip this (and use the same program
246 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
247 header->fSurfaceOriginKey =
Robert Phillips7de13332019-10-09 15:44:54 -0400248 GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin());
249 header->fProcessorFeatures = (uint8_t)programInfo.requestedFeatures();
250 // Ensure enough bits.
251 SkASSERT(header->fProcessorFeatures == (int) programInfo.requestedFeatures());
Robert Phillips901aff02019-10-08 12:32:56 -0400252 header->fSnapVerticesToPixelCenters = programInfo.pipeline().snapVerticesToPixelCenters();
Robert Phillipsfcaae482019-11-07 10:17:03 -0500253 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
254 // specific versions (e.g., Vulkan) require more detail
255 header->fHasPointSize = (programInfo.primitiveType() == GrPrimitiveType::kPoints);
Robert Phillipsc15e8902019-11-26 14:26:36 -0500256
257 header->fInitialKeyLength = desc->keyLength();
258 // Fail if the initial key length won't fit in 27 bits.
259 if (header->fInitialKeyLength != desc->keyLength()) {
260 desc->key().reset();
261 return false;
262 }
263
bsalomon848faf02014-07-11 10:01:02 -0700264 return true;
265}