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 | |
| 9 | #include "GrGLProcessor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 10 | #include "GrProcessor.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 11 | #include "GrGLGpu.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 12 | #include "GrPipeline.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 13 | #include "SkChecksum.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 14 | #include "gl/builders/GrGLFragmentShaderBuilder.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 | */ |
| 21 | static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
| 22 | uint32_t configComponentMask, |
| 23 | uint32_t swizzleComponentMask) { |
| 24 | if (caps.textureSwizzleSupport()) { |
| 25 | // Any remapping is handled using texture swizzling not shader modifications. |
| 26 | return false; |
| 27 | } |
| 28 | // check if the texture is alpha-only |
| 29 | if (kA_GrColorComponentFlag == configComponentMask) { |
| 30 | if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { |
| 31 | // we must map the swizzle 'a's to 'r'. |
| 32 | return true; |
| 33 | } |
| 34 | if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 35 | // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that |
| 36 | // alpha-only textures smear alpha across all four channels when read. |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 43 | static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 44 | uint32_t key = 0; |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 45 | int numTextures = proc.numTextures(); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 46 | for (int t = 0; t < numTextures; ++t) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 47 | const GrTextureAccess& access = proc.textureAccess(t); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 48 | uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); |
| 49 | if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { |
| 50 | key |= 1 << t; |
| 51 | } |
| 52 | } |
| 53 | return key; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 58 | * be dependent on properties of the effect that the effect itself doesn't use |
| 59 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 60 | * 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] | 61 | * 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] | 62 | * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share |
| 63 | * 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] | 64 | */ |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 65 | static bool get_meta_key(const GrProcessor& proc, |
| 66 | const GrGLCaps& caps, |
| 67 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 68 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 69 | size_t processorKeySize = b->size(); |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 70 | uint32_t textureKey = gen_texture_key(proc, caps); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 71 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 72 | |
| 73 | // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they |
| 74 | // don't fit. |
| 75 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
| 76 | if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 77 | return false; |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 78 | } |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 79 | if (processorKeySize > SK_MaxU16) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 83 | uint32_t* key = b->add32n(2); |
| 84 | key[0] = (textureKey << 16 | transformKey); |
| 85 | key[1] = (classID << 16 | SkToU16(processorKeySize)); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 89 | bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, |
| 90 | const GrPrimitiveProcessor& primProc, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 91 | const GrPipeline& pipeline, |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 92 | const GrGLGpu* gpu, |
| 93 | const GrBatchTracker& batchTracker) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 94 | // The descriptor is used as a cache key. Thus when a field of the |
| 95 | // descriptor will not affect program generation (because of the attribute |
| 96 | // bindings in use or other descriptor field settings) it should be set |
| 97 | // to a canonical value to avoid duplicate programs with different keys. |
| 98 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 99 | GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; |
| 100 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 101 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 102 | // Make room for everything up to the effect keys. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 103 | glDesc->key().reset(); |
| 104 | glDesc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 105 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 106 | GrProcessorKeyBuilder b(&glDesc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 107 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame^] | 108 | primProc.getGLProcessorKey(batchTracker, *gpu->glCaps().glslCaps(), &b); |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 109 | //**** use glslCaps here? |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 110 | if (!get_meta_key(primProc, gpu->glCaps(), 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 111 | glDesc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 112 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 113 | } |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 114 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 115 | for (int s = 0; s < pipeline.numFragmentStages(); ++s) { |
| 116 | const GrPendingFragmentStage& fps = pipeline.getFragmentStage(s); |
joshualitt | 40d4bd8 | 2014-12-29 09:04:40 -0800 | [diff] [blame] | 117 | const GrFragmentProcessor& fp = *fps.processor(); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame^] | 118 | fp.getGLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 119 | //**** use glslCaps here? |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 120 | if (!get_meta_key(fp, gpu->glCaps(), primProc.getTransformKey(fp.coordTransforms()), &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 121 | glDesc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 124 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 125 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 126 | const GrXferProcessor& xp = *pipeline.getXferProcessor(); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame^] | 127 | xp.getGLProcessorKey(*gpu->glCaps().glslCaps(), &b); |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 128 | //**** use glslCaps here? |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 129 | if (!get_meta_key(xp, gpu->glCaps(), 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 130 | glDesc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 134 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 135 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 136 | // below here. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 137 | KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 138 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 139 | // make sure any padding in the header is zeroed. |
| 140 | memset(header, 0, kHeaderSize); |
| 141 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 142 | if (pipeline.readsFragPosition()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 143 | header->fFragPosKey = |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 144 | GrGLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget(), |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 145 | gpu->glCaps()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 146 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 147 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 148 | } |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 149 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | 6be6f7c | 2015-02-26 13:05:21 -0800 | [diff] [blame] | 150 | header->fColorEffectCnt = pipeline.numColorFragmentStages(); |
| 151 | header->fCoverageEffectCnt = pipeline.numCoverageFragmentStages(); |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 152 | glDesc->finalize(); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |