Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1030)

Side by Side Diff: src/gpu/gl/GrGLProgramDesc.cpp

Issue 674543004: OptState owns program descriptor (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: feedback inc Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 8
8 #include "gl/builders/GrGLFragmentShaderBuilder.h" 9 #include "GrGLProcessor.h"
9 #include "GrGLProgramDesc.h"
10 #include "GrBackendProcessorFactory.h" 10 #include "GrBackendProcessorFactory.h"
11 #include "GrProcessor.h" 11 #include "GrProcessor.h"
12 #include "GrGpuGL.h" 12 #include "GrGpuGL.h"
13 #include "GrOptDrawState.h" 13 #include "GrOptDrawState.h"
14
15 #include "SkChecksum.h" 14 #include "SkChecksum.h"
15 #include "gl/builders/GrGLFragmentShaderBuilder.h"
16 16
17 /** 17 /**
18 * The key for an individual coord transform is made up of a matrix type and a b it that 18 * The key for an individual coord transform is made up of a matrix type and a b it that
19 * indicates the source of the input coords. 19 * indicates the source of the input coords.
20 */ 20 */
21 enum { 21 enum {
22 kMatrixTypeKeyBits = 1, 22 kMatrixTypeKeyBits = 1,
23 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, 23 kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1,
24 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), 24 kPositionCoords_Flag = (1 << kMatrixTypeKeyBits),
25 kTransformKeyBits = kMatrixTypeKeyBits + 1, 25 kTransformKeyBits = kMatrixTypeKeyBits + 1,
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 uint16_t* keySize) { 169 uint16_t* keySize) {
170 /* 0 because no attribs on a fP */ 170 /* 0 because no attribs on a fP */
171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, us eLocalCoords), 0, 171 return get_meta_key(*fps.getProcessor(), caps, gen_transform_key(fps, us eLocalCoords), 0,
172 b, keySize); 172 b, keySize);
173 } 173 }
174 }; 174 };
175 175
176 176
177 template <class ProcessorKeyBuilder> 177 template <class ProcessorKeyBuilder>
178 bool 178 bool
179 GrGLProgramDesc::BuildStagedProcessorKey(const typename ProcessorKeyBuilder::Sta gedProcessor& stage, 179 GrGLProgramDescBuilder::BuildStagedProcessorKey(const typename ProcessorKeyBuild er::StagedProcessor& stage,
180 const GrGLCaps& caps, 180 const GrGLCaps& caps,
181 bool requiresLocalCoordAttrib, 181 bool requiresLocalCoordAttrib,
182 GrGLProgramDesc* desc, 182 GrProgramDesc* desc,
183 int* offsetAndSizeIndex) { 183 int* offsetAndSizeIndex) {
184 GrProcessorKeyBuilder b(&desc->fKey); 184 GrProcessorKeyBuilder b(&desc->fKey);
185 uint16_t processorKeySize; 185 uint16_t processorKeySize;
186 uint32_t processorOffset = desc->fKey.count(); 186 uint32_t processorOffset = desc->fKey.count();
187 if (processorOffset > SK_MaxU16 || 187 if (processorOffset > SK_MaxU16 ||
188 !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoor dAttrib, &b, 188 !ProcessorKeyBuilder::GetProcessorKey(stage, caps, requiresLocalCoor dAttrib, &b,
189 &processorKeySize)){ 189 &processorKeySize)){
190 desc->fKey.reset(); 190 desc->fKey.reset();
191 return false; 191 return false;
192 } 192 }
193 193
194 uint16_t* offsetAndSize = 194 uint16_t* offsetAndSize =
195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kEffectKeyOffsetsAn dLengthOffset + 195 reinterpret_cast<uint16_t*>(desc->fKey.begin() + kProcessorKeyOffset sAndLengthOffset +
196 *offsetAndSizeIndex * 2 * sizeof(uint16_ t)); 196 *offsetAndSizeIndex * 2 * sizeof(uint16_ t));
197 offsetAndSize[0] = SkToU16(processorOffset); 197 offsetAndSize[0] = SkToU16(processorOffset);
198 offsetAndSize[1] = processorKeySize; 198 offsetAndSize[1] = processorKeySize;
199 ++(*offsetAndSizeIndex); 199 ++(*offsetAndSizeIndex);
200 return true; 200 return true;
201 } 201 }
202 202
203 bool GrGLProgramDesc::Build(const GrOptDrawState& optState, 203 bool GrGLProgramDescBuilder::Build(const GrOptDrawState& optState,
204 const GrProgramDesc::DescInfo& descInfo,
204 GrGpu::DrawType drawType, 205 GrGpu::DrawType drawType,
205 GrGpuGL* gpu, 206 GrGpuGL* gpu,
206 const GrDeviceCoordTexture* dstCopy, 207 const GrDeviceCoordTexture* dstCopy,
207 GrGLProgramDesc* desc) { 208 GrProgramDesc* desc) {
208 bool inputColorIsUsed = optState.inputColorIsUsed(); 209 bool inputColorIsUsed = descInfo.fInputColorIsUsed;
209 bool inputCoverageIsUsed = optState.inputCoverageIsUsed(); 210 bool inputCoverageIsUsed = descInfo.fInputCoverageIsUsed;
210 211
211 // The descriptor is used as a cache key. Thus when a field of the 212 // 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 213 // descriptor will not affect program generation (because of the attribute
213 // bindings in use or other descriptor field settings) it should be set 214 // bindings in use or other descriptor field settings) it should be set
214 // to a canonical value to avoid duplicate programs with different keys. 215 // to a canonical value to avoid duplicate programs with different keys.
215 216
216 bool requiresLocalCoordAttrib = optState.requiresLocalCoordAttrib(); 217 bool requiresLocalCoordAttrib = descInfo.fRequiresLocalCoordAttrib;
217 218
218 int numStages = optState.numTotalStages(); 219 int numStages = optState.numTotalStages();
219 220
220 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t)); 221 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 // Make room for everything up to and including the array of offsets to effe ct keys.
222 desc->fKey.reset(); 223 desc->fKey.reset();
223 desc->fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_ t) * numStages); 224 desc->fKey.push_back_n(kProcessorKeyOffsetsAndLengthOffset + 2 * sizeof(uint 16_t) * numStages);
224 225
225 int offsetAndSizeIndex = 0; 226 int offsetAndSizeIndex = 0;
226 227
227 // We can only have one effect which touches the vertex shader 228 // We can only have one effect which touches the vertex shader
228 if (optState.hasGeometryProcessor()) { 229 if (optState.hasGeometryProcessor()) {
229 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getG eometryProcessor(), 230 if (!BuildStagedProcessorKey<GeometryProcessorKeyBuilder>(*optState.getG eometryProcessor(),
230 gpu->glCaps(), 231 gpu->glCaps(),
231 false, 232 false,
232 desc, 233 desc,
233 &offsetAndSize Index)) { 234 &offsetAndSize Index)) {
234 return false; 235 return false;
235 } 236 }
236 } 237 }
237 238
238 for (int s = 0; s < optState.numFragmentStages(); ++s) { 239 for (int s = 0; s < optState.numFragmentStages(); ++s) {
239 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFr agmentStage(s), 240 if (!BuildStagedProcessorKey<FragmentProcessorKeyBuilder>(optState.getFr agmentStage(s),
240 gpu->glCaps(), 241 gpu->glCaps(),
241 requiresLocalC oordAttrib, 242 requiresLocalC oordAttrib,
242 desc, 243 desc,
243 &offsetAndSize Index)) { 244 &offsetAndSize Index)) {
244 return false; 245 return false;
245 } 246 }
246 } 247 }
247 248
248 // --------DO NOT MOVE HEADER ABOVE THIS LINE------------------------------- ------------------- 249 // --------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 250 // Because header is a pointer into the dynamic array, we can't push any new data into the key
250 // below here. 251 // below here.
251 KeyHeader* header = desc->header(); 252 GLKeyHeader* header = desc->atOffset<GLKeyHeader, kHeaderOffset>();
252 253
253 // make sure any padding in the header is zeroed. 254 // make sure any padding in the header is zeroed.
254 memset(header, 0, kHeaderSize); 255 memset(header, 0, kHeaderSize);
255 256
256 header->fHasGeometryProcessor = optState.hasGeometryProcessor(); 257 header->fHasGeometryProcessor = optState.hasGeometryProcessor();
257 258
258 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType; 259 header->fEmitsPointSize = GrGpu::kDrawPoints_DrawType == drawType;
259 260
260 if (gpu->caps()->pathRenderingSupport() && 261 if (gpu->caps()->pathRenderingSupport() &&
261 GrGpu::IsPathRenderingDrawType(drawType) && 262 GrGpu::IsPathRenderingDrawType(drawType) &&
262 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::FixedFunct ion_TexturingMode) { 263 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::FixedFunct ion_TexturingMode) {
263 header->fUseFragShaderOnly = true; 264 header->fUseFragShaderOnly = true;
264 SkASSERT(!optState.hasGeometryProcessor()); 265 SkASSERT(!optState.hasGeometryProcessor());
265 } else { 266 } else {
266 header->fUseFragShaderOnly = false; 267 header->fUseFragShaderOnly = false;
267 } 268 }
268 269
269 bool defaultToUniformInputs = GrGpu::IsPathRenderingDrawType(drawType) || 270 bool defaultToUniformInputs = GrGpu::IsPathRenderingDrawType(drawType) ||
270 GR_GL_NO_CONSTANT_ATTRIBUTES; 271 GR_GL_NO_CONSTANT_ATTRIBUTES;
271 272
272 if (!inputColorIsUsed) { 273 if (!inputColorIsUsed) {
273 header->fColorInput = kAllOnes_ColorInput; 274 header->fColorInput = GrProgramDesc::kAllOnes_ColorInput;
274 } else if (defaultToUniformInputs && !optState.hasColorVertexAttribute()) { 275 } else if (defaultToUniformInputs && !descInfo.hasColorVertexAttribute()) {
275 header->fColorInput = kUniform_ColorInput; 276 header->fColorInput = GrProgramDesc::kUniform_ColorInput;
276 } else { 277 } else {
277 header->fColorInput = kAttribute_ColorInput; 278 header->fColorInput = GrProgramDesc::kAttribute_ColorInput;
278 SkASSERT(!header->fUseFragShaderOnly); 279 SkASSERT(!header->fUseFragShaderOnly);
279 } 280 }
280 281
281 bool covIsSolidWhite = !optState.hasCoverageVertexAttribute() && 282 bool covIsSolidWhite = !descInfo.hasCoverageVertexAttribute() &&
282 0xffffffff == optState.getCoverageColor(); 283 0xffffffff == optState.getCoverageColor();
283 284
284 if (covIsSolidWhite || !inputCoverageIsUsed) { 285 if (covIsSolidWhite || !inputCoverageIsUsed) {
285 header->fCoverageInput = kAllOnes_ColorInput; 286 header->fCoverageInput = GrProgramDesc::kAllOnes_ColorInput;
286 } else if (defaultToUniformInputs && !optState.hasCoverageVertexAttribute()) { 287 } else if (defaultToUniformInputs && !descInfo.hasCoverageVertexAttribute()) {
287 header->fCoverageInput = kUniform_ColorInput; 288 header->fCoverageInput = GrProgramDesc::kUniform_ColorInput;
288 } else { 289 } else {
289 header->fCoverageInput = kAttribute_ColorInput; 290 header->fCoverageInput = GrProgramDesc::kAttribute_ColorInput;
290 SkASSERT(!header->fUseFragShaderOnly); 291 SkASSERT(!header->fUseFragShaderOnly);
291 } 292 }
292 293
293 if (optState.readsDst()) { 294 if (descInfo.fReadsDst) {
294 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport()); 295 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
295 const GrTexture* dstCopyTexture = NULL; 296 const GrTexture* dstCopyTexture = NULL;
296 if (dstCopy) { 297 if (dstCopy) {
297 dstCopyTexture = dstCopy->texture(); 298 dstCopyTexture = dstCopy->texture();
298 } 299 }
299 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTe xture, 300 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTe xture,
300 gpu->glCa ps()); 301 gpu->glCa ps());
301 SkASSERT(0 != header->fDstReadKey); 302 SkASSERT(0 != header->fDstReadKey);
302 } else { 303 } else {
303 header->fDstReadKey = 0; 304 header->fDstReadKey = 0;
304 } 305 }
305 306
306 if (optState.readsFragPosition()) { 307 if (descInfo.fReadsFragPosition) {
307 header->fFragPosKey = 308 header->fFragPosKey =
308 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRe nderTarget(), 309 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRe nderTarget(),
309 gpu->glCaps()) ; 310 gpu->glCaps()) ;
310 } else { 311 } else {
311 header->fFragPosKey = 0; 312 header->fFragPosKey = 0;
312 } 313 }
313 314
314 // Record attribute indices 315 // Record attribute indices
315 header->fPositionAttributeIndex = optState.positionAttributeIndex(); 316 header->fPositionAttributeIndex = descInfo.positionAttributeIndex();
316 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex(); 317 header->fLocalCoordAttributeIndex = descInfo.localCoordAttributeIndex();
317 318
318 // For constant color and coverage we need an attribute with an index beyond those already set 319 // For constant color and coverage we need an attribute with an index beyond those already set
319 int availableAttributeIndex = optState.getVertexAttribCount(); 320 int availableAttributeIndex = optState.getVertexAttribCount();
320 if (optState.hasColorVertexAttribute()) { 321 if (descInfo.hasColorVertexAttribute()) {
321 header->fColorAttributeIndex = optState.colorVertexAttributeIndex(); 322 header->fColorAttributeIndex = descInfo.colorVertexAttributeIndex();
322 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { 323 } else if (GrProgramDesc::kAttribute_ColorInput == header->fColorInput) {
323 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); 324 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
324 header->fColorAttributeIndex = availableAttributeIndex; 325 header->fColorAttributeIndex = availableAttributeIndex;
325 availableAttributeIndex++; 326 availableAttributeIndex++;
326 } else { 327 } else {
327 header->fColorAttributeIndex = -1; 328 header->fColorAttributeIndex = -1;
328 } 329 }
329 330
330 if (optState.hasCoverageVertexAttribute()) { 331 if (descInfo.hasCoverageVertexAttribute()) {
331 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex( ); 332 header->fCoverageAttributeIndex = descInfo.coverageVertexAttributeIndex( );
332 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { 333 } else if (GrProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
333 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); 334 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
334 header->fCoverageAttributeIndex = availableAttributeIndex; 335 header->fCoverageAttributeIndex = availableAttributeIndex;
335 } else { 336 } else {
336 header->fCoverageAttributeIndex = -1; 337 header->fCoverageAttributeIndex = -1;
337 } 338 }
338 339
339 header->fPrimaryOutputType = optState.getPrimaryOutputType(); 340 header->fPrimaryOutputType = descInfo.fPrimaryOutputType;
340 header->fSecondaryOutputType = optState.getSecondaryOutputType(); 341 header->fSecondaryOutputType = descInfo.fSecondaryOutputType;
341 342
342 header->fColorEffectCnt = optState.numColorStages(); 343 header->fColorEffectCnt = optState.numColorStages();
343 header->fCoverageEffectCnt = optState.numCoverageStages(); 344 header->fCoverageEffectCnt = optState.numCoverageStages();
344 desc->finalize(); 345 desc->finalize();
345 return true; 346 return true;
346 } 347 }
347
348 void GrGLProgramDesc::finalize() {
349 int keyLength = fKey.count();
350 SkASSERT(0 == (keyLength % 4));
351 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
352
353 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
354 *checksum = 0;
355 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k eyLength);
356 }
357
358 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
359 size_t keyLength = other.keyLength();
360 fKey.reset(keyLength);
361 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
362 return *this;
363 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698