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/GrGLSLVarying.h"
15 #include "src/gpu/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 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->integerSupport()) {
30 if (numTextureSamplers <= 1) {
31 args.fVertBuilder->codeAppendf(R"code(
32 int texIdx = 0;
33 float2 unormTexCoords = float2(%s.x, %s.y);
34 )code", inTexCoordsName, inTexCoordsName);
35 } else {
36 #ifdef SK_ENABLE_SMALL_PAGE
37 args.fVertBuilder->codeAppendf(R"code(
38 int2 coords = int2(%s.x, %s.y);
39 int texIdx = ((coords.x >> 12) & 0xc) + ((coords.y >> 14) & 0x3);
40 float2 unormTexCoords = float2(coords.x & 0x3FFF, coords.y & 0x3FFF);
41 )code", inTexCoordsName, inTexCoordsName);
42 #else
43 args.fVertBuilder->codeAppendf(R"code(
44 int2 coords = int2(%s.x, %s.y);
45 int texIdx = coords.x >> 13;
46 float2 unormTexCoords = float2(coords.x & 0x1FFF, coords.y);
47 )code", inTexCoordsName, inTexCoordsName);
48 #endif
49 }
50 } else {
51 if (numTextureSamplers <= 1) {
52 args.fVertBuilder->codeAppendf(R"code(
53 float texIdx = 0;
54 float2 unormTexCoords = float2(%s.x, %s.y);
55 )code", inTexCoordsName, inTexCoordsName);
56 } else {
57 #ifdef SK_ENABLE_SMALL_PAGE
58 args.fVertBuilder->codeAppendf(R"code(
59 float2 coord = float2(%s.x, %s.y);
60 float diff0 = floor(coord.x * exp2(-14));
61 float diff1 = floor(coord.y * exp2(-14));
62 float texIdx = 4 * diff0 + diff1;
63 float2 unormTexCoords = float2(coord.x - diff0, coord.y - diff1);
64 )code", inTexCoordsName, inTexCoordsName);
65 #else
66 args.fVertBuilder->codeAppendf(R"code(
67 float2 coord = float2(%s.x, %s.y);
68 float texIdx = floor(coord.x * exp2(-13));
69 float2 unormTexCoords = float2(coord.x - texIdx * exp2(13), coord.y);
70 )code", inTexCoordsName, inTexCoordsName);
71 #endif
72 }
73 }
74
75 // Multiply by 1/atlasDimensions to get normalized texture coordinates
76 uv->reset(kFloat2_GrSLType);
77 args.fVaryingHandler->addVarying("TextureCoords", uv);
78 args.fVertBuilder->codeAppendf(
79 "%s = unormTexCoords * %s;", uv->vsOut(), atlasDimensionsInvName);
80
81 // On ANGLE there is a significant cost to using an int varying. We don't know of any case where
82 // it is worse to use a float so for now we always do.
83 texIdx->reset(kFloat_GrSLType);
84 // If we computed the local var "texIdx" as an int we will need to cast it to float
85 const char* cast = args.fShaderCaps->integerSupport() ? "float" : "";
86 args.fVaryingHandler->addVarying("TexIndex", texIdx, Interpolation::kCanBeFlat);
87 args.fVertBuilder->codeAppendf("%s = %s(texIdx);", texIdx->vsOut(), cast);
88
89 if (st) {
90 st->reset(kFloat2_GrSLType);
91 args.fVaryingHandler->addVarying("IntTextureCoords", st);
92 args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut());
93 }
94 }
95
append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs & args,int numTextureSamplers,const GrGLSLVarying & texIdx,const char * coordName,const char * colorName)96 static void append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs& args,
97 int numTextureSamplers,
98 const GrGLSLVarying& texIdx,
99 const char* coordName,
100 const char* colorName) {
101 SkASSERT(numTextureSamplers > 0);
102 // This shouldn't happen, but will avoid a crash if it does
103 if (numTextureSamplers <= 0) {
104 args.fFragBuilder->codeAppendf("%s = float4(1, 1, 1, 1);", colorName);
105 return;
106 }
107
108 // conditionally load from the indexed texture sampler
109 for (int i = 0; i < numTextureSamplers-1; ++i) {
110 args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName);
111 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName);
112 args.fFragBuilder->codeAppend("; } else ");
113 }
114 args.fFragBuilder->codeAppendf("{ %s = ", colorName);
115 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers - 1], coordName);
116 args.fFragBuilder->codeAppend("; }");
117 }
118
119 #endif
120