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 | */ |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 7 | #include "GrProgramDesc.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" |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 11 | #include "GrRenderTargetPriv.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 | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLCaps.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 17 | static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShaderFlags visibility, |
cdalton | a6b92ad | 2016-04-11 12:03:08 -0700 | [diff] [blame] | 18 | const GrGLSLCaps& caps) { |
| 19 | enum { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 20 | kFirstSamplerType = kTexture2DSampler_GrSLType, |
| 21 | kLastSamplerType = kTextureBufferSampler_GrSLType, |
cdalton | a6b92ad | 2016-04-11 12:03:08 -0700 | [diff] [blame] | 22 | kSamplerTypeKeyBits = 4 |
| 23 | }; |
| 24 | GR_STATIC_ASSERT(kLastSamplerType - kFirstSamplerType < (1 << kSamplerTypeKeyBits)); |
| 25 | |
| 26 | SkASSERT((int)samplerType >= kFirstSamplerType && (int)samplerType <= kLastSamplerType); |
| 27 | int samplerTypeKey = samplerType - kFirstSamplerType; |
| 28 | |
| 29 | return SkToU16(caps.configTextureSwizzle(config).asKey() | |
| 30 | (samplerTypeKey << 8) | |
| 31 | (caps.samplerPrecision(config, visibility) << (8 + kSamplerTypeKeyBits))); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 32 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 33 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 34 | static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrProcessor& proc, |
| 35 | const GrGLSLCaps& caps) { |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 36 | int numTextures = proc.numTextures(); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 37 | int numSamplers = numTextures + proc.numBuffers(); |
cdalton | a6b92ad | 2016-04-11 12:03:08 -0700 | [diff] [blame] | 38 | // Need two bytes per key (swizzle, sampler type, and precision). |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 39 | int word32Count = (numSamplers + 1) / 2; |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 40 | if (0 == word32Count) { |
| 41 | return; |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 42 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 43 | uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count)); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 44 | int i = 0; |
| 45 | for (; i < numTextures; ++i) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 46 | const GrTextureAccess& access = proc.textureAccess(i); |
cdalton | a6b92ad | 2016-04-11 12:03:08 -0700 | [diff] [blame] | 47 | const GrTexture* tex = access.getTexture(); |
Ben Wagner | ce4d04a | 2016-11-06 12:46:37 +0000 | [diff] [blame^] | 48 | k16[i] = sampler_key(tex->samplerType(), tex->config(), access.getVisibility(), caps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 49 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 50 | for (; i < numSamplers; ++i) { |
| 51 | const GrBufferAccess& access = proc.bufferAccess(i - numTextures); |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 52 | k16[i] = sampler_key(kTextureBufferSampler_GrSLType, access.texelConfig(), |
| 53 | access.visibility(), caps); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 54 | } |
| 55 | // zero the last 16 bits if the number of samplers is odd. |
| 56 | if (numSamplers & 0x1) { |
| 57 | k16[numSamplers] = 0; |
bsalomon | 65859ec | 2016-01-11 09:17:52 -0800 | [diff] [blame] | 58 | } |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * A function which emits a meta key into the key builder. This is required because shader code may |
| 63 | * be dependent on properties of the effect that the effect itself doesn't use |
| 64 | * in its key (e.g. the pixel format of textures used). So we create a meta-key for |
| 65 | * 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] | 66 | * 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] | 67 | * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this |
| 68 | * 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] | 69 | */ |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 70 | static bool gen_meta_key(const GrProcessor& proc, |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 71 | const GrGLSLCaps& glslCaps, |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 72 | uint32_t transformKey, |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 73 | GrProcessorKeyBuilder* b) { |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 74 | size_t processorKeySize = b->size(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 75 | uint32_t classID = proc.classID(); |
joshualitt | 89c7a2e | 2014-10-10 14:11:59 -0700 | [diff] [blame] | 76 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 77 | // Currently we allow 16 bits for the class id and the overall processor key size. |
egdaniel | 0d9990f | 2016-07-29 07:36:52 -0700 | [diff] [blame] | 78 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 79 | if ((processorKeySize | classID) & kMetaKeyInvalidMask) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 83 | add_sampler_keys(b, proc, glslCaps); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 84 | |
| 85 | uint32_t* key = b->add32n(2); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 86 | key[0] = (classID << 16) | SkToU32(processorKeySize); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 87 | key[1] = transformKey; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 88 | return true; |
| 89 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 91 | static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 92 | const GrFragmentProcessor& fp, |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 93 | const GrGLSLCaps& glslCaps, |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 94 | GrProcessorKeyBuilder* b) { |
| 95 | for (int i = 0; i < fp.numChildProcessors(); ++i) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 96 | if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) { |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 101 | fp.getGLSLProcessorKey(glslCaps, b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 102 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 103 | return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 104 | fp.numCoordTransforms()), b); |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 105 | } |
| 106 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 107 | bool GrProgramDesc::Build(GrProgramDesc* desc, |
| 108 | const GrPrimitiveProcessor& primProc, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 109 | bool hasPointSize, |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 110 | const GrPipeline& pipeline, |
| 111 | const GrGLSLCaps& glslCaps) { |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 112 | // The descriptor is used as a cache key. Thus when a field of the |
| 113 | // descriptor will not affect program generation (because of the attribute |
| 114 | // bindings in use or other descriptor field settings) it should be set |
| 115 | // to a canonical value to avoid duplicate programs with different keys. |
| 116 | |
egdaniel | c67870c | 2014-11-26 08:50:50 -0800 | [diff] [blame] | 117 | GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t)); |
| 118 | // Make room for everything up to the effect keys. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 119 | desc->key().reset(); |
| 120 | desc->key().push_back_n(kProcessorKeysOffset); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 121 | |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 122 | GrProcessorKeyBuilder b(&desc->key()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 123 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 124 | primProc.getGLSLProcessorKey(glslCaps, &b); |
| 125 | if (!gen_meta_key(primProc, glslCaps, 0, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 126 | desc->key().reset(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 127 | return false; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 128 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 129 | GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures(); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 130 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 131 | for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) { |
| 132 | const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 133 | if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 134 | desc->key().reset(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 137 | requiredFeatures |= fp.requiredFeatures(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 138 | } |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 139 | |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 140 | const GrXferProcessor& xp = pipeline.getXferProcessor(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 141 | xp.getGLSLProcessorKey(glslCaps, &b); |
| 142 | if (!gen_meta_key(xp, glslCaps, 0, &b)) { |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 143 | desc->key().reset(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 144 | return false; |
| 145 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 146 | requiredFeatures |= xp.requiredFeatures(); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 147 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 148 | // --------DO NOT MOVE HEADER ABOVE THIS LINE-------------------------------------------------- |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 149 | // Because header is a pointer into the dynamic array, we can't push any new data into the key |
| 150 | // below here. |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 151 | KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>(); |
bsalomon@google.com | 798c8c4 | 2013-03-27 19:50:27 +0000 | [diff] [blame] | 152 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 153 | // make sure any padding in the header is zeroed. |
| 154 | memset(header, 0, kHeaderSize); |
| 155 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 156 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
| 157 | |
| 158 | if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature | |
| 159 | GrProcessor::kSampleLocations_RequiredFeature)) { |
| 160 | header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(rt->origin()); |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 161 | } else { |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 162 | header->fSurfaceOriginKey = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 163 | } |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 164 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 165 | if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) { |
| 166 | SkASSERT(pipeline.isHWAntialiasState()); |
| 167 | header->fSamplePatternKey = |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 168 | rt->renderTargetPriv().getMultisampleSpecs(pipeline).fUniqueID; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 169 | } else { |
| 170 | header->fSamplePatternKey = 0; |
| 171 | } |
| 172 | |
| 173 | header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 174 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 175 | header->fIgnoresCoverage = pipeline.ignoresCoverage() ? 1 : 0; |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 176 | |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 177 | header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters(); |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 178 | header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors(); |
| 179 | header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors(); |
| 180 | // Fail if the client requested more processors than the key can fit. |
| 181 | if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() || |
| 182 | header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) { |
| 183 | return false; |
| 184 | } |
| 185 | header->fHasPointSize = hasPointSize ? 1 : 0; |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 186 | return true; |
| 187 | } |