blob: d9afbac85b7048f58835ab32ef3d3db3f5e24626 [file] [log] [blame]
bsalomon@google.com798c8c42013-03-27 19:50:27 +00001/*
egdaniel0d9990f2016-07-29 07:36:52 -07002 * Copyright 2016 Google Inc.
bsalomon@google.com798c8c42013-03-27 19:50:27 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
egdaniel5d8f69f2016-09-07 07:24:12 -07007#include "GrProgramDesc.h"
joshualitt79f8fae2014-10-28 17:59:26 -07008
joshualittb0a8a372014-09-23 09:50:21 -07009#include "GrProcessor.h"
egdaniel8dd688b2015-01-22 10:16:09 -080010#include "GrPipeline.h"
cdalton28f45b92016-03-07 13:58:26 -080011#include "GrRenderTargetPriv.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050012#include "GrTexturePriv.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000013#include "SkChecksum.h"
egdaniel64c47282015-11-13 06:54:19 -080014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080015#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomone5286e02016-01-14 09:24:09 -080016#include "glsl/GrGLSLCaps.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000017
cdalton74b8d322016-04-11 14:47:28 -070018static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShaderFlags visibility,
cdaltona6b92ad2016-04-11 12:03:08 -070019 const GrGLSLCaps& caps) {
20 enum {
egdaniel990dbc82016-07-13 14:09:30 -070021 kFirstSamplerType = kTexture2DSampler_GrSLType,
csmartdalton22458032016-11-16 11:28:16 -070022 kLastSamplerType = kBufferSampler_GrSLType,
cdaltona6b92ad2016-04-11 12:03:08 -070023 kSamplerTypeKeyBits = 4
24 };
25 GR_STATIC_ASSERT(kLastSamplerType - kFirstSamplerType < (1 << kSamplerTypeKeyBits));
26
27 SkASSERT((int)samplerType >= kFirstSamplerType && (int)samplerType <= kLastSamplerType);
28 int samplerTypeKey = samplerType - kFirstSamplerType;
29
30 return SkToU16(caps.configTextureSwizzle(config).asKey() |
31 (samplerTypeKey << 8) |
32 (caps.samplerPrecision(config, visibility) << (8 + kSamplerTypeKeyBits)));
bsalomone5286e02016-01-14 09:24:09 -080033}
joshualitt23e280d2014-09-18 12:26:38 -070034
cdalton74b8d322016-04-11 14:47:28 -070035static void add_sampler_keys(GrProcessorKeyBuilder* b, const GrProcessor& proc,
36 const GrGLSLCaps& caps) {
Brian Salomon0bbecb22016-11-17 11:38:22 -050037 int numTextureSamplers = proc.numTextureSamplers();
38 int numSamplers = numTextureSamplers + proc.numBuffers();
cdaltona6b92ad2016-04-11 12:03:08 -070039 // Need two bytes per key (swizzle, sampler type, and precision).
cdalton74b8d322016-04-11 14:47:28 -070040 int word32Count = (numSamplers + 1) / 2;
bsalomoncdee0092016-01-08 13:20:12 -080041 if (0 == word32Count) {
42 return;
joshualitt23e280d2014-09-18 12:26:38 -070043 }
bsalomoncdee0092016-01-08 13:20:12 -080044 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count));
cdalton74b8d322016-04-11 14:47:28 -070045 int i = 0;
Brian Salomon0bbecb22016-11-17 11:38:22 -050046 for (; i < numTextureSamplers; ++i) {
47 const GrProcessor::TextureSampler& textureSampler = proc.textureSampler(i);
48 const GrTexture* tex = textureSampler.getTexture();
Brian Salomon739c5bf2016-11-07 09:53:44 -050049 k16[i] = sampler_key(tex->texturePriv().samplerType(), tex->config(),
Brian Salomon0bbecb22016-11-17 11:38:22 -050050 textureSampler.getVisibility(), caps);
bsalomoncdee0092016-01-08 13:20:12 -080051 }
cdalton74b8d322016-04-11 14:47:28 -070052 for (; i < numSamplers; ++i) {
Brian Salomon0bbecb22016-11-17 11:38:22 -050053 const GrBufferAccess& access = proc.bufferAccess(i - numTextureSamplers);
csmartdalton22458032016-11-16 11:28:16 -070054 k16[i] = sampler_key(kBufferSampler_GrSLType, access.texelConfig(),
egdaniel990dbc82016-07-13 14:09:30 -070055 access.visibility(), caps);
cdalton74b8d322016-04-11 14:47:28 -070056 }
57 // zero the last 16 bits if the number of samplers is odd.
58 if (numSamplers & 0x1) {
59 k16[numSamplers] = 0;
bsalomon65859ec2016-01-11 09:17:52 -080060 }
joshualitt23e280d2014-09-18 12:26:38 -070061}
62
63/**
64 * A function which emits a meta key into the key builder. This is required because shader code may
65 * be dependent on properties of the effect that the effect itself doesn't use
66 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
67 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070068 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080069 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
70 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070071 */
bsalomon7ea33f52015-11-22 14:51:00 -080072static bool gen_meta_key(const GrProcessor& proc,
bsalomon7f9b2e42016-01-12 13:29:26 -080073 const GrGLSLCaps& glslCaps,
joshualitta5305a12014-10-10 17:47:00 -070074 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080075 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080076 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -080077 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070078
bsalomon7ea33f52015-11-22 14:51:00 -080079 // Currently we allow 16 bits for the class id and the overall processor key size.
egdaniel0d9990f2016-07-29 07:36:52 -070080 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -080081 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070082 return false;
83 }
84
cdalton74b8d322016-04-11 14:47:28 -070085 add_sampler_keys(b, proc, glslCaps);
bsalomoncdee0092016-01-08 13:20:12 -080086
87 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -080088 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -080089 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -070090 return true;
91}
joshualittb0a8a372014-09-23 09:50:21 -070092
bsalomon7ea33f52015-11-22 14:51:00 -080093static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -070094 const GrFragmentProcessor& fp,
bsalomon7f9b2e42016-01-12 13:29:26 -080095 const GrGLSLCaps& glslCaps,
wangyixa7f4c432015-08-20 07:25:02 -070096 GrProcessorKeyBuilder* b) {
97 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7f9b2e42016-01-12 13:29:26 -080098 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -070099 return false;
100 }
101 }
102
bsalomon7f9b2e42016-01-12 13:29:26 -0800103 fp.getGLSLProcessorKey(glslCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700104
bsalomon7f9b2e42016-01-12 13:29:26 -0800105 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(),
bsalomona624bf32016-09-20 09:12:47 -0700106 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700107}
108
egdaniel5d8f69f2016-09-07 07:24:12 -0700109bool GrProgramDesc::Build(GrProgramDesc* desc,
110 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -0700111 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -0700112 const GrPipeline& pipeline,
113 const GrGLSLCaps& glslCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000114 // The descriptor is used as a cache key. Thus when a field of the
115 // descriptor will not affect program generation (because of the attribute
116 // bindings in use or other descriptor field settings) it should be set
117 // to a canonical value to avoid duplicate programs with different keys.
118
egdanielc67870c2014-11-26 08:50:50 -0800119 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
120 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700121 desc->key().reset();
122 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700123
egdaniel5d8f69f2016-09-07 07:24:12 -0700124 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800125
bsalomon7f9b2e42016-01-12 13:29:26 -0800126 primProc.getGLSLProcessorKey(glslCaps, &b);
127 if (!gen_meta_key(primProc, glslCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700128 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800129 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000130 }
cdalton87332102016-02-26 12:22:02 -0800131 GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700132
bsalomonac856c92015-08-27 06:30:17 -0700133 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
134 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7f9b2e42016-01-12 13:29:26 -0800135 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700136 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700137 return false;
138 }
cdalton87332102016-02-26 12:22:02 -0800139 requiredFeatures |= fp.requiredFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000140 }
egdaniel170f90b2014-09-16 12:54:40 -0700141
bsalomon2047b782015-12-21 13:12:54 -0800142 const GrXferProcessor& xp = pipeline.getXferProcessor();
bsalomon7f9b2e42016-01-12 13:29:26 -0800143 xp.getGLSLProcessorKey(glslCaps, &b);
144 if (!gen_meta_key(xp, glslCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700145 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800146 return false;
147 }
cdalton87332102016-02-26 12:22:02 -0800148 requiredFeatures |= xp.requiredFeatures();
egdanielc2304142014-12-11 13:15:13 -0800149
joshualitt65171342014-10-09 07:25:36 -0700150 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700151 // Because header is a pointer into the dynamic array, we can't push any new data into the key
152 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700153 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000154
joshualitt65171342014-10-09 07:25:36 -0700155 // make sure any padding in the header is zeroed.
156 memset(header, 0, kHeaderSize);
157
cdalton28f45b92016-03-07 13:58:26 -0800158 GrRenderTarget* rt = pipeline.getRenderTarget();
159
160 if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature |
161 GrProcessor::kSampleLocations_RequiredFeature)) {
162 header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(rt->origin());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000163 } else {
cdalton28f45b92016-03-07 13:58:26 -0800164 header->fSurfaceOriginKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000165 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800166
cdalton28f45b92016-03-07 13:58:26 -0800167 if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) {
168 SkASSERT(pipeline.isHWAntialiasState());
169 header->fSamplePatternKey =
csmartdaltonc633abb2016-11-01 08:55:55 -0700170 rt->renderTargetPriv().getMultisampleSpecs(pipeline).fUniqueID;
cdalton28f45b92016-03-07 13:58:26 -0800171 } else {
172 header->fSamplePatternKey = 0;
173 }
174
175 header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey();
bsalomon7f9b2e42016-01-12 13:29:26 -0800176
bsalomon2eda5b32016-09-21 10:53:24 -0700177 header->fIgnoresCoverage = pipeline.ignoresCoverage() ? 1 : 0;
egdaniel56cf6dc2015-11-30 10:15:58 -0800178
bsalomond79c5492015-04-27 10:07:04 -0700179 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700180 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
181 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
182 // Fail if the client requested more processors than the key can fit.
183 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
184 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
185 return false;
186 }
187 header->fHasPointSize = hasPointSize ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700188 return true;
189}