blob: a20b99f74948c91377c7127ef12a17ff683b019b [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
joshualitt29473822014-12-10 15:03:00 -080058 ///////////////////////////////////////////////////////////////////////////
59 /// @name Stage Output Types
60 ////
61
62 enum PrimaryOutputType {
63 // Modulate color and coverage, write result as the color output.
64 kModulate_PrimaryOutputType,
65 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
66 // can only be set if fDstReadKey is non-zero.
67 kCombineWithDst_PrimaryOutputType,
68
69 kPrimaryOutputTypeCnt,
70 };
71
72 enum SecondaryOutputType {
73 // There is no secondary output
74 kNone_SecondaryOutputType,
75 // Writes coverage as the secondary output. Only set if dual source blending is supported
76 // and primary output is kModulate.
77 kCoverage_SecondaryOutputType,
78 // Writes coverage * (1 - colorA) as the secondary output. Only set if dual source blending
79 // is supported and primary output is kModulate.
80 kCoverageISA_SecondaryOutputType,
81 // Writes coverage * (1 - colorRGBA) as the secondary output. Only set if dual source
82 // blending is supported and primary output is kModulate.
83 kCoverageISC_SecondaryOutputType,
84
85 kSecondaryOutputTypeCnt,
86 };
87
joshualitt79f8fae2014-10-28 17:59:26 -070088 // Specifies where the initial color comes from before the stages are applied.
89 enum ColorInput {
90 kAllOnes_ColorInput,
91 kAttribute_ColorInput,
92 kUniform_ColorInput,
93
94 kColorInputCnt
95 };
96
97 struct KeyHeader {
98 uint8_t fDstReadKey; // set by GrGLShaderBuilder if there
99 // are effects that must read the dst.
100 // Otherwise, 0.
101 uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are
102 // effects that read the fragment position.
103 // Otherwise, 0.
104
joshualitt79f8fae2014-10-28 17:59:26 -0700105 ColorInput fColorInput : 8;
106 ColorInput fCoverageInput : 8;
107
joshualitt29473822014-12-10 15:03:00 -0800108 PrimaryOutputType fPrimaryOutputType : 8;
109 SecondaryOutputType fSecondaryOutputType : 8;
110
joshualitt79f8fae2014-10-28 17:59:26 -0700111 SkBool8 fHasGeometryProcessor;
112 int8_t fColorEffectCnt;
113 int8_t fCoverageEffectCnt;
114 };
115
116
117 bool hasGeometryProcessor() const {
118 return SkToBool(this->header().fHasGeometryProcessor);
119 }
120
121 int numColorEffects() const {
122 return this->header().fColorEffectCnt;
123 }
124
125 int numCoverageEffects() const {
126 return this->header().fCoverageEffectCnt;
127 }
128
129 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
130
131 // This should really only be used internally, base classes should return their own headers
132 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
133
joshualitt79f8fae2014-10-28 17:59:26 -0700134 // A struct to communicate descriptor information to the program descriptor builder
135 struct DescInfo {
joshualittdafa4d02014-12-04 08:59:10 -0800136 bool operator==(const DescInfo& that) const {
137 return fHasVertexColor == that.fHasVertexColor &&
138 fHasVertexCoverage == that.fHasVertexCoverage &&
139 fInputColorIsUsed == that.fInputColorIsUsed &&
140 fInputCoverageIsUsed == that.fInputCoverageIsUsed &&
141 fReadsDst == that.fReadsDst &&
142 fReadsFragPosition == that.fReadsFragPosition &&
joshualitt29473822014-12-10 15:03:00 -0800143 fRequiresLocalCoordAttrib == that.fRequiresLocalCoordAttrib &&
144 fPrimaryOutputType == that.fPrimaryOutputType &&
145 fSecondaryOutputType == that.fSecondaryOutputType;
146
joshualittdafa4d02014-12-04 08:59:10 -0800147 }
148 bool operator!=(const DescInfo& that) const { return !(*this == that); };
joshualitt2dd1ae02014-12-03 06:24:10 -0800149 // TODO when GPs control uniform / attribute handling of color / coverage, then we can
150 // clean this up
151 bool fHasVertexColor;
152 bool fHasVertexCoverage;
joshualitt79f8fae2014-10-28 17:59:26 -0700153
154 // These flags are needed to protect the code from creating an unused uniform color/coverage
155 // which will cause shader compiler errors.
156 bool fInputColorIsUsed;
157 bool fInputCoverageIsUsed;
158
159 // These flags give aggregated info on the processor stages that are used when building
160 // programs.
161 bool fReadsDst;
162 bool fReadsFragPosition;
163 bool fRequiresLocalCoordAttrib;
164
joshualitt29473822014-12-10 15:03:00 -0800165 // Fragment shader color outputs
166 GrProgramDesc::PrimaryOutputType fPrimaryOutputType : 8;
167 GrProgramDesc::SecondaryOutputType fSecondaryOutputType : 8;
joshualitt79f8fae2014-10-28 17:59:26 -0700168 };
169
170private:
171 template<typename T, size_t OFFSET> T* atOffset() {
172 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
173 }
174
175 template<typename T, size_t OFFSET> const T* atOffset() const {
176 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
177 }
178
179 void finalize() {
180 int keyLength = fKey.count();
181 SkASSERT(0 == (keyLength % 4));
182 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
183
184 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
185 *checksum = 0;
186 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
187 }
188
189 // The key, stored in fKey, is composed of four parts:
190 // 1. uint32_t for total key length.
191 // 2. uint32_t for a checksum.
192 // 3. Header struct defined above. Also room for extensions to the header
193 // 4. A Backend specific payload. Room is preallocated for this
194 enum KeyOffsets {
195 // Part 1.
196 kLengthOffset = 0,
197 // Part 2.
198 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
199 // Part 3.
200 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
201 kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)),
202 };
203
204 enum {
205 kMaxPreallocProcessors = 8,
206 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
207 kPreAllocSize = kHeaderOffset + kHeaderSize +
208 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
209 };
210
211 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
212
213 friend class GrGLProgramDescBuilder;
214};
215
216#endif