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 | */ |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 7 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 8 | #include "GrProgramDesc.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 10 | #include "GrPipeline.h" |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 11 | #include "GrPrimitiveProcessor.h" |
| 12 | #include "GrProcessor.h" |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 13 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 14 | #include "GrShaderCaps.h" |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 15 | #include "GrTexturePriv.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | #include "SkChecksum.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 17 | #include "SkTo.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 20 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 21 | enum { |
| 22 | kSamplerOrImageTypeKeyBits = 4 |
| 23 | }; |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 24 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 25 | static inline uint16_t texture_type_key(GrTextureType type) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 26 | int value = UINT16_MAX; |
| 27 | switch (type) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 28 | case GrTextureType::k2D: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 29 | value = 0; |
| 30 | break; |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 31 | case GrTextureType::kExternal: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 32 | value = 1; |
| 33 | break; |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 34 | case GrTextureType::kRectangle: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 35 | value = 2; |
| 36 | break; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 37 | } |
| 38 | SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 39 | return SkToU16(value); |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 42 | static uint16_t sampler_key(GrTextureType textureType, GrPixelConfig config, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 43 | const GrShaderCaps& caps) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 44 | int samplerTypeKey = texture_type_key(textureType); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 45 | |
| 46 | GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey())); |
| 47 | return SkToU16(samplerTypeKey | |
| 48 | caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits | |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 49 | (GrSLSamplerPrecision(config) << (8 + kSamplerOrImageTypeKeyBits))); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 52 | static void add_sampler_and_image_keys(GrProcessorKeyBuilder* b, const GrResourceIOProcessor& proc, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 53 | const GrShaderCaps& caps) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 54 | int numTextureSamplers = proc.numTextureSamplers(); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 55 | // Need two bytes per key. |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 56 | int word32Count = (numTextureSamplers + 1) / 2; |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 57 | if (0 == word32Count) { |
| 58 | return; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 59 | } |
Mike Klein | e72e773 | 2018-06-14 14:41:22 -0400 | [diff] [blame] | 60 | uint16_t* k16 = reinterpret_cast<uint16_t*>(b->add32n(word32Count)); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 61 | int j = 0; |
| 62 | for (int i = 0; i < numTextureSamplers; ++i, ++j) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 63 | const GrResourceIOProcessor::TextureSampler& sampler = proc.textureSampler(i); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 64 | const GrTexture* tex = sampler.peekTexture(); |
| 65 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame^] | 66 | k16[j] = sampler_key(tex->texturePriv().textureType(), tex->config(), caps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 67 | } |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 68 | // zero the last 16 bits if the number of uniforms for samplers is odd. |
| 69 | if (numTextureSamplers & 0x1) { |
| 70 | k16[numTextureSamplers] = 0; |
bsalomon | 65859ec | 2016-01-11 09:17:52 -0800 | [diff] [blame] | 71 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 76 | * be dependent on properties of the effect that the effect itself doesn't use |
| 77 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 78 | * 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] | 79 | * 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] | 80 | * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this |
| 81 | * 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] | 82 | */ |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 83 | static bool gen_meta_key(const GrResourceIOProcessor& proc, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 84 | const GrShaderCaps& shaderCaps, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 85 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 86 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 87 | size_t processorKeySize = b->size(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 88 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 89 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 90 | // Currently we allow 16 bits for the class id and the overall processor key size. |
Ben Wagner | b089765 | 2018-06-15 15:37:57 +0000 | [diff] [blame] | 91 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 92 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 96 | add_sampler_and_image_keys(b, proc, shaderCaps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 97 | |
| 98 | uint32_t* key = b->add32n(2); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 99 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 100 | key[1] = transformKey; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 101 | return true; |
| 102 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 104 | static bool gen_meta_key(const GrXferProcessor& xp, |
| 105 | const GrShaderCaps& shaderCaps, |
| 106 | GrProcessorKeyBuilder* b) { |
| 107 | size_t processorKeySize = b->size(); |
| 108 | uint32_t classID = xp.classID(); |
| 109 | |
| 110 | // Currently we allow 16 bits for the class id and the overall processor key size. |
Ben Wagner | b089765 | 2018-06-15 15:37:57 +0000 | [diff] [blame] | 111 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)UINT16_MAX); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 112 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | b->add32((classID << 16) | SkToU32(processorKeySize)); |
| 117 | return true; |
| 118 | } |
| 119 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 120 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 121 | const GrFragmentProcessor& fp, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 122 | const GrShaderCaps& shaderCaps, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 123 | GrProcessorKeyBuilder* b) { |
| 124 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 125 | if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), shaderCaps, b)) { |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 130 | fp.getGLSLProcessorKey(shaderCaps, b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 131 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 132 | return gen_meta_key(fp, shaderCaps, primProc.getTransformKey(fp.coordTransforms(), |
| 133 | fp.numCoordTransforms()), b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 134 | } |
| 135 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 136 | bool GrProgramDesc::Build(GrProgramDesc* desc, |
| 137 | const GrPrimitiveProcessor& primProc, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 138 | bool hasPointSize, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 139 | const GrPipeline& pipeline, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 140 | const GrShaderCaps& shaderCaps) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 141 | // The descriptor is used as a cache key. Thus when a field of the |
| 142 | // descriptor will not affect program generation (because of the attribute |
| 143 | // bindings in use or other descriptor field settings) it should be set |
| 144 | // to a canonical value to avoid duplicate programs with different keys. |
| 145 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 146 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 147 | // Make room for everything up to the effect keys. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 148 | desc->key().reset(); |
| 149 | desc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 150 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 151 | GrProcessorKeyBuilder b(&desc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 152 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 153 | primProc.getGLSLProcessorKey(shaderCaps, &b); |
| 154 | if (!gen_meta_key(primProc, shaderCaps, 0, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 155 | desc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 156 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 157 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 158 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 159 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 160 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 161 | if (!gen_frag_proc_and_meta_keys(primProc, fp, shaderCaps, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 162 | desc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 165 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 166 | |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 167 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 168 | const GrSurfaceOrigin* originIfDstTexture = nullptr; |
| 169 | GrSurfaceOrigin origin; |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 170 | if (pipeline.dstTextureProxy()) { |
| 171 | origin = pipeline.dstTextureProxy()->origin(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 172 | originIfDstTexture = &origin; |
| 173 | } |
| 174 | xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 175 | if (!gen_meta_key(xp, shaderCaps, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 176 | desc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 180 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 181 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 182 | // below here. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 183 | KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 184 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 185 | // make sure any padding in the header is zeroed. |
| 186 | memset(header, 0, kHeaderSize); |
Chris Dalton | 535ba8d | 2018-02-20 09:51:59 -0700 | [diff] [blame] | 187 | header->fOutputSwizzle = shaderCaps.configOutputSwizzle(pipeline.proxy()->config()).asKey(); |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 188 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 189 | header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors(); |
| 190 | header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors(); |
| 191 | // Fail if the client requested more processors than the key can fit. |
| 192 | if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() || |
| 193 | header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) { |
| 194 | return false; |
| 195 | } |
| 196 | header->fHasPointSize = hasPointSize ? 1 : 0; |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |