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 | processorKeySize) & kMetaKeyInval idMask) { |
142 return false; | 137 return false; |
143 } | 138 } |
144 | 139 |
145 uint32_t* key = b->add32n(2); | 140 uint32_t* key = b->add32n(2); |
146 key[0] = (textureKey << 16 | transformKey); | 141 key[0] = (textureKey << 16 | transformKey); |
147 key[1] = (classID << 16); | 142 key[1] = (classID << 16 | processorKeySize); |
bsalomon
2014/11/26 15:25:06
fail if key size and classID overlap?
egdaniel
2014/11/26 15:47:50
In practice the keys would have never overlapped,
| |
148 return true; | 143 return true; |
149 } | 144 } |
150 | 145 |
151 struct GeometryProcessorKeyBuilder { | |
bsalomon
2014/11/26 15:25:06
good riddance.
| |
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, | 146 bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState, |
204 const GrProgramDesc::DescInfo& descInfo, | 147 const GrProgramDesc::DescInfo& descInfo, |
205 GrGpu::DrawType drawType, | 148 GrGpu::DrawType drawType, |
206 GrGpuGL* gpu, | 149 GrGpuGL* gpu, |
207 GrProgramDesc* desc) { | 150 GrProgramDesc* desc) { |
208 bool inputColorIsUsed = descInfo.fInputColorIsUsed; | 151 bool inputColorIsUsed = descInfo.fInputColorIsUsed; |
209 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed; | 152 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed; |
210 | 153 |
211 // The descriptor is used as a cache key. Thus when a field of the | 154 // 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 | 155 // descriptor will not affect program generation (because of the attribute |
213 // bindings in use or other descriptor field settings) it should be set | 156 // bindings in use or other descriptor field settings) it should be set |
214 // to a canonical value to avoid duplicate programs with different keys. | 157 // to a canonical value to avoid duplicate programs with different keys. |
215 | 158 |
216 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib; | 159 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib; |
217 | 160 |
218 int numStages = optState.numTotalStages(); | 161 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
219 | 162 // 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(); | 163 desc->fKey.reset(); |
223 desc->fKey.push_back_n(kProcessorKeyOffsetsAndLengthOffset + 2 * sizeof(uint 16_t) * numStages); | 164 desc->fKey.push_back_n(kProcessorKeysOffset); |
224 | |
225 int offsetAndSizeIndex = 0; | |
226 | 165 |
227 // We can only have one effect which touches the vertex shader | 166 // We can only have one effect which touches the vertex shader |
228 if (optState.hasGeometryProcessor()) { | 167 if (optState.hasGeometryProcessor()) { |
229 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getG eometryProcessor(), | 168 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); |
230 gpu->glCaps(), | 169 GrProcessorKeyBuilder b(&desc->fKey); |
231 false, | 170 if (!get_meta_key(gp, gpu->glCaps(), 0, gen_attrib_key(gp), &b)) { |
232 desc, | 171 desc->fKey.reset(); |
233 &offsetAndSize Index)) { | |
234 return false; | 172 return false; |
235 } | 173 } |
236 } | 174 } |
237 | 175 |
238 for (int s = 0; s < optState.numFragmentStages(); ++s) { | 176 for (int s = 0; s < optState.numFragmentStages(); ++s) { |
239 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFr agmentStage(s), | 177 const GrPendingFragmentStage& fps = optState.getFragmentStage(s); |
240 gpu->glCaps(), | 178 GrProcessorKeyBuilder b(&desc->fKey); |
241 requiresLocalC oordAttrib, | 179 if (!get_meta_key(*fps.getProcessor(), gpu->glCaps(), |
242 desc, | 180 gen_transform_key(fps, requiresLocalCoordAttrib), 0, &b )) { |
243 &offsetAndSize Index)) { | 181 desc->fKey.reset(); |
244 return false; | 182 return false; |
245 } | 183 } |
246 } | 184 } |
247 | 185 |
248 // --------DO NOT MOVE HEADER ABOVE THIS LINE------------------------------- ------------------- | 186 // --------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 | 187 // Because header is a pointer into the dynamic array, we can't push any new data into the key |
250 // below here. | 188 // below here. |
251 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>(); | 189 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>(); |
252 | 190 |
253 // make sure any padding in the header is zeroed. | 191 // make sure any padding in the header is zeroed. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 } | 278 } |
341 | 279 |
342 header->fPrimaryOutputType = descInfo.fPrimaryOutputType; | 280 header->fPrimaryOutputType = descInfo.fPrimaryOutputType; |
343 header->fSecondaryOutputType = descInfo.fSecondaryOutputType; | 281 header->fSecondaryOutputType = descInfo.fSecondaryOutputType; |
344 | 282 |
345 header->fColorEffectCnt = optState.numColorStages(); | 283 header->fColorEffectCnt = optState.numColorStages(); |
346 header->fCoverageEffectCnt = optState.numCoverageStages(); | 284 header->fCoverageEffectCnt = optState.numCoverageStages(); |
347 desc->finalize(); | 285 desc->finalize(); |
348 return true; | 286 return true; |
349 } | 287 } |
OLD | NEW |