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