Flutter Engine
The Flutter Engine
Functions
GrAtlasedShaderHelpers.h File Reference
#include "src/gpu/ganesh/GrDrawOpAtlas.h"
#include "src/gpu/ganesh/GrShaderCaps.h"
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
#include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"

Go to the source code of this file.

Functions

static void append_index_uv_varyings (GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const char *inTexCoordsName, const char *atlasDimensionsInvName, GrGLSLVarying *uv, GrGLSLVarying *texIdx, GrGLSLVarying *st)
 
static void append_multitexture_lookup (GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const GrGLSLVarying &texIdx, const char *coordName, const char *colorName)
 
static void append_multitexture_lookup_lcd (GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const GrGLSLVarying &texIdx, const char *coordName, const char *offsetName, const char *distanceName)
 

Function Documentation

◆ append_index_uv_varyings()

static void append_index_uv_varyings ( GrGeometryProcessor::ProgramImpl::EmitArgs args,
int  numTextureSamplers,
const char *  inTexCoordsName,
const char *  atlasDimensionsInvName,
GrGLSLVarying uv,
GrGLSLVarying texIdx,
GrGLSLVarying st 
)
inlinestatic

Definition at line 17 of file GrAtlasedShaderHelpers.h.

23 {
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
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) {
73 args.fVaryingHandler->addVarying("IntTextureCoords", st);
74 args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut());
75 }
76}
SI D cast(const S &v)
const char * vsOut() const
Definition: GrGLSLVarying.h:67
void reset(SkSLType type, Scope scope=Scope::kVertToFrag)
Definition: GrGLSLVarying.h:54
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

◆ append_multitexture_lookup()

static void append_multitexture_lookup ( GrGeometryProcessor::ProgramImpl::EmitArgs args,
int  numTextureSamplers,
const GrGLSLVarying texIdx,
const char *  coordName,
const char *  colorName 
)
inlinestatic

Definition at line 78 of file GrAtlasedShaderHelpers.h.

82 {
83 SkASSERT(numTextureSamplers > 0);
84 // This shouldn't happen, but will avoid a crash if it does
85 if (numTextureSamplers <= 0) {
86 args.fFragBuilder->codeAppendf("%s = float4(1);", colorName);
87 return;
88 }
89
90 // conditionally load from the indexed texture sampler
91 for (int i = 0; i < numTextureSamplers-1; ++i) {
92 args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName);
93 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i],
94 coordName);
95 args.fFragBuilder->codeAppend("; } else ");
96 }
97 args.fFragBuilder->codeAppendf("{ %s = ", colorName);
98 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers - 1],
99 coordName);
100 args.fFragBuilder->codeAppend("; }");
101}
#define SkASSERT(cond)
Definition: SkAssert.h:116
const char * fsIn() const
Definition: GrGLSLVarying.h:68

◆ append_multitexture_lookup_lcd()

static void append_multitexture_lookup_lcd ( GrGeometryProcessor::ProgramImpl::EmitArgs args,
int  numTextureSamplers,
const GrGLSLVarying texIdx,
const char *  coordName,
const char *  offsetName,
const char *  distanceName 
)
inlinestatic

Definition at line 104 of file GrAtlasedShaderHelpers.h.

109 {
110 SkASSERT(numTextureSamplers > 0);
111 // This shouldn't happen, but will avoid a crash if it does
112 if (numTextureSamplers <= 0) {
113 args.fFragBuilder->codeAppendf("%s = half3(1);", distanceName);
114 return;
115 }
116
117 // conditionally load from the indexed texture sampler
118 for (int i = 0; i < numTextureSamplers; ++i) {
119 args.fFragBuilder->codeAppendf("if (%s == %d) {", texIdx.fsIn(), i);
120
121 // green is distance to uv center
122 args.fFragBuilder->codeAppendf("%s.y = ", distanceName);
123 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName);
124 args.fFragBuilder->codeAppend(".r;");
125
126 // red is distance to left offset
127 args.fFragBuilder->codeAppendf("half2 uv_adjusted = half2(%s) - %s;",
128 coordName, offsetName);
129 args.fFragBuilder->codeAppendf("%s.x = ", distanceName);
130 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted");
131 args.fFragBuilder->codeAppend(".r;");
132
133 // blue is distance to right offset
134 args.fFragBuilder->codeAppendf("uv_adjusted = half2(%s) + %s;", coordName, offsetName);
135 args.fFragBuilder->codeAppendf("%s.z = ", distanceName);
136 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted");
137 args.fFragBuilder->codeAppend(".r;");
138
139 if (i < numTextureSamplers-1) {
140 args.fFragBuilder->codeAppend("} else ");
141 } else {
142 args.fFragBuilder->codeAppend("}");
143 }
144 }
145}