bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 1 | /* |
egdaniel | 0d9990f | 2016-07-29 07:36:52 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 7 | #include "GrProgramDesc.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 8 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 9 | #include "GrProcessor.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 10 | #include "GrPipeline.h" |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 11 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 12 | #include "GrShaderCaps.h" |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 13 | #include "GrTexturePriv.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 14 | #include "SkChecksum.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 17 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 18 | enum { |
| 19 | kSamplerOrImageTypeKeyBits = 4 |
| 20 | }; |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 21 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 22 | static inline uint16_t image_storage_or_sampler_uniform_type_key(GrSLType type ) { |
| 23 | int value = UINT16_MAX; |
| 24 | switch (type) { |
| 25 | case kTexture2DSampler_GrSLType: |
| 26 | value = 0; |
| 27 | break; |
| 28 | case kITexture2DSampler_GrSLType: |
| 29 | value = 1; |
| 30 | break; |
| 31 | case kTextureExternalSampler_GrSLType: |
| 32 | value = 2; |
| 33 | break; |
| 34 | case kTexture2DRectSampler_GrSLType: |
| 35 | value = 3; |
| 36 | break; |
| 37 | case kBufferSampler_GrSLType: |
| 38 | value = 4; |
| 39 | break; |
| 40 | case kImageStorage2D_GrSLType: |
| 41 | value = 5; |
| 42 | break; |
| 43 | case kIImageStorage2D_GrSLType: |
| 44 | value = 6; |
| 45 | break; |
Brian Salomon | 59dc411 | 2016-11-23 01:02:43 +0000 | [diff] [blame] | 46 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 47 | default: |
| 48 | break; |
| 49 | } |
| 50 | SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value); |
| 51 | return value; |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 54 | static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShaderFlags visibility, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 55 | const GrShaderCaps& caps) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 56 | int samplerTypeKey = image_storage_or_sampler_uniform_type_key(samplerType); |
| 57 | |
| 58 | GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey())); |
| 59 | return SkToU16(samplerTypeKey | |
| 60 | caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits | |
| 61 | (caps.samplerPrecision(config, visibility) << (8 + kSamplerOrImageTypeKeyBits))); |
| 62 | } |
| 63 | |
| 64 | static uint16_t storage_image_key(const GrProcessor::ImageStorageAccess& imageAccess) { |
| 65 | GrSLType type = imageAccess.texture()->texturePriv().imageStorageType(); |
| 66 | return image_storage_or_sampler_uniform_type_key(type) | |
| 67 | (int)imageAccess.format() << kSamplerOrImageTypeKeyBits; |
| 68 | } |
| 69 | |
| 70 | static void add_sampler_and_image_keys(GrProcessorKeyBuilder* b, const GrProcessor& proc, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 71 | const GrShaderCaps& caps) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 72 | int numTextureSamplers = proc.numTextureSamplers(); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 73 | int numBuffers = proc.numBuffers(); |
| 74 | int numImageStorages = proc.numImageStorages(); |
| 75 | int numUniforms = numTextureSamplers + numBuffers + numImageStorages; |
| 76 | // Need two bytes per key. |
| 77 | int word32Count = (numUniforms + 1) / 2; |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 78 | if (0 == word32Count) { |
| 79 | return; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 80 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 81 | uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count)); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 82 | int j = 0; |
| 83 | for (int i = 0; i < numTextureSamplers; ++i, ++j) { |
| 84 | const GrProcessor::TextureSampler& sampler = proc.textureSampler(i); |
| 85 | const GrTexture* tex = sampler.texture(); |
| 86 | k16[j] = sampler_key(tex->texturePriv().samplerType(), tex->config(), sampler.visibility(), |
| 87 | caps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 88 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 89 | for (int i = 0; i < numBuffers; ++i, ++j) { |
| 90 | const GrProcessor::BufferAccess& access = proc.bufferAccess(i); |
| 91 | k16[j] = sampler_key(kBufferSampler_GrSLType, access.texelConfig(), access.visibility(), |
| 92 | caps); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 93 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 94 | for (int i = 0; i < numImageStorages; ++i, ++j) { |
| 95 | k16[j] = storage_image_key(proc.imageStorageAccess(i)); |
| 96 | } |
| 97 | // zero the last 16 bits if the number of uniforms for samplers and image storages is odd. |
| 98 | if (numUniforms & 0x1) { |
| 99 | k16[numUniforms] = 0; |
bsalomon | 65859ec | 2016-01-11 09:17:52 -0800 | [diff] [blame] | 100 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 105 | * be dependent on properties of the effect that the effect itself doesn't use |
| 106 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 107 | * every effect using this function. It is also responsible for inserting the effect's class ID |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 108 | * which must be different for every GrProcessor subclass. It can fail if an effect uses too many |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 109 | * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this |
| 110 | * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 111 | */ |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 112 | static bool gen_meta_key(const GrProcessor& proc, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 113 | const GrShaderCaps& shaderCaps, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 114 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 115 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 116 | size_t processorKeySize = b->size(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 117 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 118 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 119 | // Currently we allow 16 bits for the class id and the overall processor key size. |
egdaniel | 0d9990f | 2016-07-29 07:36:52 -0700 | [diff] [blame] | 120 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 121 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 125 | add_sampler_and_image_keys(b, proc, shaderCaps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 126 | |
| 127 | uint32_t* key = b->add32n(2); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 128 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 129 | key[1] = transformKey; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 132 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 133 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 134 | const GrFragmentProcessor& fp, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 135 | const GrShaderCaps& shaderCaps, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 136 | GrProcessorKeyBuilder* b) { |
| 137 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 138 | if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), shaderCaps, b)) { |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 143 | fp.getGLSLProcessorKey(shaderCaps, b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 144 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 145 | return gen_meta_key(fp, shaderCaps, primProc.getTransformKey(fp.coordTransforms(), |
| 146 | fp.numCoordTransforms()), b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 147 | } |
| 148 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 149 | bool GrProgramDesc::Build(GrProgramDesc* desc, |
| 150 | const GrPrimitiveProcessor& primProc, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 151 | bool hasPointSize, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 152 | const GrPipeline& pipeline, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 153 | const GrShaderCaps& shaderCaps) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 154 | // The descriptor is used as a cache key. Thus when a field of the |
| 155 | // descriptor will not affect program generation (because of the attribute |
| 156 | // bindings in use or other descriptor field settings) it should be set |
| 157 | // to a canonical value to avoid duplicate programs with different keys. |
| 158 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 159 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 160 | // Make room for everything up to the effect keys. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 161 | desc->key().reset(); |
| 162 | desc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 163 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 164 | GrProcessorKeyBuilder b(&desc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 165 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 166 | primProc.getGLSLProcessorKey(shaderCaps, &b); |
| 167 | if (!gen_meta_key(primProc, shaderCaps, 0, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 168 | desc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 169 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 170 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 171 | GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 172 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 173 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 174 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 175 | if (!gen_frag_proc_and_meta_keys(primProc, fp, shaderCaps, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 176 | desc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 179 | requiredFeatures |= fp.requiredFeatures(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 180 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 181 | |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 182 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 183 | xp.getGLSLProcessorKey(shaderCaps, &b); |
| 184 | if (!gen_meta_key(xp, shaderCaps, 0, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 185 | desc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 188 | requiredFeatures |= xp.requiredFeatures(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 189 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 190 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 191 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 192 | // below here. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 193 | KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 194 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 195 | // make sure any padding in the header is zeroed. |
| 196 | memset(header, 0, kHeaderSize); |
| 197 | |
Ethan Nicholas | de4d301 | 2017-01-19 16:58:02 -0500 | [diff] [blame] | 198 | header->fSurfaceOriginKey = 0; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 199 | |
Ethan Nicholas | de4d301 | 2017-01-19 16:58:02 -0500 | [diff] [blame] | 200 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 201 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 202 | if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) { |
| 203 | SkASSERT(pipeline.isHWAntialiasState()); |
| 204 | header->fSamplePatternKey = |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 205 | rt->renderTargetPriv().getMultisampleSpecs(pipeline).fUniqueID; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 206 | } else { |
| 207 | header->fSamplePatternKey = 0; |
| 208 | } |
| 209 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 210 | header->fOutputSwizzle = shaderCaps.configOutputSwizzle(rt->config()).asKey(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 211 | |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 212 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 213 | header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors(); |
| 214 | header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors(); |
| 215 | // Fail if the client requested more processors than the key can fit. |
| 216 | if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() || |
| 217 | header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) { |
| 218 | return false; |
| 219 | } |
| 220 | header->fHasPointSize = hasPointSize ? 1 : 0; |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |