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