blob: ef7be23cf3a0cf8441c5d00e8683423d5cb40752 [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
11#include "GrBackendProcessorFactory.h"
12#include "GrColor.h"
13#include "GrTypesPriv.h"
14#include "SkChecksum.h"
15
16class GrGpuGL;
17
18/** This class describes a program to generate. It also serves as a program cache key. Very little
19 of this is GL-specific. The GL-specific parts could be factored out into a subclass. */
20class GrProgramDesc {
21public:
22 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
23 GrProgramDesc() {}
24
25 // Returns this as a uint32_t array to be used as a key in the program cache.
26 const uint32_t* asKey() const {
27 return reinterpret_cast<const uint32_t*>(fKey.begin());
28 }
29
30 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
31 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
32 // keys and thus the memcmp will exit early if the keys are of different lengths.
33 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
34
35 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
36 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
37
38 GrProgramDesc& operator= (const GrProgramDesc& other) {
39 size_t keyLength = other.keyLength();
40 fKey.reset(keyLength);
41 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
42 return *this;
43 }
44
45 bool operator== (const GrProgramDesc& other) const {
46 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
47 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
48 }
49
50 bool operator!= (const GrProgramDesc& other) const {
51 return !(*this == other);
52 }
53
54 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
55 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
56 }
57
58
59 ///////////////////////////////////////////////////////////////////////////
60 /// @name Stage Output Types
61 ////
62
63 enum PrimaryOutputType {
64 // Modulate color and coverage, write result as the color output.
65 kModulate_PrimaryOutputType,
66 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
67 // can only be set if fDstReadKey is non-zero.
68 kCombineWithDst_PrimaryOutputType,
69
70 kPrimaryOutputTypeCnt,
71 };
72
73 enum SecondaryOutputType {
74 // There is no secondary output
75 kNone_SecondaryOutputType,
76 // Writes coverage as the secondary output. Only set if dual source blending is supported
77 // and primary output is kModulate.
78 kCoverage_SecondaryOutputType,
79 // Writes coverage * (1 - colorA) as the secondary output. Only set if dual source blending
80 // is supported and primary output is kModulate.
81 kCoverageISA_SecondaryOutputType,
82 // Writes coverage * (1 - colorRGBA) as the secondary output. Only set if dual source
83 // blending is supported and primary output is kModulate.
84 kCoverageISC_SecondaryOutputType,
85
86 kSecondaryOutputTypeCnt,
87 };
88
89 // Specifies where the initial color comes from before the stages are applied.
90 enum ColorInput {
91 kAllOnes_ColorInput,
92 kAttribute_ColorInput,
93 kUniform_ColorInput,
94
95 kColorInputCnt
96 };
97
98 struct KeyHeader {
99 uint8_t fDstReadKey; // set by GrGLShaderBuilder if there
100 // are effects that must read the dst.
101 // Otherwise, 0.
102 uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are
103 // effects that read the fragment position.
104 // Otherwise, 0.
105
joshualitt79f8fae2014-10-28 17:59:26 -0700106 ColorInput fColorInput : 8;
107 ColorInput fCoverageInput : 8;
108
109 PrimaryOutputType fPrimaryOutputType : 8;
110 SecondaryOutputType fSecondaryOutputType : 8;
111
joshualitt79f8fae2014-10-28 17:59:26 -0700112 SkBool8 fHasGeometryProcessor;
113 int8_t fColorEffectCnt;
114 int8_t fCoverageEffectCnt;
115 };
116
117
118 bool hasGeometryProcessor() const {
119 return SkToBool(this->header().fHasGeometryProcessor);
120 }
121
122 int numColorEffects() const {
123 return this->header().fColorEffectCnt;
124 }
125
126 int numCoverageEffects() const {
127 return this->header().fCoverageEffectCnt;
128 }
129
130 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
131
132 // This should really only be used internally, base classes should return their own headers
133 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
134
joshualitt79f8fae2014-10-28 17:59:26 -0700135 // A struct to communicate descriptor information to the program descriptor builder
136 struct DescInfo {
joshualittdafa4d02014-12-04 08:59:10 -0800137 bool operator==(const DescInfo& that) const {
138 return fHasVertexColor == that.fHasVertexColor &&
139 fHasVertexCoverage == that.fHasVertexCoverage &&
140 fInputColorIsUsed == that.fInputColorIsUsed &&
141 fInputCoverageIsUsed == that.fInputCoverageIsUsed &&
142 fReadsDst == that.fReadsDst &&
143 fReadsFragPosition == that.fReadsFragPosition &&
144 fRequiresLocalCoordAttrib == that.fRequiresLocalCoordAttrib &&
145 fPrimaryOutputType == that.fPrimaryOutputType &&
146 fSecondaryOutputType == that.fSecondaryOutputType;
147
148 }
149 bool operator!=(const DescInfo& that) const { return !(*this == that); };
joshualitt2dd1ae02014-12-03 06:24:10 -0800150 // TODO when GPs control uniform / attribute handling of color / coverage, then we can
151 // clean this up
152 bool fHasVertexColor;
153 bool fHasVertexCoverage;
joshualitt79f8fae2014-10-28 17:59:26 -0700154
155 // These flags are needed to protect the code from creating an unused uniform color/coverage
156 // which will cause shader compiler errors.
157 bool fInputColorIsUsed;
158 bool fInputCoverageIsUsed;
159
160 // These flags give aggregated info on the processor stages that are used when building
161 // programs.
162 bool fReadsDst;
163 bool fReadsFragPosition;
164 bool fRequiresLocalCoordAttrib;
165
166 // Fragment shader color outputs
167 GrProgramDesc::PrimaryOutputType fPrimaryOutputType : 8;
168 GrProgramDesc::SecondaryOutputType fSecondaryOutputType : 8;
169 };
170
171private:
172 template<typename T, size_t OFFSET> T* atOffset() {
173 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
174 }
175
176 template<typename T, size_t OFFSET> const T* atOffset() const {
177 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
178 }
179
180 void finalize() {
181 int keyLength = fKey.count();
182 SkASSERT(0 == (keyLength % 4));
183 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
184
185 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
186 *checksum = 0;
187 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
188 }
189
190 // The key, stored in fKey, is composed of four parts:
191 // 1. uint32_t for total key length.
192 // 2. uint32_t for a checksum.
193 // 3. Header struct defined above. Also room for extensions to the header
194 // 4. A Backend specific payload. Room is preallocated for this
195 enum KeyOffsets {
196 // Part 1.
197 kLengthOffset = 0,
198 // Part 2.
199 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
200 // Part 3.
201 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
202 kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)),
203 };
204
205 enum {
206 kMaxPreallocProcessors = 8,
207 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
208 kPreAllocSize = kHeaderOffset + kHeaderSize +
209 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
210 };
211
212 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
213
214 friend class GrGLProgramDescBuilder;
215};
216
217#endif