Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrProgramDesc.cpp
Go to the documentation of this file.
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
9
11#include "src/core/SkChecksum.h"
12#include "src/gpu/KeyBuilder.h"
24
25enum {
27};
28
29static inline uint16_t texture_type_key(GrTextureType type) {
30 int value = UINT16_MAX;
31 switch (type) {
33 value = 0;
34 break;
36 value = 1;
37 break;
39 value = 2;
40 break;
41 default:
42 SK_ABORT("Unexpected texture type");
43 value = 3;
44 break;
45 }
47 return SkToU16(value);
48}
49
50static uint32_t sampler_key(GrTextureType textureType, const skgpu::Swizzle& swizzle,
51 const GrCaps& caps) {
52 int samplerTypeKey = texture_type_key(textureType);
53
54 static_assert(2 == sizeof(swizzle.asKey()));
55 uint16_t swizzleKey = swizzle.asKey();
56 return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits);
57}
58
60 const GrGeometryProcessor& geomProc,
61 const GrCaps& caps) {
62 int numTextureSamplers = geomProc.numTextureSamplers();
63 b->add32(numTextureSamplers, "ppNumSamplers");
64 for (int i = 0; i < numTextureSamplers; ++i) {
65 const GrGeometryProcessor::TextureSampler& sampler = geomProc.textureSampler(i);
66 const GrBackendFormat& backendFormat = sampler.backendFormat();
67
68 uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps);
69 b->add32(samplerKey);
70
71 caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat);
72 }
73}
74
75// Currently we allow 8 bits for the class id
76static constexpr uint32_t kClassIDBits = 8;
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 */
85static void gen_geomproc_key(const GrGeometryProcessor& geomProc,
86 const GrCaps& caps,
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
97static void gen_xp_key(const GrXferProcessor& xp,
98 const GrCaps& caps,
99 const GrPipeline& pipeline,
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,
120}
121
122static void gen_fp_key(const GrFragmentProcessor& fp,
123 const GrCaps& caps,
125 b->appendComment(fp.name());
126 b->addBits(kClassIDBits, fp.classID(), "fpClassID");
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
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
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
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}
@ kSamplerOrImageTypeKeyBits
static void gen_geomproc_key(const GrGeometryProcessor &geomProc, const GrCaps &caps, skgpu::KeyBuilder *b)
static void gen_key(skgpu::KeyBuilder *b, const GrProgramInfo &programInfo, const GrCaps &caps)
static constexpr uint32_t kClassIDBits
static void add_geomproc_sampler_keys(skgpu::KeyBuilder *b, const GrGeometryProcessor &geomProc, const GrCaps &caps)
static uint32_t sampler_key(GrTextureType textureType, const skgpu::Swizzle &swizzle, const GrCaps &caps)
static void gen_xp_key(const GrXferProcessor &xp, const GrCaps &caps, const GrPipeline &pipeline, skgpu::KeyBuilder *b)
static void gen_fp_key(const GrFragmentProcessor &fp, const GrCaps &caps, skgpu::KeyBuilder *b)
static uint16_t texture_type_key(GrTextureType type)
GrTextureType
GrSurfaceOrigin
Definition GrTypes.h:147
#define SK_ABORT(message,...)
Definition SkAssert.h:70
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr uint16_t SkToU16(S x)
Definition SkTo.h:24
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
GrTextureType textureType() const
virtual void addExtraSamplerKey(skgpu::KeyBuilder *, GrSamplerState, const GrBackendFormat &) const
Definition GrCaps.h:507
const GrShaderCaps * shaderCaps() const
Definition GrCaps.h:63
const GrBackendFormat & backendFormat() const
const skgpu::Swizzle & swizzle() const
const TextureSampler & textureSampler(int index) const
static constexpr int kCoordTransformKeyBits
virtual void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const =0
static uint32_t ComputeCoordTransformsKey(const GrFragmentProcessor &fp)
void getAttributeKey(skgpu::KeyBuilder *b) const
const skgpu::Swizzle & writeSwizzle() const
Definition GrPipeline.h:197
bool snapVerticesToPixelCenters() const
Definition GrPipeline.h:171
int numFragmentProcessors() const
Definition GrPipeline.h:99
int numColorFragmentProcessors() const
Definition GrPipeline.h:100
GrDstSampleFlags dstSampleFlags() const
Definition GrPipeline.h:142
const GrFragmentProcessor & getFragmentProcessor(int idx) const
Definition GrPipeline.h:157
const GrSurfaceProxyView & dstProxyView() const
Definition GrPipeline.h:138
const GrXferProcessor & getXferProcessor() const
Definition GrPipeline.h:116
ClassID classID() const
virtual const char * name() const =0
static void Build(GrProgramDesc *, const GrProgramInfo &, const GrCaps &)
static SkString Describe(const GrProgramInfo &, const GrCaps &)
GrPrimitiveType primitiveType() const
const GrPipeline & pipeline() const
const GrGeometryProcessor & geomProc() const
skgpu::Swizzle swizzle() const
GrSurfaceOrigin origin() const
GrSurfaceProxy * proxy() const
const GrBackendFormat & backendFormat() const
void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *, const GrSurfaceOrigin *originIfDstTexture, bool usesInputAttachmentForDstRead) const
constexpr uint16_t asKey() const
Definition Swizzle.h:43
static bool b
uint8_t value