| 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 |
|
| 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 |
args.fVertBuilder->codeAppendf( |
| 39 |
"int2 coords = int2(%s.x, %s.y);" |
| 40 |
"int texIdx = coords.x >> 13;" |
| 41 |
"float2 unormTexCoords = float2(coords.x & 0x1FFF, coords.y);" |
| 42 |
, inTexCoordsName, inTexCoordsName); |
| 43 |
} |
| 44 |
} else { |
| 45 |
if (numTextureSamplers <= 1) { |
| 46 |
args.fVertBuilder->codeAppendf( |
| 47 |
"float texIdx = 0;" |
| 48 |
"float2 unormTexCoords = float2(%s.x, %s.y);" |
| 49 |
, inTexCoordsName, inTexCoordsName); |
| 50 |
} else { |
| 51 |
args.fVertBuilder->codeAppendf( |
| 52 |
"float2 coord = float2(%s.x, %s.y);" |
| 53 |
"float texIdx = floor(coord.x * exp2(-13));" |
| 54 |
"float2 unormTexCoords = float2(coord.x - texIdx * exp2(13), coord.y);" |
| 55 |
, inTexCoordsName, inTexCoordsName); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Multiply by 1/atlasDimensions to get normalized texture coordinates |
| 60 |
uv->reset(SkSLType::kFloat2); |
| 61 |
args.fVaryingHandler->addVarying("TextureCoords", uv); |
| 62 |
args.fVertBuilder->codeAppendf( |
| 63 |
"%s = unormTexCoords * %s;", uv->vsOut(), atlasDimensionsInvName); |
| 64 |
|
| 65 |
// On ANGLE there is a significant cost to using an int varying. We don't know of any case where |
| 66 |
// it is worse to use a float so for now we always do. |
| 67 |
texIdx->reset(SkSLType::kFloat); |
| 68 |
// If we computed the local var "texIdx" as an int we will need to cast it to float |
| 69 |
const char* cast = args.fShaderCaps->fIntegerSupport ? "float" : ""; |
| 70 |
args.fVaryingHandler->addVarying("TexIndex", texIdx, Interpolation::kCanBeFlat); |
| 71 |
args.fVertBuilder->codeAppendf("%s = %s(texIdx);", texIdx->vsOut(), cast); |
| 72 |
|
| 73 |
if (st) { |
| 74 |
st->reset(SkSLType::kFloat2); |
| 75 |
args.fVaryingHandler->addVarying("IntTextureCoords", st); |
| 76 |
args.fVertBuilder->codeAppendf("%s = unormTexCoords;", st->vsOut()); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static inline void append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs& args, |
| 81 |
int numTextureSamplers, |
| 82 |
const GrGLSLVarying& texIdx, |
| 83 |
const char* coordName, |
| 84 |
const char* colorName) { |
| 85 |
SkASSERT(numTextureSamplers > 0); |
| 86 |
// This shouldn't happen, but will avoid a crash if it does |
| 87 |
if (numTextureSamplers <= 0) { |
| 88 |
args.fFragBuilder->codeAppendf("%s = float4(1);", colorName); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// conditionally load from the indexed texture sampler |
| 93 |
for (int i = 0; i < numTextureSamplers-1; ++i) { |
| 94 |
args.fFragBuilder->codeAppendf("if (%s == %d) { %s = ", texIdx.fsIn(), i, colorName); |
| 95 |
args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], |
| 96 |
coordName); |
| 97 |
args.fFragBuilder->codeAppend("; } else "); |
| 98 |
} |
| 99 |
args.fFragBuilder->codeAppendf("{ %s = ", colorName); |
| 100 |
args.fFragBuilder->appendTextureLookup(args.fTexSamplers[numTextureSamplers - 1], |
| 101 |
coordName); |
| 102 |
args.fFragBuilder->codeAppend("; }"); |
| 103 |
} |
| 104 |
|
| 105 |
// Special lookup function for sdf lcd -- avoids duplicating conditional logic three times |
| 106 |
static inline void append_multitexture_lookup_lcd(GrGeometryProcessor::ProgramImpl::EmitArgs& args, |
| 107 |
int numTextureSamplers, |
| 108 |
const GrGLSLVarying& texIdx, |
| 109 |
const char* coordName, |
| 110 |
const char* offsetName, |
| 111 |
const char* distanceName) { |
| 112 |
SkASSERT(numTextureSamplers > 0); |
| 113 |
// This shouldn't happen, but will avoid a crash if it does |
| 114 |
if (numTextureSamplers <= 0) { |
| 115 |
args.fFragBuilder->codeAppendf("%s = half3(1);", distanceName); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
// conditionally load from the indexed texture sampler |
| 120 |
for (int i = 0; i < numTextureSamplers; ++i) { |
| 121 |
args.fFragBuilder->codeAppendf("if (%s == %d) {", texIdx.fsIn(), i); |
| 122 |
|
| 123 |
// green is distance to uv center |
| 124 |
args.fFragBuilder->codeAppendf("%s.y = ", distanceName); |
| 125 |
args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], coordName); |
| 126 |
args.fFragBuilder->codeAppend(".r;"); |
| 127 |
|
| 128 |
// red is distance to left offset |
| 129 |
args.fFragBuilder->codeAppendf("half2 uv_adjusted = half2(%s) - %s;", |
| 130 |
coordName, offsetName); |
| 131 |
args.fFragBuilder->codeAppendf("%s.x = ", distanceName); |
| 132 |
args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted"); |
| 133 |
args.fFragBuilder->codeAppend(".r;"); |
| 134 |
|
| 135 |
// blue is distance to right offset |
| 136 |
args.fFragBuilder->codeAppendf("uv_adjusted = half2(%s) + %s;", coordName, offsetName); |
| 137 |
args.fFragBuilder->codeAppendf("%s.z = ", distanceName); |
| 138 |
args.fFragBuilder->appendTextureLookup(args.fTexSamplers[i], "uv_adjusted"); |
| 139 |
args.fFragBuilder->codeAppend(".r;"); |
| 140 |
|
| 141 |
if (i < numTextureSamplers-1) { |
| 142 |
args.fFragBuilder->codeAppend("} else "); |
| 143 |
} else { |
| 144 |
args.fFragBuilder->codeAppend("}"); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
#endif |