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

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: cleaup Created 6 years, 1 month 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
« no previous file with comments | « src/gpu/gl/GrGLProgramDesc.h ('k') | src/gpu/gl/GrGpuGL.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType); 261 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
261 if (gpu->caps()->pathRenderingSupport() && isPathRendering) { 262 if (gpu->caps()->pathRenderingSupport() && isPathRendering) {
262 header->fUseNvpr = true; 263 header->fUseNvpr = true;
263 SkASSERT(!optState.hasGeometryProcessor()); 264 SkASSERT(!optState.hasGeometryProcessor());
264 } else { 265 } else {
265 header->fUseNvpr = false; 266 header->fUseNvpr = false;
266 } 267 }
267 268
268 bool hasUniformColor = inputColorIsUsed && 269 bool hasUniformColor = inputColorIsUsed &&
269 (isPathRendering || !optState.hasColorVertexAttribute ()); 270 (isPathRendering || !descInfo.hasColorVertexAttribute ());
270 271
271 bool hasUniformCoverage = inputCoverageIsUsed && 272 bool hasUniformCoverage = inputCoverageIsUsed &&
272 (isPathRendering || !optState.hasCoverageVertexAtt ribute()); 273 (isPathRendering || !descInfo.hasCoverageVertexAtt ribute());
273 274
274 if (!inputColorIsUsed) { 275 if (!inputColorIsUsed) {
275 header->fColorInput = kAllOnes_ColorInput; 276 header->fColorInput = GrProgramDesc::kAllOnes_ColorInput;
276 } else if (hasUniformColor) { 277 } else if (hasUniformColor) {
277 header->fColorInput = kUniform_ColorInput; 278 header->fColorInput = GrProgramDesc::kUniform_ColorInput;
278 } else { 279 } else {
279 header->fColorInput = kAttribute_ColorInput; 280 header->fColorInput = GrProgramDesc::kAttribute_ColorInput;
280 SkASSERT(!header->fUseNvpr); 281 SkASSERT(!header->fUseNvpr);
281 } 282 }
282 283
283 bool covIsSolidWhite = !optState.hasCoverageVertexAttribute() && 284 bool covIsSolidWhite = !descInfo.hasCoverageVertexAttribute() &&
284 0xffffffff == optState.getCoverageColor(); 285 0xffffffff == optState.getCoverageColor();
285 286
286 if (covIsSolidWhite || !inputCoverageIsUsed) { 287 if (covIsSolidWhite || !inputCoverageIsUsed) {
287 header->fCoverageInput = kAllOnes_ColorInput; 288 header->fCoverageInput = GrProgramDesc::kAllOnes_ColorInput;
288 } else if (hasUniformCoverage) { 289 } else if (hasUniformCoverage) {
289 header->fCoverageInput = kUniform_ColorInput; 290 header->fCoverageInput = GrProgramDesc::kUniform_ColorInput;
290 } else { 291 } else {
291 header->fCoverageInput = kAttribute_ColorInput; 292 header->fCoverageInput = GrProgramDesc::kAttribute_ColorInput;
292 SkASSERT(!header->fUseNvpr); 293 SkASSERT(!header->fUseNvpr);
293 } 294 }
294 295
295 if (optState.readsDst()) { 296 if (descInfo.fReadsDst) {
296 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport()); 297 SkASSERT(dstCopy || gpu->caps()->dstReadInShaderSupport());
297 const GrTexture* dstCopyTexture = NULL; 298 const GrTexture* dstCopyTexture = NULL;
298 if (dstCopy) { 299 if (dstCopy) {
299 dstCopyTexture = dstCopy->texture(); 300 dstCopyTexture = dstCopy->texture();
300 } 301 }
301 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTe xture, 302 header->fDstReadKey = GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTe xture,
302 gpu->glCa ps()); 303 gpu->glCa ps());
303 SkASSERT(0 != header->fDstReadKey); 304 SkASSERT(0 != header->fDstReadKey);
304 } else { 305 } else {
305 header->fDstReadKey = 0; 306 header->fDstReadKey = 0;
306 } 307 }
307 308
308 if (optState.readsFragPosition()) { 309 if (descInfo.fReadsFragPosition) {
309 header->fFragPosKey = 310 header->fFragPosKey =
310 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRe nderTarget(), 311 GrGLFragmentShaderBuilder::KeyForFragmentPosition(optState.getRe nderTarget(),
311 gpu->glCaps()) ; 312 gpu->glCaps()) ;
312 } else { 313 } else {
313 header->fFragPosKey = 0; 314 header->fFragPosKey = 0;
314 } 315 }
315 316
316 // Record attribute indices 317 // Record attribute indices
317 header->fPositionAttributeIndex = optState.positionAttributeIndex(); 318 header->fPositionAttributeIndex = descInfo.positionAttributeIndex();
318 header->fLocalCoordAttributeIndex = optState.localCoordAttributeIndex(); 319 header->fLocalCoordAttributeIndex = descInfo.localCoordAttributeIndex();
319 320
320 // For constant color and coverage we need an attribute with an index beyond those already set 321 // For constant color and coverage we need an attribute with an index beyond those already set
321 int availableAttributeIndex = optState.getVertexAttribCount(); 322 int availableAttributeIndex = optState.getVertexAttribCount();
322 if (optState.hasColorVertexAttribute()) { 323 if (descInfo.hasColorVertexAttribute()) {
323 header->fColorAttributeIndex = optState.colorVertexAttributeIndex(); 324 header->fColorAttributeIndex = descInfo.colorVertexAttributeIndex();
324 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fColorInput) { 325 } else if (GrProgramDesc::kAttribute_ColorInput == header->fColorInput) {
325 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); 326 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
326 header->fColorAttributeIndex = availableAttributeIndex; 327 header->fColorAttributeIndex = availableAttributeIndex;
327 availableAttributeIndex++; 328 availableAttributeIndex++;
328 } else { 329 } else {
329 header->fColorAttributeIndex = -1; 330 header->fColorAttributeIndex = -1;
330 } 331 }
331 332
332 if (optState.hasCoverageVertexAttribute()) { 333 if (descInfo.hasCoverageVertexAttribute()) {
333 header->fCoverageAttributeIndex = optState.coverageVertexAttributeIndex( ); 334 header->fCoverageAttributeIndex = descInfo.coverageVertexAttributeIndex( );
334 } else if (GrGLProgramDesc::kAttribute_ColorInput == header->fCoverageInput) { 335 } else if (GrProgramDesc::kAttribute_ColorInput == header->fCoverageInput) {
335 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt); 336 SkASSERT(availableAttributeIndex < GrDrawState::kMaxVertexAttribCnt);
336 header->fCoverageAttributeIndex = availableAttributeIndex; 337 header->fCoverageAttributeIndex = availableAttributeIndex;
337 } else { 338 } else {
338 header->fCoverageAttributeIndex = -1; 339 header->fCoverageAttributeIndex = -1;
339 } 340 }
340 341
341 header->fPrimaryOutputType = optState.getPrimaryOutputType(); 342 header->fPrimaryOutputType = descInfo.fPrimaryOutputType;
342 header->fSecondaryOutputType = optState.getSecondaryOutputType(); 343 header->fSecondaryOutputType = descInfo.fSecondaryOutputType;
343 344
344 header->fColorEffectCnt = optState.numColorStages(); 345 header->fColorEffectCnt = optState.numColorStages();
345 header->fCoverageEffectCnt = optState.numCoverageStages(); 346 header->fCoverageEffectCnt = optState.numCoverageStages();
346 desc->finalize(); 347 desc->finalize();
347 return true; 348 return true;
348 } 349 }
349
350 void GrGLProgramDesc::finalize() {
351 int keyLength = fKey.count();
352 SkASSERT(0 == (keyLength % 4));
353 *this->atOffset<uint32_t, kLengthOffset>() = SkToU32(keyLength);
354
355 uint32_t* checksum = this->atOffset<uint32_t, kChecksumOffset>();
356 *checksum = 0;
357 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), k eyLength);
358 }
359
360 GrGLProgramDesc& GrGLProgramDesc::operator= (const GrGLProgramDesc& other) {
361 size_t keyLength = other.keyLength();
362 fKey.reset(keyLength);
363 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
364 return *this;
365 }
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLProgramDesc.h ('k') | src/gpu/gl/GrGpuGL.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698