blob: 0a558e06b9595b0189dad92bc843d7e5b3080abe [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"
egdaniel8dd688b2015-01-22 10:16:09 -08008#include "GrPipeline.h"
Brian Salomone7d30482017-03-29 12:09:15 -04009#include "GrPrimitiveProcessor.h"
10#include "GrProcessor.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;
Brian Salomon57111332018-02-05 15:55:54 -050028 case kTextureExternalSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050029 value = 1;
30 break;
Brian Salomon57111332018-02-05 15:55:54 -050031 case kTexture2DRectSampler_GrSLType:
Brian Salomonf9f45122016-11-29 11:59:17 -050032 value = 2;
33 break;
Brian Salomonf9f45122016-11-29 11:59:17 -050034 case kBufferSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -050035 value = 3;
Brian Salomonf9f45122016-11-29 11:59:17 -050036 break;
Brian Salomon59dc4112016-11-23 01:02:43 +000037
Brian Salomonf9f45122016-11-29 11:59:17 -050038 default:
39 break;
40 }
41 SkASSERT((value & ((1 << kSamplerOrImageTypeKeyBits) - 1)) == value);
42 return value;
Brian Salomonbe348822016-11-22 15:56:30 -050043}
44
Brian Salomonf9f45122016-11-29 11:59:17 -050045static uint16_t sampler_key(GrSLType samplerType, GrPixelConfig config, GrShaderFlags visibility,
Brian Salomon94efbf52016-11-29 13:43:05 -050046 const GrShaderCaps& caps) {
Brian Salomonf9f45122016-11-29 11:59:17 -050047 int samplerTypeKey = image_storage_or_sampler_uniform_type_key(samplerType);
48
49 GR_STATIC_ASSERT(1 == sizeof(caps.configTextureSwizzle(config).asKey()));
50 return SkToU16(samplerTypeKey |
51 caps.configTextureSwizzle(config).asKey() << kSamplerOrImageTypeKeyBits |
Chris Dalton47c8ed32017-11-15 18:27:09 -070052 (GrSLSamplerPrecision(config) << (8 + kSamplerOrImageTypeKeyBits)));
Brian Salomonf9f45122016-11-29 11:59:17 -050053}
54
Brian Salomonab015ef2017-04-04 10:15:51 -040055static void add_sampler_and_image_keys(GrProcessorKeyBuilder* b, const GrResourceIOProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -050056 const GrShaderCaps& caps) {
Brian Salomon0bbecb22016-11-17 11:38:22 -050057 int numTextureSamplers = proc.numTextureSamplers();
Brian Salomonf9f45122016-11-29 11:59:17 -050058 int numBuffers = proc.numBuffers();
Brian Salomon559f5562017-11-15 14:28:33 -050059 int numUniforms = numTextureSamplers + numBuffers;
Brian Salomonf9f45122016-11-29 11:59:17 -050060 // Need two bytes per key.
61 int word32Count = (numUniforms + 1) / 2;
bsalomoncdee0092016-01-08 13:20:12 -080062 if (0 == word32Count) {
63 return;
joshualitt23e280d2014-09-18 12:26:38 -070064 }
bsalomoncdee0092016-01-08 13:20:12 -080065 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count));
Brian Salomonf9f45122016-11-29 11:59:17 -050066 int j = 0;
67 for (int i = 0; i < numTextureSamplers; ++i, ++j) {
Brian Salomonab015ef2017-04-04 10:15:51 -040068 const GrResourceIOProcessor::TextureSampler& sampler = proc.textureSampler(i);
Robert Phillips9bee2e52017-05-29 12:37:20 -040069 const GrTexture* tex = sampler.peekTexture();
70
Brian Salomonf9f45122016-11-29 11:59:17 -050071 k16[j] = sampler_key(tex->texturePriv().samplerType(), tex->config(), sampler.visibility(),
72 caps);
bsalomoncdee0092016-01-08 13:20:12 -080073 }
Brian Salomonf9f45122016-11-29 11:59:17 -050074 for (int i = 0; i < numBuffers; ++i, ++j) {
Brian Salomonab015ef2017-04-04 10:15:51 -040075 const GrResourceIOProcessor::BufferAccess& access = proc.bufferAccess(i);
Brian Salomonf9f45122016-11-29 11:59:17 -050076 k16[j] = sampler_key(kBufferSampler_GrSLType, access.texelConfig(), access.visibility(),
77 caps);
cdalton74b8d322016-04-11 14:47:28 -070078 }
Brian Salomonf9f45122016-11-29 11:59:17 -050079 // zero the last 16 bits if the number of uniforms for samplers and image storages is odd.
80 if (numUniforms & 0x1) {
81 k16[numUniforms] = 0;
bsalomon65859ec2016-01-11 09:17:52 -080082 }
joshualitt23e280d2014-09-18 12:26:38 -070083}
84
85/**
86 * A function which emits a meta key into the key builder. This is required because shader code may
87 * be dependent on properties of the effect that the effect itself doesn't use
88 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
89 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070090 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080091 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
92 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070093 */
Brian Salomonab015ef2017-04-04 10:15:51 -040094static bool gen_meta_key(const GrResourceIOProcessor& proc,
Brian Salomon1edc5b92016-11-29 13:43:46 -050095 const GrShaderCaps& shaderCaps,
joshualitta5305a12014-10-10 17:47:00 -070096 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080097 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080098 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -080099 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -0700100
bsalomon7ea33f52015-11-22 14:51:00 -0800101 // Currently we allow 16 bits for the class id and the overall processor key size.
egdaniel0d9990f2016-07-29 07:36:52 -0700102 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -0800103 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -0700104 return false;
105 }
106
Brian Salomon1edc5b92016-11-29 13:43:46 -0500107 add_sampler_and_image_keys(b, proc, shaderCaps);
bsalomoncdee0092016-01-08 13:20:12 -0800108
109 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -0800110 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -0800111 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -0700112 return true;
113}
joshualittb0a8a372014-09-23 09:50:21 -0700114
Brian Salomonab015ef2017-04-04 10:15:51 -0400115static bool gen_meta_key(const GrXferProcessor& xp,
116 const GrShaderCaps& shaderCaps,
117 GrProcessorKeyBuilder* b) {
118 size_t processorKeySize = b->size();
119 uint32_t classID = xp.classID();
120
121 // Currently we allow 16 bits for the class id and the overall processor key size.
122 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t)SK_MaxU16);
123 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
124 return false;
125 }
126
127 b->add32((classID << 16) | SkToU32(processorKeySize));
128 return true;
129}
130
bsalomon7ea33f52015-11-22 14:51:00 -0800131static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -0700132 const GrFragmentProcessor& fp,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500133 const GrShaderCaps& shaderCaps,
wangyixa7f4c432015-08-20 07:25:02 -0700134 GrProcessorKeyBuilder* b) {
135 for (int i = 0; i < fp.numChildProcessors(); ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500136 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), shaderCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -0700137 return false;
138 }
139 }
140
Brian Salomon1edc5b92016-11-29 13:43:46 -0500141 fp.getGLSLProcessorKey(shaderCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -0700142
Brian Salomon1edc5b92016-11-29 13:43:46 -0500143 return gen_meta_key(fp, shaderCaps, primProc.getTransformKey(fp.coordTransforms(),
144 fp.numCoordTransforms()), b);
wangyixa7f4c432015-08-20 07:25:02 -0700145}
146
egdaniel5d8f69f2016-09-07 07:24:12 -0700147bool GrProgramDesc::Build(GrProgramDesc* desc,
148 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -0700149 bool hasPointSize,
egdaniel5d8f69f2016-09-07 07:24:12 -0700150 const GrPipeline& pipeline,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500151 const GrShaderCaps& shaderCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000152 // The descriptor is used as a cache key. Thus when a field of the
153 // descriptor will not affect program generation (because of the attribute
154 // bindings in use or other descriptor field settings) it should be set
155 // to a canonical value to avoid duplicate programs with different keys.
156
egdanielc67870c2014-11-26 08:50:50 -0800157 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
158 // Make room for everything up to the effect keys.
egdaniel5d8f69f2016-09-07 07:24:12 -0700159 desc->key().reset();
160 desc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700161
egdaniel5d8f69f2016-09-07 07:24:12 -0700162 GrProcessorKeyBuilder b(&desc->key());
joshualitt9b989322014-12-15 14:16:27 -0800163
Brian Salomon1edc5b92016-11-29 13:43:46 -0500164 primProc.getGLSLProcessorKey(shaderCaps, &b);
165 if (!gen_meta_key(primProc, shaderCaps, 0, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700166 desc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800167 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000168 }
bsalomon929f29a2014-07-17 07:55:11 -0700169
bsalomonac856c92015-08-27 06:30:17 -0700170 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
171 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500172 if (!gen_frag_proc_and_meta_keys(primProc, fp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700173 desc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700174 return false;
175 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000176 }
egdaniel170f90b2014-09-16 12:54:40 -0700177
bsalomon2047b782015-12-21 13:12:54 -0800178 const GrXferProcessor& xp = pipeline.getXferProcessor();
Brian Salomon18dfa982017-04-03 16:57:43 -0400179 const GrSurfaceOrigin* originIfDstTexture = nullptr;
180 GrSurfaceOrigin origin;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400181 if (pipeline.dstTextureProxy()) {
182 origin = pipeline.dstTextureProxy()->origin();
Brian Salomon18dfa982017-04-03 16:57:43 -0400183 originIfDstTexture = &origin;
184 }
185 xp.getGLSLProcessorKey(shaderCaps, &b, originIfDstTexture);
Brian Salomonab015ef2017-04-04 10:15:51 -0400186 if (!gen_meta_key(xp, shaderCaps, &b)) {
egdaniel5d8f69f2016-09-07 07:24:12 -0700187 desc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800188 return false;
189 }
190
joshualitt65171342014-10-09 07:25:36 -0700191 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700192 // Because header is a pointer into the dynamic array, we can't push any new data into the key
193 // below here.
egdaniel5d8f69f2016-09-07 07:24:12 -0700194 KeyHeader* header = desc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000195
joshualitt65171342014-10-09 07:25:36 -0700196 // make sure any padding in the header is zeroed.
197 memset(header, 0, kHeaderSize);
Chris Dalton535ba8d2018-02-20 09:51:59 -0700198 header->fOutputSwizzle = shaderCaps.configOutputSwizzle(pipeline.proxy()->config()).asKey();
bsalomond79c5492015-04-27 10:07:04 -0700199 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon2eda5b32016-09-21 10:53:24 -0700200 header->fColorFragmentProcessorCnt = pipeline.numColorFragmentProcessors();
201 header->fCoverageFragmentProcessorCnt = pipeline.numCoverageFragmentProcessors();
202 // Fail if the client requested more processors than the key can fit.
203 if (header->fColorFragmentProcessorCnt != pipeline.numColorFragmentProcessors() ||
204 header->fCoverageFragmentProcessorCnt != pipeline.numCoverageFragmentProcessors()) {
205 return false;
206 }
207 header->fHasPointSize = hasPointSize ? 1 : 0;
bsalomon848faf02014-07-11 10:01:02 -0700208 return true;
209}