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" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 10 | #include "GrPipeline.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 11 | #include "SkChecksum.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 12 | #include "gl/GrGLDefines.h" |
| 13 | #include "gl/GrGLTexture.h" |
| 14 | #include "gl/GrGLTypes.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 | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLCaps.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 18 | |
bsalomon | ae04ecf | 2016-01-15 08:25:26 -0800 | [diff] [blame] | 19 | static uint8_t texture_target_key(GrGLenum target) { |
| 20 | switch (target) { |
| 21 | case GR_GL_TEXTURE_2D: |
| 22 | return 0; |
| 23 | case GR_GL_TEXTURE_EXTERNAL: |
| 24 | return 1; |
| 25 | case GR_GL_TEXTURE_RECTANGLE: |
| 26 | return 2; |
| 27 | default: |
| 28 | SkFAIL("Unexpected texture target."); |
| 29 | return 0; |
| 30 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 31 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 32 | |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 33 | static void add_texture_key(GrProcessorKeyBuilder* b, const GrProcessor& proc, |
| 34 | const GrGLSLCaps& caps) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 35 | int numTextures = proc.numTextures(); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 36 | // Need two bytes per key (swizzle and target). |
| 37 | int word32Count = (proc.numTextures() + 1) / 2; |
| 38 | if (0 == word32Count) { |
| 39 | return; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 40 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 41 | uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count)); |
| 42 | for (int i = 0; i < numTextures; ++i) { |
| 43 | const GrTextureAccess& access = proc.textureAccess(i); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 44 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
bsalomon | ae04ecf | 2016-01-15 08:25:26 -0800 | [diff] [blame] | 45 | k16[i] = SkToU16(caps.configTextureSwizzle(texture->config()).asKey() | |
| 46 | (texture_target_key(texture->target()) << 8)); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 47 | } |
bsalomon | 65859ec | 2016-01-11 09:17:52 -0800 | [diff] [blame] | 48 | // zero the last 16 bits if the number of textures is odd. |
| 49 | if (numTextures & 0x1) { |
| 50 | k16[numTextures] = 0; |
| 51 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 56 | * be dependent on properties of the effect that the effect itself doesn't use |
| 57 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 58 | * 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] | 59 | * 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] | 60 | * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this |
| 61 | * 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] | 62 | */ |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 63 | static bool gen_meta_key(const GrProcessor& proc, |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 64 | const GrGLSLCaps& glslCaps, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 65 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 66 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 67 | size_t processorKeySize = b->size(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 68 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 69 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 70 | // 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] | 71 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 72 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 76 | add_texture_key(b, proc, glslCaps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 77 | |
| 78 | uint32_t* key = b->add32n(2); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 79 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 80 | key[1] = transformKey; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 81 | return true; |
| 82 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 84 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 85 | const GrFragmentProcessor& fp, |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 86 | const GrGLSLCaps& glslCaps, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 87 | GrProcessorKeyBuilder* b) { |
| 88 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 89 | if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) { |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 94 | fp.getGLSLProcessorKey(glslCaps, b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 95 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 96 | return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(), |
| 97 | fp.numTransformsExclChildren()), b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 98 | } |
| 99 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 100 | bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc, |
| 101 | const GrPrimitiveProcessor& primProc, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 102 | const GrPipeline& pipeline, |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 103 | const GrGLSLCaps& glslCaps) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 104 | // The descriptor is used as a cache key. Thus when a field of the |
| 105 | // descriptor will not affect program generation (because of the attribute |
| 106 | // bindings in use or other descriptor field settings) it should be set |
| 107 | // to a canonical value to avoid duplicate programs with different keys. |
| 108 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 109 | GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc; |
| 110 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 111 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 112 | // Make room for everything up to the effect keys. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 113 | glDesc->key().reset(); |
| 114 | glDesc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 115 | |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 116 | GrProcessorKeyBuilder b(&glDesc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 117 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 118 | primProc.getGLSLProcessorKey(glslCaps, &b); |
| 119 | if (!gen_meta_key(primProc, glslCaps, 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 120 | glDesc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 121 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 122 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame^] | 123 | GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 124 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 125 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 126 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 127 | if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 128 | glDesc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame^] | 131 | requiredFeatures |= fp.requiredFeatures(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 132 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 133 | |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 134 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 135 | xp.getGLSLProcessorKey(glslCaps, &b); |
| 136 | if (!gen_meta_key(xp, glslCaps, 0, &b)) { |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 137 | glDesc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 138 | return false; |
| 139 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame^] | 140 | requiredFeatures |= xp.requiredFeatures(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 141 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 142 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 143 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 144 | // below here. |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 145 | KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 146 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 147 | // make sure any padding in the header is zeroed. |
| 148 | memset(header, 0, kHeaderSize); |
| 149 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame^] | 150 | if (requiredFeatures & GrProcessor::kFragmentPosition_RequiredFeature) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 151 | header->fFragPosKey = |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 152 | GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 153 | } else { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 154 | header->fFragPosKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 155 | } |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 156 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 157 | header->fOutputSwizzle = |
| 158 | glslCaps.configOutputSwizzle(pipeline.getRenderTarget()->config()).asKey(); |
| 159 | |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 160 | if (pipeline.ignoresCoverage()) { |
| 161 | header->fIgnoresCoverage = 1; |
| 162 | } else { |
| 163 | header->fIgnoresCoverage = 0; |
| 164 | } |
| 165 | |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 166 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 167 | header->fColorEffectCnt = pipeline.numColorFragmentProcessors(); |
| 168 | header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors(); |
jvanverth | d1e7287 | 2015-04-20 12:29:37 -0700 | [diff] [blame] | 169 | glDesc->finalize(); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 170 | return true; |
| 171 | } |