blob: 94b6f98a512f1e50ad241964d997e256dbb43b2a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrShaderCaps.h"
12#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
14#include "src/gpu/glsl/GrGLSLVarying.h"
15#include "src/gpu/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 Verth1694a862018-12-17 19:48:42 +000023 using Interpolation = GrGLSLVaryingHandler::Interpolation;
24
25 // This extracts the texture index and texel coordinates from the same variable
Jim Van Verth6a7a7042017-09-11 11:04:10 -040026 // Packing structure: texel coordinates are multiplied by 2 (or shifted left 1)
27 // texture index is stored as lower bits of both x and y
Jim Van Verth1694a862018-12-17 19:48:42 +000028 if (args.fShaderCaps->integerSupport()) {
29 args.fVertBuilder->codeAppendf("int2 signedCoords = int2(%s.x, %s.y);",
30 inTexCoordsName, inTexCoordsName);
31 args.fVertBuilder->codeAppend("int texIdx = 2*(signedCoords.x & 0x1) + (signedCoords.y & 0x1);");
32 args.fVertBuilder->codeAppend("float2 unormTexCoords = float2(signedCoords.x/2, signedCoords.y/2);");
33 } else {
34 args.fVertBuilder->codeAppendf("float2 indexTexCoords = float2(%s.x, %s.y);",
35 inTexCoordsName, inTexCoordsName);
36 args.fVertBuilder->codeAppend("float2 unormTexCoords = floor(0.5*indexTexCoords);");
37 args.fVertBuilder->codeAppend("float2 diff = indexTexCoords - 2.0*unormTexCoords;");
38 args.fVertBuilder->codeAppend("float texIdx = 2.0*diff.x + diff.y;");
39 }
Jim Van Verth6a7a7042017-09-11 11:04:10 -040040
41 // Multiply by 1/atlasSize to get normalized texture coordinates
Chris Daltonfdde34e2017-10-16 14:15:26 -060042 args.fVaryingHandler->addVarying("TextureCoords", uv);
Jim Van Verthfc4f7682018-01-25 16:26:25 -050043 args.fVertBuilder->codeAppendf("%s = unormTexCoords * %s;", uv->vsOut(), atlasSizeInvName);
Jim Van Verth6a7a7042017-09-11 11:04:10 -040044
Jim Van Verth1694a862018-12-17 19:48:42 +000045 args.fVaryingHandler->addVarying("TexIndex", texIdx, args.fShaderCaps->integerSupport()
46 ? Interpolation::kMustBeFlat
47 : Interpolation::kCanBeFlat);
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) {
Jim Van Verth1694a862018-12-17 19:48:42 +000061 // conditionally load from the indexed texture sampler
62 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 Verth1694a862018-12-17 19:48:42 +000069 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