blob: e3d292c062aedefdee962693097377a865e3598f [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/**
joshualitt23e280d2014-09-18 12:26:38 -070017 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are
18 * present in the texture's config. swizzleComponentMask indicates the channels present in the
19 * shader swizzle.
20 */
egdanielb7e7d572015-11-04 04:23:53 -080021static bool swizzle_requires_alpha_remapping(const GrGLSLCaps& caps, GrPixelConfig config) {
22 if (!caps.mustSwizzleInShader()) {
joshualitt23e280d2014-09-18 12:26:38 -070023 // Any remapping is handled using texture swizzling not shader modifications.
24 return false;
25 }
egdanielb7e7d572015-11-04 04:23:53 -080026 const char* swizzleMap = caps.getSwizzleMap(config);
27
28 return SkToBool(memcmp(swizzleMap, "rgba", 4));
joshualitt23e280d2014-09-18 12:26:38 -070029}
30
joshualitta5305a12014-10-10 17:47:00 -070031static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -070032 uint32_t key = 0;
joshualitta5305a12014-10-10 17:47:00 -070033 int numTextures = proc.numTextures();
bsalomon7ea33f52015-11-22 14:51:00 -080034 int shift = 0;
joshualitt23e280d2014-09-18 12:26:38 -070035 for (int t = 0; t < numTextures; ++t) {
joshualitta5305a12014-10-10 17:47:00 -070036 const GrTextureAccess& access = proc.textureAccess(t);
egdanielb7e7d572015-11-04 04:23:53 -080037 if (swizzle_requires_alpha_remapping(*caps.glslCaps(), access.getTexture()->config())) {
bsalomon7ea33f52015-11-22 14:51:00 -080038 key |= 1 << shift;
joshualitt23e280d2014-09-18 12:26:38 -070039 }
bsalomon7ea33f52015-11-22 14:51:00 -080040 if (GR_GL_TEXTURE_EXTERNAL == static_cast<GrGLTexture*>(access.getTexture())->target()) {
41 key |= 2 << shift;
42 }
43 shift += 2;
joshualitt23e280d2014-09-18 12:26:38 -070044 }
45 return key;
46}
47
48/**
49 * A function which emits a meta key into the key builder. This is required because shader code may
50 * be dependent on properties of the effect that the effect itself doesn't use
51 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
52 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070053 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
joshualitta5305a12014-10-10 17:47:00 -070054 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
55 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070056 */
bsalomon7ea33f52015-11-22 14:51:00 -080057static bool gen_meta_key(const GrProcessor& proc,
joshualitta5305a12014-10-10 17:47:00 -070058 const GrGLCaps& caps,
59 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080060 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080061 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -070062 uint32_t textureKey = gen_texture_key(proc, caps);
joshualitteb2a6762014-12-04 11:35:33 -080063 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070064
bsalomon7ea33f52015-11-22 14:51:00 -080065 // Currently we allow 16 bits for the class id and the overall processor key size.
joshualitt89c7a2e2014-10-10 14:11:59 -070066 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -080067 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070068 return false;
69 }
70
bsalomon7ea33f52015-11-22 14:51:00 -080071 uint32_t* key = b->add32n(3);
72 key[0] = (classID << 16) | SkToU32(processorKeySize);
73 key[1] = textureKey;
74 key[2] = transformKey;
joshualitt65171342014-10-09 07:25:36 -070075 return true;
76}
joshualittb0a8a372014-09-23 09:50:21 -070077
bsalomon7ea33f52015-11-22 14:51:00 -080078static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -070079 const GrFragmentProcessor& fp,
80 const GrGLCaps& caps,
81 GrProcessorKeyBuilder* b) {
82 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7ea33f52015-11-22 14:51:00 -080083 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -070084 return false;
85 }
86 }
87
egdaniel57d3b032015-11-13 11:57:27 -080088 fp.getGLSLProcessorKey(*caps.glslCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -070089
90 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -080091 return gen_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(),
wangyixa7f4c432015-08-20 07:25:02 -070092 fp.numTransformsExclChildren()), b);
93}
94
joshualitt873ad0e2015-01-20 09:08:51 -080095bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
96 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -080097 const GrPipeline& pipeline,
joshualitt465283c2015-09-11 08:19:35 -070098 const GrGLGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000099 // The descriptor is used as a cache key. Thus when a field of the
100 // descriptor will not affect program generation (because of the attribute
101 // bindings in use or other descriptor field settings) it should be set
102 // to a canonical value to avoid duplicate programs with different keys.
103
jvanverthd1e72872015-04-20 12:29:37 -0700104 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
105
egdanielc67870c2014-11-26 08:50:50 -0800106 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
107 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -0700108 glDesc->key().reset();
109 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700110
jvanverthd1e72872015-04-20 12:29:37 -0700111 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -0800112
egdaniel57d3b032015-11-13 11:57:27 -0800113 primProc.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700114 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -0800115 if (!gen_meta_key(primProc, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700116 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800117 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000118 }
bsalomon929f29a2014-07-17 07:55:11 -0700119
bsalomonac856c92015-08-27 06:30:17 -0700120 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
121 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7ea33f52015-11-22 14:51:00 -0800122 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700123 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700124 return false;
125 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000126 }
egdaniel170f90b2014-09-16 12:54:40 -0700127
egdaniel8dd688b2015-01-22 10:16:09 -0800128 const GrXferProcessor& xp = *pipeline.getXferProcessor();
egdaniel57d3b032015-11-13 11:57:27 -0800129 xp.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700130 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -0800131 if (!gen_meta_key(xp, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700132 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800133 return false;
134 }
135
joshualitt65171342014-10-09 07:25:36 -0700136 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700137 // Because header is a pointer into the dynamic array, we can't push any new data into the key
138 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700139 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000140
joshualitt65171342014-10-09 07:25:36 -0700141 // make sure any padding in the header is zeroed.
142 memset(header, 0, kHeaderSize);
143
bsalomon50785a32015-02-06 07:02:37 -0800144 if (pipeline.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700145 header->fFragPosKey =
egdaniel2d721d32015-11-11 13:06:05 -0800146 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000147 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000148 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000149 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800150
151 if (pipeline.ignoresCoverage()) {
152 header->fIgnoresCoverage = 1;
153 } else {
154 header->fIgnoresCoverage = 0;
155 }
156
bsalomond79c5492015-04-27 10:07:04 -0700157 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomonac856c92015-08-27 06:30:17 -0700158 header->fColorEffectCnt = pipeline.numColorFragmentProcessors();
159 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors();
jvanverthd1e72872015-04-20 12:29:37 -0700160 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700161 return true;
162}