blob: 032562550cec5d5541f5405f77fcb861e8b046ed [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 }
joshualitt23e280d2014-09-18 12:26:38 -070033}
34
35/**
36 * A function which emits a meta key into the key builder. This is required because shader code may
37 * be dependent on properties of the effect that the effect itself doesn't use
38 * in its key (e.g. the pixel format of textures used). So we create a meta-key for
39 * every effect using this function. It is also responsible for inserting the effect's class ID
joshualittb0a8a372014-09-23 09:50:21 -070040 * which must be different for every GrProcessor subclass. It can fail if an effect uses too many
bsalomoncdee0092016-01-08 13:20:12 -080041 * transforms, etc, for the space allotted in the meta-key. NOTE, both FPs and GPs share this
42 * function because it is hairy, though FPs do not have attribs, and GPs do not have transforms
joshualitt23e280d2014-09-18 12:26:38 -070043 */
bsalomon7ea33f52015-11-22 14:51:00 -080044static bool gen_meta_key(const GrProcessor& proc,
joshualitta5305a12014-10-10 17:47:00 -070045 const GrGLCaps& caps,
46 uint32_t transformKey,
egdanielc67870c2014-11-26 08:50:50 -080047 GrProcessorKeyBuilder* b) {
egdanielc67870c2014-11-26 08:50:50 -080048 size_t processorKeySize = b->size();
joshualitteb2a6762014-12-04 11:35:33 -080049 uint32_t classID = proc.classID();
joshualitt89c7a2e2014-10-10 14:11:59 -070050
bsalomon7ea33f52015-11-22 14:51:00 -080051 // Currently we allow 16 bits for the class id and the overall processor key size.
joshualitt89c7a2e2014-10-10 14:11:59 -070052 static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16);
bsalomon7ea33f52015-11-22 14:51:00 -080053 if ((processorKeySize | classID) & kMetaKeyInvalidMask) {
joshualitt65171342014-10-09 07:25:36 -070054 return false;
55 }
56
bsalomoncdee0092016-01-08 13:20:12 -080057 add_texture_key(b, proc, *caps.glslCaps());
58
59 uint32_t* key = b->add32n(2);
bsalomon7ea33f52015-11-22 14:51:00 -080060 key[0] = (classID << 16) | SkToU32(processorKeySize);
bsalomoncdee0092016-01-08 13:20:12 -080061 key[1] = transformKey;
joshualitt65171342014-10-09 07:25:36 -070062 return true;
63}
joshualittb0a8a372014-09-23 09:50:21 -070064
bsalomon7ea33f52015-11-22 14:51:00 -080065static bool gen_frag_proc_and_meta_keys(const GrPrimitiveProcessor& primProc,
wangyixa7f4c432015-08-20 07:25:02 -070066 const GrFragmentProcessor& fp,
67 const GrGLCaps& caps,
68 GrProcessorKeyBuilder* b) {
69 for (int i = 0; i < fp.numChildProcessors(); ++i) {
bsalomon7ea33f52015-11-22 14:51:00 -080070 if (!gen_frag_proc_and_meta_keys(primProc, fp.childProcessor(i), caps, b)) {
wangyixa7f4c432015-08-20 07:25:02 -070071 return false;
72 }
73 }
74
egdaniel57d3b032015-11-13 11:57:27 -080075 fp.getGLSLProcessorKey(*caps.glslCaps(), b);
wangyixa7f4c432015-08-20 07:25:02 -070076
77 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -080078 return gen_meta_key(fp, caps, primProc.getTransformKey(fp.coordTransforms(),
wangyixa7f4c432015-08-20 07:25:02 -070079 fp.numTransformsExclChildren()), b);
80}
81
joshualitt873ad0e2015-01-20 09:08:51 -080082bool GrGLProgramDescBuilder::Build(GrProgramDesc* desc,
83 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -080084 const GrPipeline& pipeline,
joshualitt465283c2015-09-11 08:19:35 -070085 const GrGLGpu* gpu) {
bsalomon@google.com798c8c42013-03-27 19:50:27 +000086 // The descriptor is used as a cache key. Thus when a field of the
87 // descriptor will not affect program generation (because of the attribute
88 // bindings in use or other descriptor field settings) it should be set
89 // to a canonical value to avoid duplicate programs with different keys.
90
jvanverthd1e72872015-04-20 12:29:37 -070091 GrGLProgramDesc* glDesc = (GrGLProgramDesc*) desc;
92
egdanielc67870c2014-11-26 08:50:50 -080093 GR_STATIC_ASSERT(0 == kProcessorKeysOffset % sizeof(uint32_t));
94 // Make room for everything up to the effect keys.
jvanverthd1e72872015-04-20 12:29:37 -070095 glDesc->key().reset();
96 glDesc->key().push_back_n(kProcessorKeysOffset);
joshualittbd769d02014-09-04 08:56:46 -070097
jvanverthd1e72872015-04-20 12:29:37 -070098 GrProcessorKeyBuilder b(&glDesc->key());
joshualitt9b989322014-12-15 14:16:27 -080099
egdaniel57d3b032015-11-13 11:57:27 -0800100 primProc.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700101 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -0800102 if (!gen_meta_key(primProc, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700103 glDesc->key().reset();
joshualitt9b989322014-12-15 14:16:27 -0800104 return false;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000105 }
bsalomon929f29a2014-07-17 07:55:11 -0700106
bsalomonac856c92015-08-27 06:30:17 -0700107 for (int i = 0; i < pipeline.numFragmentProcessors(); ++i) {
108 const GrFragmentProcessor& fp = pipeline.getFragmentProcessor(i);
bsalomon7ea33f52015-11-22 14:51:00 -0800109 if (!gen_frag_proc_and_meta_keys(primProc, fp, gpu->glCaps(), &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700110 glDesc->key().reset();
joshualittb0a8a372014-09-23 09:50:21 -0700111 return false;
112 }
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000113 }
egdaniel170f90b2014-09-16 12:54:40 -0700114
bsalomon2047b782015-12-21 13:12:54 -0800115 const GrXferProcessor& xp = pipeline.getXferProcessor();
egdaniel57d3b032015-11-13 11:57:27 -0800116 xp.getGLSLProcessorKey(*gpu->glCaps().glslCaps(), &b);
jvanverthcfc18862015-04-28 08:48:20 -0700117 //**** use glslCaps here?
bsalomon7ea33f52015-11-22 14:51:00 -0800118 if (!gen_meta_key(xp, gpu->glCaps(), 0, &b)) {
jvanverthd1e72872015-04-20 12:29:37 -0700119 glDesc->key().reset();
egdanielc2304142014-12-11 13:15:13 -0800120 return false;
121 }
122
joshualitt65171342014-10-09 07:25:36 -0700123 // --------DO NOT MOVE HEADER ABOVE THIS LINE--------------------------------------------------
bsalomon848faf02014-07-11 10:01:02 -0700124 // Because header is a pointer into the dynamic array, we can't push any new data into the key
125 // below here.
jvanverthd1e72872015-04-20 12:29:37 -0700126 KeyHeader* header = glDesc->atOffset<KeyHeader, kHeaderOffset>();
bsalomon@google.com798c8c42013-03-27 19:50:27 +0000127
joshualitt65171342014-10-09 07:25:36 -0700128 // make sure any padding in the header is zeroed.
129 memset(header, 0, kHeaderSize);
130
bsalomon50785a32015-02-06 07:02:37 -0800131 if (pipeline.readsFragPosition()) {
joshualitt47bb3822014-10-07 16:43:25 -0700132 header->fFragPosKey =
egdaniel2d721d32015-11-11 13:06:05 -0800133 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(pipeline.getRenderTarget());
bsalomon@google.comb5158812013-05-13 18:50:25 +0000134 } else {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000135 header->fFragPosKey = 0;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000136 }
egdaniel56cf6dc2015-11-30 10:15:58 -0800137
138 if (pipeline.ignoresCoverage()) {
139 header->fIgnoresCoverage = 1;
140 } else {
141 header->fIgnoresCoverage = 0;
142 }
143
bsalomond79c5492015-04-27 10:07:04 -0700144 header->fSnapVerticesToPixelCenters = pipeline.snapVerticesToPixelCenters();
bsalomonac856c92015-08-27 06:30:17 -0700145 header->fColorEffectCnt = pipeline.numColorFragmentProcessors();
146 header->fCoverageEffectCnt = pipeline.numCoverageFragmentProcessors();
jvanverthd1e72872015-04-20 12:29:37 -0700147 glDesc->finalize();
bsalomon848faf02014-07-11 10:01:02 -0700148 return true;
149}