blob: a8ebd0293105768ff9eb7a946d52cf59bf7b3f5d [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"
Robert Phillips787fd9d2021-03-22 14:48:09 -040012#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrPipeline.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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"
Robert Phillips550de7f2021-07-06 16:28:52 -040019#include "src/gpu/effects/GrTextureEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
21#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000022
Brian Salomonf9f45122016-11-29 11:59:17 -050023enum {
24 kSamplerOrImageTypeKeyBits = 4
25};
Brian Salomonbe348822016-11-22 15:56:30 -050026
Brian Salomon60dd8c72018-07-30 10:24:13 -040027static inline uint16_t texture_type_key(GrTextureType type) {
Brian Salomonf9f45122016-11-29 11:59:17 -050028 int value = UINT16_MAX;
29 switch (type) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040030 case GrTextureType::k2D:
Brian Salomonf9f45122016-11-29 11:59:17 -050031 value = 0;
32 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040033 case GrTextureType::kExternal:
Brian Salomonf9f45122016-11-29 11:59:17 -050034 value = 1;
35 break;
Brian Salomon60dd8c72018-07-30 10:24:13 -040036 case GrTextureType::kRectangle:
Brian Salomonf9f45122016-11-29 11:59:17 -050037 value = 2;
38 break;
Robert Phillipsf209e882019-06-25 15:59:50 -040039 default:
40 SK_ABORT("Unexpected texture type");
41 value = 3;
42 break;
Brian Salomonf9f45122016-11-29 11:59:17 -050043 }
44 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
Brian Salomon60dd8c72018-07-30 10:24:13 -040045 return SkToU16(value);
Brian Salomonbe348822016-11-22 15:56:30 -050046}
47
Greg Daniel2c3398d2019-06-19 11:58:01 -040048static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle,
Robert Phillips323471e2019-11-11 11:33:37 -050049 const GrCaps& caps) {
Brian Salomon60dd8c72018-07-30 10:24:13 -040050 int samplerTypeKey = texture_type_key(textureType);
Brian Salomonf9f45122016-11-29 11:59:17 -050051
Brian Salomon4dea72a2019-12-18 10:43:10 -050052 static_assert(2 == sizeof(swizzle.asKey()));
Brian Salomon280fa3d2020-08-27 17:16:49 -040053 uint16_t swizzleKey = swizzle.asKey();
Brian Salomon67529b22019-08-13 15:31:04 -040054 return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
Brian Salomonf9f45122016-11-29 11:59:17 -050055}
56
Robert Phillips787fd9d2021-03-22 14:48:09 -040057static void add_geomproc_sampler_keys(GrProcessorKeyBuilder* b,
58 const GrGeometryProcessor& geomProc,
59 const GrCaps& caps) {
60 int numTextureSamplers = geomProc.numTextureSamplers();
Brian Osman33167962021-03-04 13:05:49 -050061 b->add32(numTextureSamplers, "ppNumSamplers");
Brian Salomone782f842018-07-31 13:53:11 -040062 for (int i = 0; i < numTextureSamplers; ++i) {
Robert Phillips787fd9d2021-03-22 14:48:09 -040063 const GrGeometryProcessor::TextureSampler& sampler = geomProc.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
Brian Osman33167962021-03-04 13:05:49 -050073// Currently we allow 8 bits for the class id
Brian Osmanf0de96f2021-02-26 13:54:11 -050074static constexpr uint32_t kClassIDBits = 8;
Brian Osmanf0de96f2021-02-26 13:54:11 -050075
joshualitt23e280d2014-09-18 12:26:38 -070076/**
Brian Osman33167962021-03-04 13:05:49 -050077 * Functions which emit processor key info into the key builder.
78 * For every effect, we include the effect's class ID (different for every GrProcessor subclass),
Brian Salomon13b28732021-08-06 15:33:58 -040079 * any information generated by the effect itself (addToKey), and some meta-information.
Brian Osman33167962021-03-04 13:05:49 -050080 * Shader code may be dependent on properties of the effect not placed in the key by the effect
81 * (e.g. pixel format of textures used).
joshualitt23e280d2014-09-18 12:26:38 -070082 */
Robert Phillips787fd9d2021-03-22 14:48:09 -040083static void gen_geomproc_key(const GrGeometryProcessor& geomProc,
84 const GrCaps& caps,
85 GrProcessorKeyBuilder* b) {
86 b->appendComment(geomProc.name());
87 b->addBits(kClassIDBits, geomProc.classID(), "geomProcClassID");
Brian Salomone782f842018-07-31 13:53:11 -040088
Brian Salomon13b28732021-08-06 15:33:58 -040089 geomProc.addToKey(*caps.shaderCaps(), b);
Robert Phillips787fd9d2021-03-22 14:48:09 -040090 geomProc.getAttributeKey(b);
Brian Salomone782f842018-07-31 13:53:11 -040091
Robert Phillips787fd9d2021-03-22 14:48:09 -040092 add_geomproc_sampler_keys(b, geomProc, caps);
joshualitt65171342014-10-09 07:25:36 -070093}
joshualittb0a8a372014-09-23 09:50:21 -070094
Brian Osman33167962021-03-04 13:05:49 -050095static void gen_xp_key(const GrXferProcessor& xp,
96 const GrCaps& caps,
97 const GrPipeline& pipeline,
98 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -050099 b->appendComment(xp.name());
Brian Osman33167962021-03-04 13:05:49 -0500100 b->addBits(kClassIDBits, xp.classID(), "xpClassID");
Brian Salomonab015ef2017-04-04 10:15:51 -0400101
Brian Osman33167962021-03-04 13:05:49 -0500102 const GrSurfaceOrigin* originIfDstTexture = nullptr;
103 GrSurfaceOrigin origin;
104 if (pipeline.dstProxyView().proxy()) {
105 origin = pipeline.dstProxyView().origin();
106 originIfDstTexture = &origin;
Brian Salomonab015ef2017-04-04 10:15:51 -0400107 }
Greg Daniel87fab9f2021-06-07 15:18:23 -0400108
Brian Salomon13b28732021-08-06 15:33:58 -0400109 xp.addToKey(*caps.shaderCaps(),
110 b,
111 originIfDstTexture,
112 pipeline.dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment);
Brian Salomonab015ef2017-04-04 10:15:51 -0400113}
114
Brian Osman33167962021-03-04 13:05:49 -0500115static void gen_fp_key(const GrFragmentProcessor& fp,
116 const GrCaps& caps,
117 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500118 b->appendComment(fp.name());
Brian Osman48d7f7c2021-03-03 13:38:08 -0500119 b->addBits(kClassIDBits, fp.classID(), "fpClassID");
Robert Phillips787fd9d2021-03-22 14:48:09 -0400120 b->addBits(GrGeometryProcessor::kCoordTransformKeyBits,
121 GrGeometryProcessor::ComputeCoordTransformsKey(fp), "fpTransforms");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500122
Brian Osman33167962021-03-04 13:05:49 -0500123 if (auto* te = fp.asTextureEffect()) {
124 const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat();
125 uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps);
Brian Osman48d7f7c2021-03-03 13:38:08 -0500126 b->add32(samplerKey, "fpSamplerKey");
Brian Osman33167962021-03-04 13:05:49 -0500127 caps.addExtraSamplerKey(b, te->samplerState(), backendFormat);
128 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500129
Brian Salomon13b28732021-08-06 15:33:58 -0400130 fp.addToKey(*caps.shaderCaps(), b);
Brian Osman48d7f7c2021-03-03 13:38:08 -0500131 b->add32(fp.numChildProcessors(), "fpNumChildren");
132
wangyixa7f4c432015-08-20 07:25:02 -0700133 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400134 if (auto child = fp.childProcessor(i)) {
Brian Osman33167962021-03-04 13:05:49 -0500135 gen_fp_key(*child, caps, b);
Brian Osman12c5d292020-07-13 16:11:35 -0400136 } else {
137 // Fold in a sentinel value as the "class ID" for any null children
Brian Osmane23c03d2021-03-05 14:58:25 -0500138 b->appendComment("Null");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500139 b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID");
wangyixa7f4c432015-08-20 07:25:02 -0700140 }
141 }
wangyixa7f4c432015-08-20 07:25:02 -0700142}
143
Brian Osmane23c03d2021-03-05 14:58:25 -0500144static void gen_key(GrProcessorKeyBuilder* b,
Brian Osmane23c03d2021-03-05 14:58:25 -0500145 const GrProgramInfo& programInfo,
146 const GrCaps& caps) {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400147 gen_geomproc_key(programInfo.geomProc(), caps, b);
bsalomon929f29a2014-07-17 07:55:11 -0700148
John Stilesd3feb6f2020-07-23 18:18:12 -0400149 const GrPipeline& pipeline = programInfo.pipeline();
Brian Osman33167962021-03-04 13:05:49 -0500150 b->addBits(2, pipeline.numFragmentProcessors(), "numFPs");
151 b->addBits(1, pipeline.numColorFragmentProcessors(), "numColorFPs");
John Stilesd3feb6f2020-07-23 18:18:12 -0400152 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
Brian Osman33167962021-03-04 13:05:49 -0500153 gen_fp_key(pipeline.getFragmentProcessor(i), caps, b);
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000154 }
egdaniel170f90b2014-09-16 12:54:40 -0700155
Brian Osman33167962021-03-04 13:05:49 -0500156 gen_xp_key(pipeline.getXferProcessor(), caps, pipeline, b);
Chris Daltond7291ba2019-03-07 14:17:03 -0700157
Brian Osmanef3725f2021-03-04 10:44:41 -0500158 b->addBits(16, pipeline.writeSwizzle().asKey(), "writeSwizzle");
Brian Osmanef3725f2021-03-04 10:44:41 -0500159 b->addBits(1, static_cast<uint32_t>(programInfo.requestedFeatures()), "requestedFeatures");
Brian Osman33167962021-03-04 13:05:49 -0500160 b->addBool(pipeline.snapVerticesToPixelCenters(), "snapVertices");
Robert Phillipsfcaae482019-11-07 10:17:03 -0500161 // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
162 // specific versions (e.g., Vulkan) require more detail
Brian Osman33167962021-03-04 13:05:49 -0500163 b->addBool((programInfo.primitiveType() == GrPrimitiveType::kPoints), "isPoints");
Robert Phillipsc15e8902019-11-26 14:26:36 -0500164
Brian Osmanf0de96f2021-02-26 13:54:11 -0500165 // Put a clean break between the "common" data written by this function, and any backend data
166 // appended later. The initial key length will just be this portion (rounded to 4 bytes).
Brian Osmanef3725f2021-03-04 10:44:41 -0500167 b->flush();
Brian Osmane23c03d2021-03-05 14:58:25 -0500168}
Robert Phillipsc15e8902019-11-26 14:26:36 -0500169
Brian Osmane23c03d2021-03-05 14:58:25 -0500170void GrProgramDesc::Build(GrProgramDesc* desc,
Brian Osmane23c03d2021-03-05 14:58:25 -0500171 const GrProgramInfo& programInfo,
172 const GrCaps& caps) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500173 desc->reset();
174 GrProcessorKeyBuilder b(desc->key());
Robert Phillipsced5e832021-03-23 09:36:53 -0400175 gen_key(&b, programInfo, caps);
Brian Osmane23c03d2021-03-05 14:58:25 -0500176 desc->fInitialKeyLength = desc->keyLength();
177}
178
Robert Phillipsced5e832021-03-23 09:36:53 -0400179SkString GrProgramDesc::Describe(const GrProgramInfo& programInfo,
Brian Osmane23c03d2021-03-05 14:58:25 -0500180 const GrCaps& caps) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500181 GrProgramDesc desc;
182 GrProcessorStringKeyBuilder b(desc.key());
Robert Phillipsced5e832021-03-23 09:36:53 -0400183 gen_key(&b, programInfo, caps);
Brian Osmane23c03d2021-03-05 14:58:25 -0500184 b.flush();
185 return b.description();
bsalomon848faf02014-07-11 10:01:02 -0700186}