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