OLD | NEW |
| (Empty) |
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 "GrGLSLProgramDesc.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 | |
17 static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShader
Flags 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 << kSamplerTypeKe
yBits)); | |
25 | |
26 SkASSERT((int)samplerType >= kFirstSamplerType && (int)samplerType <= kLastS
amplerType); | |
27 int samplerTypeKey = samplerType - kFirstSamplerType; | |
28 | |
29 return SkToU16(caps.configTextureSwizzle(config).asKey() | | |
30 (samplerTypeKey << 8) | | |
31 (caps.samplerPrecision(config, visibility) << (8 + kSamplerTy
peKeyBits))); | |
32 } | |
33 | |
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.getVisibi
lity(), 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 bec
ause 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 ef
fect's class ID | |
66 * which must be different for every GrProcessor subclass. It can fail if an eff
ect 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 */ | |
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 | |
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), glslCap
s, b)) { | |
97 return false; | |
98 } | |
99 } | |
100 | |
101 fp.getGLSLProcessorKey(glslCaps, b); | |
102 | |
103 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransform
s(), | |
104 fp.numTransformsE
xclChildren()), b); | |
105 } | |
106 | |
107 bool GrGLSLProgramDescBuilder::Build(GrProgramDesc* desc, | |
108 const GrPrimitiveProcessor& primProc, | |
109 const GrPipeline& pipeline, | |
110 const GrGLSLCaps& glslCaps) { | |
111 // The descriptor is used as a cache key. Thus when a field of the | |
112 // descriptor will not affect program generation (because of the attribute | |
113 // bindings in use or other descriptor field settings) it should be set | |
114 // to a canonical value to avoid duplicate programs with different keys. | |
115 | |
116 GrGLSLProgramDesc* glDesc = (GrGLSLProgramDesc*)desc; | |
117 | |
118 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); | |
119 // Make room for everything up to the effect keys. | |
120 glDesc->key().reset(); | |
121 glDesc->key().push_back_n(kProcessorKeysOffset); | |
122 | |
123 GrProcessorKeyBuilder b(&glDesc->key()); | |
124 | |
125 primProc.getGLSLProcessorKey(glslCaps, &b); | |
126 if (!gen_meta_key(primProc, glslCaps, 0, &b)) { | |
127 glDesc->key().reset(); | |
128 return false; | |
129 } | |
130 GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures()
; | |
131 | |
132 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { | |
133 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); | |
134 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) { | |
135 glDesc->key().reset(); | |
136 return false; | |
137 } | |
138 requiredFeatures |= fp.requiredFeatures(); | |
139 } | |
140 | |
141 const GrXferProcessor& xp = pipeline.getXferProcessor(); | |
142 xp.getGLSLProcessorKey(glslCaps, &b); | |
143 if (!gen_meta_key(xp, glslCaps, 0, &b)) { | |
144 glDesc->key().reset(); | |
145 return false; | |
146 } | |
147 requiredFeatures |= xp.requiredFeatures(); | |
148 | |
149 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- | |
150 // Because header is a pointer into the dynamic array, we can't push any new
data into the key | |
151 // below here. | |
152 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); | |
153 | |
154 // make sure any padding in the header is zeroed. | |
155 memset(header, 0, kHeaderSize); | |
156 | |
157 GrRenderTarget* rt = pipeline.getRenderTarget(); | |
158 | |
159 if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature | | |
160 GrProcessor::kSampleLocations_RequiredFeature)) { | |
161 header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOr
igin(rt->origin()); | |
162 } else { | |
163 header->fSurfaceOriginKey = 0; | |
164 } | |
165 | |
166 if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) { | |
167 SkASSERT(pipeline.isHWAntialiasState()); | |
168 header->fSamplePatternKey = | |
169 rt->renderTargetPriv().getMultisampleSpecs(pipeline.getStencil()).fU
niqueID; | |
170 } else { | |
171 header->fSamplePatternKey = 0; | |
172 } | |
173 | |
174 header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey(); | |
175 | |
176 if (pipeline.ignoresCoverage()) { | |
177 header->fIgnoresCoverage = 1; | |
178 } else { | |
179 header->fIgnoresCoverage = 0; | |
180 } | |
181 | |
182 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); | |
183 header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); | |
184 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); | |
185 glDesc->finalize(); | |
186 return true; | |
187 } | |
OLD | NEW |