Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Static Public Attributes | List of all members
skgpu::ganesh::AtlasInstancedHelper Class Reference

#include <AtlasInstancedHelper.h>

Classes

struct  Instance
 

Public Types

enum class  ShaderFlags { kNone = 0 , kInvertCoverage = 1 << 0 , kCheckBounds = 1 << 1 }
 

Public Member Functions

 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS (ShaderFlags)
 
 AtlasInstancedHelper (GrSurfaceProxyView atlasView, ShaderFlags shaderFlags)
 
GrSurfaceProxyproxy () const
 
const skgpu::SwizzleatlasSwizzle () const
 
bool isCompatible (const AtlasInstancedHelper &helper)
 
void getKeyBits (KeyBuilder *b) const
 
void appendInstanceAttribs (skia_private::TArray< GrGeometryProcessor::Attribute > *instanceAttribs) const
 
void writeInstanceData (VertexWriter *instanceWriter, const Instance *) const
 
void injectShaderCode (const GrGeometryProcessor::ProgramImpl::EmitArgs &, const GrShaderVar &devCoord, GrGLSLUniformHandler::UniformHandle *atlasAdjustUniformHandle) const
 
void setUniformData (const GrGLSLProgramDataManager &, const GrGLSLUniformHandler::UniformHandle &atlasAdjustUniformHandle) const
 

Static Public Attributes

static constexpr int kNumShaderFlags = 2
 

Detailed Description

Definition at line 20 of file AtlasInstancedHelper.h.

Member Enumeration Documentation

◆ ShaderFlags

Enumerator
kNone 
kInvertCoverage 
kCheckBounds 

Definition at line 22 of file AtlasInstancedHelper.h.

22 {
23 kNone = 0,
24 kInvertCoverage = 1 << 0,
25 kCheckBounds = 1 << 1
26 };
@ kNone
Definition: layer.h:53

Constructor & Destructor Documentation

◆ AtlasInstancedHelper()

skgpu::ganesh::AtlasInstancedHelper::AtlasInstancedHelper ( GrSurfaceProxyView  atlasView,
ShaderFlags  shaderFlags 
)
inline

Definition at line 32 of file AtlasInstancedHelper.h.

33 : fAtlasProxy(atlasView.detachProxy())
34 , fAtlasSwizzle(atlasView.swizzle())
35 , fShaderFlags(shaderFlags) {
36 // Bottom left origin is not supported.
38 }
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
#define SkASSERT(cond)
Definition: SkAssert.h:116
skgpu::Swizzle swizzle() const
sk_sp< GrSurfaceProxy > detachProxy()
GrSurfaceOrigin origin() const

Member Function Documentation

◆ appendInstanceAttribs()

void skgpu::ganesh::AtlasInstancedHelper::appendInstanceAttribs ( skia_private::TArray< GrGeometryProcessor::Attribute > *  instanceAttribs) const

Definition at line 24 of file AtlasInstancedHelper.cpp.

25 {
26 instanceAttribs->emplace_back("locations", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
27 if (fShaderFlags & ShaderFlags::kCheckBounds) {
28 instanceAttribs->emplace_back("sizeInAtlas", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
29 }
30}
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kFloat4_GrVertexAttribType
Definition: GrTypesPriv.h:316
T & emplace_back(Args &&... args)
Definition: SkTArray.h:248

◆ atlasSwizzle()

const skgpu::Swizzle & skgpu::ganesh::AtlasInstancedHelper::atlasSwizzle ( ) const
inline

Definition at line 41 of file AtlasInstancedHelper.h.

41{ return fAtlasSwizzle; }

◆ getKeyBits()

void skgpu::ganesh::AtlasInstancedHelper::getKeyBits ( KeyBuilder b) const

Definition at line 20 of file AtlasInstancedHelper.cpp.

20 {
21 b->addBits(kNumShaderFlags, (int)fShaderFlags, "atlasFlags");
22}
static bool b

◆ GR_DECL_BITFIELD_CLASS_OPS_FRIENDS()

skgpu::ganesh::AtlasInstancedHelper::GR_DECL_BITFIELD_CLASS_OPS_FRIENDS ( ShaderFlags  )

◆ injectShaderCode()

void skgpu::ganesh::AtlasInstancedHelper::injectShaderCode ( const GrGeometryProcessor::ProgramImpl::EmitArgs args,
const GrShaderVar devCoord,
GrGLSLUniformHandler::UniformHandle atlasAdjustUniformHandle 
) const

Definition at line 48 of file AtlasInstancedHelper.cpp.

51 {
53 args.fVaryingHandler->addVarying("atlasCoord", &atlasCoord);
54
55 const char* atlasAdjustName;
56 *atlasAdjustUniformHandle = args.fUniformHandler->addUniform(
57 nullptr, kVertex_GrShaderFlag, SkSLType::kFloat2, "atlas_adjust", &atlasAdjustName);
58
59 args.fVertBuilder->codeAppendf(
60 // A negative x coordinate in the atlas indicates that the path is transposed.
61 // We also added 1 since we can't negate zero.
62 "float2 atlasTopLeft = float2(abs(locations.x) - 1, locations.y);"
63 "float2 devTopLeft = locations.zw;"
64 "bool transposed = locations.x < 0;"
65 "float2 atlasCoord = %s - devTopLeft;"
66 "if (transposed) {"
67 "atlasCoord = atlasCoord.yx;"
68 "}"
69 "atlasCoord += atlasTopLeft;"
70 "%s = atlasCoord * %s;", devCoord.c_str(), atlasCoord.vsOut(), atlasAdjustName);
71
72 if (fShaderFlags & ShaderFlags::kCheckBounds) {
74 args.fVaryingHandler->addVarying("atlasbounds", &atlasBounds,
76 args.fVertBuilder->codeAppendf(R"(
77 float4 atlasBounds = atlasTopLeft.xyxy + (transposed ? sizeInAtlas.00yx
78 : sizeInAtlas.00xy);
79 %s = atlasBounds * %s.xyxy;)", atlasBounds.vsOut(), atlasAdjustName);
80
81 args.fFragBuilder->codeAppendf(
82 "half atlasCoverage = 0;"
83 "float2 atlasCoord = %s;"
84 "float4 atlasBounds = %s;"
85 "if (all(greaterThan(atlasCoord, atlasBounds.xy)) &&"
86 "all(lessThan(atlasCoord, atlasBounds.zw))) {"
87 "atlasCoverage = ", atlasCoord.fsIn(), atlasBounds.fsIn());
88 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], "atlasCoord");
89 args.fFragBuilder->codeAppendf(R"(.a;
90 })");
91 } else {
92 args.fFragBuilder->codeAppendf("half atlasCoverage = ");
93 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn());
94 args.fFragBuilder->codeAppendf(".a;");
95 }
96
97 if (fShaderFlags & ShaderFlags::kInvertCoverage) {
98 args.fFragBuilder->codeAppendf("%s *= (1 - atlasCoverage);", args.fOutputCoverage);
99 } else {
100 args.fFragBuilder->codeAppendf("%s *= atlasCoverage;", args.fOutputCoverage);
101 }
102}
@ kVertex_GrShaderFlag
Definition: GrTypesPriv.h:286
const char * c_str() const
Definition: GrShaderVar.h:94
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

◆ isCompatible()

bool skgpu::ganesh::AtlasInstancedHelper::isCompatible ( const AtlasInstancedHelper helper)
inline

Definition at line 44 of file AtlasInstancedHelper.h.

44 {
45 // TODO: We may want to consider two helpers compatible if they only differ in the
46 // kCheckBounds flag -- we can always promote one to checking its bounds.
47 SkASSERT(fAtlasProxy != helper.fAtlasProxy || fAtlasSwizzle == helper.fAtlasSwizzle);
48 return fAtlasProxy == helper.fAtlasProxy && fShaderFlags == helper.fShaderFlags;
49 }

◆ proxy()

GrSurfaceProxy * skgpu::ganesh::AtlasInstancedHelper::proxy ( ) const
inline

Definition at line 40 of file AtlasInstancedHelper.h.

40{ return fAtlasProxy.get(); }
T * get() const
Definition: SkRefCnt.h:303

◆ setUniformData()

void skgpu::ganesh::AtlasInstancedHelper::setUniformData ( const GrGLSLProgramDataManager pdman,
const GrGLSLUniformHandler::UniformHandle atlasAdjustUniformHandle 
) const

Definition at line 104 of file AtlasInstancedHelper.cpp.

106 {
107 SkASSERT(fAtlasProxy->isInstantiated());
108 SkISize dimensions = fAtlasProxy->backingStoreDimensions();
109 pdman.set2f(atlasAdjustUniformHandle, 1.f / dimensions.width(), 1.f / dimensions.height());
110}
virtual void set2f(UniformHandle, float, float) const =0
SkISize backingStoreDimensions() const
bool isInstantiated() const
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37

◆ writeInstanceData()

void skgpu::ganesh::AtlasInstancedHelper::writeInstanceData ( VertexWriter instanceWriter,
const Instance i 
) const

Definition at line 32 of file AtlasInstancedHelper.cpp.

33 {
34 SkASSERT(i->fLocationInAtlas.x() >= 0);
35 SkASSERT(i->fLocationInAtlas.y() >= 0);
36 *instanceWriter <<
37 // A negative x coordinate in the atlas indicates that the path is transposed.
38 // Also add 1 since we can't negate zero.
39 ((float)(i->fTransposedInAtlas ? -i->fLocationInAtlas.x() - 1
40 : i->fLocationInAtlas.x() + 1)) <<
41 (float)i->fLocationInAtlas.y() <<
42 (float)i->fPathDevIBounds.left() <<
43 (float)i->fPathDevIBounds.top() <<
45 SkSize::Make(i->fPathDevIBounds.size()));
46}
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
static Conditional< T > If(bool condition, const T &value)
Definition: BufferWriter.h:153

Member Data Documentation

◆ kNumShaderFlags

constexpr int skgpu::ganesh::AtlasInstancedHelper::kNumShaderFlags = 2
staticconstexpr

Definition at line 30 of file AtlasInstancedHelper.h.


The documentation for this class was generated from the following files: