blob: a20cdfc56bcaec4e47cd8bb17250057fe9c7127e [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"
Ethan Nicholas38657112017-02-09 17:01:22 -050015#include "glsl/GrGLSLFragmentShaderBuilder.h"
joshualitt79f8fae2014-10-28 17:59:26 -070016
Brian Salomon94efbf52016-11-29 13:43:05 -050017class GrShaderCaps;
egdaniel5d8f69f2016-09-07 07:24:12 -070018class GrPipeline;
19class GrPrimitiveProcessor;
20
21/** This class describes a program to generate. It also serves as a program cache key */
joshualitt79f8fae2014-10-28 17:59:26 -070022class GrProgramDesc {
23public:
24 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
25 GrProgramDesc() {}
26
egdaniel5d8f69f2016-09-07 07:24:12 -070027 /**
28 * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
29 * on the returned GrProgramDesc.
30 *
31 * @param GrPrimitiveProcessor The geometry
bsalomon2eda5b32016-09-21 10:53:24 -070032 * @param hasPointSize Controls whether the shader will output a point size.
egdaniel5d8f69f2016-09-07 07:24:12 -070033 * @param GrPipeline The optimized drawstate. The descriptor will represent a program
34 * which this optstate can use to draw with. The optstate contains
35 * general draw information, as well as the specific color, geometry,
36 * and coverage stages which will be used to generate the GL Program for
37 * this optstate.
Brian Salomon94efbf52016-11-29 13:43:05 -050038 * @param GrShaderCaps Capabilities of the shading language.
egdaniel5d8f69f2016-09-07 07:24:12 -070039 * @param GrProgramDesc The built and finalized descriptor
40 **/
41 static bool Build(GrProgramDesc*,
42 const GrPrimitiveProcessor&,
bsalomon2eda5b32016-09-21 10:53:24 -070043 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -070044 const GrPipeline&,
Brian Salomon94efbf52016-11-29 13:43:05 -050045 const GrShaderCaps&);
egdaniel5d8f69f2016-09-07 07:24:12 -070046
joshualitt79f8fae2014-10-28 17:59:26 -070047 // Returns this as a uint32_t array to be used as a key in the program cache.
48 const uint32_t* asKey() const {
49 return reinterpret_cast<const uint32_t*>(fKey.begin());
50 }
51
52 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
53 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
54 // keys and thus the memcmp will exit early if the keys are of different lengths.
55 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
56
57 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
58 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
59
60 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080061 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080062 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070063 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
64 return *this;
65 }
66
bsalomon89d59882015-06-04 15:34:34 -070067 bool operator== (const GrProgramDesc& that) const {
68 SkASSERT(SkIsAlign4(this->keyLength()));
69 int l = this->keyLength() >> 2;
70 const uint32_t* aKey = this->asKey();
71 const uint32_t* bKey = that.asKey();
72 for (int i = 0; i < l; ++i) {
73 if (aKey[i] != bKey[i]) {
74 return false;
75 }
76 }
77 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070078 }
79
80 bool operator!= (const GrProgramDesc& other) const {
81 return !(*this == other);
82 }
83
Ethan Nicholas38657112017-02-09 17:01:22 -050084 void setSurfaceOriginKey(int key) {
85 KeyHeader* header = this->atOffset<KeyHeader, kHeaderOffset>();
86 header->fSurfaceOriginKey = key;
87 }
88
joshualitt79f8fae2014-10-28 17:59:26 -070089 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
bsalomon89d59882015-06-04 15:34:34 -070090 SkASSERT(SkIsAlign4(a.keyLength()));
91 int l = a.keyLength() >> 2;
92 const uint32_t* aKey = a.asKey();
93 const uint32_t* bKey = b.asKey();
94 for (int i = 0; i < l; ++i) {
95 if (aKey[i] != bKey[i]) {
96 return aKey[i] < bKey[i] ? true : false;
97 }
98 }
99 return false;
joshualitt79f8fae2014-10-28 17:59:26 -0700100 }
101
joshualitt79f8fae2014-10-28 17:59:26 -0700102 struct KeyHeader {
cdalton28f45b92016-03-07 13:58:26 -0800103 // Set to uniquely identify the sample pattern, or 0 if the shader doesn't use sample
104 // locations.
105 uint8_t fSamplePatternKey;
bsalomon7f9b2e42016-01-12 13:29:26 -0800106 // Set to uniquely idenitify any swizzling of the shader's output color(s).
107 uint8_t fOutputSwizzle;
bsalomon2eda5b32016-09-21 10:53:24 -0700108 uint8_t fColorFragmentProcessorCnt : 4;
109 uint8_t fCoverageFragmentProcessorCnt : 4;
110 // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
111 uint8_t fSurfaceOriginKey : 2;
bsalomon2eda5b32016-09-21 10:53:24 -0700112 uint8_t fSnapVerticesToPixelCenters : 1;
113 uint8_t fHasPointSize : 1;
Brian Salomon8c852be2017-01-04 10:44:42 -0500114 uint8_t fPad : 4;
joshualitt79f8fae2014-10-28 17:59:26 -0700115 };
bsalomon2eda5b32016-09-21 10:53:24 -0700116 GR_STATIC_ASSERT(sizeof(KeyHeader) == 4);
joshualitt79f8fae2014-10-28 17:59:26 -0700117
118 // This should really only be used internally, base classes should return their own headers
119 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
120
joshualitt79f8fae2014-10-28 17:59:26 -0700121 void finalize() {
122 int keyLength = fKey.count();
123 SkASSERT(0 == (keyLength % 4));
124 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
125
126 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
mtklein56da0252015-11-16 11:16:23 -0800127 *checksum = 0; // We'll hash through these bytes, so make sure they're initialized.
mtklein4e976072016-08-08 09:06:27 -0700128 *checksum = SkOpts::hash(fKey.begin(), keyLength);
joshualitt79f8fae2014-10-28 17:59:26 -0700129 }
130
egdaniel5d8f69f2016-09-07 07:24:12 -0700131protected:
132 template<typename T, size_t OFFSET> T* atOffset() {
133 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
134 }
135
136 template<typename T, size_t OFFSET> const T* atOffset() const {
137 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
138 }
139
joshualitt79f8fae2014-10-28 17:59:26 -0700140 // The key, stored in fKey, is composed of four parts:
141 // 1. uint32_t for total key length.
142 // 2. uint32_t for a checksum.
egdaniel5d8f69f2016-09-07 07:24:12 -0700143 // 3. Header struct defined above.
144 // 4. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700145 enum KeyOffsets {
146 // Part 1.
147 kLengthOffset = 0,
148 // Part 2.
149 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
150 // Part 3.
151 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
egdaniel5d8f69f2016-09-07 07:24:12 -0700152 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
153 // Part 4.
154 // This is the offset into the backenend specific part of the key, which includes
155 // per-processor keys.
156 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700157 };
158
159 enum {
160 kMaxPreallocProcessors = 8,
161 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
162 kPreAllocSize = kHeaderOffset + kHeaderSize +
163 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
164 };
165
jvanverthd1e72872015-04-20 12:29:37 -0700166 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
167 const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700168
jvanverthd1e72872015-04-20 12:29:37 -0700169private:
170 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700171};
172
173#endif