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/GrPipeline.h" |
13 | #include "src/gpu/GrPrimitiveProcessor.h" |
14 | #include "src/gpu/GrProcessor.h" |
15 | #include "src/gpu/GrProgramInfo.h" |
16 | #include "src/gpu/GrRenderTarget.h" |
17 | #include "src/gpu/GrShaderCaps.h" |
18 | #include "src/gpu/GrTexture.h" |
19 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
20 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
21 | |
22 | enum { |
23 | kSamplerOrImageTypeKeyBits = 4 |
24 | }; |
25 | |
26 | static inline uint16_t texture_type_key(GrTextureType type) { |
27 | int value = UINT16_MAX; |
28 | switch (type) { |
29 | case GrTextureType::k2D: |
30 | value = 0; |
31 | break; |
32 | case GrTextureType::kExternal: |
33 | value = 1; |
34 | break; |
35 | case GrTextureType::kRectangle: |
36 | value = 2; |
37 | break; |
38 | default: |
39 | SK_ABORT("Unexpected texture type" ); |
40 | value = 3; |
41 | break; |
42 | } |
43 | SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value); |
44 | return SkToU16(value); |
45 | } |
46 | |
47 | static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle, |
48 | const GrCaps& caps) { |
49 | int samplerTypeKey = texture_type_key(textureType); |
50 | |
51 | static_assert(2 == sizeof(swizzle.asKey())); |
52 | uint16_t swizzleKey = 0; |
53 | if (caps.shaderCaps()->textureSwizzleAppliedInShader()) { |
54 | swizzleKey = swizzle.asKey(); |
55 | } |
56 | return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits); |
57 | } |
58 | |
59 | static void add_pp_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp, |
60 | const GrCaps& caps) { |
61 | int numTextureSamplers = pp.numTextureSamplers(); |
62 | if (!numTextureSamplers) { |
63 | return; |
64 | } |
65 | for (int i = 0; i < numTextureSamplers; ++i) { |
66 | const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i); |
67 | const GrBackendFormat& backendFormat = sampler.backendFormat(); |
68 | |
69 | uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps); |
70 | b->add32(samplerKey); |
71 | |
72 | caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * A function which emits a meta key into the key builder. This is required because shader code may |
78 | * be dependent on properties of the effect that the effect itself doesn't use |
79 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
80 | * every effect using this function. It is also responsible for inserting the effect's class ID |
81 | * which must be different for every GrProcessor subclass. It can fail if an effect uses too many |
82 | * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this |
83 | * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms |
84 | */ |
85 | static bool gen_fp_meta_key(const GrFragmentProcessor& fp, |
86 | const GrCaps& caps, |
87 | uint32_t transformKey, |
88 | GrProcessorKeyBuilder* b) { |
89 | size_t processorKeySize = b->size(); |
90 | uint32_t classID = fp.classID(); |
91 | |
92 | // Currently we allow 16 bits for the class id and the overall processor key size. |
93 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX); |
94 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
95 | return false; |
96 | } |
97 | |
98 | fp.visitTextureEffects([&](const GrTextureEffect& te) { |
99 | const GrBackendFormat& backendFormat = te.view().proxy()->backendFormat(); |
100 | uint32_t samplerKey = sampler_key(backendFormat.textureType(), te.view().swizzle(), caps); |
101 | b->add32(samplerKey); |
102 | caps.addExtraSamplerKey(b, te.samplerState(), backendFormat); |
103 | }); |
104 | |
105 | uint32_t* key = b->add32n(2); |
106 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
107 | key[1] = transformKey; |
108 | return true; |
109 | } |
110 | |
111 | static bool gen_pp_meta_key(const GrPrimitiveProcessor& pp, |
112 | const GrCaps& caps, |
113 | uint32_t transformKey, |
114 | GrProcessorKeyBuilder* b) { |
115 | size_t processorKeySize = b->size(); |
116 | uint32_t classID = pp.classID(); |
117 | |
118 | // Currently we allow 16 bits for the class id and the overall processor key size. |
119 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX); |
120 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
121 | return false; |
122 | } |
123 | |
124 | add_pp_sampler_keys(b, pp, caps); |
125 | |
126 | uint32_t* key = b->add32n(2); |
127 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
128 | key[1] = transformKey; |
129 | return true; |
130 | } |
131 | |
132 | static bool gen_xp_meta_key(const GrXferProcessor& xp, GrProcessorKeyBuilder* b) { |
133 | size_t processorKeySize = b->size(); |
134 | uint32_t classID = xp.classID(); |
135 | |
136 | // Currently we allow 16 bits for the class id and the overall processor key size. |
137 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX); |
138 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
139 | return false; |
140 | } |
141 | |
142 | b->add32((classID << 16) | SkToU32(processorKeySize)); |
143 | return true; |
144 | } |
145 | |
146 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
147 | const GrFragmentProcessor& fp, |
148 | const GrCaps& caps, |
149 | GrProcessorKeyBuilder* b) { |
150 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
151 | if (auto child = fp.childProcessor(i)) { |
152 | if (!gen_frag_proc_and_meta_keys(primProc, *child, caps, b)) { |
153 | return false; |
154 | } |
155 | } else { |
156 | // Fold in a sentinel value as the "class ID" for any null children |
157 | b->add32(GrProcessor::ClassID::kNull_ClassID); |
158 | } |
159 | } |
160 | |
161 | fp.getGLSLProcessorKey(*caps.shaderCaps(), b); |
162 | |
163 | return gen_fp_meta_key(fp, caps, primProc.computeCoordTransformsKey(fp), b); |
164 | } |
165 | |
166 | bool GrProgramDesc::Build(GrProgramDesc* desc, |
167 | GrRenderTarget* renderTarget, |
168 | const GrProgramInfo& programInfo, |
169 | const GrCaps& caps) { |
170 | #ifdef SK_DEBUG |
171 | if (renderTarget) { |
172 | SkASSERT(programInfo.backendFormat() == renderTarget->backendFormat()); |
173 | } |
174 | #endif |
175 | |
176 | // The descriptor is used as a cache key. Thus when a field of the |
177 | // descriptor will not affect program generation (because of the attribute |
178 | // bindings in use or other descriptor field settings) it should be set |
179 | // to a canonical value to avoid duplicate programs with different keys. |
180 | |
181 | static_assert(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
182 | // Make room for everything up to the effect keys. |
183 | desc->key().reset(); |
184 | desc->key().push_back_n(kProcessorKeysOffset); |
185 | |
186 | GrProcessorKeyBuilder b(&desc->key()); |
187 | |
188 | const GrPrimitiveProcessor& primitiveProcessor = programInfo.primProc(); |
189 | primitiveProcessor.getGLSLProcessorKey(*caps.shaderCaps(), &b); |
190 | primitiveProcessor.getAttributeKey(&b); |
191 | if (!gen_pp_meta_key(primitiveProcessor, caps, 0, &b)) { |
192 | desc->key().reset(); |
193 | return false; |
194 | } |
195 | |
196 | const GrPipeline& pipeline = programInfo.pipeline(); |
197 | int numColorFPs = 0, numCoverageFPs = 0; |
198 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
199 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
200 | if (!gen_frag_proc_and_meta_keys(primitiveProcessor, fp, caps, &b)) { |
201 | desc->key().reset(); |
202 | return false; |
203 | } |
204 | if (pipeline.isColorFragmentProcessor(i)) { |
205 | ++numColorFPs; |
206 | } else if (pipeline.isCoverageFragmentProcessor(i)) { |
207 | ++numCoverageFPs; |
208 | } |
209 | } |
210 | |
211 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
212 | const GrSurfaceOrigin* originIfDstTexture = nullptr; |
213 | GrSurfaceOrigin origin; |
214 | if (pipeline.dstProxyView().proxy()) { |
215 | origin = pipeline.dstProxyView().origin(); |
216 | originIfDstTexture = &origin; |
217 | } |
218 | xp.getGLSLProcessorKey(*caps.shaderCaps(), &b, originIfDstTexture); |
219 | if (!gen_xp_meta_key(xp, &b)) { |
220 | desc->key().reset(); |
221 | return false; |
222 | } |
223 | |
224 | if (programInfo.requestedFeatures() & GrProcessor::CustomFeatures::kSampleLocations) { |
225 | SkASSERT(pipeline.isHWAntialiasState()); |
226 | b.add32(renderTarget->getSamplePatternKey()); |
227 | } |
228 | |
229 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
230 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
231 | // below here. |
232 | KeyHeader* = desc->atOffset<KeyHeader, kHeaderOffset>(); |
233 | |
234 | // make sure any padding in the header is zeroed. |
235 | memset(header, 0, kHeaderSize); |
236 | header->fWriteSwizzle = pipeline.writeSwizzle().asKey(); |
237 | header->fColorFragmentProcessorCnt = numColorFPs; |
238 | header->fCoverageFragmentProcessorCnt = numCoverageFPs; |
239 | SkASSERT(header->fColorFragmentProcessorCnt == numColorFPs); |
240 | SkASSERT(header->fCoverageFragmentProcessorCnt == numCoverageFPs); |
241 | // If we knew the shader won't depend on origin, we could skip this (and use the same program |
242 | // for both origins). Instrumenting all fragment processors would be difficult and error prone. |
243 | header->fSurfaceOriginKey = |
244 | GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(programInfo.origin()); |
245 | header->fProcessorFeatures = (uint8_t)programInfo.requestedFeatures(); |
246 | // Ensure enough bits. |
247 | SkASSERT(header->fProcessorFeatures == (int) programInfo.requestedFeatures()); |
248 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
249 | // The base descriptor only stores whether or not the primitiveType is kPoints. Backend- |
250 | // specific versions (e.g., Vulkan) require more detail |
251 | header->fHasPointSize = (programInfo.primitiveType() == GrPrimitiveType::kPoints); |
252 | |
253 | header->fInitialKeyLength = desc->keyLength(); |
254 | // Fail if the initial key length won't fit in 27 bits. |
255 | if (header->fInitialKeyLength != desc->keyLength()) { |
256 | desc->key().reset(); |
257 | return false; |
258 | } |
259 | |
260 | return true; |
261 | } |
262 | |