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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrProgramDesc.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/SkChecksum.h" |
| 11 | #include "include/private/SkTo.h" |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrGeometryProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrPipeline.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrProcessor.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrProgramInfo.h" |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrShaderCaps.h" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrTexture.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 19 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 21 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 22 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 23 | enum { |
| 24 | kSamplerOrImageTypeKeyBits = 4 |
| 25 | }; |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 26 | |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 27 | static inline uint16_t texture_type_key(GrTextureType type) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 28 | int value = UINT16_MAX; |
| 29 | switch (type) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 30 | case GrTextureType::k2D: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 31 | value = 0; |
| 32 | break; |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 33 | case GrTextureType::kExternal: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 34 | value = 1; |
| 35 | break; |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 36 | case GrTextureType::kRectangle: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 37 | value = 2; |
| 38 | break; |
Robert Phillips | f209e88 | 2019-06-25 15:59:50 -0400 | [diff] [blame] | 39 | default: |
| 40 | SK_ABORT("Unexpected texture type"); |
| 41 | value = 3; |
| 42 | break; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 43 | } |
| 44 | SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value); |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 45 | return SkToU16(value); |
Brian Salomon | be34882 | 2016-11-22 15:56:30 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 48 | static uint32_t sampler_key(GrTextureType textureType, const GrSwizzle& swizzle, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 49 | const GrCaps& caps) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 50 | int samplerTypeKey = texture_type_key(textureType); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 51 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 52 | static_assert(2 == sizeof(swizzle.asKey())); |
Brian Salomon | 280fa3d | 2020-08-27 17:16:49 -0400 | [diff] [blame] | 53 | uint16_t swizzleKey = swizzle.asKey(); |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 54 | return SkToU32(samplerTypeKey | swizzleKey << kSamplerOrImageTypeKeyBits); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 57 | static void add_geomproc_sampler_keys(GrProcessorKeyBuilder* b, |
| 58 | const GrGeometryProcessor& geomProc, |
| 59 | const GrCaps& caps) { |
| 60 | int numTextureSamplers = geomProc.numTextureSamplers(); |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 61 | b->add32(numTextureSamplers, "ppNumSamplers"); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 62 | for (int i = 0; i < numTextureSamplers; ++i) { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 63 | const GrGeometryProcessor::TextureSampler& sampler = geomProc.textureSampler(i); |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 64 | const GrBackendFormat& backendFormat = sampler.backendFormat(); |
| 65 | |
| 66 | uint32_t samplerKey = sampler_key(backendFormat.textureType(), sampler.swizzle(), caps); |
| 67 | b->add32(samplerKey); |
| 68 | |
| 69 | caps.addExtraSamplerKey(b, sampler.samplerState(), backendFormat); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 70 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 73 | // Currently we allow 8 bits for the class id |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 74 | static constexpr uint32_t kClassIDBits = 8; |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 75 | |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 76 | /** |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 77 | * Functions which emit processor key info into the key builder. |
| 78 | * For every effect, we include the effect's class ID (different for every GrProcessor subclass), |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 79 | * any information generated by the effect itself (addToKey), and some meta-information. |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 80 | * Shader code may be dependent on properties of the effect not placed in the key by the effect |
| 81 | * (e.g. pixel format of textures used). |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 82 | */ |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 83 | static void gen_geomproc_key(const GrGeometryProcessor& geomProc, |
| 84 | const GrCaps& caps, |
| 85 | GrProcessorKeyBuilder* b) { |
| 86 | b->appendComment(geomProc.name()); |
| 87 | b->addBits(kClassIDBits, geomProc.classID(), "geomProcClassID"); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 88 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 89 | geomProc.addToKey(*caps.shaderCaps(), b); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 90 | geomProc.getAttributeKey(b); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 91 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 92 | add_geomproc_sampler_keys(b, geomProc, caps); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 93 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 95 | static void gen_xp_key(const GrXferProcessor& xp, |
| 96 | const GrCaps& caps, |
| 97 | const GrPipeline& pipeline, |
| 98 | GrProcessorKeyBuilder* b) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 99 | b->appendComment(xp.name()); |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 100 | b->addBits(kClassIDBits, xp.classID(), "xpClassID"); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 101 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 102 | const GrSurfaceOrigin* originIfDstTexture = nullptr; |
| 103 | GrSurfaceOrigin origin; |
| 104 | if (pipeline.dstProxyView().proxy()) { |
| 105 | origin = pipeline.dstProxyView().origin(); |
| 106 | originIfDstTexture = &origin; |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 107 | } |
Greg Daniel | 87fab9f | 2021-06-07 15:18:23 -0400 | [diff] [blame] | 108 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 109 | xp.addToKey(*caps.shaderCaps(), |
| 110 | b, |
| 111 | originIfDstTexture, |
| 112 | pipeline.dstSampleFlags() & GrDstSampleFlags::kAsInputAttachment); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 115 | static void gen_fp_key(const GrFragmentProcessor& fp, |
| 116 | const GrCaps& caps, |
| 117 | GrProcessorKeyBuilder* b) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 118 | b->appendComment(fp.name()); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 119 | b->addBits(kClassIDBits, fp.classID(), "fpClassID"); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 120 | b->addBits(GrGeometryProcessor::kCoordTransformKeyBits, |
| 121 | GrGeometryProcessor::ComputeCoordTransformsKey(fp), "fpTransforms"); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 122 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 123 | if (auto* te = fp.asTextureEffect()) { |
| 124 | const GrBackendFormat& backendFormat = te->view().proxy()->backendFormat(); |
| 125 | uint32_t samplerKey = sampler_key(backendFormat.textureType(), te->view().swizzle(), caps); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 126 | b->add32(samplerKey, "fpSamplerKey"); |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 127 | caps.addExtraSamplerKey(b, te->samplerState(), backendFormat); |
| 128 | } |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 129 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 130 | fp.addToKey(*caps.shaderCaps(), b); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 131 | b->add32(fp.numChildProcessors(), "fpNumChildren"); |
| 132 | |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 133 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 134 | if (auto child = fp.childProcessor(i)) { |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 135 | gen_fp_key(*child, caps, b); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 136 | } else { |
| 137 | // Fold in a sentinel value as the "class ID" for any null children |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 138 | b->appendComment("Null"); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 139 | b->addBits(kClassIDBits, GrProcessor::ClassID::kNull_ClassID, "fpClassID"); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 144 | static void gen_key(GrProcessorKeyBuilder* b, |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 145 | const GrProgramInfo& programInfo, |
| 146 | const GrCaps& caps) { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 147 | gen_geomproc_key(programInfo.geomProc(), caps, b); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 148 | |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 149 | const GrPipeline& pipeline = programInfo.pipeline(); |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 150 | b->addBits(2, pipeline.numFragmentProcessors(), "numFPs"); |
| 151 | b->addBits(1, pipeline.numColorFragmentProcessors(), "numColorFPs"); |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 152 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 153 | gen_fp_key(pipeline.getFragmentProcessor(i), caps, b); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 154 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 155 | |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 156 | gen_xp_key(pipeline.getXferProcessor(), caps, pipeline, b); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 157 | |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 158 | b->addBits(16, pipeline.writeSwizzle().asKey(), "writeSwizzle"); |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 159 | b->addBits(1, static_cast<uint32_t>(programInfo.requestedFeatures()), "requestedFeatures"); |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 160 | b->addBool(pipeline.snapVerticesToPixelCenters(), "snapVertices"); |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 161 | // The base descriptor only stores whether or not the primitiveType is kPoints. Backend- |
| 162 | // specific versions (e.g., Vulkan) require more detail |
Brian Osman | 3316796 | 2021-03-04 13:05:49 -0500 | [diff] [blame] | 163 | b->addBool((programInfo.primitiveType() == GrPrimitiveType::kPoints), "isPoints"); |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 164 | |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 165 | // Put a clean break between the "common" data written by this function, and any backend data |
| 166 | // appended later. The initial key length will just be this portion (rounded to 4 bytes). |
Brian Osman | ef3725f | 2021-03-04 10:44:41 -0500 | [diff] [blame] | 167 | b->flush(); |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 168 | } |
Robert Phillips | c15e890 | 2019-11-26 14:26:36 -0500 | [diff] [blame] | 169 | |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 170 | void GrProgramDesc::Build(GrProgramDesc* desc, |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 171 | const GrProgramInfo& programInfo, |
| 172 | const GrCaps& caps) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 173 | desc->reset(); |
| 174 | GrProcessorKeyBuilder b(desc->key()); |
Robert Phillips | ced5e83 | 2021-03-23 09:36:53 -0400 | [diff] [blame] | 175 | gen_key(&b, programInfo, caps); |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 176 | desc->fInitialKeyLength = desc->keyLength(); |
| 177 | } |
| 178 | |
Robert Phillips | ced5e83 | 2021-03-23 09:36:53 -0400 | [diff] [blame] | 179 | SkString GrProgramDesc::Describe(const GrProgramInfo& programInfo, |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 180 | const GrCaps& caps) { |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 181 | GrProgramDesc desc; |
| 182 | GrProcessorStringKeyBuilder b(desc.key()); |
Robert Phillips | ced5e83 | 2021-03-23 09:36:53 -0400 | [diff] [blame] | 183 | gen_key(&b, programInfo, caps); |
Brian Osman | e23c03d | 2021-03-05 14:58:25 -0500 | [diff] [blame] | 184 | b.flush(); |
| 185 | return b.description(); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 186 | } |