blob: 7eaf880f6f9b2bee6085f945fb6609863fa063f6 [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:
Brian Osman9b510a32021-02-26 14:29:39 -050025 GrProgramDesc(const GrProgramDesc& other) = default;
Robert Phillipsf6a0b452020-02-18 14:26:46 -050026
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);
Brian Osman9b510a32021-02-26 14:29:39 -050044 fInitialKeyLength = other.fInitialKeyLength;
joshualitt79f8fae2014-10-28 17:59:26 -070045 return *this;
46 }
47
bsalomon89d59882015-06-04 15:34:34 -070048 bool operator== (const GrProgramDesc& that) const {
Greg Daniel2d2c09f2019-01-07 16:14:12 -050049 if (this->keyLength() != that.keyLength()) {
50 return false;
51 }
52
bsalomon89d59882015-06-04 15:34:34 -070053 SkASSERT(SkIsAlign4(this->keyLength()));
54 int l = this->keyLength() >> 2;
55 const uint32_t* aKey = this->asKey();
56 const uint32_t* bKey = that.asKey();
57 for (int i = 0; i < l; ++i) {
58 if (aKey[i] != bKey[i]) {
59 return false;
60 }
61 }
62 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070063 }
64
65 bool operator!= (const GrProgramDesc& other) const {
66 return !(*this == other);
67 }
68
Brian Osman9b510a32021-02-26 14:29:39 -050069 uint32_t initialKeyLength() const { return fInitialKeyLength; }
Robert Phillipsc15e8902019-11-26 14:26:36 -050070
Robert Phillipsd5c1f342019-10-14 09:50:05 -040071protected:
Robert Phillips03e4c952019-11-26 16:20:22 -050072 friend class GrDawnCaps;
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050073 friend class GrD3DCaps;
Robert Phillips03e4c952019-11-26 16:20:22 -050074 friend class GrGLCaps;
75 friend class GrMockCaps;
76 friend class GrMtlCaps;
77 friend class GrVkCaps;
78
79 friend class GrGLGpu; // for ProgramCache to access BuildFromData
80
81 // Creates an uninitialized key that must be populated by Build
82 GrProgramDesc() {}
83
84 /**
85 * Builds a program descriptor.
86 *
87 * @param desc The built descriptor
88 * @param renderTarget The target of the draw
89 * @param programInfo Program information need to build the key
90 * @param caps the caps
91 **/
Brian Salomonf7f54332020-07-28 09:23:35 -040092 static bool Build(GrProgramDesc*, GrRenderTarget*, const GrProgramInfo&, const GrCaps&);
Robert Phillips03e4c952019-11-26 16:20:22 -050093
John Stilesd3feb6f2020-07-23 18:18:12 -040094 // This is strictly an OpenGL call since the other backends have additional data in their keys.
Robert Phillips03e4c952019-11-26 16:20:22 -050095 static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) {
96 if (!SkTFitsIn<int>(keyLength)) {
97 return false;
98 }
99 desc->fKey.reset(SkToInt(keyLength));
100 memcpy(desc->fKey.begin(), keyData, keyLength);
101 return true;
102 }
103
joshualitt79f8fae2014-10-28 17:59:26 -0700104 enum {
Brian Osman9b510a32021-02-26 14:29:39 -0500105 kHeaderSize = 4, // "header" in ::Build
joshualitt79f8fae2014-10-28 17:59:26 -0700106 kMaxPreallocProcessors = 8,
107 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
Brian Osman9b510a32021-02-26 14:29:39 -0500108 kPreAllocSize = kHeaderSize +
joshualitt79f8fae2014-10-28 17:59:26 -0700109 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
110 };
111
jvanverthd1e72872015-04-20 12:29:37 -0700112 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700113
jvanverthd1e72872015-04-20 12:29:37 -0700114private:
115 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
Brian Osman9b510a32021-02-26 14:29:39 -0500116 uint32_t fInitialKeyLength = 0;
joshualitt79f8fae2014-10-28 17:59:26 -0700117};
118
119#endif