blob: e85133cc97613a0ab5a7bbb71f2b863e34b0e610 [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
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000058 * outputs the color and coverage stages referenced by the generated descriptor. This may
59 * not contain all stages from the draw state and coverage stages from the drawState may
60 * be treated as color stages in the output.
bsalomon@google.com31ec7982013-03-27 18:14:57 +000061 */
62 static void Build(const GrDrawState&,
63 bool isPoints,
64 GrDrawState::BlendOptFlags,
65 GrBlendCoeff srcCoeff,
66 GrBlendCoeff dstCoeff,
67 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000068 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000069 SkTArray<const GrEffectStage*, true>* outColorStages,
70 SkTArray<const GrEffectStage*, true>* outCoverageStages,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000071 GrGLProgramDesc* outDesc);
72
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000073 int numColorEffects() const {
74 GrAssert(fInitialized);
75 return this->getHeader().fColorEffectCnt;
76 }
77
78 int numCoverageEffects() const {
79 GrAssert(fInitialized);
80 return this->getHeader().fCoverageEffectCnt;
81 }
82
83 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
84
85 GrGLProgramDesc& operator= (const GrGLProgramDesc& other);
86
87 bool operator== (const GrGLProgramDesc& other) const {
88 GrAssert(fInitialized && other.fInitialized);
89 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
90 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
91 }
92
93 bool operator!= (const GrGLProgramDesc& other) const {
94 return !(*this == other);
95 }
96
97 static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) {
98 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
99 }
100
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000101private:
102 // Specifies where the initial color comes from before the stages are applied.
103 enum ColorInput {
104 kSolidWhite_ColorInput,
105 kTransBlack_ColorInput,
106 kAttribute_ColorInput,
107 kUniform_ColorInput,
108
109 kColorInputCnt
110 };
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000111
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000112 enum CoverageOutput {
113 // modulate color and coverage, write result as the color output.
114 kModulate_CoverageOutput,
115 // Writes color*coverage as the primary color output and also writes coverage as the
116 // secondary output. Only set if dual source blending is supported.
117 kSecondaryCoverage_CoverageOutput,
118 // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA)
119 // as the secondary output. Only set if dual source blending is supported.
120 kSecondaryCoverageISA_CoverageOutput,
121 // Writes color*coverage as the primary color output and also writes coverage *
122 // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported.
123 kSecondaryCoverageISC_CoverageOutput,
124 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
bsalomon@google.comb5158812013-05-13 18:50:25 +0000125 // can only be set if fDstReadKey is non-zero.
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000126 kCombineWithDst_CoverageOutput,
127
128 kCoverageOutputCnt
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000129 };
130
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000131 static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) {
132 switch (co) {
133 case kSecondaryCoverage_CoverageOutput: // fallthru
134 case kSecondaryCoverageISA_CoverageOutput:
135 case kSecondaryCoverageISC_CoverageOutput:
136 return true;
137 default:
138 return false;
139 }
140 }
141
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000142 struct KeyHeader {
143 GrGLShaderBuilder::DstReadKey fDstReadKey; // set by GrGLShaderBuilder if there
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000144 // are effects that must read the dst.
145 // Otherwise, 0.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000146 GrGLShaderBuilder::FragPosKey fFragPosKey; // set by GrGLShaderBuilder if there are
bsalomon@google.comb5158812013-05-13 18:50:25 +0000147 // effects that read the fragment position.
148 // Otherwise, 0.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000149
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000150 // should the FS discard if the coverage is zero (to avoid stencil manipulation)
151 SkBool8 fDiscardIfZeroCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000152
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000153 uint8_t fColorInput; // casts to enum ColorInput
154 uint8_t fCoverageInput; // casts to enum ColorInput
155 uint8_t fCoverageOutput; // casts to enum CoverageOutput
jvanverth@google.com054ae992013-04-01 20:06:51 +0000156
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000157 SkBool8 fEmitsPointSize;
158 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000159
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000160 // To enable experimental geometry shader code (not for use in
161 // production)
162#if GR_GL_EXPERIMENTAL_GS
163 SkBool8 fExperimentalGS;
164#endif
165
166 int8_t fPositionAttributeIndex;
167 int8_t fLocalCoordAttributeIndex;
168 int8_t fColorAttributeIndex;
169 int8_t fCoverageAttributeIndex;
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000170
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000171 int8_t fColorEffectCnt;
172 int8_t fCoverageEffectCnt;
173 };
174
175 // The key is 1 uint32_t for the length, followed another for the checksum, the header, and then
176 // the effect keys. Everything is fixed length except the effect key array.
177 enum {
178 kLengthOffset = 0,
179 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
180 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
181 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
182 kEffectKeyOffset = kHeaderOffset + kHeaderSize,
183 };
184
185 template<typename T, size_t OFFSET> T* atOffset() {
186 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
187 }
188
189 template<typename T, size_t OFFSET> const T* atOffset() const {
190 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.get()) + OFFSET);
191 }
192
193 typedef GrGLEffect::EffectKey EffectKey;
194
195 uint32_t* checksum() { return this->atOffset<uint32_t, kChecksumOffset>(); }
196 KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); }
197 EffectKey* effectKeys() { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
198
199 const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
200 const EffectKey* getEffectKeys() const { return this->atOffset<EffectKey, kEffectKeyOffset>(); }
201
202 static size_t KeyLength(int effectCnt) {
203 GR_STATIC_ASSERT(!(sizeof(EffectKey) & 0x3));
204 return kEffectKeyOffset + effectCnt * sizeof(EffectKey);
205 }
206
207 enum {
208 kMaxPreallocEffects = 16,
209 kPreAllocSize = kEffectKeyOffset + kMaxPreallocEffects * sizeof(EffectKey),
210 };
211
212 SkAutoSMalloc<kPreAllocSize> fKey;
213 bool fInitialized;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000214
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000215 // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Move all
216 // code generation to GrGLShaderBuilder (and maybe add getters rather than friending).
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000217 friend class GrGLProgram;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000218 friend class GrGLShaderBuilder;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000219};
220
221#endif