blob: 9d9ea0088c9d1f5e237ed85849628e79e62e7339 [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
Robert Phillips373bda62019-11-12 13:30:05 -050021/** This class is used to generate a generic program cache key. The Dawn, Metal and Vulkan
22 * backends derive backend-specific versions which add additional information.
23 */
joshualitt79f8fae2014-10-28 17:59:26 -070024class GrProgramDesc {
25public:
Robert Phillips373bda62019-11-12 13:30:05 -050026 // Creates an uninitialized key that must be populated by Build
joshualitt79f8fae2014-10-28 17:59:26 -070027 GrProgramDesc() {}
28
egdaniel5d8f69f2016-09-07 07:24:12 -070029 /**
Robert Phillips373bda62019-11-12 13:30:05 -050030 * Builds a program descriptor.
Robert Phillipsd5c1f342019-10-14 09:50:05 -040031 *
Robert Phillips373bda62019-11-12 13:30:05 -050032 * @param desc The built descriptor
Robert Phillipsd5c1f342019-10-14 09:50:05 -040033 * @param renderTarget The target of the draw
34 * @param programInfo Program information need to build the key
Robert Phillips323471e2019-11-11 11:33:37 -050035 * @param caps the caps
Robert Phillipsd5c1f342019-10-14 09:50:05 -040036 **/
Robert Phillips323471e2019-11-11 11:33:37 -050037 static bool Build(GrProgramDesc*, const GrRenderTarget*, const GrProgramInfo&, const GrCaps&);
egdaniel5d8f69f2016-09-07 07:24:12 -070038
Robert Phillipsd5c1f342019-10-14 09:50:05 -040039 // This is strictly an OpenGL call since the other backends have additional data in their
40 // keys
Brian Osmaned58e002019-09-06 14:42:43 -040041 static bool BuildFromData(GrProgramDesc* desc, const void* keyData, size_t keyLength) {
42 if (!SkTFitsIn<int>(keyLength)) {
43 return false;
44 }
45 desc->fKey.reset(SkToInt(keyLength));
46 memcpy(desc->fKey.begin(), keyData, keyLength);
47 return true;
48 }
49
joshualitt79f8fae2014-10-28 17:59:26 -070050 // Returns this as a uint32_t array to be used as a key in the program cache.
51 const uint32_t* asKey() const {
52 return reinterpret_cast<const uint32_t*>(fKey.begin());
53 }
54
Greg Daniel2d2c09f2019-01-07 16:14:12 -050055 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value.
56 uint32_t keyLength() const {
57 SkASSERT(0 == (fKey.count() % 4));
58 return fKey.count();
59 }
joshualitt79f8fae2014-10-28 17:59:26 -070060
61 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080062 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080063 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070064 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
65 return *this;
66 }
67
bsalomon89d59882015-06-04 15:34:34 -070068 bool operator== (const GrProgramDesc& that) const {
Greg Daniel2d2c09f2019-01-07 16:14:12 -050069 if (this->keyLength() != that.keyLength()) {
70 return false;
71 }
72
bsalomon89d59882015-06-04 15:34:34 -070073 SkASSERT(SkIsAlign4(this->keyLength()));
74 int l = this->keyLength() >> 2;
75 const uint32_t* aKey = this->asKey();
76 const uint32_t* bKey = that.asKey();
77 for (int i = 0; i < l; ++i) {
78 if (aKey[i] != bKey[i]) {
79 return false;
80 }
81 }
82 return true;
joshualitt79f8fae2014-10-28 17:59:26 -070083 }
84
85 bool operator!= (const GrProgramDesc& other) const {
86 return !(*this == other);
87 }
88
Robert Phillipsd5c1f342019-10-14 09:50:05 -040089protected:
Robert Phillips373bda62019-11-12 13:30:05 -050090 // TODO: this should be removed and converted to just data added to the key
Robert Phillipsd5c1f342019-10-14 09:50:05 -040091 struct KeyHeader {
92 // Set to uniquely identify any swizzling of the shader's output color(s).
Greg Danielf259b8b2019-02-14 09:03:43 -050093 uint16_t fOutputSwizzle;
Chris Dalton535ba8d2018-02-20 09:51:59 -070094 uint8_t fColorFragmentProcessorCnt; // Can be packed into 4 bits if required.
95 uint8_t fCoverageFragmentProcessorCnt;
bsalomon2eda5b32016-09-21 10:53:24 -070096 // 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 -070097 uint8_t fSurfaceOriginKey : 2;
Chris Daltond7291ba2019-03-07 14:17:03 -070098 uint8_t fProcessorFeatures : 1;
Chris Dalton535ba8d2018-02-20 09:51:59 -070099 bool fSnapVerticesToPixelCenters : 1;
100 bool fHasPointSize : 1;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400101 uint8_t fPad : 3;
joshualitt79f8fae2014-10-28 17:59:26 -0700102 };
Greg Danielf259b8b2019-02-14 09:03:43 -0500103 GR_STATIC_ASSERT(sizeof(KeyHeader) == 6);
joshualitt79f8fae2014-10-28 17:59:26 -0700104
egdaniel5d8f69f2016-09-07 07:24:12 -0700105 template<typename T, size_t OFFSET> T* atOffset() {
106 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
107 }
108
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500109 // The key, stored in fKey, is composed of two parts:
110 // 1. Header struct defined above.
111 // 2. A Backend specific payload which includes the per-processor keys.
joshualitt79f8fae2014-10-28 17:59:26 -0700112 enum KeyOffsets {
Greg Daniel2d2c09f2019-01-07 16:14:12 -0500113 kHeaderOffset = 0,
egdaniel5d8f69f2016-09-07 07:24:12 -0700114 kHeaderSize = SkAlign4(sizeof(KeyHeader)),
egdaniel5d8f69f2016-09-07 07:24:12 -0700115 // This is the offset into the backenend specific part of the key, which includes
116 // per-processor keys.
117 kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
joshualitt79f8fae2014-10-28 17:59:26 -0700118 };
119
120 enum {
121 kMaxPreallocProcessors = 8,
122 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
123 kPreAllocSize = kHeaderOffset + kHeaderSize +
124 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
125 };
126
jvanverthd1e72872015-04-20 12:29:37 -0700127 SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
joshualitt79f8fae2014-10-28 17:59:26 -0700128
jvanverthd1e72872015-04-20 12:29:37 -0700129private:
130 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
joshualitt79f8fae2014-10-28 17:59:26 -0700131};
132
133#endif