2021-12-21 15:36:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
#ifndef SkMesh_DEFINED
|
|
|
|
#define SkMesh_DEFINED
|
2021-12-21 15:36:58 +00:00
|
|
|
|
2022-04-28 15:04:30 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2022-01-06 16:18:03 +00:00
|
|
|
|
|
|
|
#ifdef SK_ENABLE_SKSL
|
2022-04-05 16:56:01 +00:00
|
|
|
#include "include/core/SkAlphaType.h"
|
2021-12-21 15:36:58 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSpan.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
|
2022-04-05 16:56:01 +00:00
|
|
|
#include <memory>
|
2021-12-21 15:36:58 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-05-03 20:45:21 +00:00
|
|
|
class GrDirectContext;
|
2022-04-05 16:56:01 +00:00
|
|
|
class SkColorSpace;
|
2022-05-03 20:45:21 +00:00
|
|
|
class SkData;
|
2022-04-05 16:56:01 +00:00
|
|
|
|
2021-12-21 15:36:58 +00:00
|
|
|
namespace SkSL { struct Program; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A specification for custom meshes. Specifies the vertex buffer attributes and stride, the
|
|
|
|
* vertex program that produces a user-defined set of varyings, a fragment program that ingests
|
|
|
|
* the interpolated varyings and produces local coordinates and optionally a color.
|
|
|
|
*
|
|
|
|
* The signature of the vertex program must be:
|
|
|
|
* float2 main(Attributes, out Varyings)
|
|
|
|
* where the return value is a local position that will be transformed by SkCanvas's matrix.
|
|
|
|
*
|
|
|
|
* The signature of the fragment program must be either:
|
2022-01-04 15:47:16 +00:00
|
|
|
* (float2|void) main(Varyings)
|
2021-12-21 15:36:58 +00:00
|
|
|
* or
|
2022-01-04 15:47:16 +00:00
|
|
|
* (float2|void) main(Varyings, out (half4|float4) color)
|
2021-12-21 15:36:58 +00:00
|
|
|
*
|
|
|
|
* where the return value is the local coordinates that will be used to access SkShader. If the
|
2022-01-04 15:47:16 +00:00
|
|
|
* return type is void then the interpolated position from vertex shader return is used as the local
|
|
|
|
* coordinate. If the color variant is used it will be blended with SkShader (or SkPaint color in
|
|
|
|
* absence of a shader) using the SkBlender provided to the SkCanvas draw call.
|
2021-12-21 15:36:58 +00:00
|
|
|
*/
|
2022-05-06 19:17:00 +00:00
|
|
|
class SkMeshSpecification : public SkNVRefCnt<SkMeshSpecification> {
|
2021-12-21 15:36:58 +00:00
|
|
|
public:
|
|
|
|
/** These values are enforced when creating a specification. */
|
|
|
|
static constexpr size_t kMaxStride = 1024;
|
|
|
|
static constexpr size_t kMaxAttributes = 8;
|
|
|
|
static constexpr size_t kStrideAlignment = 4;
|
|
|
|
static constexpr size_t kOffsetAlignment = 4;
|
2022-01-04 15:47:16 +00:00
|
|
|
static constexpr size_t kMaxVaryings = 6;
|
2021-12-21 15:36:58 +00:00
|
|
|
|
|
|
|
struct Attribute {
|
|
|
|
enum class Type : uint32_t { // CPU representation Shader Type
|
|
|
|
kFloat, // float float
|
|
|
|
kFloat2, // two floats float2
|
|
|
|
kFloat3, // three floats float3
|
|
|
|
kFloat4, // four floats float4
|
|
|
|
kUByte4_unorm, // four bytes half4
|
|
|
|
|
|
|
|
kLast = kUByte4_unorm
|
|
|
|
};
|
|
|
|
Type type;
|
|
|
|
size_t offset;
|
|
|
|
SkString name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Varying {
|
|
|
|
enum class Type : uint32_t {
|
|
|
|
kFloat, // "float"
|
|
|
|
kFloat2, // "float2"
|
|
|
|
kFloat3, // "float3"
|
|
|
|
kFloat4, // "float4"
|
|
|
|
kHalf, // "half"
|
|
|
|
kHalf2, // "half2"
|
|
|
|
kHalf3, // "half3"
|
|
|
|
kHalf4, // "half4"
|
|
|
|
|
|
|
|
kLast = kHalf4
|
|
|
|
};
|
|
|
|
Type type;
|
|
|
|
SkString name;
|
|
|
|
};
|
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
~SkMeshSpecification();
|
2021-12-21 15:36:58 +00:00
|
|
|
|
|
|
|
struct Result {
|
2022-05-06 19:17:00 +00:00
|
|
|
sk_sp<SkMeshSpecification> specification;
|
|
|
|
SkString error;
|
2021-12-21 15:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If successful the return is a specification and an empty error string. Otherwise, it is a
|
|
|
|
* null specification a non-empty error string.
|
|
|
|
*
|
|
|
|
* @param attributes The vertex attributes that will be consumed by 'vs'. Attributes need
|
|
|
|
* not be tightly packed but attribute offsets must be aligned to
|
|
|
|
* kOffsetAlignment and offset + size may not be greater than
|
|
|
|
* 'vertexStride'. At least one attribute is required.
|
|
|
|
* @param vertexStride The offset between successive attribute values. This must be aligned to
|
|
|
|
* kStrideAlignment.
|
|
|
|
* @param varyings The varyings that will be written by 'vs' and read by 'fs'. This may
|
|
|
|
* be empty.
|
|
|
|
* @param vs The vertex shader code that computes a vertex position and the varyings
|
|
|
|
* from the attributes.
|
|
|
|
* @param fs The fragment code that computes a local coordinate and optionally a
|
|
|
|
* color from the varyings. The local coordinate is used to sample
|
|
|
|
* SkShader.
|
2021-12-22 15:27:05 +00:00
|
|
|
* @param cs The colorspace of the color produced by 'fs'. Ignored if 'fs's main()
|
|
|
|
* function does not have a color out param.
|
|
|
|
* @param at The alpha type of the color produced by 'fs'. Ignored if 'fs's main()
|
|
|
|
* function does not have a color out param. Cannot be kUnknown.
|
2021-12-21 15:36:58 +00:00
|
|
|
*/
|
2022-04-05 16:56:01 +00:00
|
|
|
static Result Make(SkSpan<const Attribute> attributes,
|
|
|
|
size_t vertexStride,
|
|
|
|
SkSpan<const Varying> varyings,
|
|
|
|
const SkString& vs,
|
|
|
|
const SkString& fs);
|
|
|
|
static Result Make(SkSpan<const Attribute> attributes,
|
|
|
|
size_t vertexStride,
|
|
|
|
SkSpan<const Varying> varyings,
|
|
|
|
const SkString& vs,
|
|
|
|
const SkString& fs,
|
|
|
|
sk_sp<SkColorSpace> cs);
|
2021-12-21 15:36:58 +00:00
|
|
|
static Result Make(SkSpan<const Attribute> attributes,
|
|
|
|
size_t vertexStride,
|
|
|
|
SkSpan<const Varying> varyings,
|
|
|
|
const SkString& vs,
|
2021-12-22 15:27:05 +00:00
|
|
|
const SkString& fs,
|
2022-04-05 16:56:01 +00:00
|
|
|
sk_sp<SkColorSpace> cs,
|
|
|
|
SkAlphaType at);
|
2021-12-21 15:36:58 +00:00
|
|
|
|
|
|
|
SkSpan<const Attribute> attributes() const { return SkMakeSpan(fAttributes); }
|
|
|
|
|
|
|
|
size_t stride() const { return fStride; }
|
|
|
|
|
|
|
|
private:
|
2022-05-06 19:17:00 +00:00
|
|
|
friend struct SkMeshSpecificationPriv;
|
2021-12-21 15:36:58 +00:00
|
|
|
|
|
|
|
enum class ColorType {
|
|
|
|
kNone,
|
|
|
|
kHalf4,
|
|
|
|
kFloat4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Result MakeFromSourceWithStructs(SkSpan<const Attribute> attributes,
|
|
|
|
size_t stride,
|
|
|
|
SkSpan<const Varying> varyings,
|
|
|
|
const SkString& vs,
|
2021-12-22 15:27:05 +00:00
|
|
|
const SkString& fs,
|
|
|
|
sk_sp<SkColorSpace> cs,
|
|
|
|
SkAlphaType at);
|
2021-12-21 15:36:58 +00:00
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
SkMeshSpecification(SkSpan<const Attribute>,
|
|
|
|
size_t,
|
|
|
|
SkSpan<const Varying>,
|
|
|
|
std::unique_ptr<SkSL::Program>,
|
|
|
|
std::unique_ptr<SkSL::Program>,
|
|
|
|
ColorType,
|
|
|
|
bool hasLocalCoords,
|
|
|
|
sk_sp<SkColorSpace>,
|
|
|
|
SkAlphaType);
|
|
|
|
|
|
|
|
SkMeshSpecification(const SkMeshSpecification&) = delete;
|
|
|
|
SkMeshSpecification(SkMeshSpecification&&) = delete;
|
|
|
|
|
|
|
|
SkMeshSpecification& operator=(const SkMeshSpecification&) = delete;
|
|
|
|
SkMeshSpecification& operator=(SkMeshSpecification&&) = delete;
|
|
|
|
|
|
|
|
const std::vector<Attribute> fAttributes;
|
|
|
|
const std::vector<Varying> fVaryings;
|
|
|
|
std::unique_ptr<SkSL::Program> fVS;
|
|
|
|
std::unique_ptr<SkSL::Program> fFS;
|
|
|
|
size_t fStride;
|
|
|
|
uint32_t fHash;
|
|
|
|
ColorType fColorType;
|
|
|
|
bool fHasLocalCoords;
|
|
|
|
sk_sp<SkColorSpace> fColorSpace;
|
|
|
|
SkAlphaType fAlphaType;
|
2021-12-21 15:36:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-05-06 19:17:00 +00:00
|
|
|
* A vertex buffer, a topology, optionally an index buffer, and a compatible SkMeshSpecification.
|
2021-12-21 15:36:58 +00:00
|
|
|
*
|
2022-05-03 20:45:21 +00:00
|
|
|
* The data in the vertex buffer is expected to contain the attributes described by the spec
|
|
|
|
* for vertexCount vertices beginning at vertexOffset. vertexOffset must be aligned to the
|
2022-05-06 19:17:00 +00:00
|
|
|
* SkMeshSpecification's vertex stride. The size of the buffer must be at least vertexOffset +
|
2022-05-03 20:45:21 +00:00
|
|
|
* spec->stride()*vertexCount (even if vertex attributes contains pad at the end of the stride). If
|
|
|
|
* the specified bounds does not contain all the points output by the spec's vertex program when
|
|
|
|
* applied to the vertices in the custom mesh then the result is undefined.
|
2021-12-21 14:54:24 +00:00
|
|
|
*
|
2022-05-03 20:45:21 +00:00
|
|
|
* MakeIndexed may be used to create an indexed mesh. indexCount indices are read from the index
|
|
|
|
* buffer at the specified offset which must be aligned to 2. The indices are always unsigned 16bit
|
|
|
|
* integers. The index count must be at least 3.
|
2021-12-21 14:54:24 +00:00
|
|
|
*
|
2022-05-03 20:45:21 +00:00
|
|
|
* If Make() is used the implicit index sequence is 0, 1, 2, 3, ... and vertexCount must be at least
|
|
|
|
* 3.
|
2021-12-21 15:36:58 +00:00
|
|
|
*/
|
2022-05-06 19:17:00 +00:00
|
|
|
class SkMesh {
|
2022-05-03 20:45:21 +00:00
|
|
|
public:
|
|
|
|
class IndexBuffer : public SkRefCnt {};
|
|
|
|
class VertexBuffer : public SkRefCnt {};
|
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
SkMesh();
|
|
|
|
~SkMesh();
|
2022-05-03 20:45:21 +00:00
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
SkMesh(const SkMesh&);
|
|
|
|
SkMesh(SkMesh&&);
|
2022-05-03 20:45:21 +00:00
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
SkMesh& operator=(const SkMesh&);
|
|
|
|
SkMesh& operator=(SkMesh&&);
|
2022-05-03 20:45:21 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-06 19:17:00 +00:00
|
|
|
* Makes an index buffer to be used with SkMeshes. The SkData is used to determine the
|
2022-05-05 19:31:44 +00:00
|
|
|
* size and contents of the buffer. The buffer may be CPU- or GPU-backed depending on whether
|
|
|
|
* GrDirectContext* is nullptr.
|
2022-05-03 20:45:21 +00:00
|
|
|
*
|
2022-05-05 19:31:44 +00:00
|
|
|
* @param GrDirectContext* If nullptr a CPU-backed object is returned that owns the SkData.
|
|
|
|
* Otherwise, the data is uploaded to the GPU and a GPU-backed buffer
|
|
|
|
* is returned. It may only be used to draw into SkSurfaces that
|
|
|
|
* are backed by the passed GrDirectContext.
|
2022-05-03 20:45:21 +00:00
|
|
|
* @param sk_sp<SkData> required. The data used to populate the buffer.
|
|
|
|
*/
|
|
|
|
static sk_sp<IndexBuffer> MakeIndexBuffer(GrDirectContext*, sk_sp<const SkData>);
|
|
|
|
|
|
|
|
/**
|
2022-05-06 19:17:00 +00:00
|
|
|
* Makes a vertex buffer to be used with SkMeshes. The SkData is used to determine the
|
2022-05-05 19:31:44 +00:00
|
|
|
* size and contents of the buffer.The buffer may be CPU- or GPU-backed depending on whether
|
|
|
|
* GrDirectContext* is nullptr.
|
2022-05-03 20:45:21 +00:00
|
|
|
*
|
2022-05-05 19:31:44 +00:00
|
|
|
* @param GrDirectContext* If nullptr a CPU-backed object is returned that owns the SkData.
|
|
|
|
* Otherwise, the data is uploaded to the GPU and a GPU-backed buffer
|
|
|
|
* is returned. It may only be used to draw into SkSurfaces that
|
|
|
|
* are backed by the passed GrDirectContext.
|
2022-05-03 20:45:21 +00:00
|
|
|
* @param sk_sp<SkData> required. The data used to populate the buffer.
|
|
|
|
*/
|
|
|
|
static sk_sp<VertexBuffer> MakeVertexBuffer(GrDirectContext*, sk_sp<const SkData>);
|
|
|
|
|
2021-12-21 15:36:58 +00:00
|
|
|
enum class Mode { kTriangles, kTriangleStrip };
|
2022-05-03 20:45:21 +00:00
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
static SkMesh Make(sk_sp<SkMeshSpecification>,
|
|
|
|
Mode,
|
|
|
|
sk_sp<VertexBuffer>,
|
|
|
|
size_t vertexCount,
|
|
|
|
size_t vertexOffset,
|
|
|
|
const SkRect& bounds);
|
|
|
|
|
|
|
|
static SkMesh MakeIndexed(sk_sp<SkMeshSpecification>,
|
|
|
|
Mode,
|
|
|
|
sk_sp<VertexBuffer>,
|
|
|
|
size_t vertexCount,
|
|
|
|
size_t vertexOffset,
|
|
|
|
sk_sp<IndexBuffer>,
|
|
|
|
size_t indexCount,
|
|
|
|
size_t indexOffset,
|
|
|
|
const SkRect& bounds);
|
|
|
|
|
|
|
|
sk_sp<SkMeshSpecification> spec() const { return fSpec; }
|
2022-05-03 20:45:21 +00:00
|
|
|
|
|
|
|
Mode mode() const { return fMode; }
|
|
|
|
|
|
|
|
sk_sp<VertexBuffer> vertexBuffer() const { return fVB; }
|
|
|
|
|
|
|
|
size_t vertexOffset() const { return fVOffset; }
|
|
|
|
size_t vertexCount() const { return fVCount; }
|
|
|
|
|
|
|
|
sk_sp<IndexBuffer> indexBuffer() const { return fIB; }
|
|
|
|
|
|
|
|
size_t indexOffset() const { return fIOffset; }
|
|
|
|
size_t indexCount() const { return fICount; }
|
|
|
|
|
|
|
|
SkRect bounds() const { return fBounds; }
|
|
|
|
|
|
|
|
bool isValid() const;
|
|
|
|
|
|
|
|
private:
|
2022-05-06 19:17:00 +00:00
|
|
|
friend struct SkMeshPriv;
|
2022-05-03 20:45:21 +00:00
|
|
|
|
|
|
|
bool validate() const;
|
|
|
|
|
2022-05-06 19:17:00 +00:00
|
|
|
sk_sp<SkMeshSpecification> fSpec;
|
2022-05-03 20:45:21 +00:00
|
|
|
|
|
|
|
sk_sp<VertexBuffer> fVB;
|
|
|
|
sk_sp<IndexBuffer> fIB;
|
|
|
|
|
|
|
|
size_t fVOffset = 0; // Must be a multiple of spec->stride()
|
|
|
|
size_t fVCount = 0;
|
|
|
|
|
|
|
|
size_t fIOffset = 0; // Must be a multiple of sizeof(uint16_t)
|
|
|
|
size_t fICount = 0;
|
|
|
|
|
|
|
|
Mode fMode = Mode::kTriangles;
|
|
|
|
|
|
|
|
SkRect fBounds = SkRect::MakeEmpty();
|
2021-12-21 15:36:58 +00:00
|
|
|
};
|
|
|
|
|
2022-01-06 16:18:03 +00:00
|
|
|
#endif // SK_ENABLE_SKSL
|
|
|
|
|
2021-12-21 15:36:58 +00:00
|
|
|
#endif
|