blob: 9ba20aa94ae5a5de4e952472cc1de8ba2589f863 [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"
13#include "SkChecksum.h"
14
bsalomon861e1032014-12-16 07:33:49 -080015class GrGLGpu;
joshualitt79f8fae2014-10-28 17:59:26 -070016
17/** This class describes a program to generate. It also serves as a program cache key. Very little
18 of this is GL-specific. The GL-specific parts could be factored out into a subclass. */
19class GrProgramDesc {
20public:
21 // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
22 GrProgramDesc() {}
23
24 // Returns this as a uint32_t array to be used as a key in the program cache.
25 const uint32_t* asKey() const {
26 return reinterpret_cast<const uint32_t*>(fKey.begin());
27 }
28
29 // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
30 // keys the size of either key can be used with memcmp() since the lengths themselves begin the
31 // keys and thus the memcmp will exit early if the keys are of different lengths.
32 uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
33
34 // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
35 uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
36
37 GrProgramDesc& operator= (const GrProgramDesc& other) {
bsalomonccb328d2014-12-11 13:31:06 -080038 uint32_t keyLength = other.keyLength();
bsalomonef3fcd82014-12-12 08:51:38 -080039 fKey.reset(SkToInt(keyLength));
joshualitt79f8fae2014-10-28 17:59:26 -070040 memcpy(fKey.begin(), other.fKey.begin(), keyLength);
41 return *this;
42 }
43
44 bool operator== (const GrProgramDesc& other) const {
45 // The length is masked as a hint to the compiler that the address will be 4 byte aligned.
46 return 0 == memcmp(this->asKey(), other.asKey(), this->keyLength() & ~0x3);
47 }
48
49 bool operator!= (const GrProgramDesc& other) const {
50 return !(*this == other);
51 }
52
53 static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
54 return memcmp(a.asKey(), b.asKey(), a.keyLength() & ~0x3) < 0;
55 }
56
joshualitt79f8fae2014-10-28 17:59:26 -070057 struct KeyHeader {
58 uint8_t fDstReadKey; // set by GrGLShaderBuilder if there
59 // are effects that must read the dst.
60 // Otherwise, 0.
61 uint8_t fFragPosKey; // set by GrGLShaderBuilder if there are
62 // effects that read the fragment position.
63 // Otherwise, 0.
64
joshualitt79f8fae2014-10-28 17:59:26 -070065 int8_t fColorEffectCnt;
66 int8_t fCoverageEffectCnt;
67 };
68
joshualitt79f8fae2014-10-28 17:59:26 -070069 int numColorEffects() const {
70 return this->header().fColorEffectCnt;
71 }
72
73 int numCoverageEffects() const {
74 return this->header().fCoverageEffectCnt;
75 }
76
77 int numTotalEffects() const { return this->numColorEffects() + this->numCoverageEffects(); }
78
79 // This should really only be used internally, base classes should return their own headers
80 const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
81
joshualitt79f8fae2014-10-28 17:59:26 -070082 // A struct to communicate descriptor information to the program descriptor builder
83 struct DescInfo {
joshualittdafa4d02014-12-04 08:59:10 -080084 bool operator==(const DescInfo& that) const {
joshualitt9b989322014-12-15 14:16:27 -080085 return fReadsDst == that.fReadsDst &&
joshualittdafa4d02014-12-04 08:59:10 -080086 fReadsFragPosition == that.fReadsFragPosition &&
egdanielc2304142014-12-11 13:15:13 -080087 fRequiresLocalCoordAttrib == that.fRequiresLocalCoordAttrib;
joshualittdafa4d02014-12-04 08:59:10 -080088 }
89 bool operator!=(const DescInfo& that) const { return !(*this == that); };
joshualitt79f8fae2014-10-28 17:59:26 -070090
91 // These flags give aggregated info on the processor stages that are used when building
92 // programs.
93 bool fReadsDst;
94 bool fReadsFragPosition;
95 bool fRequiresLocalCoordAttrib;
joshualitt79f8fae2014-10-28 17:59:26 -070096 };
97
98private:
99 template<typename T, size_t OFFSET> T* atOffset() {
100 return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
101 }
102
103 template<typename T, size_t OFFSET> const T* atOffset() const {
104 return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
105 }
106
107 void finalize() {
108 int keyLength = fKey.count();
109 SkASSERT(0 == (keyLength % 4));
110 *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
111
112 uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
113 *checksum = 0;
114 *checksum = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.begin()), keyLength);
115 }
116
117 // The key, stored in fKey, is composed of four parts:
118 // 1. uint32_t for total key length.
119 // 2. uint32_t for a checksum.
120 // 3. Header struct defined above. Also room for extensions to the header
121 // 4. A Backend specific payload. Room is preallocated for this
122 enum KeyOffsets {
123 // Part 1.
124 kLengthOffset = 0,
125 // Part 2.
126 kChecksumOffset = kLengthOffset + sizeof(uint32_t),
127 // Part 3.
128 kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
129 kHeaderSize = SkAlign4(2 * sizeof(KeyHeader)),
130 };
131
132 enum {
133 kMaxPreallocProcessors = 8,
134 kIntsPerProcessor = 4, // This is an overestimate of the average effect key size.
135 kPreAllocSize = kHeaderOffset + kHeaderSize +
136 kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
137 };
138
139 SkSTArray<kPreAllocSize, uint8_t, true> fKey;
140
141 friend class GrGLProgramDescBuilder;
142};
143
144#endif