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 | 1694a86 | 2018-12-17 19:48:42 +0000 | [diff] [blame^] | 23 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 24 | |
| 25 | // This extracts the texture index and texel coordinates from the same variable |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 26 | // 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 Verth | 1694a86 | 2018-12-17 19:48:42 +0000 | [diff] [blame^] | 28 | 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 Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 40 | |
| 41 | // Multiply by 1/atlasSize to get normalized texture coordinates |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 42 | args.fVaryingHandler->addVarying("TextureCoords", uv); |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 43 | args.fVertBuilder->codeAppendf("%s = unormTexCoords * %s;", uv->vsOut(), atlasSizeInvName); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 44 | |
Jim Van Verth | 1694a86 | 2018-12-17 19:48:42 +0000 | [diff] [blame^] | 45 | args.fVaryingHandler->addVarying("TexIndex", texIdx, args.fShaderCaps->integerSupport() |
| 46 | ? Interpolation::kMustBeFlat |
| 47 | : Interpolation::kCanBeFlat); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 48 | args.fVertBuilder->codeAppendf("%s = texIdx;", texIdx->vsOut()); |
| 49 | |
| 50 | if (st) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 51 | args.fVaryingHandler->addVarying("IntTextureCoords", st); |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 52 | args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut()); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | static void append_multitexture_lookup(GrGLSLPrimitiveProcessor::EmitArgs& args, |
| 57 | int numTextureSamplers, |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 58 | const GrGLSLVarying &texIdx, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 59 | const char* coordName, |
| 60 | const char* colorName) { |
Jim Van Verth | 1694a86 | 2018-12-17 19:48:42 +0000 | [diff] [blame^] | 61 | // 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 Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 64 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 65 | kFloat2_GrSLType); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 66 | args.fFragBuilder->codeAppend("; } else "); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 67 | } |
| 68 | args.fFragBuilder->codeAppendf("{ %s = ", colorName); |
Jim Van Verth | 1694a86 | 2018-12-17 19:48:42 +0000 | [diff] [blame^] | 69 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers-1], coordName, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 70 | kFloat2_GrSLType); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 71 | args.fFragBuilder->codeAppend("; }"); |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | #endif |