blob: bd7c6ecef9feda52aa7afff55fed7e2209627189 [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"
joshualitt79f8fae2014-10-28 17:59:26 -070014
Robert Phillips03e4c952019-11-26 16:20:22 -050015class GrCaps;
Robert Phillips901aff02019-10-08 12:32:56 -040016class GrProgramInfo;
Robert Phillips03e4c952019-11-26 16:20:22 -050017class GrRenderTarget;
Brian Salomon94efbf52016-11-29 13:43:05 -050018class GrShaderCaps;
egdaniel5d8f69f2016-09-07 07:24:12 -070019
Robert Phillips373bda62019-11-12 13:30:05 -050020/** This class is used to generate a generic program cache key. The Dawn, Metal and Vulkan
21 * backends derive backend-specific versions which add additional information.
22 */
joshualitt79f8fae2014-10-28 17:59:26 -070023class GrProgramDesc {
24public:
Robert Phillipsf6a0b452020-02-18 14:26:46 -050025 GrProgramDesc(const GrProgramDesc& other) : fKey(other.fKey) {} // for SkLRUCache
26
Robert Phillips03e4c952019-11-26 16:20:22 -050027 bool isValid() const { return !fKey.empty(); }
Brian Osmaned58e002019-09-06 14:42:43 -040028
joshualitt79f8fae2014-10-28 17:59:26 -070029 // Returns this as a uint32_t array to be used as a key in the program cache.
30 const uint32_t* asKey() const {
31 return reinterpret_cast<const uint32_t*>(fKey.begin());
32 }
33
Greg Daniel2d2c09f2019-01-07 16:14:12 -050034 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value.
35 uint32_t keyLength() const {
36 SkASSERT(0 == (fKey.count() % 4));
37 return fKey.count();
38 }
joshualitt79f8fae2014-10-28 17:59:26 -070039
40 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080041 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080042 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070043 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
44 return *this;
45 }
46
bsalomon89d59882015-06-04 15:34:34 -070047 bool operator== (const GrProgramDesc& that) const {
Greg Daniel2d2c09f2019-01-07 16:14:12 -050048 if (this->keyLength() != that.keyLength()) {
49 return false;
50 }
51
bsalomon89d59882015-06-04 15:34:34 -070052 SkASSERT(SkIsAlign4(this->keyLength()));
53 int l = this->keyLength() >> 2;
54 const uint32_t* aKey = this->asKey();
55 const uint32_t* bKey = that.asKey();
56 for (int i = 0; i < l; ++i) {
57 if (aKey[i] != bKey[i]) {
58 return false;
59 }
60 }
61 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070062 }
63
64 bool operator!= (const GrProgramDesc& other) const {
65 return !(*this == other);
66 }
67
Robert Phillipsc15e8902019-11-26 14:26:36 -050068 uint32_t initialKeyLength() const { return this->header().fInitialKeyLength; }
69
Robert Phillipsd5c1f342019-10-14 09:50:05 -040070protected:
Robert Phillips03e4c952019-11-26 16:20:22 -050071 friend class GrDawnCaps;
72 friend class GrGLCaps;
73 friend class GrMockCaps;
74 friend class GrMtlCaps;
75 friend class GrVkCaps;
76
77 friend class GrGLGpu; // for ProgramCache to access BuildFromData
78
79 // Creates an uninitialized key that must be populated by Build
80 GrProgramDesc() {}
81
82 /**
83 * Builds a program descriptor.
84 *
85 * @param desc The built descriptor
86 * @param renderTarget The target of the draw
87 * @param programInfo Program information need to build the key
88 * @param caps the caps
89 **/
90 static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrProgramInfo&, const GrCaps&);
91
92 // This is strictly an OpenGL call since the other backends have additional data in their
93 // keys
94 static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) {
95 if (!SkTFitsIn<int>(keyLength)) {
96 return false;
97 }
98 desc->fKey.reset(SkToInt(keyLength));
99 memcpy(desc->fKey.begin(), keyData, keyLength);
100 return true;
101 }
102
Robert Phillips373bda62019-11-12 13:30:05 -0500103 // TODO: this should be removed and converted to just data added to the key
Robert Phillipsd5c1f342019-10-14 09:50:05 -0400104 struct KeyHeader {
105 // Set to uniquely identify any swizzling of the shader's output color(s).
Greg Danielf259b8b2019-02-14 09:03:43 -0500106 uint16_t fOutputSwizzle;
Chris Dalton535ba8d2018-02-20 09:51:59 -0700107 uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
108 uint8_t fCoverageFragmentProcessorCnt;
bsalomon2eda5b32016-09-21 10:53:24 -0700109 // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
Robert Phillipsc15e8902019-11-26 14:26:36 -0500110 uint32_t fSurfaceOriginKey : 2;
111 uint32_t fProcessorFeatures : 1;
112 uint32_t fSnapVerticesToPixelCenters : 1;
113 uint32_t fHasPointSize : 1;
114 // This is the key size (in bytes) after core key construction. It doesn't include any
115 // portions added by the platform-specific backends.
116 uint32_t fInitialKeyLength : 27;
joshualitt79f8fae2014-10-28 17:59:26 -0700117 };
Brian Salomon4dea72a2019-12-18 10:43:10 -0500118 static_assert(sizeof(KeyHeader) == 8);
Robert Phillipsc15e8902019-11-26 14:26:36 -0500119
120 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
joshualitt79f8fae2014-10-28 17:59:26 -0700121
egdaniel5d8f69f2016-09-07 07:24:12 -0700122 template<typename T, size_t OFFSET> T* atOffset() {
123 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
124 }
125
Robert Phillipsc15e8902019-11-26 14:26:36 -0500126 template<typename T, size_t OFFSET> const T* atOffset() const {
127 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
128 }
129
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500130 // The key, stored in fKey, is composed of two parts:
131 // 1. Header struct defined above.
132 // 2. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700133 enum KeyOffsets {
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500134 kHeaderOffset = 0,
egdaniel5d8f69f2016-09-07 07:24:12 -0700135 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
egdaniel5d8f69f2016-09-07 07:24:12 -0700136 // This is the offset into the backenend specific part of the key, which includes
137 // per-processor keys.
138 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700139 };
140
141 enum {
142 kMaxPreallocProcessors = 8,
143 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
144 kPreAllocSize = kHeaderOffset + kHeaderSize +
145 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
146 };
147
jvanverthd1e72872015-04-20 12:29:37 -0700148 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700149
jvanverthd1e72872015-04-20 12:29:37 -0700150private:
151 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700152};
153
154#endif