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 "GrProgramDesc.h"
9 
10 #include "GrPipeline.h"
11 #include "GrPrimitiveProcessor.h"
12 #include "GrProcessor.h"
13 #include "GrRenderTargetPriv.h"
14 #include "GrShaderCaps.h"
15 #include "GrTexturePriv.h"
16 #include "SkChecksum.h"
17 #include "SkTo.h"
18 #include "glsl/GrGLSLFragmentProcessor.h"
19 #include "glsl/GrGLSLFragmentShaderBuilder.h"
20 
21 enum {
22     kSamplerOrImageTypeKeyBits = 4
23 };
24 
texture_type_key(GrTextureType type)25 static inline uint16_t texture_type_key(GrTextureType type) {
26     int value = UINT16_MAX;
27     switch (type) {
28         case GrTextureType::k2D:
29             value = 0;
30             break;
31         case GrTextureType::kExternal:
32             value = 1;
33             break;
34         case GrTextureType::kRectangle:
35             value = 2;
36             break;
37     }
38     SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
39     return SkToU16(value);
40 }
41 
sampler_key(GrTextureType textureType,GrPixelConfig config,const GrShaderCaps & caps)42 static uint16_t sampler_key(GrTextureType textureType, GrPixelConfig config,
43                             const GrShaderCaps& caps) {
44     int samplerTypeKey = texture_type_key(textureType);
45 
46     GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey()));
47     return SkToU16(samplerTypeKey |
48                    caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
49                    (GrSLSamplerPrecision(config) << (8 + kSamplerOrImageTypeKeyBits)));
50 }
51 
add_sampler_keys(GrProcessorKeyBuilder * b,const GrFragmentProcessor & fp,GrGpu * gpu,const GrShaderCaps & caps)52 static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrFragmentProcessor& fp,
53                              GrGpu* gpu, const GrShaderCaps& caps) {
54     int numTextureSamplers = fp.numTextureSamplers();
55     // Need two bytes per key.
56     int word32Count = (numTextureSamplers + 1) / 2;
57     if (0 == word32Count) {
58         return;
59     }
60     uint16_t* k16 = reinterpret_cast<uint16_t*>(b->add32n(word32Count));
61     for (int i = 0; i < numTextureSamplers; ++i) {
62         const GrFragmentProcessor::TextureSampler& sampler = fp.textureSampler(i);
63         const GrTexture* tex = sampler.peekTexture();
64         k16[i] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps);
65         uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(
66                 sampler.samplerState(), sampler.proxy()->backendFormat());
67         if (extraSamplerKey) {
68             SkASSERT(sampler.proxy()->textureType() == GrTextureType::kExternal);
69             // We first mark the normal sampler key with last bit to flag that it has an extra
70             // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
71             SkASSERT((k16[i] & (1 << 15)) == 0);
72             k16[i] = k16[i] | (1 << 15);
73             b->add32(extraSamplerKey);
74         }
75     }
76     // zero the last 16 bits if the number of uniforms for samplers is odd.
77     if (numTextureSamplers & 0x1) {
78         k16[numTextureSamplers] = 0;
79     }
80 }
81 
add_sampler_keys(GrProcessorKeyBuilder * b,const GrPrimitiveProcessor & pp,const GrShaderCaps & caps)82 static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrPrimitiveProcessor& pp,
83                              const GrShaderCaps& caps) {
84     int numTextureSamplers = pp.numTextureSamplers();
85     // Need two bytes per key.
86     int word32Count = (numTextureSamplers + 1) / 2;
87     if (0 == word32Count) {
88         return;
89     }
90     uint16_t* k16 = reinterpret_cast<uint16_t*>(b->add32n(word32Count));
91     for (int i = 0; i < numTextureSamplers; ++i) {
92         const GrPrimitiveProcessor::TextureSampler& sampler = pp.textureSampler(i);
93         k16[i] = sampler_key(sampler.textureType(), sampler.config(), caps);
94         uint32_t extraSamplerKey = sampler.extraSamplerKey();
95         if (extraSamplerKey) {
96             SkASSERT(sampler.textureType() == GrTextureType::kExternal);
97             // We first mark the normal sampler key with last bit to flag that it has an extra
98             // sampler key. We then add all the extraSamplerKeys to the end of the normal ones.
99             SkASSERT((k16[i] & (1 << 15)) == 0);
100             k16[i] = k16[i] | (1 << 15);
101             b->add32(extraSamplerKey);
102         }
103     }
104     // zero the last 16 bits if the number of uniforms for samplers is odd.
105     if (numTextureSamplers & 0x1) {
106         k16[numTextureSamplers] = 0;
107     }
108 }
109 
110 /**
111  * A function which emits a meta key into the key builder.  This is required because shader code may
112  * be dependent on properties of the effect that the effect itself doesn't use
113  * in its key (e.g. the pixel format of textures used). So we create a meta-key for
114  * every effect using this function. It is also responsible for inserting the effect's class ID
115  * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
116  * transforms, etc, for the space allotted in the meta-key.  NOTE, both FPs and GPs share this
117  * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
118  */
gen_meta_key(const GrFragmentProcessor & fp,GrGpu * gpu,const GrShaderCaps & shaderCaps,uint32_t transformKey,GrProcessorKeyBuilder * b)119 static bool gen_meta_key(const GrFragmentProcessor& fp,
120                          GrGpu* gpu,
121                          const GrShaderCaps& shaderCaps,
122                          uint32_t transformKey,
123                          GrProcessorKeyBuilder* b) {
124     size_t processorKeySize = b->size();
125     uint32_t classID = fp.classID();
126 
127     // Currently we allow 16 bits for the class id and the overall processor key size.
128     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
129     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
130         return false;
131     }
132 
133     add_sampler_keys(b, fp, gpu, shaderCaps);
134 
135     uint32_t* key = b->add32n(2);
136     key[0] = (classID << 16) | SkToU32(processorKeySize);
137     key[1] = transformKey;
138     return true;
139 }
140 
gen_meta_key(const GrPrimitiveProcessor & pp,const GrShaderCaps & shaderCaps,uint32_t transformKey,GrProcessorKeyBuilder * b)141 static bool gen_meta_key(const GrPrimitiveProcessor& pp,
142                          const GrShaderCaps& shaderCaps,
143                          uint32_t transformKey,
144                          GrProcessorKeyBuilder* b) {
145     size_t processorKeySize = b->size();
146     uint32_t classID = pp.classID();
147 
148     // Currently we allow 16 bits for the class id and the overall processor key size.
149     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
150     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
151         return false;
152     }
153 
154     add_sampler_keys(b, pp, shaderCaps);
155 
156     uint32_t* key = b->add32n(2);
157     key[0] = (classID << 16) | SkToU32(processorKeySize);
158     key[1] = transformKey;
159     return true;
160 }
161 
gen_meta_key(const GrXferProcessor & xp,const GrShaderCaps & shaderCaps,GrProcessorKeyBuilder * b)162 static bool gen_meta_key(const GrXferProcessor& xp,
163                          const GrShaderCaps& shaderCaps,
164                          GrProcessorKeyBuilder* b) {
165     size_t processorKeySize = b->size();
166     uint32_t classID = xp.classID();
167 
168     // Currently we allow 16 bits for the class id and the overall processor key size.
169     static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX);
170     if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
171         return false;
172     }
173 
174     b->add32((classID << 16) | SkToU32(processorKeySize));
175     return true;
176 }
177 
gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor & primProc,const GrFragmentProcessor & fp,GrGpu * gpu,const GrShaderCaps & shaderCaps,GrProcessorKeyBuilder * b)178 static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
179                                         const GrFragmentProcessor& fp,
180                                         GrGpu* gpu,
181                                         const GrShaderCaps& shaderCaps,
182                                         GrProcessorKeyBuilder* b) {
183     for (int i = 0; i < fp.numChildProcessors(); ++i) {
184         if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), gpu, shaderCaps, b)) {
185             return false;
186         }
187     }
188 
189     fp.getGLSLProcessorKey(shaderCaps, b);
190 
191     return gen_meta_key(fp, gpu, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
192                                                                       fp.numCoordTransforms()), b);
193 }
194 
Build(GrProgramDesc * desc,GrPixelConfig config,const GrPrimitiveProcessor & primProc,bool hasPointSize,const GrPipeline & pipeline,GrGpu * gpu)195 bool GrProgramDesc::Build(GrProgramDesc* desc,
196                           GrPixelConfig config,
197                           const GrPrimitiveProcessor& primProc,
198                           bool hasPointSize,
199                           const GrPipeline& pipeline,
200                           GrGpu* gpu) {
201     // The descriptor is used as a cache key. Thus when a field of the
202     // descriptor will not affect program generation (because of the attribute
203     // bindings in use or other descriptor field settings) it should be set
204     // to a canonical value to avoid duplicate programs with different keys.
205 
206     const GrShaderCaps& shaderCaps = *gpu->caps()->shaderCaps();
207 
208     GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
209     // Make room for everything up to the effect keys.
210     desc->key().reset();
211     desc->key().push_back_n(kProcessorKeysOffset);
212 
213     GrProcessorKeyBuilder b(&desc->key());
214 
215     primProc.getGLSLProcessorKey(shaderCaps, &b);
216     primProc.getAttributeKey(&b);
217     if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
218         desc->key().reset();
219         return false;
220     }
221 
222     for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
223         const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
224         if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu, shaderCaps, &b)) {
225             desc->key().reset();
226             return false;
227         }
228     }
229 
230     const GrXferProcessor& xp = pipeline.getXferProcessor();
231     const GrSurfaceOrigin* originIfDstTexture = nullptr;
232     GrSurfaceOrigin origin;
233     if (pipeline.dstTextureProxy()) {
234         origin = pipeline.dstTextureProxy()->origin();
235         originIfDstTexture = &origin;
236     }
237     xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
238     if (!gen_meta_key(xp, shaderCaps, &b)) {
239         desc->key().reset();
240         return false;
241     }
242 
243     // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
244     // Because header is a pointer into the dynamic array, we can't push any new data into the key
245     // below here.
246     KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
247 
248     // make sure any padding in the header is zeroed.
249     memset(header, 0, kHeaderSize);
250     header->fOutputSwizzle = shaderCaps.configOutputSwizzle(config).asKey();
251     header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
252     header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
253     header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
254     // Fail if the client requested more processors than the key can fit.
255     if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
256         header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
257         return false;
258     }
259     header->fHasPointSize = hasPointSize ? 1 : 0;
260     return true;
261 }
262