blob: 391bfe1a80c5b58cd0cc1012fc2bc3590e58266c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrTypesPriv.h"
12#include "include/private/SkTArray.h"
13#include "include/private/SkTo.h"
14#include "src/core/SkOpts.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
joshualitt79f8fae2014-10-28 17:59:26 -070017
Robert Phillips901aff02019-10-08 12:32:56 -040018class GrProgramInfo;
Brian Salomon94efbf52016-11-29 13:43:05 -050019class GrShaderCaps;
egdaniel5d8f69f2016-09-07 07:24:12 -070020
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 /**
Robert Phillipsd5c1f342019-10-14 09:50:05 -040028 * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
29 * on the filled in GrProgramDesc.
30 *
31 * @param desc The built and finalized descriptor
32 * @param renderTarget The target of the draw
33 * @param programInfo Program information need to build the key
34 * @param primitiveType Controls whether the shader will output a point size.
35 * @param gpu Pointer to the GrGpu object the program will be used with.
36 **/
Robert Phillips901aff02019-10-08 12:32:56 -040037 static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrProgramInfo&,
Robert Phillips4face832019-10-10 11:58:11 -040038 GrPrimitiveType, GrGpu*);
egdaniel5d8f69f2016-09-07 07:24:12 -070039
Robert Phillipsd5c1f342019-10-14 09:50:05 -040040 // This is strictly an OpenGL call since the other backends have additional data in their
41 // keys
Brian Osmaned58e002019-09-06 14:42:43 -040042 static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) {
43 if (!SkTFitsIn<int>(keyLength)) {
44 return false;
45 }
46 desc->fKey.reset(SkToInt(keyLength));
47 memcpy(desc->fKey.begin(), keyData, keyLength);
48 return true;
49 }
50
joshualitt79f8fae2014-10-28 17:59:26 -070051 // Returns this as a uint32_t array to be used as a key in the program cache.
52 const uint32_t* asKey() const {
53 return reinterpret_cast<const uint32_t*>(fKey.begin());
54 }
55
Greg Daniel2d2c09f2019-01-07 16:14:12 -050056 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value.
57 uint32_t keyLength() const {
58 SkASSERT(0 == (fKey.count() % 4));
59 return fKey.count();
60 }
joshualitt79f8fae2014-10-28 17:59:26 -070061
62 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080063 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080064 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070065 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
66 return *this;
67 }
68
bsalomon89d59882015-06-04 15:34:34 -070069 bool operator== (const GrProgramDesc& that) const {
Greg Daniel2d2c09f2019-01-07 16:14:12 -050070 if (this->keyLength() != that.keyLength()) {
71 return false;
72 }
73
bsalomon89d59882015-06-04 15:34:34 -070074 SkASSERT(SkIsAlign4(this->keyLength()));
75 int l = this->keyLength() >> 2;
76 const uint32_t* aKey = this->asKey();
77 const uint32_t* bKey = that.asKey();
78 for (int i = 0; i < l; ++i) {
79 if (aKey[i] != bKey[i]) {
80 return false;
81 }
82 }
83 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070084 }
85
86 bool operator!= (const GrProgramDesc& other) const {
87 return !(*this == other);
88 }
89
Robert Phillipsd5c1f342019-10-14 09:50:05 -040090 // TODO: remove this use of the header
91 bool hasPointSize() const { return this->header().fHasPointSize; }
Robert Phillips4face832019-10-10 11:58:11 -040092
Robert Phillipsd5c1f342019-10-14 09:50:05 -040093protected:
94 struct KeyHeader {
95 // Set to uniquely identify any swizzling of the shader's output color(s).
Greg Danielf259b8b2019-02-14 09:03:43 -050096 uint16_t fOutputSwizzle;
Chris Dalton535ba8d2018-02-20 09:51:59 -070097 uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
98 uint8_t fCoverageFragmentProcessorCnt;
bsalomon2eda5b32016-09-21 10:53:24 -070099 // 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 -0700100 uint8_t fSurfaceOriginKey : 2;
Chris Daltond7291ba2019-03-07 14:17:03 -0700101 uint8_t fProcessorFeatures : 1;
Chris Dalton535ba8d2018-02-20 09:51:59 -0700102 bool fSnapVerticesToPixelCenters : 1;
103 bool fHasPointSize : 1;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400104 uint8_t fPad : 3;
joshualitt79f8fae2014-10-28 17:59:26 -0700105 };
Greg Danielf259b8b2019-02-14 09:03:43 -0500106 GR_STATIC_ASSERT(sizeof(KeyHeader) == 6);
joshualitt79f8fae2014-10-28 17:59:26 -0700107
joshualitt79f8fae2014-10-28 17:59:26 -0700108 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
109
egdaniel5d8f69f2016-09-07 07:24:12 -0700110 template<typename T, size_t OFFSET> T* atOffset() {
111 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
112 }
113
114 template<typename T, size_t OFFSET> const T* atOffset() const {
115 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
116 }
117
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500118 // The key, stored in fKey, is composed of two parts:
119 // 1. Header struct defined above.
120 // 2. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700121 enum KeyOffsets {
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500122 kHeaderOffset = 0,
egdaniel5d8f69f2016-09-07 07:24:12 -0700123 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
124 // Part 4.
125 // This is the offset into the backenend specific part of the key, which includes
126 // per-processor keys.
127 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700128 };
129
130 enum {
131 kMaxPreallocProcessors = 8,
132 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
133 kPreAllocSize = kHeaderOffset + kHeaderSize +
134 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
135 };
136
jvanverthd1e72872015-04-20 12:29:37 -0700137 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
138 const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700139
jvanverthd1e72872015-04-20 12:29:37 -0700140private:
141 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700142};
143
144#endif