blob: 5365ffbf043aac5c102f60f542ffdd1758784c4d [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
wangyix6af0c932015-07-22 10:21:17 -07009#include "GrGLFragmentProcessor.h"
joshualittb0a8a372014-09-23 09:50:21 -070010#include "GrProcessor.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "GrGLGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080012#include "GrPipeline.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000013#include "SkChecksum.h"
joshualitt79f8fae2014-10-28 17:59:26 -070014#include "gl/builders/GrGLFragmentShaderBuilder.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 */
21static bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
22 uint32_t configComponentMask,
23 uint32_t swizzleComponentMask) {
24 if (caps.textureSwizzleSupport()) {
25 // Any remapping is handled using texture swizzling not shader modifications.
26 return false;
27 }
28 // check if the texture is alpha-only
29 if (kA_GrColorComponentFlag == configComponentMask) {
30 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) {
31 // we must map the swizzle 'a's to 'r'.
32 return true;
33 }
34 if (kRGB_GrColorComponentFlags & swizzleComponentMask) {
35 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that
36 // alpha-only textures smear alpha across all four channels when read.
37 return true;
38 }
39 }
40 return false;
41}
42
joshualitta5305a12014-10-10 17:47:00 -070043static uint32_t gen_texture_key(const GrProcessor& proc, const GrGLCaps& caps) {
joshualitt23e280d2014-09-18 12:26:38 -070044 uint32_t key = 0;
joshualitta5305a12014-10-10 17:47:00 -070045 int numTextures = proc.numTextures();
joshualitt23e280d2014-09-18 12:26:38 -070046 for (int t = 0; t < numTextures; ++t) {
joshualitta5305a12014-10-10 17:47:00 -070047 const GrTextureAccess& access = proc.textureAccess(t);
joshualitt23e280d2014-09-18 12:26:38 -070048 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config());
49 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) {
50 key |= 1 << t;
51 }
52 }
53 return key;
54}
55
56/**
57 * A function which emits a meta key into the key builder. This is required because shader code may
58 * be dependent on properties of the effect that the effect itself doesn't use
59 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
60 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070061 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
joshualitta5305a12014-10-10 17:47:00 -070062 * textures, transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share
63 * this function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
wangyixa7f4c432015-08-20 07:25:02 -070064 *
65 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt23e280d2014-09-18 12:26:38 -070066 */
joshualitta5305a12014-10-10 17:47:00 -070067static bool get_meta_key(const GrProcessor& proc,
68 const GrGLCaps& caps,
69 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080070 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080071 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -070072 uint32_t textureKey = gen_texture_key(proc, caps);
joshualitteb2a6762014-12-04 11:35:33 -080073 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070074
75 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
76 // don't fit.
77 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
78 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -070079 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -070080 }
egdanielc67870c2014-11-26 08:50:50 -080081 if (processorKeySize > SK_MaxU16) {
joshualitt65171342014-10-09 07:25:36 -070082 return false;
83 }
84
egdanielc67870c2014-11-26 08:50:50 -080085 uint32_t* key = b->add32n(2);
86 key[0] = (textureKey << 16 | transformKey);
87 key[1] = (classID << 16 | SkToU16(processorKeySize));
joshualitt65171342014-10-09 07:25:36 -070088 return true;
89}
joshualittb0a8a372014-09-23 09:50:21 -070090
wangyixa7f4c432015-08-20 07:25:02 -070091/*
92 * TODO: A better name for this function would be "compute" instead of "get".
93 */
94static bool get_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
95 const GrFragmentProcessor& fp,
96 const GrGLCaps& caps,
97 GrProcessorKeyBuilder* b) {
98 for (int i = 0; i < fp.numChildProcessors(); ++i) {
99 if (!get_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) {
100 return false;
101 }
102 }
103
104 fp.getGLProcessorKey(*caps.glslCaps(), b);
105
106 //**** use glslCaps here?
107 return get_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(),
108 fp.numTransformsExclChildren()), b);
109}
110
joshualitt873ad0e2015-01-20 09:08:51 -0800111bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
112 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800113 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -0800114 const GrGLGpu* gpu,
115 const GrBatchTracker& batchTracker) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000116 // The descriptor is used as a cache key. Thus when a field of the
117 // descriptor will not affect program generation (because of the attribute
118 // bindings in use or other descriptor field settings) it should be set
119 // to a canonical value to avoid duplicate programs with different keys.
120
jvanverthd1e72872015-04-20 12:29:37 -0700121 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
122
egdanielc67870c2014-11-26 08:50:50 -0800123 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
124 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -0700125 glDesc->key().reset();
126 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700127
jvanverthd1e72872015-04-20 12:29:37 -0700128 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -0800129
jvanverthe9c0fc62015-04-29 11:18:05 -0700130 primProc.getGLProcessorKey(batchTracker, *gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700131 //**** use glslCaps here?
joshualitt9b989322014-12-15 14:16:27 -0800132 if (!get_meta_key(primProc, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700133 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800134 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000135 }
bsalomon929f29a2014-07-17 07:55:11 -0700136
egdaniel8dd688b2015-01-22 10:16:09 -0800137 for (int s = 0; s < pipeline.numFragmentStages(); ++s) {
138 const GrPendingFragmentStage& fps = pipeline.getFragmentStage(s);
joshualitt40d4bd82014-12-29 09:04:40 -0800139 const GrFragmentProcessor& fp = *fps.processor();
wangyixa7f4c432015-08-20 07:25:02 -0700140 if (!get_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700141 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700142 return false;
143 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000144 }
egdaniel170f90b2014-09-16 12:54:40 -0700145
egdaniel8dd688b2015-01-22 10:16:09 -0800146 const GrXferProcessor& xp = *pipeline.getXferProcessor();
jvanverthe9c0fc62015-04-29 11:18:05 -0700147 xp.getGLProcessorKey(*gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700148 //**** use glslCaps here?
joshualitt9b989322014-12-15 14:16:27 -0800149 if (!get_meta_key(xp, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700150 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800151 return false;
152 }
153
joshualitt65171342014-10-09 07:25:36 -0700154 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700155 // Because header is a pointer into the dynamic array, we can't push any new data into the key
156 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700157 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000158
joshualitt65171342014-10-09 07:25:36 -0700159 // make sure any padding in the header is zeroed.
160 memset(header, 0, kHeaderSize);
161
bsalomon50785a32015-02-06 07:02:37 -0800162 if (pipeline.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700163 header->fFragPosKey =
egdaniel8dd688b2015-01-22 10:16:09 -0800164 GrGLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget(),
joshualitt47bb3822014-10-07 16:43:25 -0700165 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000166 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000167 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000168 }
bsalomond79c5492015-04-27 10:07:04 -0700169 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon6be6f7c2015-02-26 13:05:21 -0800170 header->fColorEffectCnt = pipeline.numColorFragmentStages();
171 header->fCoverageEffectCnt = pipeline.numCoverageFragmentStages();
jvanverthd1e72872015-04-20 12:29:37 -0700172 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700173 return true;
174}