bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 7 | #include "GrGLProgramDesc.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" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 10 | #include "GrGLGpu.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 11 | #include "GrPipeline.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 12 | #include "SkChecksum.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 15 | |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 16 | /** |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 17 | * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are |
| 18 | * present in the texture's config. swizzleComponentMask indicates the channels present in the |
| 19 | * shader swizzle. |
| 20 | */ |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 21 | static bool swizzle_requires_alpha_remapping(const GrGLSLCaps& caps, GrPixelConfig config) { |
| 22 | if (!caps.mustSwizzleInShader()) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 23 | // Any remapping is handled using texture swizzling not shader modifications. |
| 24 | return false; |
| 25 | } |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 26 | const char* swizzleMap = caps.getSwizzleMap(config); |
| 27 | |
| 28 | return SkToBool(memcmp(swizzleMap, "rgba", 4)); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 29 | } |
| 30 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 31 | static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 32 | uint32_t key = 0; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 33 | int numTextures = proc.numTextures(); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 34 | int shift = 0; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 35 | for (int t = 0; t < numTextures; ++t) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 36 | const GrTextureAccess& access = proc.textureAccess(t); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 37 | if (swizzle_requires_alpha_remapping(*caps.glslCaps(), access.getTexture()->config())) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 38 | key |= 1 << shift; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 39 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 40 | if (GR_GL_TEXTURE_EXTERNAL == static_cast<GrGLTexture*>(access.getTexture())->target()) { |
| 41 | key |= 2 << shift; |
| 42 | } |
| 43 | shift += 2; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 44 | } |
| 45 | return key; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 50 | * 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 |
| 52 | * 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] | 53 | * which must be different for every GrProcessor subclass. It can fail if an effect uses too many |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 54 | * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share |
| 55 | * this 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] | 56 | */ |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 57 | static bool gen_meta_key(const GrProcessor& proc, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 58 | const GrGLCaps& caps, |
| 59 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 60 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 61 | size_t processorKeySize = b->size(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 62 | uint32_t textureKey = gen_texture_key(proc, caps); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 63 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 64 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 65 | // Currently we allow 16 bits for the class id and the overall processor key size. |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 66 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 67 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 71 | uint32_t* key = b->add32n(3); |
| 72 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
| 73 | key[1] = textureKey; |
| 74 | key[2] = transformKey; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 75 | return true; |
| 76 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 78 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 79 | const GrFragmentProcessor& fp, |
| 80 | const GrGLCaps& caps, |
| 81 | GrProcessorKeyBuilder* b) { |
| 82 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 83 | if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) { |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 88 | fp.getGLSLProcessorKey(*caps.glslCaps(), b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 89 | |
| 90 | //**** use glslCaps here? |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 91 | return gen_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(), |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 92 | fp.numTransformsExclChildren()), b); |
| 93 | } |
| 94 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 95 | bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, |
| 96 | const GrPrimitiveProcessor& primProc, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 97 | const GrPipeline& pipeline, |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 98 | const GrGLGpu* gpu) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 99 | // The descriptor is used as a cache key. Thus when a field of the |
| 100 | // descriptor will not affect program generation (because of the attribute |
| 101 | // bindings in use or other descriptor field settings) it should be set |
| 102 | // to a canonical value to avoid duplicate programs with different keys. |
| 103 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 104 | GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; |
| 105 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 106 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 107 | // Make room for everything up to the effect keys. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 108 | glDesc->key().reset(); |
| 109 | glDesc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 110 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 111 | GrProcessorKeyBuilder b(&glDesc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 112 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 113 | primProc.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 114 | //**** use glslCaps here? |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 115 | if (!gen_meta_key(primProc, gpu->glCaps(), 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 116 | glDesc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 117 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 118 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 119 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 120 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 121 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 122 | if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 123 | glDesc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 126 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 127 | |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame^] | 128 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 129 | xp.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 130 | //**** use glslCaps here? |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 131 | if (!gen_meta_key(xp, gpu->glCaps(), 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 132 | glDesc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 136 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 137 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 138 | // below here. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 139 | KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 140 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 141 | // make sure any padding in the header is zeroed. |
| 142 | memset(header, 0, kHeaderSize); |
| 143 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 144 | if (pipeline.readsFragPosition()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 145 | header->fFragPosKey = |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 146 | GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 147 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 148 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 149 | } |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 150 | |
| 151 | if (pipeline.ignoresCoverage()) { |
| 152 | header->fIgnoresCoverage = 1; |
| 153 | } else { |
| 154 | header->fIgnoresCoverage = 0; |
| 155 | } |
| 156 | |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 157 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 158 | header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); |
| 159 | header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 160 | glDesc->finalize(); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |