blob: 8c09291d6ab0e7acfcb5883c297696ac62b40551 [file] [log] [blame]
Jim Van Verth6a7a7042017-09-11 11:04:10 -04001/*
2 * Copyright 2017 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 */
7
8#ifndef GrAtlasedShaderHelpers_DEFINED
9#define GrAtlasedShaderHelpers_DEFINED
10
Jim Van Verthfc4f7682018-01-25 16:26:25 -050011#include "GrShaderCaps.h"
Jim Van Verth6a7a7042017-09-11 11:04:10 -040012#include "glsl/GrGLSLPrimitiveProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060015#include "glsl/GrGLSLVertexGeoBuilder.h"
Jim Van Verth6a7a7042017-09-11 11:04:10 -040016
17static void append_index_uv_varyings(GrGLSLPrimitiveProcessor::EmitArgs& args,
18 const char* inTexCoordsName,
19 const char* atlasSizeInvName,
Chris Dalton27372882017-12-08 13:34:21 -070020 GrGLSLVarying *uv,
21 GrGLSLVarying *texIdx,
22 GrGLSLVarying *st) {
Jim Van Verth6a7a7042017-09-11 11:04:10 -040023 // This extracts the texture index and texel coordinates from the same variable
24 // Packing structure: texel coordinates are multiplied by 2 (or shifted left 1)
25 // texture index is stored as lower bits of both x and y
Jim Van Verthfc4f7682018-01-25 16:26:25 -050026 if (args.fShaderCaps->integerSupport()) {
27 args.fVertBuilder->codeAppendf("int2 signedCoords = int2(%s.x, %s.y);",
28 inTexCoordsName, inTexCoordsName);
29 args.fVertBuilder->codeAppend("int texIdx = 2*(signedCoords.x & 0x1) + (signedCoords.y & 0x1);");
30 args.fVertBuilder->codeAppend("float2 unormTexCoords = float2(signedCoords.x/2, signedCoords.y/2);");
31 } else {
32 args.fVertBuilder->codeAppendf("float2 indexTexCoords = float2(%s.x, %s.y);",
33 inTexCoordsName, inTexCoordsName);
34 args.fVertBuilder->codeAppend("float2 unormTexCoords = floor(0.5*indexTexCoords);");
35 args.fVertBuilder->codeAppend("float2 diff = indexTexCoords - 2.0*unormTexCoords;");
36 args.fVertBuilder->codeAppend("float texIdx = 2.0*diff.x + diff.y;");
37 }
Jim Van Verth6a7a7042017-09-11 11:04:10 -040038
39 // Multiply by 1/atlasSize to get normalized texture coordinates
Chris Daltonfdde34e2017-10-16 14:15:26 -060040 args.fVaryingHandler->addVarying("TextureCoords", uv);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050041 args.fVertBuilder->codeAppendf("%s = unormTexCoords * %s;", uv->vsOut(), atlasSizeInvName);
Jim Van Verth6a7a7042017-09-11 11:04:10 -040042
Jim Van Verthfc4f7682018-01-25 16:26:25 -050043 if (args.fShaderCaps->integerSupport()) {
44 args.fVaryingHandler->addFlatVarying("TexIndex", texIdx);
45 } else {
46 args.fVaryingHandler->addVarying("TexIndex", texIdx);
47 }
Jim Van Verth6a7a7042017-09-11 11:04:10 -040048 args.fVertBuilder->codeAppendf("%s = texIdx;", texIdx->vsOut());
49
50 if (st) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060051 args.fVaryingHandler->addVarying("IntTextureCoords", st);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050052 args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut());
Jim Van Verth6a7a7042017-09-11 11:04:10 -040053 }
54}
55
56static void append_multitexture_lookup(GrGLSLPrimitiveProcessor::EmitArgs& args,
57 int numTextureSamplers,
Chris Dalton27372882017-12-08 13:34:21 -070058 const GrGLSLVarying &texIdx,
Jim Van Verth6a7a7042017-09-11 11:04:10 -040059 const char* coordName,
60 const char* colorName) {
61 // conditionally load from the indexed texture sampler
Jim Van Vertheafa64b2017-09-18 10:05:00 -040062 for (int i = 0; i < numTextureSamplers-1; ++i) {
63 args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040064 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040065 kFloat2_GrSLType);
Jim Van Vertheafa64b2017-09-18 10:05:00 -040066 args.fFragBuilder->codeAppend("; } else ");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040067 }
68 args.fFragBuilder->codeAppendf("{ %s = ", colorName);
Jim Van Vertheafa64b2017-09-18 10:05:00 -040069 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers-1], coordName,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040070 kFloat2_GrSLType);
Jim Van Verth6a7a7042017-09-11 11:04:10 -040071 args.fFragBuilder->codeAppend("; }");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040072}
73
74#endif