OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "GrGLProgramDesc.h" | 7 #include "GrGLProgramDesc.h" |
8 | 8 |
9 #include "GrProcessor.h" | 9 #include "GrProcessor.h" |
10 #include "GrGLGpu.h" | 10 #include "GrGLGpu.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 return false; | 24 return false; |
25 } | 25 } |
26 const char* swizzleMap = caps.getSwizzleMap(config); | 26 const char* swizzleMap = caps.getSwizzleMap(config); |
27 | 27 |
28 return SkToBool(memcmp(swizzleMap, "rgba", 4)); | 28 return SkToBool(memcmp(swizzleMap, "rgba", 4)); |
29 } | 29 } |
30 | 30 |
31 static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) { | 31 static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) { |
32 uint32_t key = 0; | 32 uint32_t key = 0; |
33 int numTextures = proc.numTextures(); | 33 int numTextures = proc.numTextures(); |
34 int shift = 0; | |
35 for (int t = 0; t < numTextures; ++t) { | 34 for (int t = 0; t < numTextures; ++t) { |
36 const GrTextureAccess& access = proc.textureAccess(t); | 35 const GrTextureAccess& access = proc.textureAccess(t); |
37 if (swizzle_requires_alpha_remapping(*caps.glslCaps(), access.getTexture
()->config())) { | 36 if (swizzle_requires_alpha_remapping(*caps.glslCaps(), access.getTexture
()->config())) { |
38 key |= 1 << shift; | 37 key |= 1 << t; |
39 } | 38 } |
40 if (GR_GL_TEXTURE_EXTERNAL == static_cast<GrGLTexture*>(access.getTextur
e())->target()) { | |
41 key |= 2 << shift; | |
42 } | |
43 shift += 2; | |
44 } | 39 } |
45 return key; | 40 return key; |
46 } | 41 } |
47 | 42 |
48 /** | 43 /** |
49 * A function which emits a meta key into the key builder. This is required bec
ause shader code may | 44 * A function which emits a meta key into the key builder. This is required bec
ause shader code may |
50 * be dependent on properties of the effect that the effect itself doesn't use | 45 * be dependent on properties of the effect that the effect itself doesn't use |
51 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for | 46 * in its key (e.g. the pixel format of textures used). So we create a meta-key
for |
52 * every effect using this function. It is also responsible for inserting the ef
fect's class ID | 47 * every effect using this function. It is also responsible for inserting the ef
fect's class ID |
53 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many | 48 * which must be different for every GrProcessor subclass. It can fail if an eff
ect uses too many |
54 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, bot
h FPs and GPs share | 49 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, bot
h FPs and GPs share |
55 * this function because it is hairy, though FPs do not have attribs, and GPs do
not have transforms | 50 * this function because it is hairy, though FPs do not have attribs, and GPs do
not have transforms |
| 51 * |
| 52 * TODO: A better name for this function would be "compute" instead of "get". |
56 */ | 53 */ |
57 static bool gen_meta_key(const GrProcessor& proc, | 54 static bool get_meta_key(const GrProcessor& proc, |
58 const GrGLCaps& caps, | 55 const GrGLCaps& caps, |
59 uint32_t transformKey, | 56 uint32_t transformKey, |
60 GrProcessorKeyBuilder* b) { | 57 GrProcessorKeyBuilder* b) { |
61 size_t processorKeySize = b->size(); | 58 size_t processorKeySize = b->size(); |
62 uint32_t textureKey = gen_texture_key(proc, caps); | 59 uint32_t textureKey = gen_texture_key(proc, caps); |
63 uint32_t classID = proc.classID(); | 60 uint32_t classID = proc.classID(); |
64 | 61 |
65 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they | 62 // Currently we allow 16 bits for each of the above portions of the meta-key
. Fail if they |
66 // don't fit. | 63 // don't fit. |
67 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); | 64 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
68 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { | 65 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { |
69 return false; | 66 return false; |
70 } | 67 } |
71 if (processorKeySize > SK_MaxU16) { | 68 if (processorKeySize > SK_MaxU16) { |
72 return false; | 69 return false; |
73 } | 70 } |
74 | 71 |
75 uint32_t* key = b->add32n(2); | 72 uint32_t* key = b->add32n(2); |
76 key[0] = (textureKey << 16 | transformKey); | 73 key[0] = (textureKey << 16 | transformKey); |
77 key[1] = (classID << 16 | SkToU16(processorKeySize)); | 74 key[1] = (classID << 16 | SkToU16(processorKeySize)); |
78 return true; | 75 return true; |
79 } | 76 } |
80 | 77 |
81 static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, | 78 /* |
| 79 * TODO: A better name for this function would be "compute" instead of "get". |
| 80 */ |
| 81 static bool get_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
82 const GrFragmentProcessor& fp, | 82 const GrFragmentProcessor& fp, |
83 const GrGLCaps& caps, | 83 const GrGLCaps& caps, |
84 GrProcessorKeyBuilder* b) { | 84 GrProcessorKeyBuilder* b) { |
85 for (int i = 0; i < fp.numChildProcessors(); ++i) { | 85 for (int i = 0; i < fp.numChildProcessors(); ++i) { |
86 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b
)) { | 86 if (!get_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b
)) { |
87 return false; | 87 return false; |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 fp.getGLSLProcessorKey(*caps.glslCaps(), b); | 91 fp.getGLSLProcessorKey(*caps.glslCaps(), b); |
92 | 92 |
93 //**** use glslCaps here? | 93 //**** use glslCaps here? |
94 return gen_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(), | 94 return get_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(), |
95 fp.numTransformsExclC
hildren()), b); | 95 fp.numTransformsExclC
hildren()), b); |
96 } | 96 } |
97 | 97 |
98 bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, | 98 bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, |
99 const GrPrimitiveProcessor& primProc, | 99 const GrPrimitiveProcessor& primProc, |
100 const GrPipeline& pipeline, | 100 const GrPipeline& pipeline, |
101 const GrGLGpu* gpu) { | 101 const GrGLGpu* gpu) { |
102 // The descriptor is used as a cache key. Thus when a field of the | 102 // The descriptor is used as a cache key. Thus when a field of the |
103 // descriptor will not affect program generation (because of the attribute | 103 // descriptor will not affect program generation (because of the attribute |
104 // bindings in use or other descriptor field settings) it should be set | 104 // bindings in use or other descriptor field settings) it should be set |
105 // to a canonical value to avoid duplicate programs with different keys. | 105 // to a canonical value to avoid duplicate programs with different keys. |
106 | 106 |
107 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; | 107 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; |
108 | 108 |
109 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); | 109 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
110 // Make room for everything up to the effect keys. | 110 // Make room for everything up to the effect keys. |
111 glDesc->key().reset(); | 111 glDesc->key().reset(); |
112 glDesc->key().push_back_n(kProcessorKeysOffset); | 112 glDesc->key().push_back_n(kProcessorKeysOffset); |
113 | 113 |
114 GrProcessorKeyBuilder b(&glDesc->key()); | 114 GrProcessorKeyBuilder b(&glDesc->key()); |
115 | 115 |
116 primProc.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); | 116 primProc.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
117 //**** use glslCaps here? | 117 //**** use glslCaps here? |
118 if (!gen_meta_key(primProc, gpu->glCaps(), 0, &b)) { | 118 if (!get_meta_key(primProc, gpu->glCaps(), 0, &b)) { |
119 glDesc->key().reset(); | 119 glDesc->key().reset(); |
120 return false; | 120 return false; |
121 } | 121 } |
122 | 122 |
123 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { | 123 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
124 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); | 124 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
125 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) { | 125 if (!get_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) { |
126 glDesc->key().reset(); | 126 glDesc->key().reset(); |
127 return false; | 127 return false; |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 const GrXferProcessor& xp = *pipeline.getXferProcessor(); | 131 const GrXferProcessor& xp = *pipeline.getXferProcessor(); |
132 xp.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); | 132 xp.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
133 //**** use glslCaps here? | 133 //**** use glslCaps here? |
134 if (!gen_meta_key(xp, gpu->glCaps(), 0, &b)) { | 134 if (!get_meta_key(xp, gpu->glCaps(), 0, &b)) { |
135 glDesc->key().reset(); | 135 glDesc->key().reset(); |
136 return false; | 136 return false; |
137 } | 137 } |
138 | 138 |
139 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- | 139 // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------
------------------- |
140 // Because header is a pointer into the dynamic array, we can't push any new
data into the key | 140 // Because header is a pointer into the dynamic array, we can't push any new
data into the key |
141 // below here. | 141 // below here. |
142 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); | 142 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); |
143 | 143 |
144 // make sure any padding in the header is zeroed. | 144 // make sure any padding in the header is zeroed. |
145 memset(header, 0, kHeaderSize); | 145 memset(header, 0, kHeaderSize); |
146 | 146 |
147 if (pipeline.readsFragPosition()) { | 147 if (pipeline.readsFragPosition()) { |
148 header->fFragPosKey = | 148 header->fFragPosKey = |
149 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.get
RenderTarget()); | 149 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.get
RenderTarget()); |
150 } else { | 150 } else { |
151 header->fFragPosKey = 0; | 151 header->fFragPosKey = 0; |
152 } | 152 } |
153 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); | 153 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
154 header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); | 154 header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); |
155 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); | 155 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); |
156 glDesc->finalize(); | 156 glDesc->finalize(); |
157 return true; | 157 return true; |
158 } | 158 } |
OLD | NEW |