blob: 0a634cbd248e39e3b6d522b48f24595ddef5be9f [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"
mtklein4e976072016-08-08 09:06:27 -070013#include "SkOpts.h"
14#include "SkTArray.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040015#include "SkTo.h"
Ethan Nicholas38657112017-02-09 17:01:22 -050016#include "glsl/GrGLSLFragmentShaderBuilder.h"
joshualitt79f8fae2014-10-28 17:59:26 -070017
Brian Salomon94efbf52016-11-29 13:43:05 -050018class GrShaderCaps;
egdaniel5d8f69f2016-09-07 07:24:12 -070019class GrPipeline;
20class GrPrimitiveProcessor;
21
22/** This class describes a program to generate. It also serves as a program cache key */
joshualitt79f8fae2014-10-28 17:59:26 -070023class GrProgramDesc {
24public:
25 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
26 GrProgramDesc() {}
27
egdaniel5d8f69f2016-09-07 07:24:12 -070028 /**
29 * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
30 * on the returned GrProgramDesc.
31 *
32 * @param GrPrimitiveProcessor The geometry
bsalomon2eda5b32016-09-21 10:53:24 -070033 * @param hasPointSize Controls whether the shader will output a point size.
egdaniel5d8f69f2016-09-07 07:24:12 -070034 * @param GrPipeline The optimized drawstate. The descriptor will represent a program
35 * which this optstate can use to draw with. The optstate contains
36 * general draw information, as well as the specific color, geometry,
37 * and coverage stages which will be used to generate the GL Program for
38 * this optstate.
Greg Daniel7a82edf2018-12-04 10:54:34 -050039 * @param GrGpu Ptr to the GrGpu object the program will be used with.
egdaniel5d8f69f2016-09-07 07:24:12 -070040 * @param GrProgramDesc The built and finalized descriptor
41 **/
42 static bool Build(GrProgramDesc*,
43 const GrPrimitiveProcessor&,
bsalomon2eda5b32016-09-21 10:53:24 -070044 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -070045 const GrPipeline&,
Greg Daniel7a82edf2018-12-04 10:54:34 -050046 GrGpu*);
egdaniel5d8f69f2016-09-07 07:24:12 -070047
joshualitt79f8fae2014-10-28 17:59:26 -070048 // Returns this as a uint32_t array to be used as a key in the program cache.
49 const uint32_t* asKey() const {
50 return reinterpret_cast<const uint32_t*>(fKey.begin());
51 }
52
53 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
54 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
55 // keys and thus the memcmp will exit early if the keys are of different lengths.
56 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
57
58 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
59 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
60
61 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080062 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080063 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070064 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
65 return *this;
66 }
67
bsalomon89d59882015-06-04 15:34:34 -070068 bool operator== (const GrProgramDesc& that) const {
69 SkASSERT(SkIsAlign4(this->keyLength()));
70 int l = this->keyLength() >> 2;
71 const uint32_t* aKey = this->asKey();
72 const uint32_t* bKey = that.asKey();
73 for (int i = 0; i < l; ++i) {
74 if (aKey[i] != bKey[i]) {
75 return false;
76 }
77 }
78 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070079 }
80
81 bool operator!= (const GrProgramDesc& other) const {
82 return !(*this == other);
83 }
84
Robert Phillips7f861922018-01-30 13:13:42 +000085 void setSurfaceOriginKey(int key) {
Ethan Nicholas38657112017-02-09 17:01:22 -050086 KeyHeader* header = this->atOffset<KeyHeader, kHeaderOffset>();
Robert Phillips7f861922018-01-30 13:13:42 +000087 header->fSurfaceOriginKey = key;
Ethan Nicholas38657112017-02-09 17:01:22 -050088 }
89
joshualitt79f8fae2014-10-28 17:59:26 -070090 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
bsalomon89d59882015-06-04 15:34:34 -070091 SkASSERT(SkIsAlign4(a.keyLength()));
92 int l = a.keyLength() >> 2;
93 const uint32_t* aKey = a.asKey();
94 const uint32_t* bKey = b.asKey();
95 for (int i = 0; i < l; ++i) {
96 if (aKey[i] != bKey[i]) {
97 return aKey[i] < bKey[i] ? true : false;
98 }
99 }
100 return false;
joshualitt79f8fae2014-10-28 17:59:26 -0700101 }
102
joshualitt79f8fae2014-10-28 17:59:26 -0700103 struct KeyHeader {
bsalomon7f9b2e42016-01-12 13:29:26 -0800104 // Set to uniquely idenitify any swizzling of the shader's output color(s).
Chris Dalton535ba8d2018-02-20 09:51:59 -0700105 uint8_t fOutputSwizzle;
106 uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
107 uint8_t fCoverageFragmentProcessorCnt;
bsalomon2eda5b32016-09-21 10:53:24 -0700108 // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
Chris Dalton535ba8d2018-02-20 09:51:59 -0700109 uint8_t fSurfaceOriginKey : 2;
110 bool fSnapVerticesToPixelCenters : 1;
111 bool fHasPointSize : 1;
112 uint8_t fPad : 4;
joshualitt79f8fae2014-10-28 17:59:26 -0700113 };
bsalomon2eda5b32016-09-21 10:53:24 -0700114 GR_STATIC_ASSERT(sizeof(KeyHeader) == 4);
joshualitt79f8fae2014-10-28 17:59:26 -0700115
116 // This should really only be used internally, base classes should return their own headers
117 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
118
joshualitt79f8fae2014-10-28 17:59:26 -0700119 void finalize() {
120 int keyLength = fKey.count();
121 SkASSERT(0 == (keyLength % 4));
122 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
123
124 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
mtklein56da0252015-11-16 11:16:23 -0800125 *checksum = 0; // We'll hash through these bytes, so make sure they're initialized.
mtklein4e976072016-08-08 09:06:27 -0700126 *checksum = SkOpts::hash(fKey.begin(), keyLength);
joshualitt79f8fae2014-10-28 17:59:26 -0700127 }
128
egdaniel5d8f69f2016-09-07 07:24:12 -0700129protected:
130 template<typename T, size_t OFFSET> T* atOffset() {
131 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
132 }
133
134 template<typename T, size_t OFFSET> const T* atOffset() const {
135 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
136 }
137
joshualitt79f8fae2014-10-28 17:59:26 -0700138 // The key, stored in fKey, is composed of four parts:
139 // 1. uint32_t for total key length.
140 // 2. uint32_t for a checksum.
egdaniel5d8f69f2016-09-07 07:24:12 -0700141 // 3. Header struct defined above.
142 // 4. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700143 enum KeyOffsets {
144 // Part 1.
145 kLengthOffset = 0,
146 // Part 2.
147 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
148 // Part 3.
149 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
egdaniel5d8f69f2016-09-07 07:24:12 -0700150 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
151 // Part 4.
152 // This is the offset into the backenend specific part of the key, which includes
153 // per-processor keys.
154 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700155 };
156
157 enum {
158 kMaxPreallocProcessors = 8,
159 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
160 kPreAllocSize = kHeaderOffset + kHeaderSize +
161 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
162 };
163
jvanverthd1e72872015-04-20 12:29:37 -0700164 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
165 const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700166
jvanverthd1e72872015-04-20 12:29:37 -0700167private:
168 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700169};
170
171#endif