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 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 11 | #include "GrShaderCaps.h" |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 12 | #include "glsl/GrGLSLPrimitiveProcessor.h" |
| 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "glsl/GrGLSLVarying.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 15 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 16 | |
| 17 | static void append_index_uv_varyings(GrGLSLPrimitiveProcessor::EmitArgs& args, |
| 18 | const char* inTexCoordsName, |
| 19 | const char* atlasSizeInvName, |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 20 | GrGLSLVarying *uv, |
| 21 | GrGLSLVarying *texIdx, |
| 22 | GrGLSLVarying *st) { |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 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 |
Jim Van Verth | 0711094 | 2018-11-20 14:46:41 -0500 | [diff] [blame] | 25 | args.fVertBuilder->codeAppendf("float2 indexTexCoords = float2(%s.x, %s.y);", |
| 26 | inTexCoordsName, inTexCoordsName); |
| 27 | args.fVertBuilder->codeAppend("float2 unormTexCoords = floor(0.5*indexTexCoords);"); |
| 28 | args.fVertBuilder->codeAppend("float2 diff = indexTexCoords - 2.0*unormTexCoords;"); |
| 29 | args.fVertBuilder->codeAppend("float 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 | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 33 | args.fVertBuilder->codeAppendf("%s = unormTexCoords * %s;", uv->vsOut(), atlasSizeInvName); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 34 | |
Jim Van Verth | 0711094 | 2018-11-20 14:46:41 -0500 | [diff] [blame] | 35 | args.fVaryingHandler->addVarying("TexIndex", texIdx); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 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 | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 40 | args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut()); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | static void append_multitexture_lookup(GrGLSLPrimitiveProcessor::EmitArgs& args, |
| 45 | int numTextureSamplers, |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 46 | const GrGLSLVarying &texIdx, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 47 | const char* coordName, |
| 48 | const char* colorName) { |
Jim Van Verth | 0711094 | 2018-11-20 14:46:41 -0500 | [diff] [blame] | 49 | // Conditionally load from the indexed texture sampler. |
| 50 | // We treat an interval of values around each int as corresponding to that int |
| 51 | // (basically round to int) to correct for floating point variation in the frag shader |
| 52 | for (int i = numTextureSamplers-1; i > 0; --i) { |
| 53 | args.fFragBuilder->codeAppendf("if (%s > %d - 0.5) { %s = ", texIdx.fsIn(), i, colorName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 54 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 55 | kFloat2_GrSLType); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 56 | args.fFragBuilder->codeAppend("; } else "); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 57 | } |
| 58 | args.fFragBuilder->codeAppendf("{ %s = ", colorName); |
Jim Van Verth | 0711094 | 2018-11-20 14:46:41 -0500 | [diff] [blame] | 59 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 60 | kFloat2_GrSLType); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 61 | args.fFragBuilder->codeAppend("; }"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #endif |