OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "GrGLProgramDesc.h" | 7 #include "GrGLProgramDesc.h" |
8 | 8 |
9 #include "GrGLProcessor.h" | 9 #include "GrGLProcessor.h" |
10 #include "GrBackendProcessorFactory.h" | 10 #include "GrBackendProcessorFactory.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for | 115 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for |
116 * every effect using this function. It is also responsible for inserting the ef
fect's class ID | 116 * every effect using this function. It is also responsible for inserting the ef
fect's class ID |
117 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many | 117 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many |
118 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, bot
h FPs and GPs share | 118 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, bot
h FPs and GPs share |
119 * this function because it is hairy, though FPs do not have attribs, and GPs do
not have transforms | 119 * this function because it is hairy, though FPs do not have attribs, and GPs do
not have transforms |
120 */ | 120 */ |
121 static bool get_meta_key(const GrProcessor& proc, | 121 static bool get_meta_key(const GrProcessor& proc, |
122 const GrGLCaps& caps, | 122 const GrGLCaps& caps, |
123 uint32_t transformKey, | 123 uint32_t transformKey, |
124 uint32_t attribKey, | 124 uint32_t attribKey, |
125 GrProcessorKeyBuilder* b, | 125 GrProcessorKeyBuilder* b) { |
126 uint16_t* processorKeySize) { | |
127 const GrBackendProcessorFactory& factory = proc.getFactory(); | 126 const GrBackendProcessorFactory& factory = proc.getFactory(); |
128 factory.getGLProcessorKey(proc, caps, b); | 127 factory.getGLProcessorKey(proc, caps, b); |
129 size_t size = b->size(); | 128 |
130 if (size > SK_MaxU16) { | 129 size_t processorKeySize = b->size(); |
131 *processorKeySize = 0; // suppresses a warning. | |
132 return false; | |
133 } | |
134 *processorKeySize = SkToU16(size); | |
135 uint32_t textureKey = gen_texture_key(proc, caps); | 130 uint32_t textureKey = gen_texture_key(proc, caps); |
136 uint32_t classID = proc.getFactory().classID(); | 131 uint32_t classID = proc.getFactory().classID(); |
137 | 132 |
138 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they | 133 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they |
139 // don't fit. | 134 // don't fit. |
140 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | 135 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
141 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { | 136 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { |
142 return false; | 137 return false; |
143 } | 138 } |
| 139 if (processorKeySize > SK_MaxU16) { |
| 140 return false; |
| 141 } |
144 | 142 |
145 uint32_t* key = b->add32n(2); | 143 uint32_t* key = b->add32n(2); |
146 key[0] = (textureKey << 16 | transformKey); | 144 key[0] = (textureKey << 16 | transformKey); |
147 key[1] = (classID << 16); | 145 key[1] = (classID << 16 | SkToU16(processorKeySize)); |
148 return true; | 146 return true; |
149 } | 147 } |
150 | 148 |
151 struct GeometryProcessorKeyBuilder { | |
152 typedef GrGeometryProcessor StagedProcessor; | |
153 static bool GetProcessorKey(const GrGeometryProcessor& gp, | |
154 const GrGLCaps& caps, | |
155 bool, | |
156 GrProcessorKeyBuilder* b, | |
157 uint16_t* keySize) { | |
158 /* 0 because no transforms on a GP */ | |
159 return get_meta_key(gp, caps, 0, gen_attrib_key(gp), b, keySize); | |
160 } | |
161 }; | |
162 | |
163 struct FragmentProcessorKeyBuilder { | |
164 typedef GrPendingFragmentStage StagedProcessor; | |
165 static bool GetProcessorKey(const GrPendingFragmentStage& fps, | |
166 const GrGLCaps& caps, | |
167 bool useLocalCoords, | |
168 GrProcessorKeyBuilder* b, | |
169 uint16_t* keySize) { | |
170 /* 0 because no attribs on a fP */ | |
171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, us
eLocalCoords), 0, | |
172 b, keySize); | |
173 } | |
174 }; | |
175 | |
176 | |
177 template <class ProcessorKeyBuilder> | |
178 bool GrGLProgramDescBuilder::BuildStagedProcessorKey( | |
179 const typename ProcessorKeyBuilder::StagedProcessor& stage, | |
180 const GrGLCaps& caps, | |
181 bool requiresLocalCoordAttrib, | |
182 GrProgramDesc* desc, | |
183 int* offsetAndSizeIndex) { | |
184 GrProcessorKeyBuilder b(&desc->fKey); | |
185 uint16_t processorKeySize; | |
186 uint32_t processorOffset = desc->fKey.count(); | |
187 if (processorOffset > SK_MaxU16 || | |
188 !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoor
dAttrib, &b, | |
189 &processorKeySize)){ | |
190 desc->fKey.reset(); | |
191 return false; | |
192 } | |
193 | |
194 uint16_t* offsetAndSize = | |
195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kProcessorKeyOffset
sAndLengthOffset + | |
196 *offsetAndSizeIndex * 2 * sizeof(uint16_
t)); | |
197 offsetAndSize[0] = SkToU16(processorOffset); | |
198 offsetAndSize[1] = processorKeySize; | |
199 ++(*offsetAndSizeIndex); | |
200 return true; | |
201 } | |
202 | |
203 bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState, | 149 bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState, |
204 const GrProgramDesc::DescInfo& descInfo, | 150 const GrProgramDesc::DescInfo& descInfo, |
205 GrGpu::DrawType drawType, | 151 GrGpu::DrawType drawType, |
206 GrGpuGL* gpu, | 152 GrGpuGL* gpu, |
207 GrProgramDesc* desc) { | 153 GrProgramDesc* desc) { |
208 bool inputColorIsUsed = descInfo.fInputColorIsUsed; | 154 bool inputColorIsUsed = descInfo.fInputColorIsUsed; |
209 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed; | 155 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed; |
210 | 156 |
211 // The descriptor is used as a cache key. Thus when a field of the | 157 // The descriptor is used as a cache key. Thus when a field of the |
212 // descriptor will not affect program generation (because of the attribute | 158 // descriptor will not affect program generation (because of the attribute |
213 // bindings in use or other descriptor field settings) it should be set | 159 // bindings in use or other descriptor field settings) it should be set |
214 // to a canonical value to avoid duplicate programs with different keys. | 160 // to a canonical value to avoid duplicate programs with different keys. |
215 | 161 |
216 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib; | 162 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib; |
217 | 163 |
218 int numStages = optState.numTotalStages(); | 164 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
219 | 165 // Make room for everything up to the effect keys. |
220 GR_STATIC_ASSERT(0 == kProcessorKeyOffsetsAndLengthOffset % sizeof(uint32_t)
); | |
221 // Make room for everything up to and including the array of offsets to effe
ct keys. | |
222 desc->fKey.reset(); | 166 desc->fKey.reset(); |
223 desc->fKey.push_back_n(kProcessorKeyOffsetsAndLengthOffset + 2 * sizeof(uint
16_t) * numStages); | 167 desc->fKey.push_back_n(kProcessorKeysOffset); |
224 | |
225 int offsetAndSizeIndex = 0; | |
226 | 168 |
227 // We can only have one effect which touches the vertex shader | 169 // We can only have one effect which touches the vertex shader |
228 if (optState.hasGeometryProcessor()) { | 170 if (optState.hasGeometryProcessor()) { |
229 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getG
eometryProcessor(), | 171 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); |
230 gpu->glCaps(), | 172 GrProcessorKeyBuilder b(&desc->fKey); |
231 false, | 173 if (!get_meta_key(gp, gpu->glCaps(), 0, gen_attrib_key(gp), &b)) { |
232 desc, | 174 desc->fKey.reset(); |
233 &offsetAndSize
Index)) { | |
234 return false; | 175 return false; |
235 } | 176 } |
236 } | 177 } |
237 | 178 |
238 for (int s = 0; s < optState.numFragmentStages(); ++s) { | 179 for (int s = 0; s < optState.numFragmentStages(); ++s) { |
239 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFr
agmentStage(s), | 180 const GrPendingFragmentStage& fps = optState.getFragmentStage(s); |
240 gpu->glCaps(), | 181 GrProcessorKeyBuilder b(&desc->fKey); |
241 requiresLocalC
oordAttrib, | 182 if (!get_meta_key(*fps.getProcessor(), gpu->glCaps(), |
242 desc, | 183 gen_transform_key(fps, requiresLocalCoordAttrib), 0, &b
)) { |
243 &offsetAndSize
Index)) { | 184 desc->fKey.reset(); |
244 return false; | 185 return false; |
245 } | 186 } |
246 } | 187 } |
247 | 188 |
248 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- | 189 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- |
249 // Because header is a pointer into the dynamic array, we can't push any new
data into the key | 190 // Because header is a pointer into the dynamic array, we can't push any new
data into the key |
250 // below here. | 191 // below here. |
251 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>(); | 192 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>(); |
252 | 193 |
253 // make sure any padding in the header is zeroed. | 194 // make sure any padding in the header is zeroed. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } | 281 } |
341 | 282 |
342 header->fPrimaryOutputType = descInfo.fPrimaryOutputType; | 283 header->fPrimaryOutputType = descInfo.fPrimaryOutputType; |
343 header->fSecondaryOutputType = descInfo.fSecondaryOutputType; | 284 header->fSecondaryOutputType = descInfo.fSecondaryOutputType; |
344 | 285 |
345 header->fColorEffectCnt = optState.numColorStages(); | 286 header->fColorEffectCnt = optState.numColorStages(); |
346 header->fCoverageEffectCnt = optState.numCoverageStages(); | 287 header->fCoverageEffectCnt = optState.numCoverageStages(); |
347 desc->finalize(); | 288 desc->finalize(); |
348 return true; | 289 return true; |
349 } | 290 } |
OLD | NEW |