blob: e2fe2702758096fc1dd9a1bbb2c852e09fa90241 [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 **/
Chris Daltond7291ba2019-03-07 14:17:03 -070042 static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrPrimitiveProcessor&,
43 bool hasPointSize, const GrPipeline&, GrGpu*);
egdaniel5d8f69f2016-09-07 07:24:12 -070044
joshualitt79f8fae2014-10-28 17:59:26 -070045 // Returns this as a uint32_t array to be used as a key in the program cache.
46 const uint32_t* asKey() const {
47 return reinterpret_cast<const uint32_t*>(fKey.begin());
48 }
49
Greg Daniel2d2c09f2019-01-07 16:14:12 -050050 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value.
51 uint32_t keyLength() const {
52 SkASSERT(0 == (fKey.count() % 4));
53 return fKey.count();
54 }
joshualitt79f8fae2014-10-28 17:59:26 -070055
56 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080057 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080058 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070059 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
60 return *this;
61 }
62
bsalomon89d59882015-06-04 15:34:34 -070063 bool operator== (const GrProgramDesc& that) const {
Greg Daniel2d2c09f2019-01-07 16:14:12 -050064 if (this->keyLength() != that.keyLength()) {
65 return false;
66 }
67
bsalomon89d59882015-06-04 15:34:34 -070068 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
Robert Phillips7f861922018-01-30 13:13:42 +000084 void setSurfaceOriginKey(int key) {
Ethan Nicholas38657112017-02-09 17:01:22 -050085 KeyHeader* header = this->atOffset<KeyHeader, kHeaderOffset>();
Robert Phillips7f861922018-01-30 13:13:42 +000086 header->fSurfaceOriginKey = key;
Ethan Nicholas38657112017-02-09 17:01:22 -050087 }
88
joshualitt79f8fae2014-10-28 17:59:26 -070089 struct KeyHeader {
Chris Daltond7291ba2019-03-07 14:17:03 -070090 bool hasSurfaceOriginKey() const {
91 return SkToBool(fSurfaceOriginKey);
92 }
93 GrProcessor::CustomFeatures processorFeatures() const {
94 return (GrProcessor::CustomFeatures)fProcessorFeatures;
95 }
96
bsalomon7f9b2e42016-01-12 13:29:26 -080097 // Set to uniquely idenitify any swizzling of the shader's output color(s).
Greg Danielf259b8b2019-02-14 09:03:43 -050098 uint16_t fOutputSwizzle;
Chris Dalton535ba8d2018-02-20 09:51:59 -070099 uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
100 uint8_t fCoverageFragmentProcessorCnt;
bsalomon2eda5b32016-09-21 10:53:24 -0700101 // 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 -0700102 uint8_t fSurfaceOriginKey : 2;
Chris Daltond7291ba2019-03-07 14:17:03 -0700103 uint8_t fProcessorFeatures : 1;
Chris Dalton535ba8d2018-02-20 09:51:59 -0700104 bool fSnapVerticesToPixelCenters : 1;
105 bool fHasPointSize : 1;
Chris Daltond7291ba2019-03-07 14:17:03 -0700106 uint8_t fPad : 3;
joshualitt79f8fae2014-10-28 17:59:26 -0700107 };
Greg Danielf259b8b2019-02-14 09:03:43 -0500108 GR_STATIC_ASSERT(sizeof(KeyHeader) == 6);
joshualitt79f8fae2014-10-28 17:59:26 -0700109
110 // This should really only be used internally, base classes should return their own headers
111 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
112
egdaniel5d8f69f2016-09-07 07:24:12 -0700113protected:
114 template<typename T, size_t OFFSET> T* atOffset() {
115 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
116 }
117
118 template<typename T, size_t OFFSET> const T* atOffset() const {
119 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
120 }
121
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500122 // The key, stored in fKey, is composed of two parts:
123 // 1. Header struct defined above.
124 // 2. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700125 enum KeyOffsets {
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500126 kHeaderOffset = 0,
egdaniel5d8f69f2016-09-07 07:24:12 -0700127 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
128 // Part 4.
129 // This is the offset into the backenend specific part of the key, which includes
130 // per-processor keys.
131 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700132 };
133
134 enum {
135 kMaxPreallocProcessors = 8,
136 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
137 kPreAllocSize = kHeaderOffset + kHeaderSize +
138 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
139 };
140
jvanverthd1e72872015-04-20 12:29:37 -0700141 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
142 const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700143
jvanverthd1e72872015-04-20 12:29:37 -0700144private:
145 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700146};
147
148#endif