blob: 68020a92d5977accaa513eabb52944a6ffffdec9 [file] [log] [blame]
bsalomon@google.com798c8c42013-03-27 19:50:27 +00001/*
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.com798c8c42013-03-27 19:50:27 +00007#include "GrGLProgramDesc.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"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000012#include "SkChecksum.h"
bsalomone5286e02016-01-14 09:24:09 -080013#include "gl/GrGLDefines.h"
14#include "gl/GrGLTexture.h"
15#include "gl/GrGLTypes.h"
egdaniel64c47282015-11-13 06:54:19 -080016#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080017#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomone5286e02016-01-14 09:24:09 -080018#include "glsl/GrGLSLCaps.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000019
bsalomonae04ecf2016-01-15 08:25:26 -080020static uint8_t texture_target_key(GrGLenum target) {
21 switch (target) {
22 case GR_GL_TEXTURE_2D:
23 return 0;
24 case GR_GL_TEXTURE_EXTERNAL:
25 return 1;
26 case GR_GL_TEXTURE_RECTANGLE:
27 return 2;
28 default:
29 SkFAIL("Unexpected texture target.");
30 return 0;
31 }
bsalomone5286e02016-01-14 09:24:09 -080032}
joshualitt23e280d2014-09-18 12:26:38 -070033
bsalomoncdee0092016-01-08 13:20:12 -080034static void add_texture_key(GrProcessorKeyBuilder* b, const GrProcessor& proc,
35 const GrGLSLCaps& caps) {
joshualitta5305a12014-10-10 17:47:00 -070036 int numTextures = proc.numTextures();
bsalomoncdee0092016-01-08 13:20:12 -080037 // Need two bytes per key (swizzle and target).
38 int word32Count = (proc.numTextures() + 1) / 2;
39 if (0 == word32Count) {
40 return;
joshualitt23e280d2014-09-18 12:26:38 -070041 }
bsalomoncdee0092016-01-08 13:20:12 -080042 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count));
43 for (int i = 0; i < numTextures; ++i) {
44 const GrTextureAccess& access = proc.textureAccess(i);
bsalomone5286e02016-01-14 09:24:09 -080045 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture());
bsalomonae04ecf2016-01-15 08:25:26 -080046 k16[i] = SkToU16(caps.configTextureSwizzle(texture->config()).asKey() |
47 (texture_target_key(texture->target()) << 8));
bsalomoncdee0092016-01-08 13:20:12 -080048 }
bsalomon65859ec2016-01-11 09:17:52 -080049 // zero the last 16 bits if the number of textures is odd.
50 if (numTextures & 0x1) {
51 k16[numTextures] = 0;
52 }
joshualitt23e280d2014-09-18 12:26:38 -070053}
54
55/**
56 * A function which emits a meta key into the key builder. This is required because shader code may
57 * be dependent on properties of the effect that the effect itself doesn't use
58 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
59 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070060 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080061 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
62 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070063 */
bsalomon7ea33f52015-11-22 14:51:00 -080064static bool gen_meta_key(const GrProcessor& proc,
bsalomon7f9b2e42016-01-12 13:29:26 -080065 const GrGLSLCaps& glslCaps,
joshualitta5305a12014-10-10 17:47:00 -070066 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080067 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080068 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -080069 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070070
bsalomon7ea33f52015-11-22 14:51:00 -080071 // Currently we allow 16 bits for the class id and the overall processor key size.
joshualitt89c7a2e2014-10-10 14:11:59 -070072 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -080073 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070074 return false;
75 }
76
bsalomon7f9b2e42016-01-12 13:29:26 -080077 add_texture_key(b, proc, glslCaps);
bsalomoncdee0092016-01-08 13:20:12 -080078
79 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -080080 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -080081 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -070082 return true;
83}
joshualittb0a8a372014-09-23 09:50:21 -070084
bsalomon7ea33f52015-11-22 14:51:00 -080085static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -070086 const GrFragmentProcessor& fp,
bsalomon7f9b2e42016-01-12 13:29:26 -080087 const GrGLSLCaps& glslCaps,
wangyixa7f4c432015-08-20 07:25:02 -070088 GrProcessorKeyBuilder* b) {
89 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7f9b2e42016-01-12 13:29:26 -080090 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -070091 return false;
92 }
93 }
94
bsalomon7f9b2e42016-01-12 13:29:26 -080095 fp.getGLSLProcessorKey(glslCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -070096
bsalomon7f9b2e42016-01-12 13:29:26 -080097 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(),
98 fp.numTransformsExclChildren()), b);
wangyixa7f4c432015-08-20 07:25:02 -070099}
100
joshualitt873ad0e2015-01-20 09:08:51 -0800101bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
102 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800103 const GrPipeline& pipeline,
bsalomon7f9b2e42016-01-12 13:29:26 -0800104 const GrGLSLCaps& glslCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000105 // The descriptor is used as a cache key. Thus when a field of the
106 // descriptor will not affect program generation (because of the attribute
107 // bindings in use or other descriptor field settings) it should be set
108 // to a canonical value to avoid duplicate programs with different keys.
109
jvanverthd1e72872015-04-20 12:29:37 -0700110 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
111
egdanielc67870c2014-11-26 08:50:50 -0800112 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
113 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -0700114 glDesc->key().reset();
115 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700116
jvanverthd1e72872015-04-20 12:29:37 -0700117 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -0800118
bsalomon7f9b2e42016-01-12 13:29:26 -0800119 primProc.getGLSLProcessorKey(glslCaps, &b);
120 if (!gen_meta_key(primProc, glslCaps, 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700121 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800122 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000123 }
cdalton87332102016-02-26 12:22:02 -0800124 GrProcessor::RequiredFeatures requiredFeatures = primProc.requiredFeatures();
bsalomon929f29a2014-07-17 07:55:11 -0700125
bsalomonac856c92015-08-27 06:30:17 -0700126 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
127 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7f9b2e42016-01-12 13:29:26 -0800128 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700129 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700130 return false;
131 }
cdalton87332102016-02-26 12:22:02 -0800132 requiredFeatures |= fp.requiredFeatures();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000133 }
egdaniel170f90b2014-09-16 12:54:40 -0700134
bsalomon2047b782015-12-21 13:12:54 -0800135 const GrXferProcessor& xp = pipeline.getXferProcessor();
bsalomon7f9b2e42016-01-12 13:29:26 -0800136 xp.getGLSLProcessorKey(glslCaps, &b);
137 if (!gen_meta_key(xp, glslCaps, 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700138 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800139 return false;
140 }
cdalton87332102016-02-26 12:22:02 -0800141 requiredFeatures |= xp.requiredFeatures();
egdanielc2304142014-12-11 13:15:13 -0800142
joshualitt65171342014-10-09 07:25:36 -0700143 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700144 // Because header is a pointer into the dynamic array, we can't push any new data into the key
145 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700146 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000147
joshualitt65171342014-10-09 07:25:36 -0700148 // make sure any padding in the header is zeroed.
149 memset(header, 0, kHeaderSize);
150
cdalton28f45b92016-03-07 13:58:26 -0800151 GrRenderTarget* rt = pipeline.getRenderTarget();
152
153 if (requiredFeatures & (GrProcessor::kFragmentPosition_RequiredFeature |
154 GrProcessor::kSampleLocations_RequiredFeature)) {
155 header->fSurfaceOriginKey = GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(rt->origin());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000156 } else {
cdalton28f45b92016-03-07 13:58:26 -0800157 header->fSurfaceOriginKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000158 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800159
cdalton28f45b92016-03-07 13:58:26 -0800160 if (requiredFeatures & GrProcessor::kSampleLocations_RequiredFeature) {
161 SkASSERT(pipeline.isHWAntialiasState());
162 header->fSamplePatternKey =
163 rt->renderTargetPriv().getMultisampleSpecs(pipeline.getStencil()).fUniqueID;
164 } else {
165 header->fSamplePatternKey = 0;
166 }
167
168 header->fOutputSwizzle = glslCaps.configOutputSwizzle(rt->config()).asKey();
bsalomon7f9b2e42016-01-12 13:29:26 -0800169
egdaniel56cf6dc2015-11-30 10:15:58 -0800170 if (pipeline.ignoresCoverage()) {
171 header->fIgnoresCoverage = 1;
172 } else {
173 header->fIgnoresCoverage = 0;
174 }
175
bsalomond79c5492015-04-27 10:07:04 -0700176 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomonac856c92015-08-27 06:30:17 -0700177 header->fColorEffectCnt = pipeline.numColorFragmentProcessors();
178 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors();
jvanverthd1e72872015-04-20 12:29:37 -0700179 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700180 return true;
181}