• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/gpu/GrDrawOpAtlas.h"
12 #include "src/gpu/GrShaderCaps.h"
13 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
15 #include "src/gpu/glsl/GrGLSLVarying.h"
16 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
17 
append_index_uv_varyings(GrGLSLGeometryProcessor::EmitArgs & args,int numTextureSamplers,const char * inTexCoordsName,const char * atlasDimensionsInvName,GrGLSLVarying * uv,GrGLSLVarying * texIdx,GrGLSLVarying * st)18 static void append_index_uv_varyings(GrGLSLGeometryProcessor::EmitArgs& args,
19                                      int numTextureSamplers,
20                                      const char* inTexCoordsName,
21                                      const char* atlasDimensionsInvName,
22                                      GrGLSLVarying* uv,
23                                      GrGLSLVarying* texIdx,
24                                      GrGLSLVarying* st) {
25     using Interpolation = GrGLSLVaryingHandler::Interpolation;
26     // This extracts the texture index and texel coordinates from the same variable
27     // Packing structure: texel coordinates have the 2-bit texture page encoded in bits 13 & 14 of
28     // the x coordinate. It would be nice to use bits 14 and 15, but iphone6 has problem with those
29     // bits when in gles. Iphone6 works fine with bits 14 and 15 in metal.
30     if (args.fShaderCaps->integerSupport()) {
31         if (numTextureSamplers <= 1) {
32             args.fVertBuilder->codeAppendf(R"code(
33                 int texIdx = 0;
34                 float2 unormTexCoords = float2(%s.x, %s.y);
35            )code", inTexCoordsName, inTexCoordsName);
36         } else {
37             args.fVertBuilder->codeAppendf(R"code(
38                 int2 coords = int2(%s.x, %s.y);
39                 int texIdx = coords.x >> 13;
40                 float2 unormTexCoords = float2(coords.x & 0x1FFF, coords.y);
41             )code", inTexCoordsName, inTexCoordsName);
42         }
43     } else {
44         if (numTextureSamplers <= 1) {
45             args.fVertBuilder->codeAppendf(R"code(
46                 float texIdx = 0;
47                 float2 unormTexCoords = float2(%s.x, %s.y);
48             )code", inTexCoordsName, inTexCoordsName);
49         } else {
50             args.fVertBuilder->codeAppendf(R"code(
51                 float2 coord = float2(%s.x, %s.y);
52                 float texIdx = floor(coord.x * exp2(-13));
53                 float2 unormTexCoords = float2(coord.x - texIdx * exp2(13), coord.y);
54             )code", inTexCoordsName, inTexCoordsName);
55         }
56     }
57 
58     // Multiply by 1/atlasDimensions to get normalized texture coordinates
59     uv->reset(kFloat2_GrSLType);
60     args.fVaryingHandler->addVarying("TextureCoords", uv);
61     args.fVertBuilder->codeAppendf(
62             "%s = unormTexCoords * %s;", uv->vsOut(), atlasDimensionsInvName);
63 
64     // On ANGLE there is a significant cost to using an int varying. We don't know of any case where
65     // it is worse to use a float so for now we always do.
66     texIdx->reset(kFloat_GrSLType);
67     // If we computed the local var "texIdx" as an int we will need to cast it to float
68     const char* cast = args.fShaderCaps->integerSupport() ? "float" : "";
69     args.fVaryingHandler->addVarying("TexIndex", texIdx, Interpolation::kCanBeFlat);
70     args.fVertBuilder->codeAppendf("%s = %s(texIdx);", texIdx->vsOut(), cast);
71 
72     if (st) {
73         st->reset(kFloat2_GrSLType);
74         args.fVaryingHandler->addVarying("IntTextureCoords", st);
75         args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut());
76     }
77 }
78 
append_multitexture_lookup(GrGLSLGeometryProcessor::EmitArgs & args,int numTextureSamplers,const GrGLSLVarying & texIdx,const char * coordName,const char * colorName)79 static void append_multitexture_lookup(GrGLSLGeometryProcessor::EmitArgs& args,
80                                        int numTextureSamplers,
81                                        const GrGLSLVarying &texIdx,
82                                        const char* coordName,
83                                        const char* colorName) {
84     SkASSERT(numTextureSamplers > 0);
85     // This shouldn't happen, but will avoid a crash if it does
86     if (numTextureSamplers <= 0) {
87         args.fFragBuilder->codeAppendf("%s = float4(1, 1, 1, 1);", colorName);
88         return;
89     }
90 
91     // conditionally load from the indexed texture sampler
92     for (int i = 0; i < numTextureSamplers-1; ++i) {
93         args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName);
94         args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName);
95         args.fFragBuilder->codeAppend("; } else ");
96     }
97     args.fFragBuilder->codeAppendf("{ %s = ", colorName);
98     args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers - 1], coordName);
99     args.fFragBuilder->codeAppend("; }");
100 }
101 
102 #endif
103