1 /*
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 
11 #include "GrColor.h"
12 #include "GrTypesPriv.h"
13 #include "SkOpts.h"
14 #include "SkTArray.h"
15 
16 class GrGLSLCaps;
17 class GrPipeline;
18 class GrPrimitiveProcessor;
19 
20 /** This class describes a program to generate. It also serves as a program cache key */
21 class GrProgramDesc {
22 public:
23     // Creates an uninitialized key that must be populated by GrGpu::buildProgramDesc()
GrProgramDesc()24     GrProgramDesc() {}
25 
26     /**
27     * Builds a program descriptor. Before the descriptor can be used, the client must call finalize
28     * on the returned GrProgramDesc.
29     *
30     * @param GrPrimitiveProcessor The geometry
31     * @param hasPointSize Controls whether the shader will output a point size.
32     * @param GrPipeline  The optimized drawstate.  The descriptor will represent a program
33     *                        which this optstate can use to draw with.  The optstate contains
34     *                        general draw information, as well as the specific color, geometry,
35     *                        and coverage stages which will be used to generate the GL Program for
36     *                        this optstate.
37     * @param GrGLSLCaps     Capabilities of the GLSL backend.
38     * @param GrProgramDesc  The built and finalized descriptor
39     **/
40     static bool Build(GrProgramDesc*,
41                       const GrPrimitiveProcessor&,
42                       bool hasPointSize,
43                       const GrPipeline&,
44                       const GrGLSLCaps&);
45 
46     // Returns this as a uint32_t array to be used as a key in the program cache.
asKey()47     const uint32_t* asKey() const {
48         return reinterpret_cast<const uint32_t*>(fKey.begin());
49     }
50 
51     // Gets the number of bytes in asKey(). It will be a 4-byte aligned value. When comparing two
52     // keys the size of either key can be used with memcmp() since the lengths themselves begin the
53     // keys and thus the memcmp will exit early if the keys are of different lengths.
keyLength()54     uint32_t keyLength() const { return *this->atOffset<uint32_t, kLengthOffset>(); }
55 
56     // Gets the a checksum of the key. Can be used as a hash value for a fast lookup in a cache.
getChecksum()57     uint32_t getChecksum() const { return *this->atOffset<uint32_t, kChecksumOffset>(); }
58 
59     GrProgramDesc& operator= (const GrProgramDesc& other) {
60         uint32_t keyLength = other.keyLength();
61         fKey.reset(SkToInt(keyLength));
62         memcpy(fKey.begin(), other.fKey.begin(), keyLength);
63         return *this;
64     }
65 
66     bool operator== (const GrProgramDesc& that) const {
67         SkASSERT(SkIsAlign4(this->keyLength()));
68         int l = this->keyLength() >> 2;
69         const uint32_t* aKey = this->asKey();
70         const uint32_t* bKey = that.asKey();
71         for (int i = 0; i < l; ++i) {
72             if (aKey[i] != bKey[i]) {
73                 return false;
74             }
75         }
76         return true;
77     }
78 
79     bool operator!= (const GrProgramDesc& other) const {
80         return !(*this == other);
81     }
82 
Less(const GrProgramDesc & a,const GrProgramDesc & b)83     static bool Less(const GrProgramDesc& a, const GrProgramDesc& b) {
84         SkASSERT(SkIsAlign4(a.keyLength()));
85         int l = a.keyLength() >> 2;
86         const uint32_t* aKey = a.asKey();
87         const uint32_t* bKey = b.asKey();
88         for (int i = 0; i < l; ++i) {
89             if (aKey[i] != bKey[i]) {
90                 return aKey[i] < bKey[i] ? true : false;
91             }
92         }
93         return false;
94     }
95 
96     struct KeyHeader {
97         // Set to uniquely identify the sample pattern, or 0 if the shader doesn't use sample
98         // locations.
99         uint8_t                     fSamplePatternKey;
100         // Set to uniquely idenitify any swizzling of the shader's output color(s).
101         uint8_t                     fOutputSwizzle;
102         uint8_t                     fColorFragmentProcessorCnt : 4;
103         uint8_t                     fCoverageFragmentProcessorCnt : 4;
104         // Set to uniquely identify the rt's origin, or 0 if the shader does not require this info.
105         uint8_t                     fSurfaceOriginKey : 2;
106         uint8_t                     fIgnoresCoverage : 1;
107         uint8_t                     fSnapVerticesToPixelCenters : 1;
108         uint8_t                     fHasPointSize : 1;
109         uint8_t                     fPad : 3;
110     };
111     GR_STATIC_ASSERT(sizeof(KeyHeader) == 4);
112 
113     // This should really only be used internally, base classes should return their own headers
header()114     const KeyHeader& header() const { return *this->atOffset<KeyHeader, kHeaderOffset>(); }
115 
finalize()116     void finalize() {
117         int keyLength = fKey.count();
118         SkASSERT(0 == (keyLength % 4));
119         *(this->atOffset<uint32_t, GrProgramDesc::kLengthOffset>()) = SkToU32(keyLength);
120 
121         uint32_t* checksum = this->atOffset<uint32_t, GrProgramDesc::kChecksumOffset>();
122         *checksum = 0;  // We'll hash through these bytes, so make sure they're initialized.
123         *checksum = SkOpts::hash(fKey.begin(), keyLength);
124     }
125 
126 protected:
atOffset()127     template<typename T, size_t OFFSET> T* atOffset() {
128         return reinterpret_cast<T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
129     }
130 
atOffset()131     template<typename T, size_t OFFSET> const T* atOffset() const {
132         return reinterpret_cast<const T*>(reinterpret_cast<intptr_t>(fKey.begin()) + OFFSET);
133     }
134 
135     // The key, stored in fKey, is composed of four parts:
136     // 1. uint32_t for total key length.
137     // 2. uint32_t for a checksum.
138     // 3. Header struct defined above.
139     // 4. A Backend specific payload which includes the per-processor keys.
140     enum KeyOffsets {
141         // Part 1.
142         kLengthOffset = 0,
143         // Part 2.
144         kChecksumOffset = kLengthOffset + sizeof(uint32_t),
145         // Part 3.
146         kHeaderOffset = kChecksumOffset + sizeof(uint32_t),
147         kHeaderSize = SkAlign4(sizeof(KeyHeader)),
148         // Part 4.
149         // This is the offset into the backenend specific part of the key, which includes
150         // per-processor keys.
151         kProcessorKeysOffset = kHeaderOffset + kHeaderSize,
152     };
153 
154     enum {
155         kMaxPreallocProcessors = 8,
156         kIntsPerProcessor      = 4,    // This is an overestimate of the average effect key size.
157         kPreAllocSize = kHeaderOffset + kHeaderSize +
158                         kMaxPreallocProcessors * sizeof(uint32_t) * kIntsPerProcessor,
159     };
160 
key()161     SkSTArray<kPreAllocSize, uint8_t, true>& key() { return fKey; }
key()162     const SkSTArray<kPreAllocSize, uint8_t, true>& key() const { return fKey; }
163 
164 private:
165     SkSTArray<kPreAllocSize, uint8_t, true> fKey;
166 };
167 
168 #endif
169