blob: f304ec53e14acaac59476c7292c71487f22aa5f8 [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"
joshualitt79f8fae2014-10-28 17:59:26 -070015
egdaniel5d8f69f2016-09-07 07:24:12 -070016class GrGLSLCaps;
17class GrPipeline;
18class GrPrimitiveProcessor;
19
20/** This class describes a program to generate. It also serves as a program cache key */
joshualitt79f8fae2014-10-28 17:59:26 -070021class GrProgramDesc {
22public:
23 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
24 GrProgramDesc() {}
25
egdaniel5d8f69f2016-09-07 07:24:12 -070026 /**
27 * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
28 * on the returned GrProgramDesc.
29 *
30 * @param GrPrimitiveProcessor The geometry
bsalomon2eda5b32016-09-21 10:53:24 -070031 * @param hasPointSize Controls whether the shader will output a point size.
egdaniel5d8f69f2016-09-07 07:24:12 -070032 * @param GrPipeline The optimized drawstate. The descriptor will represent a program
33 * which this optstate can use to draw with. The optstate contains
34 * general draw information, as well as the specific color, geometry,
35 * and coverage stages which will be used to generate the GL Program for
36 * this optstate.
37 * @param GrGLSLCaps Capabilities of the GLSL backend.
38 * @param GrProgramDesc The built and finalized descriptor
39 **/
40 static bool Build(GrProgramDesc*,
41 const GrPrimitiveProcessor&,
bsalomon2eda5b32016-09-21 10:53:24 -070042 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -070043 const GrPipeline&,
44 const GrGLSLCaps&);
45
joshualitt79f8fae2014-10-28 17:59:26 -070046 // Returns this as a uint32_t array to be used as a key in the program cache.
47 const uint32_t* asKey() const {
48 return reinterpret_cast<const uint32_t*>(fKey.begin());
49 }
50
51 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
52 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
53 // keys and thus the memcmp will exit early if the keys are of different lengths.
54 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
55
56 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
57 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
58
59 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080060 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080061 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070062 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
63 return *this;
64 }
65
bsalomon89d59882015-06-04 15:34:34 -070066 bool operator== (const GrProgramDesc& that) const {
67 SkASSERT(SkIsAlign4(this->keyLength()));
68 int l = this->keyLength() >> 2;
69 const uint32_t* aKey = this->asKey();
70 const uint32_t* bKey = that.asKey();
71 for (int i = 0; i < l; ++i) {
72 if (aKey[i] != bKey[i]) {
73 return false;
74 }
75 }
76 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070077 }
78
79 bool operator!= (const GrProgramDesc& other) const {
80 return !(*this == other);
81 }
82
83 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
bsalomon89d59882015-06-04 15:34:34 -070084 SkASSERT(SkIsAlign4(a.keyLength()));
85 int l = a.keyLength() >> 2;
86 const uint32_t* aKey = a.asKey();
87 const uint32_t* bKey = b.asKey();
88 for (int i = 0; i < l; ++i) {
89 if (aKey[i] != bKey[i]) {
90 return aKey[i] < bKey[i] ? true : false;
91 }
92 }
93 return false;
joshualitt79f8fae2014-10-28 17:59:26 -070094 }
95
joshualitt79f8fae2014-10-28 17:59:26 -070096 struct KeyHeader {
cdalton28f45b92016-03-07 13:58:26 -080097 // Set to uniquely identify the sample pattern, or 0 if the shader doesn't use sample
98 // locations.
99 uint8_t fSamplePatternKey;
bsalomon7f9b2e42016-01-12 13:29:26 -0800100 // Set to uniquely idenitify any swizzling of the shader's output color(s).
101 uint8_t fOutputSwizzle;
bsalomon2eda5b32016-09-21 10:53:24 -0700102 uint8_t fColorFragmentProcessorCnt : 4;
103 uint8_t fCoverageFragmentProcessorCnt : 4;
104 // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
105 uint8_t fSurfaceOriginKey : 2;
106 uint8_t fIgnoresCoverage : 1;
107 uint8_t fSnapVerticesToPixelCenters : 1;
108 uint8_t fHasPointSize : 1;
109 uint8_t fPad : 3;
joshualitt79f8fae2014-10-28 17:59:26 -0700110 };
bsalomon2eda5b32016-09-21 10:53:24 -0700111 GR_STATIC_ASSERT(sizeof(KeyHeader) == 4);
joshualitt79f8fae2014-10-28 17:59:26 -0700112
113 // This should really only be used internally, base classes should return their own headers
114 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
115
joshualitt79f8fae2014-10-28 17:59:26 -0700116 void finalize() {
117 int keyLength = fKey.count();
118 SkASSERT(0 == (keyLength % 4));
119 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
120
121 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
mtklein56da0252015-11-16 11:16:23 -0800122 *checksum = 0; // We'll hash through these bytes, so make sure they're initialized.
mtklein4e976072016-08-08 09:06:27 -0700123 *checksum = SkOpts::hash(fKey.begin(), keyLength);
joshualitt79f8fae2014-10-28 17:59:26 -0700124 }
125
egdaniel5d8f69f2016-09-07 07:24:12 -0700126protected:
127 template<typename T, size_t OFFSET> T* atOffset() {
128 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
129 }
130
131 template<typename T, size_t OFFSET> const T* atOffset() const {
132 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
133 }
134
joshualitt79f8fae2014-10-28 17:59:26 -0700135 // The key, stored in fKey, is composed of four parts:
136 // 1. uint32_t for total key length.
137 // 2. uint32_t for a checksum.
egdaniel5d8f69f2016-09-07 07:24:12 -0700138 // 3. Header struct defined above.
139 // 4. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700140 enum KeyOffsets {
141 // Part 1.
142 kLengthOffset = 0,
143 // Part 2.
144 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
145 // Part 3.
146 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
egdaniel5d8f69f2016-09-07 07:24:12 -0700147 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
148 // Part 4.
149 // This is the offset into the backenend specific part of the key, which includes
150 // per-processor keys.
151 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700152 };
153
154 enum {
155 kMaxPreallocProcessors = 8,
156 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
157 kPreAllocSize = kHeaderOffset + kHeaderSize +
158 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
159 };
160
jvanverthd1e72872015-04-20 12:29:37 -0700161 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
162 const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700163
jvanverthd1e72872015-04-20 12:29:37 -0700164private:
165 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700166};
167
168#endif