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