blob: 67f2c242c2aee456766dad0f9f1704677869ff71 [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
11#include "glsl/GrGLSLPrimitiveProcessor.h"
12#include "glsl/GrGLSLFragmentShaderBuilder.h"
13#include "glsl/GrGLSLVarying.h"
14#include "glsl/GrGLSLVertexShaderBuilder.h"
15
16static void append_index_uv_varyings(GrGLSLPrimitiveProcessor::EmitArgs& args,
17 const char* inTexCoordsName,
18 const char* atlasSizeInvName,
19 GrGLSLVertToFrag *uv,
20 GrGLSLVertToFrag *texIdx,
21 GrGLSLVertToFrag *st) {
22 // This extracts the texture index and texel coordinates from the same variable
23 // Packing structure: texel coordinates are multiplied by 2 (or shifted left 1)
24 // texture index is stored as lower bits of both x and y
Ethan Nicholas27185a92017-09-18 02:41:08 +000025 args.fVertBuilder->codeAppendf("float2 indexTexCoords = float2(%s.x, %s.y);",
Jim Van Verth6a7a7042017-09-11 11:04:10 -040026 inTexCoordsName, inTexCoordsName);
Ethan Nicholas27185a92017-09-18 02:41:08 +000027 args.fVertBuilder->codeAppend("float2 intCoords = floor(0.5*indexTexCoords);");
28 args.fVertBuilder->codeAppend("float2 diff = indexTexCoords - 2.0*intCoords;");
29 args.fVertBuilder->codeAppend("float texIdx = 2.0*diff.x + diff.y;");
Jim Van Verth6a7a7042017-09-11 11:04:10 -040030
31 // Multiply by 1/atlasSize to get normalized texture coordinates
32 args.fVaryingHandler->addVarying("TextureCoords", uv, kHigh_GrSLPrecision);
33 args.fVertBuilder->codeAppendf("%s = intCoords * %s;", uv->vsOut(), atlasSizeInvName);
34
35 args.fVaryingHandler->addVarying("TexIndex", texIdx);
36 args.fVertBuilder->codeAppendf("%s = texIdx;", texIdx->vsOut());
37
38 if (st) {
39 args.fVaryingHandler->addVarying("IntTextureCoords", st, kHigh_GrSLPrecision);
40 args.fVertBuilder->codeAppendf("%s = intCoords;", st->vsOut());
41 }
42}
43
44static void append_multitexture_lookup(GrGLSLPrimitiveProcessor::EmitArgs& args,
45 int numTextureSamplers,
46 const GrGLSLVertToFrag &texIdx,
47 const char* coordName,
48 const char* colorName) {
49 // conditionally load from the indexed texture sampler
Jim Van Verthad70c412017-09-15 17:14:20 +000050 if (numTextureSamplers > 1) {
51 args.fFragBuilder->codeAppendf("if (%s == 0) ", texIdx.fsIn());
Jim Van Verth6a7a7042017-09-11 11:04:10 -040052 }
53 args.fFragBuilder->codeAppendf("{ %s = ", colorName);
Ethan Nicholas27185a92017-09-18 02:41:08 +000054 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], coordName, kVec2f_GrSLType);
Jim Van Verth6a7a7042017-09-11 11:04:10 -040055 args.fFragBuilder->codeAppend("; }");
Jim Van Verthad70c412017-09-15 17:14:20 +000056 for (int i = 1; i < numTextureSamplers; ++i) {
57 args.fFragBuilder->codeAppendf("else if (%s == %d) { %s =", texIdx.fsIn(), i, colorName);
Ethan Nicholas27185a92017-09-18 02:41:08 +000058 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName, kVec2f_GrSLType);
Jim Van Verthad70c412017-09-15 17:14:20 +000059 args.fFragBuilder->codeAppend("; }");
60 }
Jim Van Verth6a7a7042017-09-11 11:04:10 -040061}
62
63#endif