blob: 0c7c8cf629949c875ee8cbdb501fb7a3ab4e9a5d [file] [log] [blame]
bsalomon@google.com31ec7982013-03-27 18:14:57 +00001/*
2 * Copyright 2013 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 GrGLProgramDesc_DEFINED
9#define GrGLProgramDesc_DEFINED
10
11#include "GrGLEffect.h"
bsalomon@google.com798c8c42013-03-27 19:50:27 +000012#include "GrDrawState.h"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000013#include "GrGLShaderBuilder.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000014
15class GrGpuGL;
16
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000017#ifdef SK_DEBUG
18 // Optionally compile the experimental GS code. Set to SK_DEBUG so that debug build bots will
19 // execute the code.
20 #define GR_GL_EXPERIMENTAL_GS 1
21#else
22 #define GR_GL_EXPERIMENTAL_GS 0
23#endif
bsalomon@google.com31ec7982013-03-27 18:14:57 +000024
25
bsalomon@google.com26e18b52013-03-29 19:22:36 +000026/** This class describes a program to generate. It also serves as a program cache key. Very little
27 of this is GL-specific. There is the generation of GrGLEffect::EffectKeys and the dst-read part
28 of the key set by GrGLShaderBuilder. If the interfaces that set those portions were abstracted
29 to be API-neutral then so could this class. */
bsalomon@google.com31ec7982013-03-27 18:14:57 +000030class GrGLProgramDesc {
31public:
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000032 GrGLProgramDesc() : fInitialized(false) {}
33 GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; }
34
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000035 // Returns this as a uint32_t array to be used as a key in the program cache.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000036 const uint32_t* asKey() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(fInitialized);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000038 return reinterpret_cast<const uint32_t*>(fKey.get());
bsalomon@google.com31ec7982013-03-27 18:14:57 +000039 }
40
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000041 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
42 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
43 // keys and thus the memcmp will exit early if the keys are of different lengths.
44 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
45
46 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
47 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
bsalomon@google.com31ec7982013-03-27 18:14:57 +000048
49 // For unit testing.
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000050 void setRandom(SkRandom*,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000051 const GrGpuGL* gpu,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000052 const GrRenderTarget* dummyDstRenderTarget,
53 const GrTexture* dummyDstCopyTexture,
54 const GrEffectStage* stages[],
55 int numColorStages,
56 int numCoverageStages,
jvanverth@google.com054ae992013-04-01 20:06:51 +000057 int currAttribIndex);
bsalomon@google.com31ec7982013-03-27 18:14:57 +000058
59 /**
60 * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000061 * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000062 * outputs the color and coverage stages referenced by the generated descriptor. This may
63 * not contain all stages from the draw state and coverage stages from the drawState may
64 * be treated as color stages in the output.
bsalomon@google.com31ec7982013-03-27 18:14:57 +000065 */
66 static void Build(const GrDrawState&,
67 bool isPoints,
68 GrDrawState::BlendOptFlags,
69 GrBlendCoeff srcCoeff,
70 GrBlendCoeff dstCoeff,
71 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000072 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000073 SkTArray<const GrEffectStage*, true>* outColorStages,
74 SkTArray<const GrEffectStage*, true>* outCoverageStages,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000075 GrGLProgramDesc* outDesc);
76
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000077 int numColorEffects() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000078 SkASSERT(fInitialized);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000079 return this->getHeader().fColorEffectCnt;
80 }
81
82 int numCoverageEffects() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000083 SkASSERT(fInitialized);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000084 return this->getHeader().fCoverageEffectCnt;
85 }
86
87 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
88
89 GrGLProgramDesc& operator= (const GrGLProgramDesc& other);
90
91 bool operator== (const GrGLProgramDesc& other) const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000092 SkASSERT(fInitialized && other.fInitialized);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000093 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
94 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
95 }
96
97 bool operator!= (const GrGLProgramDesc& other) const {
98 return !(*this == other);
99 }
100
101 static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) {
102 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
103 }
104
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000105private:
106 // Specifies where the initial color comes from before the stages are applied.
107 enum ColorInput {
108 kSolidWhite_ColorInput,
109 kTransBlack_ColorInput,
110 kAttribute_ColorInput,
111 kUniform_ColorInput,
112
113 kColorInputCnt
114 };
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000115
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000116 static GrSLConstantVec KnownColorInputValue(ColorInput ci) {
117 switch (ci) {
118 case GrGLProgramDesc::kTransBlack_ColorInput:
119 return kZeros_GrSLConstantVec;
120 case GrGLProgramDesc::kSolidWhite_ColorInput:
121 return kOnes_GrSLConstantVec;
122 default:
123 return kNone_GrSLConstantVec;
124 }
125 }
126
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000127 enum CoverageOutput {
128 // modulate color and coverage, write result as the color output.
129 kModulate_CoverageOutput,
130 // Writes color*coverage as the primary color output and also writes coverage as the
131 // secondary output. Only set if dual source blending is supported.
132 kSecondaryCoverage_CoverageOutput,
133 // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA)
134 // as the secondary output. Only set if dual source blending is supported.
135 kSecondaryCoverageISA_CoverageOutput,
136 // Writes color*coverage as the primary color output and also writes coverage *
137 // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported.
138 kSecondaryCoverageISC_CoverageOutput,
139 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
bsalomon@google.comb5158812013-05-13 18:50:25 +0000140 // can only be set if fDstReadKey is non-zero.
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000141 kCombineWithDst_CoverageOutput,
142
143 kCoverageOutputCnt
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000144 };
145
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000146 static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) {
147 switch (co) {
148 case kSecondaryCoverage_CoverageOutput: // fallthru
149 case kSecondaryCoverageISA_CoverageOutput:
150 case kSecondaryCoverageISC_CoverageOutput:
151 return true;
152 default:
153 return false;
154 }
155 }
156
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000157 struct KeyHeader {
158 GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000159 // are effects that must read the dst.
160 // Otherwise, 0.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000161 GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are
bsalomon@google.comb5158812013-05-13 18:50:25 +0000162 // effects that read the fragment position.
163 // Otherwise, 0.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000164
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000165 // should the FS discard if the coverage is zero (to avoid stencil manipulation)
166 SkBool8 fDiscardIfZeroCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000167
commit-bot@chromium.org949eef02013-10-01 18:43:29 +0000168 ColorInput fColorInput : 8;
169 ColorInput fCoverageInput : 8;
170 CoverageOutput fCoverageOutput : 8;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000171
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000172 SkBool8 fHasVertexCode;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000173 SkBool8 fEmitsPointSize;
commit-bot@chromium.org949eef02013-10-01 18:43:29 +0000174 SkXfermode::Mode fColorFilterXfermode : 8;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000175
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000176 // To enable experimental geometry shader code (not for use in
177 // production)
178#if GR_GL_EXPERIMENTAL_GS
179 SkBool8 fExperimentalGS;
180#endif
181
182 int8_t fPositionAttributeIndex;
183 int8_t fLocalCoordAttributeIndex;
184 int8_t fColorAttributeIndex;
185 int8_t fCoverageAttributeIndex;
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000186
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000187 int8_t fColorEffectCnt;
188 int8_t fCoverageEffectCnt;
189 };
190
191 // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then
192 // the effect keys. Everything is fixed length except the effect key array.
193 enum {
194 kLengthOffset = 0,
195 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
196 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
197 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
198 kEffectKeyOffset = kHeaderOffset + kHeaderSize,
199 };
200
201 template<typename T, size_t OFFSET> T* atOffset() {
202 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
203 }
204
205 template<typename T, size_t OFFSET> const T* atOffset() const {
206 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
207 }
208
209 typedef GrGLEffect::EffectKey EffectKey;
210
211 uint32_t* checksum() { return this->atOffset<uint32_t, kChecksumOffset>(); }
212 KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); }
213 EffectKey* effectKeys() { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
214
215 const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
216 const EffectKey* getEffectKeys() const { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
217
218 static size_t KeyLength(int effectCnt) {
219 GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3));
220 return kEffectKeyOffset + effectCnt * sizeof(EffectKey);
221 }
222
223 enum {
224 kMaxPreallocEffects = 16,
225 kPreAllocSize = kEffectKeyOffset + kMaxPreallocEffects * sizeof(EffectKey),
226 };
227
228 SkAutoSMalloc<kPreAllocSize> fKey;
229 bool fInitialized;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000230
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000231 // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
232 // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000233 friend class GrGLProgram;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000234 friend class GrGLShaderBuilder;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000235 friend class GrGLFullShaderBuilder;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000236};
237
238#endif