Skia
2D Graphics Library
SkMesh.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 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 #ifndef SkMesh_DEFINED
9 #define SkMesh_DEFINED
10 
11 #include "include/core/SkData.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSpan.h"
15 #include "include/core/SkString.h"
17 #include "include/private/base/SkAPI.h"
18 #include "include/private/base/SkTArray.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include <string_view>
24 #include <tuple>
25 #include <vector>
26 
27 class GrDirectContext;
28 class SkColorSpace;
29 enum SkAlphaType : int;
30 
31 namespace SkSL { struct Program; }
32 
65 class SkMeshSpecification : public SkNVRefCnt<SkMeshSpecification> {
66 public:
68  static constexpr size_t kMaxStride = 1024;
69  static constexpr size_t kMaxAttributes = 8;
70  static constexpr size_t kStrideAlignment = 4;
71  static constexpr size_t kOffsetAlignment = 4;
72  static constexpr size_t kMaxVaryings = 6;
73 
74  struct Attribute {
75  enum class Type : uint32_t { // CPU representation Shader Type
76  kFloat, // float float
77  kFloat2, // two floats float2
78  kFloat3, // three floats float3
79  kFloat4, // four floats float4
80  kUByte4_unorm, // four bytes half4
81 
83  };
85  size_t offset;
87  };
88 
89  struct Varying {
90  enum class Type : uint32_t {
91  kFloat, // "float"
92  kFloat2, // "float2"
93  kFloat3, // "float3"
94  kFloat4, // "float4"
95  kHalf, // "half"
96  kHalf2, // "half2"
97  kHalf3, // "half3"
98  kHalf4, // "half4"
99 
100  kLast = kHalf4
101  };
104  };
105 
108 
110 
111  struct Result {
114  };
115 
138  static Result Make(SkSpan<const Attribute> attributes,
139  size_t vertexStride,
140  SkSpan<const Varying> varyings,
141  const SkString& vs,
142  const SkString& fs);
143  static Result Make(SkSpan<const Attribute> attributes,
144  size_t vertexStride,
145  SkSpan<const Varying> varyings,
146  const SkString& vs,
147  const SkString& fs,
149  static Result Make(SkSpan<const Attribute> attributes,
150  size_t vertexStride,
151  SkSpan<const Varying> varyings,
152  const SkString& vs,
153  const SkString& fs,
155  SkAlphaType at);
156 
157  SkSpan<const Attribute> attributes() const { return SkSpan(fAttributes); }
158 
164  size_t uniformSize() const;
165 
170  SkSpan<const Uniform> uniforms() const { return SkSpan(fUniforms); }
171 
173  SkSpan<const Child> children() const { return SkSpan(fChildren); }
174 
176  const Child* findChild(std::string_view name) const;
177 
179  const Uniform* findUniform(std::string_view name) const;
180 
182  const Attribute* findAttribute(std::string_view name) const;
183 
185  const Varying* findVarying(std::string_view name) const;
186 
187  size_t stride() const { return fStride; }
188 
189 private:
190  friend struct SkMeshSpecificationPriv;
191 
192  enum class ColorType {
193  kNone,
194  kHalf4,
195  kFloat4,
196  };
197 
198  static Result MakeFromSourceWithStructs(SkSpan<const Attribute> attributes,
199  size_t stride,
200  SkSpan<const Varying> varyings,
201  const SkString& vs,
202  const SkString& fs,
204  SkAlphaType at);
205 
206  SkMeshSpecification(SkSpan<const Attribute>,
207  size_t,
208  SkSpan<const Varying>,
209  int passthroughLocalCoordsVaryingIndex,
210  uint32_t deadVaryingMask,
211  std::vector<Uniform> uniforms,
212  std::vector<Child> children,
213  std::unique_ptr<const SkSL::Program>,
214  std::unique_ptr<const SkSL::Program>,
215  ColorType,
217  SkAlphaType);
218 
219  SkMeshSpecification(const SkMeshSpecification&) = delete;
221 
222  SkMeshSpecification& operator=(const SkMeshSpecification&) = delete;
223  SkMeshSpecification& operator=(SkMeshSpecification&&) = delete;
224 
225  const std::vector<Attribute> fAttributes;
226  const std::vector<Varying> fVaryings;
227  const std::vector<Uniform> fUniforms;
228  const std::vector<Child> fChildren;
229  const std::unique_ptr<const SkSL::Program> fVS;
230  const std::unique_ptr<const SkSL::Program> fFS;
231  const size_t fStride;
232  uint32_t fHash;
233  const int fPassthroughLocalCoordsVaryingIndex;
234  const uint32_t fDeadVaryingMask;
235  const ColorType fColorType;
236  const sk_sp<SkColorSpace> fColorSpace;
237  const SkAlphaType fAlphaType;
238 };
239 
261 class SkMesh {
262 public:
263  class IndexBuffer : public SkRefCnt {
264  public:
265  virtual size_t size() const = 0;
266 
274  bool update(GrDirectContext*, const void* data, size_t offset, size_t size);
275 
276  private:
277  virtual bool onUpdate(GrDirectContext*, const void* data, size_t offset, size_t size) = 0;
278  };
279 
280  class VertexBuffer : public SkRefCnt {
281  public:
282  virtual size_t size() const = 0;
283 
291  bool update(GrDirectContext*, const void* data, size_t offset, size_t size);
292 
293  private:
294  virtual bool onUpdate(GrDirectContext*, const void* data, size_t offset, size_t size) = 0;
295  };
296 
299 
300  SkMesh(const SkMesh&);
302 
305 
306  enum class Mode { kTriangles, kTriangleStrip };
307 
308  struct Result;
309 
311 
319  Mode,
321  size_t vertexCount,
322  size_t vertexOffset,
324  SkSpan<ChildPtr> children,
325  const SkRect& bounds);
326 
334  Mode,
336  size_t vertexCount,
337  size_t vertexOffset,
339  size_t indexCount,
340  size_t indexOffset,
342  SkSpan<ChildPtr> children,
343  const SkRect& bounds);
344 
345  sk_sp<SkMeshSpecification> refSpec() const { return fSpec; }
346  SkMeshSpecification* spec() const { return fSpec.get(); }
347 
348  Mode mode() const { return fMode; }
349 
350  sk_sp<VertexBuffer> refVertexBuffer() const { return fVB; }
351  VertexBuffer* vertexBuffer() const { return fVB.get(); }
352 
353  size_t vertexOffset() const { return fVOffset; }
354  size_t vertexCount() const { return fVCount; }
355 
356  sk_sp<IndexBuffer> refIndexBuffer() const { return fIB; }
357  IndexBuffer* indexBuffer() const { return fIB.get(); }
358 
359  size_t indexOffset() const { return fIOffset; }
360  size_t indexCount() const { return fICount; }
361 
362  sk_sp<const SkData> refUniforms() const { return fUniforms; }
363  const SkData* uniforms() const { return fUniforms.get(); }
364 
365  SkSpan<const ChildPtr> children() const { return SkSpan(fChildren); }
366 
367  SkRect bounds() const { return fBounds; }
368 
369  bool isValid() const;
370 
371 private:
372  std::tuple<bool, SkString> validate() const;
373 
375 
377  sk_sp<IndexBuffer> fIB;
378 
379  sk_sp<const SkData> fUniforms;
380  skia_private::STArray<2, ChildPtr> fChildren;
381 
382  size_t fVOffset = 0; // Must be a multiple of spec->stride()
383  size_t fVCount = 0;
384 
385  size_t fIOffset = 0; // Must be a multiple of sizeof(uint16_t)
386  size_t fICount = 0;
387 
388  Mode fMode = Mode::kTriangles;
389 
390  SkRect fBounds = SkRect::MakeEmpty();
391 };
392 
394 
395 namespace SkMeshes {
404 SK_API sk_sp<SkMesh::IndexBuffer> MakeIndexBuffer(const void* data, size_t size);
405 
410 
419 SK_API sk_sp<SkMesh::VertexBuffer> MakeVertexBuffer(const void*, size_t size);
420 
425 } // namespace SkMeshes
426 
427 #endif
SkAlphaType
Describes how to interpret the alpha component of a pixel.
Definition: SkAlphaType.h:26
@ kNone
glyph outlines unchanged
Definition: GrDirectContext.h:62
Definition: SkColorSpace.h:107
SkData holds an immutable data buffer.
Definition: SkData.h:25
A specification for custom meshes.
Definition: SkMesh.h:65
const Uniform * findUniform(std::string_view name) const
Returns a pointer to the named uniform variable's description, or nullptr if not found.
static constexpr size_t kStrideAlignment
Definition: SkMesh.h:70
const Child * findChild(std::string_view name) const
Returns a pointer to the named child's description, or nullptr if not found.
size_t stride() const
Definition: SkMesh.h:187
static constexpr size_t kMaxVaryings
Definition: SkMesh.h:72
static Result Make(SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs, sk_sp< SkColorSpace > cs, SkAlphaType at)
const Attribute * findAttribute(std::string_view name) const
Returns a pointer to the named attribute, or nullptr if not found.
static constexpr size_t kMaxAttributes
Definition: SkMesh.h:69
static constexpr size_t kOffsetAlignment
Definition: SkMesh.h:71
static Result Make(SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs)
If successful the return is a specification and an empty error string.
SkSpan< const Child > children() const
Provides basic info about individual children: names, indices and runtime effect type.
Definition: SkMesh.h:173
const Varying * findVarying(std::string_view name) const
Returns a pointer to the named varying, or nullptr if not found.
static constexpr size_t kMaxStride
These values are enforced when creating a specification.
Definition: SkMesh.h:68
size_t uniformSize() const
Combined size of all 'uniform' variables.
friend struct SkMeshSpecificationPriv
Definition: SkMesh.h:190
SkSpan< const Uniform > uniforms() const
Provides info about individual uniforms including the offset into an SkData where each uniform value ...
Definition: SkMesh.h:170
static Result Make(SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs, sk_sp< SkColorSpace > cs)
SkSpan< const Attribute > attributes() const
Definition: SkMesh.h:157
Definition: SkMesh.h:263
bool update(GrDirectContext *, const void *data, size_t offset, size_t size)
Modifies the data in the IndexBuffer by copying size bytes from data into the buffer at offset.
virtual size_t size() const =0
Definition: SkMesh.h:280
bool update(GrDirectContext *, const void *data, size_t offset, size_t size)
Modifies the data in the IndexBuffer by copying size bytes from data into the buffer at offset.
virtual size_t size() const =0
A vertex buffer, a topology, optionally an index buffer, and a compatible SkMeshSpecification.
Definition: SkMesh.h:261
SkSpan< const ChildPtr > children() const
Definition: SkMesh.h:365
static Result MakeIndexed(sk_sp< SkMeshSpecification >, Mode, sk_sp< VertexBuffer >, size_t vertexCount, size_t vertexOffset, sk_sp< IndexBuffer >, size_t indexCount, size_t indexOffset, sk_sp< const SkData > uniforms, SkSpan< ChildPtr > children, const SkRect &bounds)
Creates an indexed SkMesh.
VertexBuffer * vertexBuffer() const
Definition: SkMesh.h:351
SkMesh & operator=(const SkMesh &)
sk_sp< SkMeshSpecification > refSpec() const
Definition: SkMesh.h:345
bool isValid() const
const SkData * uniforms() const
Definition: SkMesh.h:363
IndexBuffer * indexBuffer() const
Definition: SkMesh.h:357
size_t vertexCount() const
Definition: SkMesh.h:354
SkMesh(const SkMesh &)
SkMesh(SkMesh &&)
size_t vertexOffset() const
Definition: SkMesh.h:353
SkMesh & operator=(SkMesh &&)
Mode mode() const
Definition: SkMesh.h:348
SkMeshSpecification * spec() const
Definition: SkMesh.h:346
size_t indexCount() const
Definition: SkMesh.h:360
sk_sp< VertexBuffer > refVertexBuffer() const
Definition: SkMesh.h:350
static Result Make(sk_sp< SkMeshSpecification >, Mode, sk_sp< VertexBuffer >, size_t vertexCount, size_t vertexOffset, sk_sp< const SkData > uniforms, SkSpan< ChildPtr > children, const SkRect &bounds)
Creates a non-indexed SkMesh.
SkRect bounds() const
Definition: SkMesh.h:367
size_t indexOffset() const
Definition: SkMesh.h:359
sk_sp< IndexBuffer > refIndexBuffer() const
Definition: SkMesh.h:356
sk_sp< const SkData > refUniforms() const
Definition: SkMesh.h:362
Mode
Definition: SkMesh.h:306
Definition: SkRefCnt.h:160
Definition: SkRefCnt.h:119
Definition: SkRuntimeEffect.h:183
Light weight class for managing strings.
Definition: SkString.h:118
T * get() const
Definition: SkRefCnt.h:303
Definition: SkMesh.h:395
SK_API sk_sp< SkMesh::IndexBuffer > MakeIndexBuffer(const void *data, size_t size)
Makes a CPU-backed index buffer to be used with SkMeshes.
SK_API sk_sp< SkMesh::IndexBuffer > CopyIndexBuffer(const sk_sp< SkMesh::IndexBuffer > &)
Makes a copy of an index buffer.
SK_API sk_sp< SkMesh::VertexBuffer > MakeVertexBuffer(const void *, size_t size)
Makes a CPU-backed vertex buffer to be used with SkMeshes.
SK_API sk_sp< SkMesh::VertexBuffer > CopyVertexBuffer(const sk_sp< SkMesh::VertexBuffer > &)
Makes a copy of a vertex buffer.
Definition: SkCapabilities.h:15
Definition: SkMesh.h:74
size_t offset
Definition: SkMesh.h:85
Type type
Definition: SkMesh.h:84
SkString name
Definition: SkMesh.h:86
Type
Definition: SkMesh.h:75
Definition: SkMesh.h:111
sk_sp< SkMeshSpecification > specification
Definition: SkMesh.h:112
SkString error
Definition: SkMesh.h:113
Definition: SkMesh.h:89
SkString name
Definition: SkMesh.h:103
Type
Definition: SkMesh.h:90
Type type
Definition: SkMesh.h:102
Definition: SkMesh.h:393
SkString error
Definition: SkMesh.h:393
SkMesh mesh
Definition: SkMesh.h:393
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
static constexpr SkRect MakeEmpty()
Returns constructed SkRect set to (0, 0, 0, 0).
Definition: SkRect.h:595
Definition: SkRuntimeEffect.h:117
Definition: SkRuntimeEffect.h:62