Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | static 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 Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 25 | args.fVertBuilder->codeAppendf("half2 indexTexCoords = half2(%s.x, %s.y);", |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 26 | inTexCoordsName, inTexCoordsName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 27 | args.fVertBuilder->codeAppend("half2 intCoords = floor(0.5*indexTexCoords);"); |
| 28 | args.fVertBuilder->codeAppend("half2 diff = indexTexCoords - 2.0*intCoords;"); |
| 29 | args.fVertBuilder->codeAppend("half texIdx = 2.0*diff.x + diff.y;"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 30 | |
| 31 | // Multiply by 1/atlasSize to get normalized texture coordinates |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 32 | args.fVaryingHandler->addVarying("TextureCoords", uv); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 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) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 39 | args.fVaryingHandler->addVarying("IntTextureCoords", st); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 40 | args.fVertBuilder->codeAppendf("%s = intCoords;", st->vsOut()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static 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 Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 50 | for (int i = 0; i < numTextureSamplers-1; ++i) { |
| 51 | args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 52 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 53 | kFloat2_GrSLType); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 54 | args.fFragBuilder->codeAppend("; } else "); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 55 | } |
| 56 | args.fFragBuilder->codeAppendf("{ %s = ", colorName); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 57 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers-1], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 58 | kFloat2_GrSLType); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 59 | args.fFragBuilder->codeAppend("; }"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #endif |