blob: 0cf3b22b622c0526d59d674f45edfd5131fa1ff9 [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
17// optionally compile the experimental GS code. Set to GR_DEBUG so that debug build bots will
18// execute the code.
19#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
20
21
bsalomon@google.com26e18b52013-03-29 19:22:36 +000022/** This class describes a program to generate. It also serves as a program cache key. Very little
23 of this is GL-specific. There is the generation of GrGLEffect::EffectKeys and the dst-read part
24 of the key set by GrGLShaderBuilder. If the interfaces that set those portions were abstracted
25 to be API-neutral then so could this class. */
bsalomon@google.com31ec7982013-03-27 18:14:57 +000026class GrGLProgramDesc {
27public:
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000028 GrGLProgramDesc() : fInitialized(false) {}
29 GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; }
30
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000031 // 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 +000032 const uint32_t* asKey() const {
33 GrAssert(fInitialized);
34 return reinterpret_cast<const uint32_t*>(fKey.get());
bsalomon@google.com31ec7982013-03-27 18:14:57 +000035 }
36
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000037 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
38 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
39 // keys and thus the memcmp will exit early if the keys are of different lengths.
40 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
41
42 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
43 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
bsalomon@google.com31ec7982013-03-27 18:14:57 +000044
45 // For unit testing.
46 void setRandom(SkMWCRandom*,
47 const GrGpuGL* gpu,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000048 const GrRenderTarget* dummyDstRenderTarget,
49 const GrTexture* dummyDstCopyTexture,
50 const GrEffectStage* stages[],
51 int numColorStages,
52 int numCoverageStages,
jvanverth@google.com054ae992013-04-01 20:06:51 +000053 int currAttribIndex);
bsalomon@google.com31ec7982013-03-27 18:14:57 +000054
55 /**
56 * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000057 * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also
58 * writes a tightly packed array of GrEffectStage* from the drawState.
bsalomon@google.com31ec7982013-03-27 18:14:57 +000059 */
60 static void Build(const GrDrawState&,
61 bool isPoints,
62 GrDrawState::BlendOptFlags,
63 GrBlendCoeff srcCoeff,
64 GrBlendCoeff dstCoeff,
65 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000066 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000067 const GrEffectStage* outStages[GrDrawState::kNumStages],
bsalomon@google.com31ec7982013-03-27 18:14:57 +000068 GrGLProgramDesc* outDesc);
69
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000070 int numColorEffects() const {
71 GrAssert(fInitialized);
72 return this->getHeader().fColorEffectCnt;
73 }
74
75 int numCoverageEffects() const {
76 GrAssert(fInitialized);
77 return this->getHeader().fCoverageEffectCnt;
78 }
79
80 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
81
82 GrGLProgramDesc& operator= (const GrGLProgramDesc& other);
83
84 bool operator== (const GrGLProgramDesc& other) const {
85 GrAssert(fInitialized && other.fInitialized);
86 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
87 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
88 }
89
90 bool operator!= (const GrGLProgramDesc& other) const {
91 return !(*this == other);
92 }
93
94 static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) {
95 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
96 }
97
bsalomon@google.com31ec7982013-03-27 18:14:57 +000098private:
99 // Specifies where the initial color comes from before the stages are applied.
100 enum ColorInput {
101 kSolidWhite_ColorInput,
102 kTransBlack_ColorInput,
103 kAttribute_ColorInput,
104 kUniform_ColorInput,
105
106 kColorInputCnt
107 };
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000108
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000109 enum CoverageOutput {
110 // modulate color and coverage, write result as the color output.
111 kModulate_CoverageOutput,
112 // Writes color*coverage as the primary color output and also writes coverage as the
113 // secondary output. Only set if dual source blending is supported.
114 kSecondaryCoverage_CoverageOutput,
115 // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA)
116 // as the secondary output. Only set if dual source blending is supported.
117 kSecondaryCoverageISA_CoverageOutput,
118 // Writes color*coverage as the primary color output and also writes coverage *
119 // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported.
120 kSecondaryCoverageISC_CoverageOutput,
121 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
bsalomon@google.comb5158812013-05-13 18:50:25 +0000122 // can only be set if fDstReadKey is non-zero.
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000123 kCombineWithDst_CoverageOutput,
124
125 kCoverageOutputCnt
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000126 };
127
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000128 static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) {
129 switch (co) {
130 case kSecondaryCoverage_CoverageOutput: // fallthru
131 case kSecondaryCoverageISA_CoverageOutput:
132 case kSecondaryCoverageISC_CoverageOutput:
133 return true;
134 default:
135 return false;
136 }
137 }
138
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000139 struct KeyHeader {
140 GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000141 // are effects that must read the dst.
142 // Otherwise, 0.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000143 GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are
bsalomon@google.comb5158812013-05-13 18:50:25 +0000144 // effects that read the fragment position.
145 // Otherwise, 0.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000146
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000147 // should the FS discard if the coverage is zero (to avoid stencil manipulation)
148 SkBool8 fDiscardIfZeroCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000149
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000150 uint8_t fColorInput; // casts to enum ColorInput
151 uint8_t fCoverageInput; // casts to enum ColorInput
152 uint8_t fCoverageOutput; // casts to enum CoverageOutput
jvanverth@google.com054ae992013-04-01 20:06:51 +0000153
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000154 SkBool8 fEmitsPointSize;
155 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000156
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000157 // To enable experimental geometry shader code (not for use in
158 // production)
159#if GR_GL_EXPERIMENTAL_GS
160 SkBool8 fExperimentalGS;
161#endif
162
163 int8_t fPositionAttributeIndex;
164 int8_t fLocalCoordAttributeIndex;
165 int8_t fColorAttributeIndex;
166 int8_t fCoverageAttributeIndex;
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000167
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000168 int8_t fColorEffectCnt;
169 int8_t fCoverageEffectCnt;
170 };
171
172 // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then
173 // the effect keys. Everything is fixed length except the effect key array.
174 enum {
175 kLengthOffset = 0,
176 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
177 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
178 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
179 kEffectKeyOffset = kHeaderOffset + kHeaderSize,
180 };
181
182 template<typename T, size_t OFFSET> T* atOffset() {
183 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
184 }
185
186 template<typename T, size_t OFFSET> const T* atOffset() const {
187 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
188 }
189
190 typedef GrGLEffect::EffectKey EffectKey;
191
192 uint32_t* checksum() { return this->atOffset<uint32_t, kChecksumOffset>(); }
193 KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); }
194 EffectKey* effectKeys() { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
195
196 const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
197 const EffectKey* getEffectKeys() const { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
198
199 static size_t KeyLength(int effectCnt) {
200 GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3));
201 return kEffectKeyOffset + effectCnt * sizeof(EffectKey);
202 }
203
204 enum {
205 kMaxPreallocEffects = 16,
206 kPreAllocSize = kEffectKeyOffset + kMaxPreallocEffects * sizeof(EffectKey),
207 };
208
209 SkAutoSMalloc<kPreAllocSize> fKey;
210 bool fInitialized;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000211
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000212 // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
213 // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000214 friend class GrGLProgram;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000215 friend class GrGLShaderBuilder;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000216};
217
218#endif