blob: 0390df01c0c9a4dcaa3af191261e981be3f333d2 [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 Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.h"
Brian Salomon739c5bf2016-11-07 09:53:44 -050013#include "GrTexturePriv.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000014#include "SkChecksum.h"
egdaniel64c47282015-11-13 06:54:19 -080015#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080016#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000017
Brian Salomonf9f45122016-11-29 11:59:17 -050018enum {
19 kSamplerOrImageTypeKeyBits = 4
20};
Brian Salomonbe348822016-11-22 15:56:30 -050021
Brian Salomonf9f45122016-11-29 11:59:17 -050022static inline uint16_t image_storage_or_sampler_uniform_type_key(GrSLType type ) {
23 int value = UINT16_MAX;
24 switch (type) {
25 case kTexture2DSampler_GrSLType:
26 value = 0;
27 break;
28 case kITexture2DSampler_GrSLType:
29 value = 1;
30 break;
31 case kTextureExternalSampler_GrSLType:
32 value = 2;
33 break;
34 case kTexture2DRectSampler_GrSLType:
35 value = 3;
36 break;
37 case kBufferSampler_GrSLType:
38 value = 4;
39 break;
40 case kImageStorage2D_GrSLType:
41 value = 5;
42 break;
43 case kIImageStorage2D_GrSLType:
44 value = 6;
45 break;
Brian Salomon59dc4112016-11-23 01:02:43 +000046
Brian Salomonf9f45122016-11-29 11:59:17 -050047 default:
48 break;
49 }
50 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
51 return value;
Brian Salomonbe348822016-11-22 15:56:30 -050052}
53
Brian Salomonf9f45122016-11-29 11:59:17 -050054static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShaderFlags visibility,
Brian Salomon94efbf52016-11-29 13:43:05 -050055 const GrShaderCaps& caps) {
Brian Salomonf9f45122016-11-29 11:59:17 -050056 int samplerTypeKey = image_storage_or_sampler_uniform_type_key(samplerType);
57
58 GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey()));
59 return SkToU16(samplerTypeKey |
60 caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
61 (caps.samplerPrecision(config, visibility) << (8 + kSamplerOrImageTypeKeyBits)));
62}
63
64static uint16_t storage_image_key(const GrProcessor::ImageStorageAccess& imageAccess) {
65 GrSLType type = imageAccess.texture()->texturePriv().imageStorageType();
66 return image_storage_or_sampler_uniform_type_key(type) |
67 (int)imageAccess.format() << kSamplerOrImageTypeKeyBits;
68}
69
70static void add_sampler_and_image_keys(GrProcessorKeyBuilder* b, const GrProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -050071 const GrShaderCaps& caps) {
Brian Salomon0bbecb22016-11-17 11:38:22 -050072 int numTextureSamplers = proc.numTextureSamplers();
Brian Salomonf9f45122016-11-29 11:59:17 -050073 int numBuffers = proc.numBuffers();
74 int numImageStorages = proc.numImageStorages();
75 int numUniforms = numTextureSamplers + numBuffers + numImageStorages;
76 // Need two bytes per key.
77 int word32Count = (numUniforms + 1) / 2;
bsalomoncdee0092016-01-08 13:20:12 -080078 if (0 == word32Count) {
79 return;
joshualitt23e280d2014-09-18 12:26:38 -070080 }
bsalomoncdee0092016-01-08 13:20:12 -080081 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count));
Brian Salomonf9f45122016-11-29 11:59:17 -050082 int j = 0;
83 for (int i = 0; i < numTextureSamplers; ++i, ++j) {
84 const GrProcessor::TextureSampler& sampler = proc.textureSampler(i);
85 const GrTexture* tex = sampler.texture();
86 k16[j] = sampler_key(tex->texturePriv().samplerType(), tex->config(), sampler.visibility(),
87 caps);
bsalomoncdee0092016-01-08 13:20:12 -080088 }
Brian Salomonf9f45122016-11-29 11:59:17 -050089 for (int i = 0; i < numBuffers; ++i, ++j) {
90 const GrProcessor::BufferAccess& access = proc.bufferAccess(i);
91 k16[j] = sampler_key(kBufferSampler_GrSLType, access.texelConfig(), access.visibility(),
92 caps);
cdalton74b8d322016-04-11 14:47:28 -070093 }
Brian Salomonf9f45122016-11-29 11:59:17 -050094 for (int i = 0; i < numImageStorages; ++i, ++j) {
95 k16[j] = storage_image_key(proc.imageStorageAccess(i));
96 }
97 // zero the last 16 bits if the number of uniforms for samplers and image storages is odd.
98 if (numUniforms & 0x1) {
99 k16[numUniforms] = 0;
bsalomon65859ec2016-01-11 09:17:52 -0800100 }
joshualitt23e280d2014-09-18 12:26:38 -0700101}
102
103/**
104 * A function which emits a meta key into the key builder. This is required because shader code may
105 * be dependent on properties of the effect that the effect itself doesn't use
106 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
107 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -0700108 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -0800109 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
110 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -0700111 */
bsalomon7ea33f52015-11-22 14:51:00 -0800112static bool gen_meta_key(const GrProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -0500113 const GrShaderCaps& glslCaps,
joshualitta5305a12014-10-10 17:47:00 -0700114 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -0800115 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -0800116 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -0800117 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700118
bsalomon7ea33f52015-11-22 14:51:00 -0800119 // Currently we allow 16 bits for the class id and the overall processor key size.
egdaniel0d9990f2016-07-29 07:36:52 -0700120 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -0800121 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700122 return false;
123 }
124
Brian Salomonf9f45122016-11-29 11:59:17 -0500125 add_sampler_and_image_keys(b, proc, glslCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800126
127 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800128 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800129 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700130 return true;
131}
joshualittb0a8a372014-09-23 09:50:21 -0700132
bsalomon7ea33f52015-11-22 14:51:00 -0800133static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700134 const GrFragmentProcessor& fp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500135 const GrShaderCaps& glslCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700136 GrProcessorKeyBuilder* b) {
137 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7f9b2e42016-01-12 13:29:26 -0800138 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700139 return false;
140 }
141 }
142
bsalomon7f9b2e42016-01-12 13:29:26 -0800143 fp.getGLSLProcessorKey(glslCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700144
bsalomon7f9b2e42016-01-12 13:29:26 -0800145 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(),
bsalomona624bf32016-09-20 09:12:47 -0700146 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700147}
148
egdaniel5d8f69f2016-09-07 07:24:12 -0700149bool GrProgramDesc::Build(GrProgramDesc* desc,
150 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -0700151 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -0700152 const GrPipeline& pipeline,
Brian Salomon94efbf52016-11-29 13:43:05 -0500153 const GrShaderCaps& glslCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000154 // The descriptor is used as a cache key. Thus when a field of the
155 // descriptor will not affect program generation (because of the attribute
156 // bindings in use or other descriptor field settings) it should be set
157 // to a canonical value to avoid duplicate programs with different keys.
158
egdanielc67870c2014-11-26 08:50:50 -0800159 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
160 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700161 desc->key().reset();
162 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700163
egdaniel5d8f69f2016-09-07 07:24:12 -0700164 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800165
bsalomon7f9b2e42016-01-12 13:29:26 -0800166 primProc.getGLSLProcessorKey(glslCaps, &b);
167 if (!gen_meta_key(primProc, glslCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700168 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800169 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000170 }
cdalton87332102016-02-26 12:22:02 -0800171 GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700172
bsalomonac856c92015-08-27 06:30:17 -0700173 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
174 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7f9b2e42016-01-12 13:29:26 -0800175 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700176 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700177 return false;
178 }
cdalton87332102016-02-26 12:22:02 -0800179 requiredFeatures |= fp.requiredFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000180 }
egdaniel170f90b2014-09-16 12:54:40 -0700181
bsalomon2047b782015-12-21 13:12:54 -0800182 const GrXferProcessor& xp = pipeline.getXferProcessor();
bsalomon7f9b2e42016-01-12 13:29:26 -0800183 xp.getGLSLProcessorKey(glslCaps, &b);
184 if (!gen_meta_key(xp, glslCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700185 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800186 return false;
187 }
cdalton87332102016-02-26 12:22:02 -0800188 requiredFeatures |= xp.requiredFeatures();
egdanielc2304142014-12-11 13:15:13 -0800189
joshualitt65171342014-10-09 07:25:36 -0700190 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700191 // Because header is a pointer into the dynamic array, we can't push any new data into the key
192 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700193 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000194
joshualitt65171342014-10-09 07:25:36 -0700195 // make sure any padding in the header is zeroed.
196 memset(header, 0, kHeaderSize);
197
cdalton28f45b92016-03-07 13:58:26 -0800198 GrRenderTarget* rt = pipeline.getRenderTarget();
199
200 if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature |
201 GrProcessor::kSampleLocations_RequiredFeature)) {
202 header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(rt->origin());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000203 } else {
cdalton28f45b92016-03-07 13:58:26 -0800204 header->fSurfaceOriginKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000205 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800206
cdalton28f45b92016-03-07 13:58:26 -0800207 if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) {
208 SkASSERT(pipeline.isHWAntialiasState());
209 header->fSamplePatternKey =
csmartdaltonc633abb2016-11-01 08:55:55 -0700210 rt->renderTargetPriv().getMultisampleSpecs(pipeline).fUniqueID;
cdalton28f45b92016-03-07 13:58:26 -0800211 } else {
212 header->fSamplePatternKey = 0;
213 }
214
215 header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey();
bsalomon7f9b2e42016-01-12 13:29:26 -0800216
bsalomon2eda5b32016-09-21 10:53:24 -0700217 header->fIgnoresCoverage = pipeline.ignoresCoverage() ? 1 : 0;
egdaniel56cf6dc2015-11-30 10:15:58 -0800218
bsalomond79c5492015-04-27 10:07:04 -0700219 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700220 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
221 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
222 // Fail if the client requested more processors than the key can fit.
223 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
224 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
225 return false;
226 }
227 header->fHasPointSize = hasPointSize ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700228 return true;
229}