blob: 4c95e2b532adc8501e285a3d98fb19b4ce336247 [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"
jvanverth39edf762014-12-22 11:44:19 -080010#include "GrGLGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080011#include "GrPipeline.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000012#include "SkChecksum.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000015
joshualitt23e280d2014-09-18 12:26:38 -070016
bsalomoncdee0092016-01-08 13:20:12 -080017static void add_texture_key(GrProcessorKeyBuilder* b, const GrProcessor& proc,
18 const GrGLSLCaps& caps) {
joshualitta5305a12014-10-10 17:47:00 -070019 int numTextures = proc.numTextures();
bsalomoncdee0092016-01-08 13:20:12 -080020 // Need two bytes per key (swizzle and target).
21 int word32Count = (proc.numTextures() + 1) / 2;
22 if (0 == word32Count) {
23 return;
joshualitt23e280d2014-09-18 12:26:38 -070024 }
bsalomoncdee0092016-01-08 13:20:12 -080025 uint16_t* k16 = SkTCast<uint16_t*>(b->add32n(word32Count));
26 for (int i = 0; i < numTextures; ++i) {
27 const GrTextureAccess& access = proc.textureAccess(i);
28 bool isExternal = (GR_GL_TEXTURE_EXTERNAL ==
29 static_cast<GrGLTexture*>(access.getTexture())->target());
30 k16[i] = caps.configTextureSwizzle(access.getTexture()->config()).asKey() |
31 (isExternal ? 0xFF00 : 0x0000);
32 }
bsalomon65859ec2016-01-11 09:17:52 -080033 // zero the last 16 bits if the number of textures is odd.
34 if (numTextures & 0x1) {
35 k16[numTextures] = 0;
36 }
joshualitt23e280d2014-09-18 12:26:38 -070037}
38
39/**
40 * A function which emits a meta key into the key builder. This is required because shader code may
41 * be dependent on properties of the effect that the effect itself doesn't use
42 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
43 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070044 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080045 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
46 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070047 */
bsalomon7ea33f52015-11-22 14:51:00 -080048static bool gen_meta_key(const GrProcessor& proc,
bsalomon7f9b2e42016-01-12 13:29:26 -080049 const GrGLSLCaps& glslCaps,
joshualitta5305a12014-10-10 17:47:00 -070050 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080051 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080052 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -080053 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070054
bsalomon7ea33f52015-11-22 14:51:00 -080055 // Currently we allow 16 bits for the class id and the overall processor key size.
joshualitt89c7a2e2014-10-10 14:11:59 -070056 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -080057 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070058 return false;
59 }
60
bsalomon7f9b2e42016-01-12 13:29:26 -080061 add_texture_key(b, proc, glslCaps);
bsalomoncdee0092016-01-08 13:20:12 -080062
63 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -080064 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -080065 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -070066 return true;
67}
joshualittb0a8a372014-09-23 09:50:21 -070068
bsalomon7ea33f52015-11-22 14:51:00 -080069static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -070070 const GrFragmentProcessor& fp,
bsalomon7f9b2e42016-01-12 13:29:26 -080071 const GrGLSLCaps& glslCaps,
wangyixa7f4c432015-08-20 07:25:02 -070072 GrProcessorKeyBuilder* b) {
73 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7f9b2e42016-01-12 13:29:26 -080074 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), glslCaps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -070075 return false;
76 }
77 }
78
bsalomon7f9b2e42016-01-12 13:29:26 -080079 fp.getGLSLProcessorKey(glslCaps, b);
wangyixa7f4c432015-08-20 07:25:02 -070080
bsalomon7f9b2e42016-01-12 13:29:26 -080081 return gen_meta_key(fp, glslCaps, primProc.getTransformKey(fp.coordTransforms(),
82 fp.numTransformsExclChildren()), b);
wangyixa7f4c432015-08-20 07:25:02 -070083}
84
joshualitt873ad0e2015-01-20 09:08:51 -080085bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
86 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -080087 const GrPipeline& pipeline,
bsalomon7f9b2e42016-01-12 13:29:26 -080088 const GrGLSLCaps& glslCaps) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000089 // The descriptor is used as a cache key. Thus when a field of the
90 // descriptor will not affect program generation (because of the attribute
91 // bindings in use or other descriptor field settings) it should be set
92 // to a canonical value to avoid duplicate programs with different keys.
93
jvanverthd1e72872015-04-20 12:29:37 -070094 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
95
egdanielc67870c2014-11-26 08:50:50 -080096 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
97 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -070098 glDesc->key().reset();
99 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700100
jvanverthd1e72872015-04-20 12:29:37 -0700101 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -0800102
bsalomon7f9b2e42016-01-12 13:29:26 -0800103 primProc.getGLSLProcessorKey(glslCaps, &b);
104 if (!gen_meta_key(primProc, glslCaps, 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700105 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800106 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000107 }
bsalomon929f29a2014-07-17 07:55:11 -0700108
bsalomonac856c92015-08-27 06:30:17 -0700109 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
110 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7f9b2e42016-01-12 13:29:26 -0800111 if (!gen_frag_proc_and_meta_keys(primProc, fp, glslCaps, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700112 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700113 return false;
114 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000115 }
egdaniel170f90b2014-09-16 12:54:40 -0700116
bsalomon2047b782015-12-21 13:12:54 -0800117 const GrXferProcessor& xp = pipeline.getXferProcessor();
bsalomon7f9b2e42016-01-12 13:29:26 -0800118 xp.getGLSLProcessorKey(glslCaps, &b);
119 if (!gen_meta_key(xp, glslCaps, 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700120 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800121 return false;
122 }
123
joshualitt65171342014-10-09 07:25:36 -0700124 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700125 // Because header is a pointer into the dynamic array, we can't push any new data into the key
126 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700127 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000128
joshualitt65171342014-10-09 07:25:36 -0700129 // make sure any padding in the header is zeroed.
130 memset(header, 0, kHeaderSize);
131
bsalomon50785a32015-02-06 07:02:37 -0800132 if (pipeline.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700133 header->fFragPosKey =
egdaniel2d721d32015-11-11 13:06:05 -0800134 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000135 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000136 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000137 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800138
bsalomon7f9b2e42016-01-12 13:29:26 -0800139 header->fOutputSwizzle =
140 glslCaps.configOutputSwizzle(pipeline.getRenderTarget()->config()).asKey();
141
egdaniel56cf6dc2015-11-30 10:15:58 -0800142 if (pipeline.ignoresCoverage()) {
143 header->fIgnoresCoverage = 1;
144 } else {
145 header->fIgnoresCoverage = 0;
146 }
147
bsalomond79c5492015-04-27 10:07:04 -0700148 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomonac856c92015-08-27 06:30:17 -0700149 header->fColorEffectCnt = pipeline.numColorFragmentProcessors();
150 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors();
jvanverthd1e72872015-04-20 12:29:37 -0700151 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700152 return true;
153}