blob: 8d995a3f7d0b9d33fccdf107f8ed8bc6168e1acd [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,
Brian Salomon67529b22019-08-13 15:31:04 -040048 const GrShaderCaps& 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;
53 if (caps.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,
60 GrGpu* gpu, const GrShaderCaps& 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 Phillips9bee2e52017-05-29 12:37:20 -040067 const GrTexture* tex = sampler.peekTexture();
Brian Osman1a22b7f2019-07-23 09:32:08 -040068 uint32_t samplerKey = sampler_key(
Brian Salomon67529b22019-08-13 15:31:04 -040069 tex->texturePriv().textureType(), sampler.swizzle(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050070 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
71 sampler.samplerState(), sampler.proxy()->backendFormat());
72 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050073 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040074 // sampler key. We then add both keys.
75 SkASSERT((samplerKey & (1 << 31)) == 0);
76 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -050077 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -040078 } else {
79 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -050080 }
Brian Salomone782f842018-07-31 13:53:11 -040081 }
Brian Salomone782f842018-07-31 13:53:11 -040082}
Robert Phillips9bee2e52017-05-29 12:37:20 -040083
Robert Phillipsf272bea2019-10-17 08:56:16 -040084static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
85 const GrShaderCaps& caps) {
Brian Salomone782f842018-07-31 13:53:11 -040086 int numTextureSamplers = pp.numTextureSamplers();
Greg Danielf259b8b2019-02-14 09:03:43 -050087 if (!numTextureSamplers) {
Brian Salomone782f842018-07-31 13:53:11 -040088 return;
89 }
Brian Salomone782f842018-07-31 13:53:11 -040090 for (int i = 0; i < numTextureSamplers; ++i) {
91 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Brian Osman1a22b7f2019-07-23 09:32:08 -040092 uint32_t samplerKey = sampler_key(
Brian Salomon67529b22019-08-13 15:31:04 -040093 sampler.textureType(), sampler.swizzle(), caps);
Greg Daniel7a82edf2018-12-04 10:54:34 -050094 uint32_t extraSamplerKey = sampler.extraSamplerKey();
95 if (extraSamplerKey) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050096 // We first mark the normal sampler key with last bit to flag that it has an extra
Brian Osman1a22b7f2019-07-23 09:32:08 -040097 // sampler key. We then add both keys.
98 SkASSERT((samplerKey & (1 << 31)) == 0);
99 b->add32(samplerKey | (1 << 31));
Greg Daniel7a82edf2018-12-04 10:54:34 -0500100 b->add32(extraSamplerKey);
Brian Osman1a22b7f2019-07-23 09:32:08 -0400101 } else {
102 b->add32(samplerKey);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500103 }
bsalomoncdee0092016-01-08 13:20:12 -0800104 }
joshualitt23e280d2014-09-18 12:26:38 -0700105}
106
107/**
108 * A function which emits a meta key into the key builder. This is required because shader code may
109 * be dependent on properties of the effect that the effect itself doesn't use
110 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
111 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700112 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800113 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
114 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700115 */
Robert Phillipsf272bea2019-10-17 08:56:16 -0400116static bool gen_fp_meta_key(const GrFragmentProcessor& fp,
117 GrGpu* gpu,
118 const GrShaderCaps& shaderCaps,
119 uint32_t transformKey,
120 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800121 size_t processorKeySize = b->size();
Brian Salomone782f842018-07-31 13:53:11 -0400122 uint32_t classID = fp.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700123
bsalomon7ea33f52015-11-22 14:51:00 -0800124 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000125 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
bsalomon7ea33f52015-11-22 14:51:00 -0800126 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700127 return false;
128 }
129
Robert Phillipsf272bea2019-10-17 08:56:16 -0400130 add_fp_sampler_keys(b, fp, gpu, shaderCaps);
Brian Salomone782f842018-07-31 13:53:11 -0400131
132 uint32_t* key = b->add32n(2);
133 key[0] = (classID << 16) | SkToU32(processorKeySize);
134 key[1] = transformKey;
135 return true;
136}
137
Robert Phillipsf272bea2019-10-17 08:56:16 -0400138static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp,
139 const GrShaderCaps& shaderCaps,
140 uint32_t transformKey,
141 GrProcessorKeyBuilder* b) {
Brian Salomone782f842018-07-31 13:53:11 -0400142 size_t processorKeySize = b->size();
143 uint32_t classID = pp.classID();
144
145 // Currently we allow 16 bits for the class id and the overall processor key size.
146 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
147 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
148 return false;
149 }
150
Robert Phillipsf272bea2019-10-17 08:56:16 -0400151 add_pp_sampler_keys(b, pp, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800152
153 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800154 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800155 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700156 return true;
157}
joshualittb0a8a372014-09-23 09:50:21 -0700158
Robert Phillipsf272bea2019-10-17 08:56:16 -0400159static bool gen_xp_meta_key(const GrXferProcessor& xp,
160 const GrShaderCaps& shaderCaps,
161 GrProcessorKeyBuilder* b) {
Brian Salomonab015ef2017-04-04 10:15:51 -0400162 size_t processorKeySize = b->size();
163 uint32_t classID = xp.classID();
164
165 // Currently we allow 16 bits for the class id and the overall processor key size.
Ben Wagnerb0897652018-06-15 15:37:57 +0000166 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
Brian Salomonab015ef2017-04-04 10:15:51 -0400167 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
168 return false;
169 }
170
171 b->add32((classID << 16) | SkToU32(processorKeySize));
172 return true;
173}
174
bsalomon7ea33f52015-11-22 14:51:00 -0800175static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700176 const GrFragmentProcessor& fp,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500177 GrGpu* gpu,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500178 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700179 GrProcessorKeyBuilder* b) {
180 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500181 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700182 return false;
183 }
184 }
185
Brian Salomon1edc5b92016-11-29 13:43:46 -0500186 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700187
Robert Phillipsf272bea2019-10-17 08:56:16 -0400188 return gen_fp_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
189 fp.numCoordTransforms()),
190 b);
wangyixa7f4c432015-08-20 07:25:02 -0700191}
192
Robert Phillips901aff02019-10-08 12:32:56 -0400193bool GrProgramDesc::Build(GrProgramDesc* desc, const GrRenderTarget* renderTarget,
Robert Phillipsfcaae482019-11-07 10:17:03 -0500194 const GrProgramInfo& programInfo, 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
Robert Phillips901aff02019-10-08 12:32:56 -0400209 programInfo.primProc().getGLSLProcessorKey(shaderCaps, &b);
210 programInfo.primProc().getAttributeKey(&b);
Robert Phillipsf272bea2019-10-17 08:56:16 -0400211 if (!gen_pp_meta_key(programInfo.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 }
bsalomon929f29a2014-07-17 07:55:11 -0700215
Robert Phillips901aff02019-10-08 12:32:56 -0400216 for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
217 const GrFragmentProcessor& fp = programInfo.pipeline().getFragmentProcessor(i);
218 if (!gen_frag_proc_and_meta_keys(programInfo.primProc(), fp, gpu, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700219 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700220 return false;
221 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000222 }
egdaniel170f90b2014-09-16 12:54:40 -0700223
Robert Phillips901aff02019-10-08 12:32:56 -0400224 const GrXferProcessor& xp = programInfo.pipeline().getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400225 const GrSurfaceOrigin* originIfDstTexture = nullptr;
226 GrSurfaceOrigin origin;
Greg Daniel524e28b2019-11-01 11:48:53 -0400227 if (programInfo.pipeline().dstProxyView().proxy()) {
228 origin = programInfo.pipeline().dstProxyView().origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400229 originIfDstTexture = &origin;
230 }
231 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Robert Phillipsf272bea2019-10-17 08:56:16 -0400232 if (!gen_xp_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700233 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800234 return false;
235 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700236
Robert Phillips7de13332019-10-09 15:44:54 -0400237 if (programInfo.requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) {
Robert Phillips901aff02019-10-08 12:32:56 -0400238 SkASSERT(programInfo.pipeline().isHWAntialiasState());
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600239 b.add32(renderTarget->renderTargetPriv().getSamplePatternKey());
Chris Daltond7291ba2019-03-07 14:17:03 -0700240 }
egdanielc2304142014-12-11 13:15:13 -0800241
joshualitt65171342014-10-09 07:25:36 -0700242 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700243 // Because header is a pointer into the dynamic array, we can't push any new data into the key
244 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700245 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000246
joshualitt65171342014-10-09 07:25:36 -0700247 // make sure any padding in the header is zeroed.
248 memset(header, 0, kHeaderSize);
Robert Phillips901aff02019-10-08 12:32:56 -0400249 header->fOutputSwizzle = programInfo.pipeline().outputSwizzle().asKey();
250 header->fColorFragmentProcessorCnt = programInfo.pipeline().numColorFragmentProcessors();
251 header->fCoverageFragmentProcessorCnt = programInfo.pipeline().numCoverageFragmentProcessors();
bsalomon2eda5b32016-09-21 10:53:24 -0700252 // Fail if the client requested more processors than the key can fit.
Robert Phillips901aff02019-10-08 12:32:56 -0400253 if (header->fColorFragmentProcessorCnt != programInfo.pipeline().numColorFragmentProcessors() ||
254 header->fCoverageFragmentProcessorCnt !=
255 programInfo.pipeline().numCoverageFragmentProcessors()) {
bsalomon2eda5b32016-09-21 10:53:24 -0700256 return false;
257 }
Robert Phillips2579de42019-10-09 09:51:59 -0400258 // If we knew the shader won't depend on origin, we could skip this (and use the same program
259 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
260 header->fSurfaceOriginKey =
Robert Phillips7de13332019-10-09 15:44:54 -0400261 GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin());
262 header->fProcessorFeatures = (uint8_t)programInfo.requestedFeatures();
263 // Ensure enough bits.
264 SkASSERT(header->fProcessorFeatures == (int) programInfo.requestedFeatures());
Robert Phillips901aff02019-10-08 12:32:56 -0400265 header->fSnapVerticesToPixelCenters = programInfo.pipeline().snapVerticesToPixelCenters();
Robert Phillipsfcaae482019-11-07 10:17:03 -0500266 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
267 // specific versions (e.g., Vulkan) require more detail
268 header->fHasPointSize = (programInfo.primitiveType() == GrPrimitiveType::kPoints);
bsalomon848faf02014-07-11 10:01:02 -0700269 return true;
270}