blob: a8d27ab26976c33b0a425a009885ac7759d4b09b [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"
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000013#include "GrGpu.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
bsalomon63e99f72014-07-21 08:03:14 -070027 of this is GL-specific. The GL-specific parts could be factored out into a subclass. */
bsalomon@google.com31ec7982013-03-27 18:14:57 +000028class GrGLProgramDesc {
29public:
bsalomon848faf02014-07-11 10:01:02 -070030 GrGLProgramDesc() {}
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000031 GrGLProgramDesc(const GrGLProgramDesc& desc) { *this = desc; }
32
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +000033 // 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 +000034 const uint32_t* asKey() const {
bsalomon848faf02014-07-11 10:01:02 -070035 return reinterpret_cast<const uint32_t*>(fKey.begin());
bsalomon@google.com31ec7982013-03-27 18:14:57 +000036 }
37
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000038 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
39 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
40 // keys and thus the memcmp will exit early if the keys are of different lengths.
41 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
42
43 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
44 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
bsalomon@google.com31ec7982013-03-27 18:14:57 +000045
46 // For unit testing.
bsalomon848faf02014-07-11 10:01:02 -070047 bool setRandom(SkRandom*,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000048 const GrGpuGL* gpu,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000049 const GrRenderTarget* dummyDstRenderTarget,
50 const GrTexture* dummyDstCopyTexture,
51 const GrEffectStage* stages[],
52 int numColorStages,
53 int numCoverageStages,
jvanverth@google.com054ae992013-04-01 20:06:51 +000054 int currAttribIndex);
bsalomon@google.com31ec7982013-03-27 18:14:57 +000055
56 /**
57 * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000058 * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs. It also
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000059 * outputs the color and coverage stages referenced by the generated descriptor. This may
60 * not contain all stages from the draw state and coverage stages from the drawState may
61 * be treated as color stages in the output.
bsalomon@google.com31ec7982013-03-27 18:14:57 +000062 */
bsalomon848faf02014-07-11 10:01:02 -070063 static bool Build(const GrDrawState&,
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000064 GrGpu::DrawType drawType,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000065 GrDrawState::BlendOptFlags,
66 GrBlendCoeff srcCoeff,
67 GrBlendCoeff dstCoeff,
68 const GrGpuGL* gpu,
bsalomon@google.com26e18b52013-03-29 19:22:36 +000069 const GrDeviceCoordTexture* dstCopy,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000070 SkTArray<const GrEffectStage*, true>* outColorStages,
71 SkTArray<const GrEffectStage*, true>* outCoverageStages,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000072 GrGLProgramDesc* outDesc);
73
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000074 int numColorEffects() const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000075 return this->getHeader().fColorEffectCnt;
76 }
77
78 int numCoverageEffects() const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000079 return this->getHeader().fCoverageEffectCnt;
80 }
81
82 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
83
84 GrGLProgramDesc& operator= (const GrGLProgramDesc& other);
85
86 bool operator== (const GrGLProgramDesc& other) const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000087 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
88 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
89 }
90
91 bool operator!= (const GrGLProgramDesc& other) const {
92 return !(*this == other);
93 }
94
95 static bool Less(const GrGLProgramDesc& a, const GrGLProgramDesc& b) {
96 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
97 }
98
bsalomon@google.com31ec7982013-03-27 18:14:57 +000099private:
100 // Specifies where the initial color comes from before the stages are applied.
101 enum ColorInput {
102 kSolidWhite_ColorInput,
103 kTransBlack_ColorInput,
104 kAttribute_ColorInput,
105 kUniform_ColorInput,
106
107 kColorInputCnt
108 };
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000109
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000110 enum CoverageOutput {
111 // modulate color and coverage, write result as the color output.
112 kModulate_CoverageOutput,
113 // Writes color*coverage as the primary color output and also writes coverage as the
114 // secondary output. Only set if dual source blending is supported.
115 kSecondaryCoverage_CoverageOutput,
116 // Writes color*coverage as the primary color output and also writes coverage * (1 - colorA)
117 // as the secondary output. Only set if dual source blending is supported.
118 kSecondaryCoverageISA_CoverageOutput,
119 // Writes color*coverage as the primary color output and also writes coverage *
120 // (1 - colorRGB) as the secondary output. Only set if dual source blending is supported.
121 kSecondaryCoverageISC_CoverageOutput,
122 // Combines the coverage, dst, and color as coverage * color + (1 - coverage) * dst. This
bsalomon@google.comb5158812013-05-13 18:50:25 +0000123 // can only be set if fDstReadKey is non-zero.
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000124 kCombineWithDst_CoverageOutput,
125
126 kCoverageOutputCnt
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000127 };
128
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000129 static bool CoverageOutputUsesSecondaryOutput(CoverageOutput co) {
130 switch (co) {
131 case kSecondaryCoverage_CoverageOutput: // fallthru
132 case kSecondaryCoverageISA_CoverageOutput:
133 case kSecondaryCoverageISC_CoverageOutput:
134 return true;
135 default:
136 return false;
137 }
138 }
139
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000140 struct KeyHeader {
bsalomon848faf02014-07-11 10:01:02 -0700141 uint8_t fDstReadKey; // set by GrGLShaderBuilder if there
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000142 // are effects that must read the dst.
143 // Otherwise, 0.
bsalomon848faf02014-07-11 10:01:02 -0700144 uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are
bsalomon@google.comb5158812013-05-13 18:50:25 +0000145 // effects that read the fragment position.
146 // Otherwise, 0.
commit-bot@chromium.org949eef02013-10-01 18:43:29 +0000147 ColorInput fColorInput : 8;
148 ColorInput fCoverageInput : 8;
149 CoverageOutput fCoverageOutput : 8;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000150
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000151 SkBool8 fHasVertexCode;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000152 SkBool8 fEmitsPointSize;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000153
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000154 // To enable experimental geometry shader code (not for use in
155 // production)
156#if GR_GL_EXPERIMENTAL_GS
157 SkBool8 fExperimentalGS;
158#endif
159
160 int8_t fPositionAttributeIndex;
161 int8_t fLocalCoordAttributeIndex;
162 int8_t fColorAttributeIndex;
163 int8_t fCoverageAttributeIndex;
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000164
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000165 int8_t fColorEffectCnt;
166 int8_t fCoverageEffectCnt;
167 };
168
bsalomon848faf02014-07-11 10:01:02 -0700169 // The key, stored in fKey, is composed of five parts:
170 // 1. uint32_t for total key length.
171 // 2. uint32_t for a checksum.
172 // 3. Header struct defined above.
bsalomon929f29a2014-07-17 07:55:11 -0700173 // 4. An array of offsets to effect keys and their sizes (see 5). uint16_t for each
174 // offset and size.
bsalomon848faf02014-07-11 10:01:02 -0700175 // 5. per-effect keys. Each effect's key is a variable length array of uint32_t.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000176 enum {
bsalomon929f29a2014-07-17 07:55:11 -0700177 // Part 1.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000178 kLengthOffset = 0,
bsalomon929f29a2014-07-17 07:55:11 -0700179 // Part 2.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000180 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
bsalomon929f29a2014-07-17 07:55:11 -0700181 // Part 3.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000182 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
183 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
bsalomon929f29a2014-07-17 07:55:11 -0700184 // Part 4.
185 // This is the offset in the overall key to the array of per-effect offset,length pairs.
186 kEffectKeyOffsetsAndLengthOffset = kHeaderOffset + kHeaderSize,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000187 };
188
189 template<typename T, size_t OFFSET> T* atOffset() {
bsalomon848faf02014-07-11 10:01:02 -0700190 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000191 }
192
193 template<typename T, size_t OFFSET> const T* atOffset() const {
bsalomon848faf02014-07-11 10:01:02 -0700194 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000195 }
196
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000197 KeyHeader* header() { return this->atOffset<KeyHeader, kHeaderOffset>(); }
bsalomon848faf02014-07-11 10:01:02 -0700198
bsalomon929f29a2014-07-17 07:55:11 -0700199 // Shared code between setRandom() and Build().
200 static bool GetEffectKeyAndUpdateStats(const GrEffectStage& stage,
201 const GrGLCaps& caps,
202 bool useExplicitLocalCoords,
203 GrEffectKeyBuilder* b,
204 uint16_t* effectKeySize,
205 bool* setTrueIfReadsDst,
206 bool* setTrueIfReadsPos,
207 bool* setTrueIfHasVertexCode);
208
bsalomon848faf02014-07-11 10:01:02 -0700209 void finalize();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210
211 const KeyHeader& getHeader() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000212
bsalomon848faf02014-07-11 10:01:02 -0700213 /** Used to provide effects' keys to their emitCode() function. */
214 class EffectKeyProvider {
215 public:
216 enum EffectType {
217 kColor_EffectType,
218 kCoverage_EffectType,
219 };
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000220
bsalomon848faf02014-07-11 10:01:02 -0700221 EffectKeyProvider(const GrGLProgramDesc* desc, EffectType type) : fDesc(desc) {
222 // Coverage effect key offsets begin immediately after those of the color effects.
223 fBaseIndex = kColor_EffectType == type ? 0 : desc->numColorEffects();
224 }
225
bsalomon63e99f72014-07-21 08:03:14 -0700226 GrEffectKey get(int index) const {
227 const uint16_t* offsetsAndLengths = reinterpret_cast<const uint16_t*>(
bsalomon929f29a2014-07-17 07:55:11 -0700228 fDesc->fKey.begin() + kEffectKeyOffsetsAndLengthOffset);
229 // We store two uint16_ts per effect, one for the offset to the effect's key and one for
230 // its length. Here we just need the offset.
bsalomon63e99f72014-07-21 08:03:14 -0700231 uint16_t offset = offsetsAndLengths[2 * (fBaseIndex + index) + 0];
232 uint16_t length = offsetsAndLengths[2 * (fBaseIndex + index) + 1];
233 // Currently effects must add to the key in units of uint32_t.
234 SkASSERT(0 == (length % sizeof(uint32_t)));
235 return GrEffectKey(reinterpret_cast<const uint32_t*>(fDesc->fKey.begin() + offset),
236 length / sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -0700237 }
238 private:
239 const GrGLProgramDesc* fDesc;
240 int fBaseIndex;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000241 };
242
bsalomon848faf02014-07-11 10:01:02 -0700243 enum {
244 kMaxPreallocEffects = 8,
245 kIntsPerEffect = 4, // This is an overestimate of the average effect key size.
bsalomon929f29a2014-07-17 07:55:11 -0700246 kPreAllocSize = kEffectKeyOffsetsAndLengthOffset +
bsalomon848faf02014-07-11 10:01:02 -0700247 kMaxPreallocEffects * sizeof(uint32_t) * kIntsPerEffect,
248 };
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000249
bsalomon848faf02014-07-11 10:01:02 -0700250 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
251
252 // GrGLProgram and GrGLShaderBuilder read the private fields to generate code. TODO: Split out
253 // part of GrGLShaderBuilder that is used by effects so that this header doesn't need to be
254 // visible to GrGLEffects. Then make public accessors as necessary and remove friends.
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000255 friend class GrGLProgram;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000256 friend class GrGLShaderBuilder;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000257 friend class GrGLFullShaderBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000258 friend class GrGLFragmentOnlyShaderBuilder;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000259};
260
261#endif