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