xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrProgramDesc.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include "src/gpu/ganesh/GrProgramDesc.h"
8 
9 #include "include/gpu/ganesh/GrBackendSurface.h"
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkTo.h"
12 #include "include/private/gpu/ganesh/GrTypesPriv.h"
13 #include "src/gpu/KeyBuilder.h"
14 #include "src/gpu/Swizzle.h"
15 #include "src/gpu/ganesh/GrCaps.h"
16 #include "src/gpu/ganesh/GrFragmentProcessor.h"
17 #include "src/gpu/ganesh/GrGeometryProcessor.h"
18 #include "src/gpu/ganesh/GrPipeline.h"
19 #include "src/gpu/ganesh/GrProcessor.h"
20 #include "src/gpu/ganesh/GrProgramInfo.h"
21 #include "src/gpu/ganesh/GrSurfaceProxy.h"
22 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
23 #include "src/gpu/ganesh/GrXferProcessor.h"
24 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
25 
26 enum GrSurfaceOrigin : int;
27 
28 // Currently we allow 8 bits for the class id
29 static constexpr uint32_t kClassIDBits = 8;
30 static constexpr uint32_t kSamplerOrImageTypeKeyBits = 4;
31 
texture_type_key(GrTextureType type)32 static inline uint16_t texture_type_key(GrTextureType type) {
33     int value = UINT16_MAX;
34     switch (type) {
35         case GrTextureType::k2D:
36             value = 0;
37             break;
38         case GrTextureType::kExternal:
39             value = 1;
40             break;
41         case GrTextureType::kRectangle:
42             value = 2;
43             break;
44         default:
45             SK_ABORT("Unexpected texture type");
46             value = 3;
47             break;
48     }
49     SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
50     return SkToU16(value);
51 }
52 
sampler_key(GrTextureType textureType,const skgpu::Swizzle & swizzle,const GrCaps & caps)53 static uint32_t sampler_key(GrTextureType textureType, const skgpu::Swizzle& swizzle,
54                             const GrCaps& caps) {
55     int samplerTypeKey = texture_type_key(textureType);
56 
57     static_assert(2 == sizeof(swizzle.asKey()));
58     uint16_t swizzleKey = swizzle.asKey();
59     return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
60 }
61 
add_geomproc_sampler_keys(skgpu::KeyBuilder * b,const GrGeometryProcessor & geomProc,const GrCaps & caps)62 static void add_geomproc_sampler_keys(skgpu::KeyBuilder* b,
63                                       const GrGeometryProcessor& geomProc,
64                                       const GrCaps& caps) {
65     int numTextureSamplers = geomProc.numTextureSamplers();
66     b->add32(numTextureSamplers, "ppNumSamplers");
67     for (int i = 0; i < numTextureSamplers; ++i) {
68         const GrGeometryProcessor::TextureSampler& sampler = geomProc.textureSampler(i);
69         const GrBackendFormat& backendFormat = sampler.backendFormat();
70 
71         uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
72         b->add32(samplerKey);
73 
74         caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
75     }
76 }
77 
78 /**
79  * Functions which emit processor key info into the key builder.
80  * For every effect, we include the effect's class ID (different for every GrProcessor subclass),
81  * any information generated by the effect itself (addToKey), and some meta-information.
82  * Shader code may be dependent on properties of the effect not placed in the key by the effect
83  * (e.g. pixel format of textures used).
84  */
gen_geomproc_key(const GrGeometryProcessor & geomProc,const GrCaps & caps,skgpu::KeyBuilder * b)85 static void gen_geomproc_key(const GrGeometryProcessor& geomProc,
86                              const GrCaps& caps,
87                              skgpu::KeyBuilder* b) {
88     b->appendComment(geomProc.name());
89     b->addBits(kClassIDBits, geomProc.classID(), "geomProcClassID");
90 
91     geomProc.addToKey(*caps.shaderCaps(), b);
92     geomProc.getAttributeKey(b);
93 
94     add_geomproc_sampler_keys(b, geomProc, caps);
95 }
96 
gen_xp_key(const GrXferProcessor & xp,const GrCaps & caps,const GrPipeline & pipeline,skgpu::KeyBuilder * b)97 static void gen_xp_key(const GrXferProcessor& xp,
98                        const GrCaps& caps,
99                        const GrPipeline& pipeline,
100                        skgpu::KeyBuilder* b) {
101     b->appendComment(xp.name());
102     b->addBits(kClassIDBits, xp.classID(), "xpClassID");
103 
104     const GrSurfaceOrigin* originIfDstTexture = nullptr;
105     GrSurfaceOrigin origin;
106     const GrSurfaceProxyView& dstView = pipeline.dstProxyView();
107     if (dstView.proxy()) {
108         origin = dstView.origin();
109         originIfDstTexture = &origin;
110 
111         uint32_t samplerKey = sampler_key(dstView.proxy()->backendFormat().textureType(),
112                                           dstView.swizzle(), caps);
113         b->add32(samplerKey);
114     }
115 
116     xp.addToKey(*caps.shaderCaps(),
117                 b,
118                 originIfDstTexture,
119                 pipeline.dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment);
120 }
121 
gen_fp_key(const GrFragmentProcessor & fp,const GrCaps & caps,skgpu::KeyBuilder * b)122 static void gen_fp_key(const GrFragmentProcessor& fp,
123                        const GrCaps& caps,
124                        skgpu::KeyBuilder* b) {
125     b->appendComment(fp.name());
126     b->addBits(kClassIDBits, fp.classID(), "fpClassID");
127     b->addBits(GrGeometryProcessor::kCoordTransformKeyBits,
128                GrGeometryProcessor::ComputeCoordTransformsKey(fp), "fpTransforms");
129 
130     if (auto* te = fp.asTextureEffect()) {
131         const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat();
132         uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps);
133         b->add32(samplerKey, "fpSamplerKey");
134         caps.addExtraSamplerKey(b, te->samplerState(), backendFormat);
135     }
136 
137     fp.addToKey(*caps.shaderCaps(), b);
138     b->add32(fp.numChildProcessors(), "fpNumChildren");
139 
140     for (int i = 0; i < fp.numChildProcessors(); ++i) {
141         if (auto child = fp.childProcessor(i)) {
142             gen_fp_key(*child, caps, b);
143         } else {
144             // Fold in a sentinel value as the "class ID" for any null children
145             b->appendComment("Null");
146             b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID");
147         }
148     }
149 }
150 
gen_key(skgpu::KeyBuilder * b,const GrProgramInfo & programInfo,const GrCaps & caps)151 static void gen_key(skgpu::KeyBuilder* b,
152                     const GrProgramInfo& programInfo,
153                     const GrCaps& caps) {
154     gen_geomproc_key(programInfo.geomProc(), caps, b);
155 
156     const GrPipeline& pipeline = programInfo.pipeline();
157     b->addBits(2, pipeline.numFragmentProcessors(),      "numFPs");
158     b->addBits(1, pipeline.numColorFragmentProcessors(), "numColorFPs");
159     for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
160         gen_fp_key(pipeline.getFragmentProcessor(i), caps, b);
161     }
162 
163     gen_xp_key(pipeline.getXferProcessor(), caps, pipeline, b);
164 
165     b->addBits(16, pipeline.writeSwizzle().asKey(), "writeSwizzle");
166     b->addBool(pipeline.snapVerticesToPixelCenters(), "snapVertices");
167     // The base descriptor only stores whether or not the primitiveType is kPoints. Backend-
168     // specific versions (e.g., Vulkan) require more detail
169     b->addBool((programInfo.primitiveType() == GrPrimitiveType::kPoints), "isPoints");
170 
171     // Put a clean break between the "common" data written by this function, and any backend data
172     // appended later. The initial key length will just be this portion (rounded to 4 bytes).
173     b->flush();
174 }
175 
Build(GrProgramDesc * desc,const GrProgramInfo & programInfo,const GrCaps & caps)176 void GrProgramDesc::Build(GrProgramDesc* desc,
177                           const GrProgramInfo& programInfo,
178                           const GrCaps& caps) {
179     desc->reset();
180     skgpu::KeyBuilder b(desc->key());
181     gen_key(&b, programInfo, caps);
182     desc->fInitialKeyLength = desc->keyLength();
183 }
184 
Describe(const GrProgramInfo & programInfo,const GrCaps & caps)185 SkString GrProgramDesc::Describe(const GrProgramInfo& programInfo,
186                                  const GrCaps& caps) {
187     GrProgramDesc desc;
188     skgpu::StringKeyBuilder b(desc.key());
189     gen_key(&b, programInfo, caps);
190     b.flush();
191     return b.description();
192 }
193