| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 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 |
#include "src/gpu/graphite/render/VerticesRenderStep.h" |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkVertices.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkPoint_impl.h" |
| 15 |
#include "include/private/base/SkSpan_impl.h" |
| 16 |
#include "include/private/base/SkTo.h" |
| 17 |
#include "src/base/SkEnumBitMask.h" |
| 18 |
#include "src/core/SkSLTypeShared.h" |
| 19 |
#include "src/core/SkVertState.h" |
| 20 |
#include "src/core/SkVerticesPriv.h" |
| 21 |
#include "src/gpu/BufferWriter.h" |
| 22 |
#include "src/gpu/graphite/Attribute.h" |
| 23 |
#include "src/gpu/graphite/DrawOrder.h" |
| 24 |
#include "src/gpu/graphite/DrawParams.h" |
| 25 |
#include "src/gpu/graphite/DrawTypes.h" |
| 26 |
#include "src/gpu/graphite/DrawWriter.h" |
| 27 |
#include "src/gpu/graphite/PipelineData.h" |
| 28 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 29 |
#include "src/gpu/graphite/geom/Transform.h" |
| 30 |
#include "src/gpu/graphite/render/CommonDepthStencilSettings.h" |
| 31 |
|
| 32 |
#include <cstdint> |
| 33 |
|
| 34 |
namespace skgpu::graphite { |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
static constexpr Attribute kPositionAttr = |
| 39 |
{"position", VertexAttribType::kFloat2, SkSLType::kFloat2}; |
| 40 |
static constexpr Attribute kTexCoordAttr = |
| 41 |
{"texCoords", VertexAttribType::kFloat2, SkSLType::kFloat2}; |
| 42 |
static constexpr Attribute kColorAttr = |
| 43 |
{"vertColor", VertexAttribType::kUByte4_norm, SkSLType::kHalf4}; |
| 44 |
static constexpr Attribute kSsboIndexAttr = |
| 45 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}; |
| 46 |
|
| 47 |
static constexpr Attribute kAttributePositionOnly[] = |
| 48 |
{kPositionAttr, kSsboIndexAttr}; |
| 49 |
static constexpr Attribute kAttributeColor[] = |
| 50 |
{kPositionAttr, kColorAttr, kSsboIndexAttr}; |
| 51 |
static constexpr Attribute kAttributeTexCoords[] = |
| 52 |
{kPositionAttr, kTexCoordAttr, kSsboIndexAttr}; |
| 53 |
static constexpr Attribute kAttributeColorAndTexCoords[] = |
| 54 |
{kPositionAttr, kColorAttr, kTexCoordAttr, kSsboIndexAttr}; |
| 55 |
|
| 56 |
static constexpr SkSpan<const Attribute> kAttributes[4] = { |
| 57 |
kAttributePositionOnly, |
| 58 |
kAttributeColor, |
| 59 |
kAttributeTexCoords, |
| 60 |
kAttributeColorAndTexCoords, |
| 61 |
}; |
| 62 |
|
| 63 |
static constexpr Varying kVaryingColor[] = |
| 64 |
{{"color", SkSLType::kHalf4}}; |
| 65 |
|
| 66 |
static constexpr SkSpan<const Varying> kVaryings[2] = { |
| 67 |
/*none*/ {}, |
| 68 |
/*color*/ kVaryingColor |
| 69 |
}; |
| 70 |
|
| 71 |
RenderStep::RenderStepID variant_id(PrimitiveType type, bool hasColor, bool hasTexCoords) { |
| 72 |
if (type == PrimitiveType::kTriangles) { |
| 73 |
if (hasColor) { |
| 74 |
if (hasTexCoords) { |
| 75 |
return RenderStep::RenderStepID::kVertices_TrisColorTexCoords; |
| 76 |
} else { |
| 77 |
return RenderStep::RenderStepID::kVertices_TrisColor; |
| 78 |
} |
| 79 |
} else { |
| 80 |
if (hasTexCoords) { |
| 81 |
return RenderStep::RenderStepID::kVertices_TrisTexCoords; |
| 82 |
} else { |
| 83 |
return RenderStep::RenderStepID::kVertices_Tris; |
| 84 |
} |
| 85 |
} |
| 86 |
} else { |
| 87 |
SkASSERT(type == PrimitiveType::kTriangleStrip); |
| 88 |
|
| 89 |
if (hasColor) { |
| 90 |
if (hasTexCoords) { |
| 91 |
return RenderStep::RenderStepID::kVertices_TristripsColorTexCoords; |
| 92 |
} else { |
| 93 |
return RenderStep::RenderStepID::kVertices_TristripsColor; |
| 94 |
} |
| 95 |
} else { |
| 96 |
if (hasTexCoords) { |
| 97 |
return RenderStep::RenderStepID::kVertices_TristripsTexCoords; |
| 98 |
} else { |
| 99 |
return RenderStep::RenderStepID::kVertices_Tristrips; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} // namespace |
| 106 |
|
| 107 |
VerticesRenderStep::VerticesRenderStep(Layout layout, PrimitiveType type, bool hasColor, |
| 108 |
bool hasTexCoords) |
| 109 |
: RenderStep(layout, |
| 110 |
variant_id(type, hasColor, hasTexCoords), |
| 111 |
(hasColor ? Flags::kEmitsPrimitiveColor : Flags::kNone) | |
| 112 |
Flags::kPerformsShading | Flags::kAppendVertices, |
| 113 |
/*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}, |
| 114 |
{"depth", SkSLType::kFloat}}, |
| 115 |
type, |
| 116 |
kDirectDepthLEqualPass, |
| 117 |
/*staticAttrs=*/ {}, |
| 118 |
/*appendAttrs=*/kAttributes[2*hasTexCoords + hasColor], |
| 119 |
/*varyings=*/ kVaryings[hasColor]) |
| 120 |
, fHasColor(hasColor) |
| 121 |
, fHasTexCoords(hasTexCoords) {} |
| 122 |
|
| 123 |
VerticesRenderStep::~VerticesRenderStep() {} |
| 124 |
|
| 125 |
std::string VerticesRenderStep::vertexSkSL() const { |
| 126 |
if (fHasColor && fHasTexCoords) { |
| 127 |
return |
| 128 |
"color = half4(vertColor.bgr * vertColor.a, vertColor.a);\n" |
| 129 |
"float4 devPosition = localToDevice * float4(position, 0.0, 1.0);\n" |
| 130 |
"devPosition.z = depth;\n" |
| 131 |
"stepLocalCoords = texCoords;\n"; |
| 132 |
} else if (fHasTexCoords) { |
| 133 |
return |
| 134 |
"float4 devPosition = localToDevice * float4(position, 0.0, 1.0);\n" |
| 135 |
"devPosition.z = depth;\n" |
| 136 |
"stepLocalCoords = texCoords;\n"; |
| 137 |
} else if (fHasColor) { |
| 138 |
return |
| 139 |
"color = half4(vertColor.bgr * vertColor.a, vertColor.a);\n" |
| 140 |
"float4 devPosition = localToDevice * float4(position, 0.0, 1.0);\n" |
| 141 |
"devPosition.z = depth;\n" |
| 142 |
"stepLocalCoords = position;\n"; |
| 143 |
} else { |
| 144 |
return |
| 145 |
"float4 devPosition = localToDevice * float4(position, 0.0, 1.0);\n" |
| 146 |
"devPosition.z = depth;\n" |
| 147 |
"stepLocalCoords = position;\n"; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
const char* VerticesRenderStep::fragmentColorSkSL() const { |
| 152 |
if (fHasColor) { |
| 153 |
return "primitiveColor = color;\n"; |
| 154 |
} else { |
| 155 |
return ""; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void VerticesRenderStep::writeVertices(DrawWriter* writer, |
| 160 |
const DrawParams& params, |
| 161 |
uint32_t ssboIndex) const { |
| 162 |
SkVerticesPriv info(params.geometry().vertices()->priv()); |
| 163 |
const int vertexCount = info.vertexCount(); |
| 164 |
const int indexCount = info.indexCount(); |
| 165 |
const SkPoint* positions = info.positions(); |
| 166 |
const uint16_t* indices = info.indices(); |
| 167 |
const SkColor* colors = info.colors(); |
| 168 |
const SkPoint* texCoords = info.texCoords(); |
| 169 |
|
| 170 |
// This should always be the case if the Renderer was chosen appropriately, but the vertex |
| 171 |
// writing loop is set up in such a way that if the shader expects color or tex coords and they |
| 172 |
// are missing, it will just read 0s, so release builds are safe. |
| 173 |
SkASSERT(fHasColor == SkToBool(colors)); |
| 174 |
SkASSERT(fHasTexCoords == SkToBool(texCoords)); |
| 175 |
|
| 176 |
// TODO: We could access the writer's DrawBufferManager and upload the SkVertices index buffer |
| 177 |
// but that would require we manually manage the VertexWriter for interleaving the position, |
| 178 |
// color, and tex coord arrays together. This wouldn't be so bad if we let ::Vertices() take |
| 179 |
// a CPU index buffer that indexes into the accumulated vertex data (and handles offsetting for |
| 180 |
// merged drawIndexed calls), or if we could bind multiple attribute sources and copy the |
| 181 |
// position/color/texCoord data separately in bulk w/o using an Appender. |
| 182 |
DrawWriter::Vertices verts{*writer}; |
| 183 |
verts.reserve(indices ? indexCount : vertexCount); |
| 184 |
|
| 185 |
VertState state(vertexCount, indices, indexCount); |
| 186 |
VertState::Proc vertProc = state.chooseProc(info.mode()); |
| 187 |
while (vertProc(&state)) { |
| 188 |
verts.append(3) << positions[state.f0] |
| 189 |
<< VertexWriter::If(fHasColor, colors ? colors[state.f0] |
| 190 |
: SK_ColorTRANSPARENT) |
| 191 |
<< VertexWriter::If(fHasTexCoords, texCoords ? texCoords[state.f0] |
| 192 |
: SkPoint{0.f, 0.f}) |
| 193 |
<< ssboIndex |
| 194 |
<< positions[state.f1] |
| 195 |
<< VertexWriter::If(fHasColor, colors ? colors[state.f1] |
| 196 |
: SK_ColorTRANSPARENT) |
| 197 |
<< VertexWriter::If(fHasTexCoords, texCoords ? texCoords[state.f1] |
| 198 |
: SkPoint{0.f, 0.f}) |
| 199 |
<< ssboIndex |
| 200 |
<< positions[state.f2] |
| 201 |
<< VertexWriter::If(fHasColor, colors ? colors[state.f2] |
| 202 |
: SK_ColorTRANSPARENT) |
| 203 |
<< VertexWriter::If(fHasTexCoords, texCoords ? texCoords[state.f2] |
| 204 |
: SkPoint{0.f, 0.f}) |
| 205 |
<< ssboIndex; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void VerticesRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 210 |
PipelineDataGatherer* gatherer) const { |
| 211 |
// Vertices are transformed on the GPU. Store PaintDepth as a uniform to avoid copying the |
| 212 |
// same depth for each vertex. |
| 213 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 214 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 215 |
gatherer->write(params.transform().matrix()); |
| 216 |
gatherer->write(params.order().depthAsFloat()); |
| 217 |
} |
| 218 |
|
| 219 |
} // namespace skgpu::graphite |