blob: 84f4ee106ab435ccd1cc740b6c8b2f851d01c183 [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();
Brian Osman33167962021-03-04 13:05:49 -050059 b->add32(numTextureSamplers, "ppNumSamplers");
Brian Salomone782f842018-07-31 13:53:11 -040060 for (int i = 0; i < numTextureSamplers; ++i) {
61 const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050062 const GrBackendFormat& backendFormat = sampler.backendFormat();
63
64 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
65 b->add32(samplerKey);
66
67 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080068 }
joshualitt23e280d2014-09-18 12:26:38 -070069}
70
Brian Osman33167962021-03-04 13:05:49 -050071// Currently we allow 8 bits for the class id
Brian Osmanf0de96f2021-02-26 13:54:11 -050072static constexpr uint32_t kClassIDBits = 8;
Brian Osmanf0de96f2021-02-26 13:54:11 -050073
joshualitt23e280d2014-09-18 12:26:38 -070074/**
Brian Osman33167962021-03-04 13:05:49 -050075 * Functions which emit processor key info into the key builder.
76 * For every effect, we include the effect's class ID (different for every GrProcessor subclass),
77 * any information generated by the effect itself (getGLSLProcessorKey), and some meta-information.
78 * Shader code may be dependent on properties of the effect not placed in the key by the effect
79 * (e.g. pixel format of textures used).
joshualitt23e280d2014-09-18 12:26:38 -070080 */
Brian Osman33167962021-03-04 13:05:49 -050081static void gen_pp_key(const GrPrimitiveProcessor& pp,
82 const GrCaps& caps,
83 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -050084 b->appendComment(pp.name());
Brian Osman33167962021-03-04 13:05:49 -050085 b->addBits(kClassIDBits, pp.classID(), "ppClassID");
Brian Salomone782f842018-07-31 13:53:11 -040086
Brian Osman33167962021-03-04 13:05:49 -050087 pp.getGLSLProcessorKey(*caps.shaderCaps(), b);
88 pp.getAttributeKey(b);
Brian Salomone782f842018-07-31 13:53:11 -040089
Robert Phillips323471e2019-11-11 11:33:37 -050090 add_pp_sampler_keys(b, pp, caps);
joshualitt65171342014-10-09 07:25:36 -070091}
joshualittb0a8a372014-09-23 09:50:21 -070092
Brian Osman33167962021-03-04 13:05:49 -050093static void gen_xp_key(const GrXferProcessor& xp,
94 const GrCaps& caps,
95 const GrPipeline& pipeline,
96 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -050097 b->appendComment(xp.name());
Brian Osman33167962021-03-04 13:05:49 -050098 b->addBits(kClassIDBits, xp.classID(), "xpClassID");
Brian Salomonab015ef2017-04-04 10:15:51 -040099
Brian Osman33167962021-03-04 13:05:49 -0500100 const GrSurfaceOrigin* originIfDstTexture = nullptr;
101 GrSurfaceOrigin origin;
102 if (pipeline.dstProxyView().proxy()) {
103 origin = pipeline.dstProxyView().origin();
104 originIfDstTexture = &origin;
Brian Salomonab015ef2017-04-04 10:15:51 -0400105 }
Brian Osman33167962021-03-04 13:05:49 -0500106 xp.getGLSLProcessorKey(*caps.shaderCaps(), b, originIfDstTexture, pipeline.dstSampleType());
Brian Salomonab015ef2017-04-04 10:15:51 -0400107}
108
Brian Osman33167962021-03-04 13:05:49 -0500109static void gen_fp_key(const GrFragmentProcessor& fp,
110 const GrCaps& caps,
111 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500112 b->appendComment(fp.name());
Brian Osman48d7f7c2021-03-03 13:38:08 -0500113 b->addBits(kClassIDBits, fp.classID(), "fpClassID");
114 b->addBits(GrPrimitiveProcessor::kCoordTransformKeyBits,
115 GrPrimitiveProcessor::ComputeCoordTransformsKey(fp), "fpTransforms");
116
Brian Osman33167962021-03-04 13:05:49 -0500117 if (auto* te = fp.asTextureEffect()) {
118 const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat();
119 uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps);
Brian Osman48d7f7c2021-03-03 13:38:08 -0500120 b->add32(samplerKey, "fpSamplerKey");
Brian Osman33167962021-03-04 13:05:49 -0500121 caps.addExtraSamplerKey(b, te->samplerState(), backendFormat);
122 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500123
124 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
125 b->add32(fp.numChildProcessors(), "fpNumChildren");
126
wangyixa7f4c432015-08-20 07:25:02 -0700127 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400128 if (auto child = fp.childProcessor(i)) {
Brian Osman33167962021-03-04 13:05:49 -0500129 gen_fp_key(*child, caps, b);
Brian Osman12c5d292020-07-13 16:11:35 -0400130 } else {
131 // Fold in a sentinel value as the "class ID" for any null children
Brian Osmane23c03d2021-03-05 14:58:25 -0500132 b->appendComment("Null");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500133 b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID");
wangyixa7f4c432015-08-20 07:25:02 -0700134 }
135 }
wangyixa7f4c432015-08-20 07:25:02 -0700136}
137
Brian Osmane23c03d2021-03-05 14:58:25 -0500138static void gen_key(GrProcessorKeyBuilder* b,
139 GrRenderTarget* renderTarget,
140 const GrProgramInfo& programInfo,
141 const GrCaps& caps) {
Brian Osman33167962021-03-04 13:05:49 -0500142 gen_pp_key(programInfo.primProc(), caps, b);
bsalomon929f29a2014-07-17 07:55:11 -0700143
John Stilesd3feb6f2020-07-23 18:18:12 -0400144 const GrPipeline& pipeline = programInfo.pipeline();
Brian Osman33167962021-03-04 13:05:49 -0500145 b->addBits(2, pipeline.numFragmentProcessors(), "numFPs");
146 b->addBits(1, pipeline.numColorFragmentProcessors(), "numColorFPs");
John Stilesd3feb6f2020-07-23 18:18:12 -0400147 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
Brian Osman33167962021-03-04 13:05:49 -0500148 gen_fp_key(pipeline.getFragmentProcessor(i), caps, b);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000149 }
egdaniel170f90b2014-09-16 12:54:40 -0700150
Brian Osman33167962021-03-04 13:05:49 -0500151 gen_xp_key(pipeline.getXferProcessor(), caps, pipeline, b);
Chris Daltond7291ba2019-03-07 14:17:03 -0700152
Brian Osmanef3725f2021-03-04 10:44:41 -0500153 b->addBits(16, pipeline.writeSwizzle().asKey(), "writeSwizzle");
Robert Phillips2579de42019-10-09 09:51:59 -0400154 // If we knew the shader won't depend on origin, we could skip this (and use the same program
155 // for both origins). Instrumenting all fragment processors would be difficult and error prone.
Brian Osmanef3725f2021-03-04 10:44:41 -0500156 b->addBits(2, GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin()), "origin");
157 b->addBits(1, static_cast<uint32_t>(programInfo.requestedFeatures()), "requestedFeatures");
Brian Osman33167962021-03-04 13:05:49 -0500158 b->addBool(pipeline.snapVerticesToPixelCenters(), "snapVertices");
Robert Phillipsfcaae482019-11-07 10:17:03 -0500159 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
160 // specific versions (e.g., Vulkan) require more detail
Brian Osman33167962021-03-04 13:05:49 -0500161 b->addBool((programInfo.primitiveType() == GrPrimitiveType::kPoints), "isPoints");
Robert Phillipsc15e8902019-11-26 14:26:36 -0500162
Brian Osmanf0de96f2021-02-26 13:54:11 -0500163 // Put a clean break between the "common" data written by this function, and any backend data
164 // appended later. The initial key length will just be this portion (rounded to 4 bytes).
Brian Osmanef3725f2021-03-04 10:44:41 -0500165 b->flush();
Brian Osmane23c03d2021-03-05 14:58:25 -0500166}
Robert Phillipsc15e8902019-11-26 14:26:36 -0500167
Brian Osmane23c03d2021-03-05 14:58:25 -0500168void GrProgramDesc::Build(GrProgramDesc* desc,
169 GrRenderTarget* renderTarget,
170 const GrProgramInfo& programInfo,
171 const GrCaps& caps) {
172 SkASSERT(!renderTarget || programInfo.backendFormat() == renderTarget->backendFormat());
173
174 desc->reset();
175 GrProcessorKeyBuilder b(desc->key());
176 gen_key(&b, renderTarget, programInfo, caps);
177 desc->fInitialKeyLength = desc->keyLength();
178}
179
180SkString GrProgramDesc::Describe(GrRenderTarget* renderTarget,
181 const GrProgramInfo& programInfo,
182 const GrCaps& caps) {
183 SkASSERT(!renderTarget || programInfo.backendFormat() == renderTarget->backendFormat());
184
185 GrProgramDesc desc;
186 GrProcessorStringKeyBuilder b(desc.key());
187 gen_key(&b, renderTarget, programInfo, caps);
188 b.flush();
189 return b.description();
bsalomon848faf02014-07-11 10:01:02 -0700190}