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 "include/private/base/SkAssert.h"
12 #include "src/core/SkSLTypeShared.h"
13 #include "src/gpu/ganesh/GrGeometryProcessor.h"
14 #include "src/gpu/ganesh/GrShaderCaps.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
17 #include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"
18
append_index_uv_varyings(GrGeometryProcessor::ProgramImpl::EmitArgs & args,int numTextureSamplers,const char * inTexCoordsName,const char * atlasDimensionsInvName,GrGLSLVarying * uv,GrGLSLVarying * texIdx,GrGLSLVarying * st)19 static inline void append_index_uv_varyings(GrGeometryProcessor::ProgramImpl::EmitArgs& args,
20 int numTextureSamplers,
21 const char* inTexCoordsName,
22 const char* atlasDimensionsInvName,
23 GrGLSLVarying* uv,
24 GrGLSLVarying* texIdx,
25 GrGLSLVarying* st) {
26 using Interpolation = GrGLSLVaryingHandler::Interpolation;
27 // This extracts the texture index and texel coordinates from the same variable
28 // Packing structure: texel coordinates have the 2-bit texture page encoded in bits 13 & 14 of
29 // the x coordinate. It would be nice to use bits 14 and 15, but iphone6 has problem with those
30 // bits when in gles. Iphone6 works fine with bits 14 and 15 in metal.
31 if (args.fShaderCaps->fIntegerSupport) {
32 if (numTextureSamplers <= 1) {
33 args.fVertBuilder->codeAppendf(
34 "int texIdx = 0;"
35 "float2 unormTexCoords = float2(%s.x, %s.y);"
36 , inTexCoordsName, inTexCoordsName);
37 } else {
38 #ifdef SK_ENABLE_SMALL_PAGE
39 args.fVertBuilder->codeAppendf(R"code(
40 int2 coords = int2(%s.x, %s.y);
41 int texIdx = ((coords.x >> 12) & 0xc) + ((coords.y >> 14) & 0x3);
42 float2 unormTexCoords = float2(coords.x & 0x3FFF, coords.y & 0x3FFF);
43 )code", inTexCoordsName, inTexCoordsName);
44 #else
45 args.fVertBuilder->codeAppendf(
46 "int2 coords = int2(%s.x, %s.y);"
47 "int texIdx = coords.x >> 13;"
48 "float2 unormTexCoords = float2(coords.x & 0x1FFF, coords.y);"
49 , inTexCoordsName, inTexCoordsName);
50 #endif
51 }
52 } else {
53 if (numTextureSamplers <= 1) {
54 args.fVertBuilder->codeAppendf(
55 "float texIdx = 0;"
56 "float2 unormTexCoords = float2(%s.x, %s.y);"
57 , inTexCoordsName, inTexCoordsName);
58 } else {
59 #ifdef SK_ENABLE_SMALL_PAGE
60 args.fVertBuilder->codeAppendf(R"code(
61 float2 coord = float2(%s.x, %s.y);
62 float diff0 = floor(coord.x * exp2(-14));
63 float diff1 = floor(coord.y * exp2(-14));
64 float texIdx = 4 * diff0 + diff1;
65 float2 unormTexCoords = float2(coord.x - diff0, coord.y - diff1);
66 )code", inTexCoordsName, inTexCoordsName);
67 #else
68 args.fVertBuilder->codeAppendf(
69 "float2 coord = float2(%s.x, %s.y);"
70 "float texIdx = floor(coord.x * exp2(-13));"
71 "float2 unormTexCoords = float2(coord.x - texIdx * exp2(13), coord.y);"
72 , inTexCoordsName, inTexCoordsName);
73 #endif
74 }
75 }
76
77 // Multiply by 1/atlasDimensions to get normalized texture coordinates
78 uv->reset(SkSLType::kFloat2);
79 args.fVaryingHandler->addVarying("TextureCoords", uv);
80 args.fVertBuilder->codeAppendf(
81 "%s = unormTexCoords * %s;", uv->vsOut(), atlasDimensionsInvName);
82
83 // On ANGLE there is a significant cost to using an int varying. We don't know of any case where
84 // it is worse to use a float so for now we always do.
85 texIdx->reset(SkSLType::kFloat);
86 // If we computed the local var "texIdx" as an int we will need to cast it to float
87 const char* cast = args.fShaderCaps->fIntegerSupport ? "float" : "";
88 args.fVaryingHandler->addVarying("TexIndex", texIdx, Interpolation::kCanBeFlat);
89 args.fVertBuilder->codeAppendf("%s = %s(texIdx);", texIdx->vsOut(), cast);
90
91 if (st) {
92 st->reset(SkSLType::kFloat2);
93 args.fVaryingHandler->addVarying("IntTextureCoords", st);
94 args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut());
95 }
96 }
97
append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs & args,int numTextureSamplers,const GrGLSLVarying & texIdx,const char * coordName,const char * colorName)98 static inline void append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs& args,
99 int numTextureSamplers,
100 const GrGLSLVarying& texIdx,
101 const char* coordName,
102 const char* colorName) {
103 SkASSERT(numTextureSamplers > 0);
104 // This shouldn't happen, but will avoid a crash if it does
105 if (numTextureSamplers <= 0) {
106 args.fFragBuilder->codeAppendf("%s = float4(1);", colorName);
107 return;
108 }
109
110 // conditionally load from the indexed texture sampler
111 for (int i = 0; i < numTextureSamplers-1; ++i) {
112 args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName);
113 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i],
114 coordName);
115 args.fFragBuilder->codeAppend("; } else ");
116 }
117 args.fFragBuilder->codeAppendf("{ %s = ", colorName);
118 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers - 1],
119 coordName);
120 args.fFragBuilder->codeAppend("; }");
121 }
122
123 // Special lookup function for sdf lcd -- avoids duplicating conditional logic three times
append_multitexture_lookup_lcd(GrGeometryProcessor::ProgramImpl::EmitArgs & args,int numTextureSamplers,const GrGLSLVarying & texIdx,const char * coordName,const char * offsetName,const char * distanceName)124 static inline void append_multitexture_lookup_lcd(GrGeometryProcessor::ProgramImpl::EmitArgs& args,
125 int numTextureSamplers,
126 const GrGLSLVarying& texIdx,
127 const char* coordName,
128 const char* offsetName,
129 const char* distanceName) {
130 SkASSERT(numTextureSamplers > 0);
131 // This shouldn't happen, but will avoid a crash if it does
132 if (numTextureSamplers <= 0) {
133 args.fFragBuilder->codeAppendf("%s = half3(1);", distanceName);
134 return;
135 }
136
137 // conditionally load from the indexed texture sampler
138 for (int i = 0; i < numTextureSamplers; ++i) {
139 args.fFragBuilder->codeAppendf("if (%s == %d) {", texIdx.fsIn(), i);
140
141 // green is distance to uv center
142 args.fFragBuilder->codeAppendf("%s.y = ", distanceName);
143 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName);
144 args.fFragBuilder->codeAppend(".r;");
145
146 // red is distance to left offset
147 args.fFragBuilder->codeAppendf("half2 uv_adjusted = half2(%s) - %s;",
148 coordName, offsetName);
149 args.fFragBuilder->codeAppendf("%s.x = ", distanceName);
150 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted");
151 args.fFragBuilder->codeAppend(".r;");
152
153 // blue is distance to right offset
154 args.fFragBuilder->codeAppendf("uv_adjusted = half2(%s) + %s;", coordName, offsetName);
155 args.fFragBuilder->codeAppendf("%s.z = ", distanceName);
156 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted");
157 args.fFragBuilder->codeAppend(".r;");
158
159 if (i < numTextureSamplers-1) {
160 args.fFragBuilder->codeAppend("} else ");
161 } else {
162 args.fFragBuilder->codeAppend("}");
163 }
164 }
165 }
166
167 #endif
168