blob: 3d53842e43ae2cab3a9ddaee0062c353c25593db [file] [log] [blame]
joshualitt79f8fae2014-10-28 17:59:26 -07001/*
2 * Copyright 2014 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
8#ifndef GrProgramDesc_DEFINED
9#define GrProgramDesc_DEFINED
10
joshualitt79f8fae2014-10-28 17:59:26 -070011#include "GrColor.h"
12#include "GrTypesPriv.h"
13#include "SkChecksum.h"
14
15class GrGpuGL;
16
17/** This class describes a program to generate. It also serves as a program cache key. Very little
18 of this is GL-specific. The GL-specific parts could be factored out into a subclass. */
19class GrProgramDesc {
20public:
21 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
22 GrProgramDesc() {}
23
24 // Returns this as a uint32_t array to be used as a key in the program cache.
25 const uint32_t* asKey() const {
26 return reinterpret_cast<const uint32_t*>(fKey.begin());
27 }
28
29 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
30 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
31 // keys and thus the memcmp will exit early if the keys are of different lengths.
32 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
33
34 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
35 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
36
37 GrProgramDesc& operator= (const GrProgramDesc& other) {
38 size_t keyLength = other.keyLength();
39 fKey.reset(keyLength);
40 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
41 return *this;
42 }
43
44 bool operator== (const GrProgramDesc& other) const {
45 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
46 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
47 }
48
49 bool operator!= (const GrProgramDesc& other) const {
50 return !(*this == other);
51 }
52
53 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
54 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
55 }
56
57
joshualitt79f8fae2014-10-28 17:59:26 -070058 // Specifies where the initial color comes from before the stages are applied.
59 enum ColorInput {
60 kAllOnes_ColorInput,
61 kAttribute_ColorInput,
62 kUniform_ColorInput,
63
64 kColorInputCnt
65 };
66
67 struct KeyHeader {
68 uint8_t fDstReadKey; // set by GrGLShaderBuilder if there
69 // are effects that must read the dst.
70 // Otherwise, 0.
71 uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are
72 // effects that read the fragment position.
73 // Otherwise, 0.
74
joshualitt79f8fae2014-10-28 17:59:26 -070075 ColorInput fColorInput : 8;
76 ColorInput fCoverageInput : 8;
77
joshualitt79f8fae2014-10-28 17:59:26 -070078 SkBool8 fHasGeometryProcessor;
79 int8_t fColorEffectCnt;
80 int8_t fCoverageEffectCnt;
81 };
82
83
84 bool hasGeometryProcessor() const {
85 return SkToBool(this->header().fHasGeometryProcessor);
86 }
87
88 int numColorEffects() const {
89 return this->header().fColorEffectCnt;
90 }
91
92 int numCoverageEffects() const {
93 return this->header().fCoverageEffectCnt;
94 }
95
96 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
97
98 // This should really only be used internally, base classes should return their own headers
99 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
100
joshualitt79f8fae2014-10-28 17:59:26 -0700101 // A struct to communicate descriptor information to the program descriptor builder
102 struct DescInfo {
joshualittdafa4d02014-12-04 08:59:10 -0800103 bool operator==(const DescInfo& that) const {
104 return fHasVertexColor == that.fHasVertexColor &&
105 fHasVertexCoverage == that.fHasVertexCoverage &&
106 fInputColorIsUsed == that.fInputColorIsUsed &&
107 fInputCoverageIsUsed == that.fInputCoverageIsUsed &&
108 fReadsDst == that.fReadsDst &&
109 fReadsFragPosition == that.fReadsFragPosition &&
egdanielc2304142014-12-11 13:15:13 -0800110 fRequiresLocalCoordAttrib == that.fRequiresLocalCoordAttrib;
joshualittdafa4d02014-12-04 08:59:10 -0800111 }
112 bool operator!=(const DescInfo& that) const { return !(*this == that); };
joshualitt2dd1ae02014-12-03 06:24:10 -0800113 // TODO when GPs control uniform / attribute handling of color / coverage, then we can
114 // clean this up
115 bool fHasVertexColor;
116 bool fHasVertexCoverage;
joshualitt79f8fae2014-10-28 17:59:26 -0700117
118 // These flags are needed to protect the code from creating an unused uniform color/coverage
119 // which will cause shader compiler errors.
120 bool fInputColorIsUsed;
121 bool fInputCoverageIsUsed;
122
123 // These flags give aggregated info on the processor stages that are used when building
124 // programs.
125 bool fReadsDst;
126 bool fReadsFragPosition;
127 bool fRequiresLocalCoordAttrib;
128
joshualitt79f8fae2014-10-28 17:59:26 -0700129 };
130
131private:
132 template<typename T, size_t OFFSET> T* atOffset() {
133 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
134 }
135
136 template<typename T, size_t OFFSET> const T* atOffset() const {
137 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
138 }
139
140 void finalize() {
141 int keyLength = fKey.count();
142 SkASSERT(0 == (keyLength % 4));
143 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
144
145 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
146 *checksum = 0;
147 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
148 }
149
150 // The key, stored in fKey, is composed of four parts:
151 // 1. uint32_t for total key length.
152 // 2. uint32_t for a checksum.
153 // 3. Header struct defined above. Also room for extensions to the header
154 // 4. A Backend specific payload. Room is preallocated for this
155 enum KeyOffsets {
156 // Part 1.
157 kLengthOffset = 0,
158 // Part 2.
159 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
160 // Part 3.
161 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
162 kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)),
163 };
164
165 enum {
166 kMaxPreallocProcessors = 8,
167 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
168 kPreAllocSize = kHeaderOffset + kHeaderSize +
169 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
170 };
171
172 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
173
174 friend class GrGLProgramDescBuilder;
175};
176
177#endif