blob: d2ed7e8b4ff8565e04bb880b104736ce15405bd3 [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"
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 Phillips787fd9d2021-03-22 14:48:09 -040056static void add_geomproc_sampler_keys(GrProcessorKeyBuilder* b,
57 const GrGeometryProcessor& geomProc,
58 const GrCaps& caps) {
59 int numTextureSamplers = geomProc.numTextureSamplers();
Brian Osman33167962021-03-04 13:05:49 -050060 b->add32(numTextureSamplers, "ppNumSamplers");
Brian Salomone782f842018-07-31 13:53:11 -040061 for (int i = 0; i < numTextureSamplers; ++i) {
Robert Phillips787fd9d2021-03-22 14:48:09 -040062 const GrGeometryProcessor::TextureSampler& sampler = geomProc.textureSampler(i);
Robert Phillips323471e2019-11-11 11:33:37 -050063 const GrBackendFormat& backendFormat = sampler.backendFormat();
64
65 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
66 b->add32(samplerKey);
67
68 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
bsalomoncdee0092016-01-08 13:20:12 -080069 }
joshualitt23e280d2014-09-18 12:26:38 -070070}
71
Brian Osman33167962021-03-04 13:05:49 -050072// Currently we allow 8 bits for the class id
Brian Osmanf0de96f2021-02-26 13:54:11 -050073static constexpr uint32_t kClassIDBits = 8;
Brian Osmanf0de96f2021-02-26 13:54:11 -050074
joshualitt23e280d2014-09-18 12:26:38 -070075/**
Brian Osman33167962021-03-04 13:05:49 -050076 * Functions which emit processor key info into the key builder.
77 * For every effect, we include the effect's class ID (different for every GrProcessor subclass),
78 * any information generated by the effect itself (getGLSLProcessorKey), and some meta-information.
79 * Shader code may be dependent on properties of the effect not placed in the key by the effect
80 * (e.g. pixel format of textures used).
joshualitt23e280d2014-09-18 12:26:38 -070081 */
Robert Phillips787fd9d2021-03-22 14:48:09 -040082static void gen_geomproc_key(const GrGeometryProcessor& geomProc,
83 const GrCaps& caps,
84 GrProcessorKeyBuilder* b) {
85 b->appendComment(geomProc.name());
86 b->addBits(kClassIDBits, geomProc.classID(), "geomProcClassID");
Brian Salomone782f842018-07-31 13:53:11 -040087
Robert Phillips787fd9d2021-03-22 14:48:09 -040088 geomProc.getGLSLProcessorKey(*caps.shaderCaps(), b);
89 geomProc.getAttributeKey(b);
Brian Salomone782f842018-07-31 13:53:11 -040090
Robert Phillips787fd9d2021-03-22 14:48:09 -040091 add_geomproc_sampler_keys(b, geomProc, caps);
joshualitt65171342014-10-09 07:25:36 -070092}
joshualittb0a8a372014-09-23 09:50:21 -070093
Brian Osman33167962021-03-04 13:05:49 -050094static void gen_xp_key(const GrXferProcessor& xp,
95 const GrCaps& caps,
96 const GrPipeline& pipeline,
97 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -050098 b->appendComment(xp.name());
Brian Osman33167962021-03-04 13:05:49 -050099 b->addBits(kClassIDBits, xp.classID(), "xpClassID");
Brian Salomonab015ef2017-04-04 10:15:51 -0400100
Brian Osman33167962021-03-04 13:05:49 -0500101 const GrSurfaceOrigin* originIfDstTexture = nullptr;
102 GrSurfaceOrigin origin;
103 if (pipeline.dstProxyView().proxy()) {
104 origin = pipeline.dstProxyView().origin();
105 originIfDstTexture = &origin;
Brian Salomonab015ef2017-04-04 10:15:51 -0400106 }
Brian Osman33167962021-03-04 13:05:49 -0500107 xp.getGLSLProcessorKey(*caps.shaderCaps(), b, originIfDstTexture, pipeline.dstSampleType());
Brian Salomonab015ef2017-04-04 10:15:51 -0400108}
109
Brian Osman33167962021-03-04 13:05:49 -0500110static void gen_fp_key(const GrFragmentProcessor& fp,
111 const GrCaps& caps,
112 GrProcessorKeyBuilder* b) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500113 b->appendComment(fp.name());
Brian Osman48d7f7c2021-03-03 13:38:08 -0500114 b->addBits(kClassIDBits, fp.classID(), "fpClassID");
Robert Phillips787fd9d2021-03-22 14:48:09 -0400115 b->addBits(GrGeometryProcessor::kCoordTransformKeyBits,
116 GrGeometryProcessor::ComputeCoordTransformsKey(fp), "fpTransforms");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500117
Brian Osman33167962021-03-04 13:05:49 -0500118 if (auto* te = fp.asTextureEffect()) {
119 const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat();
120 uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps);
Brian Osman48d7f7c2021-03-03 13:38:08 -0500121 b->add32(samplerKey, "fpSamplerKey");
Brian Osman33167962021-03-04 13:05:49 -0500122 caps.addExtraSamplerKey(b, te->samplerState(), backendFormat);
123 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500124
125 fp.getGLSLProcessorKey(*caps.shaderCaps(), b);
126 b->add32(fp.numChildProcessors(), "fpNumChildren");
127
wangyixa7f4c432015-08-20 07:25:02 -0700128 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Osman12c5d292020-07-13 16:11:35 -0400129 if (auto child = fp.childProcessor(i)) {
Brian Osman33167962021-03-04 13:05:49 -0500130 gen_fp_key(*child, caps, b);
Brian Osman12c5d292020-07-13 16:11:35 -0400131 } else {
132 // Fold in a sentinel value as the "class ID" for any null children
Brian Osmane23c03d2021-03-05 14:58:25 -0500133 b->appendComment("Null");
Brian Osman48d7f7c2021-03-03 13:38:08 -0500134 b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID");
wangyixa7f4c432015-08-20 07:25:02 -0700135 }
136 }
wangyixa7f4c432015-08-20 07:25:02 -0700137}
138
Brian Osmane23c03d2021-03-05 14:58:25 -0500139static void gen_key(GrProcessorKeyBuilder* b,
Brian Osmane23c03d2021-03-05 14:58:25 -0500140 const GrProgramInfo& programInfo,
141 const GrCaps& caps) {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400142 gen_geomproc_key(programInfo.geomProc(), 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,
Brian Osmane23c03d2021-03-05 14:58:25 -0500169 const GrProgramInfo& programInfo,
170 const GrCaps& caps) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500171 desc->reset();
172 GrProcessorKeyBuilder b(desc->key());
Robert Phillipsced5e832021-03-23 09:36:53 -0400173 gen_key(&b, programInfo, caps);
Brian Osmane23c03d2021-03-05 14:58:25 -0500174 desc->fInitialKeyLength = desc->keyLength();
175}
176
Robert Phillipsced5e832021-03-23 09:36:53 -0400177SkString GrProgramDesc::Describe(const GrProgramInfo& programInfo,
Brian Osmane23c03d2021-03-05 14:58:25 -0500178 const GrCaps& caps) {
Brian Osmane23c03d2021-03-05 14:58:25 -0500179 GrProgramDesc desc;
180 GrProcessorStringKeyBuilder b(desc.key());
Robert Phillipsced5e832021-03-23 09:36:53 -0400181 gen_key(&b, programInfo, caps);
Brian Osmane23c03d2021-03-05 14:58:25 -0500182 b.flush();
183 return b.description();
bsalomon848faf02014-07-11 10:01:02 -0700184}