blob: 87e7f2d834c3641384f4f62d327fe613528b8843 [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
9#include "GrGLProcessor.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
joshualitt23e280d2014-09-18 12:26:38 -070064 */
joshualitta5305a12014-10-10 17:47:00 -070065static bool get_meta_key(const GrProcessor& proc,
66 const GrGLCaps& caps,
67 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080068 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080069 size_t processorKeySize = b->size();
joshualitta5305a12014-10-10 17:47:00 -070070 uint32_t textureKey = gen_texture_key(proc, caps);
joshualitteb2a6762014-12-04 11:35:33 -080071 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070072
73 // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they
74 // don't fit.
75 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
76 if ((textureKey | transformKey | classID) & kMetaKeyInvalidMask) {
joshualitta5305a12014-10-10 17:47:00 -070077 return false;
joshualitt89c7a2e2014-10-10 14:11:59 -070078 }
egdanielc67870c2014-11-26 08:50:50 -080079 if (processorKeySize > SK_MaxU16) {
joshualitt65171342014-10-09 07:25:36 -070080 return false;
81 }
82
egdanielc67870c2014-11-26 08:50:50 -080083 uint32_t* key = b->add32n(2);
84 key[0] = (textureKey << 16 | transformKey);
85 key[1] = (classID << 16 | SkToU16(processorKeySize));
joshualitt65171342014-10-09 07:25:36 -070086 return true;
87}
joshualittb0a8a372014-09-23 09:50:21 -070088
joshualitt873ad0e2015-01-20 09:08:51 -080089bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
90 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -080091 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -080092 const GrGLGpu* gpu,
93 const GrBatchTracker& batchTracker) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000094 // The descriptor is used as a cache key. Thus when a field of the
95 // descriptor will not affect program generation (because of the attribute
96 // bindings in use or other descriptor field settings) it should be set
97 // to a canonical value to avoid duplicate programs with different keys.
98
jvanverthd1e72872015-04-20 12:29:37 -070099 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
100
egdanielc67870c2014-11-26 08:50:50 -0800101 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
102 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -0700103 glDesc->key().reset();
104 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -0700105
jvanverthd1e72872015-04-20 12:29:37 -0700106 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -0800107
joshualitt873ad0e2015-01-20 09:08:51 -0800108 primProc.getGLProcessorKey(batchTracker, gpu->glCaps(), &b);
joshualitt9b989322014-12-15 14:16:27 -0800109 if (!get_meta_key(primProc, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700110 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800111 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000112 }
bsalomon929f29a2014-07-17 07:55:11 -0700113
egdaniel8dd688b2015-01-22 10:16:09 -0800114 for (int s = 0; s < pipeline.numFragmentStages(); ++s) {
115 const GrPendingFragmentStage& fps = pipeline.getFragmentStage(s);
joshualitt40d4bd82014-12-29 09:04:40 -0800116 const GrFragmentProcessor& fp = *fps.processor();
joshualitteb2a6762014-12-04 11:35:33 -0800117 fp.getGLProcessorKey(gpu->glCaps(), &b);
joshualittabb52a12015-01-13 15:02:10 -0800118 if (!get_meta_key(fp, gpu->glCaps(), primProc.getTransformKey(fp.coordTransforms()), &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700119 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700120 return false;
121 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000122 }
egdaniel170f90b2014-09-16 12:54:40 -0700123
egdaniel8dd688b2015-01-22 10:16:09 -0800124 const GrXferProcessor& xp = *pipeline.getXferProcessor();
egdanielc2304142014-12-11 13:15:13 -0800125 xp.getGLProcessorKey(gpu->glCaps(), &b);
joshualitt9b989322014-12-15 14:16:27 -0800126 if (!get_meta_key(xp, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700127 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800128 return false;
129 }
130
joshualitt65171342014-10-09 07:25:36 -0700131 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700132 // Because header is a pointer into the dynamic array, we can't push any new data into the key
133 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700134 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000135
joshualitt65171342014-10-09 07:25:36 -0700136 // make sure any padding in the header is zeroed.
137 memset(header, 0, kHeaderSize);
138
bsalomon50785a32015-02-06 07:02:37 -0800139 if (pipeline.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700140 header->fFragPosKey =
egdaniel8dd688b2015-01-22 10:16:09 -0800141 GrGLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget(),
joshualitt47bb3822014-10-07 16:43:25 -0700142 gpu->glCaps());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000143 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000144 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000145 }
bsalomond79c5492015-04-27 10:07:04 -0700146 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomon6be6f7c2015-02-26 13:05:21 -0800147 header->fColorEffectCnt = pipeline.numColorFragmentStages();
148 header->fCoverageEffectCnt = pipeline.numCoverageFragmentStages();
jvanverthd1e72872015-04-20 12:29:37 -0700149 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700150 return true;
151}