Skip to content

Commit

Permalink
Make RenderBuffer mappable
Browse files Browse the repository at this point in the history
Adds map() and unmap() functions to RenderBuffer, and changes Mesh::draw() to map and update the vertex buffer instead of making a new one.

Also refactors RenderBuffer to just be a blob of data as opposed to an array of elements of fixed size and type. Removes the type-specific factory methods for creating RenderBuffers in favor of a single one that just takes a buffer type and buffer size.

Diffs=
06a187288 Make RenderBuffer mappable (#5907)

Co-authored-by: Chris Dalton <[email protected]>
  • Loading branch information
csmartdalton and csmartdalton committed Aug 31, 2023
1 parent 282ba71 commit 9ae1356
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
654d4488e936ac8c63930253b83229ef1c82618b
06a1872884b3b7cf2be58673e5f5246c57c3d39c
31 changes: 15 additions & 16 deletions wasm/src/bindings_c2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class RendererWrapper : public wrapper<rive::Renderer>
rive::rcp<rive::RenderBuffer> vertices_f32,
rive::rcp<rive::RenderBuffer> uvCoords_f32,
rive::rcp<rive::RenderBuffer> indices_u16,
uint32_t vertexCount,
uint32_t indexCount,
rive::BlendMode value,
float opacity) override
{
Expand All @@ -117,21 +119,24 @@ class RendererWrapper : public wrapper<rive::Renderer>
auto uv = rive::DataRenderBuffer::Cast(uvCoords_f32.get());
auto indices = rive::DataRenderBuffer::Cast(indices_u16.get());

assert(uv->count() == vtx->count());
if (!vtx->count() || !indices->count())
assert(vtx->sizeInBytes() == vertexCount * sizeof(Vec2D));
assert(uv->sizeInBytes() == vertexCount * sizeof(Vec2D));
assert(indices->sizeInBytes() == indexCount * sizeof(uint16_t));

if (vertexCount == 0 || indexCount == 0)
{
return;
}

emscripten::val uvJS{emscripten::typed_memory_view(uv->count(), uv->f32s())};
emscripten::val vtxJS{emscripten::typed_memory_view(vtx->count(), vtx->f32s())};
emscripten::val indicesJS{emscripten::typed_memory_view(indices->count(), indices->u16s())};
emscripten::val uvJS{emscripten::typed_memory_view(vertexCount, uv->f32s())};
emscripten::val vtxJS{emscripten::typed_memory_view(vertexCount, vtx->f32s())};
emscripten::val indicesJS{emscripten::typed_memory_view(indexCount, indices->u16s())};

// Compute the mesh's bounding box.
float m[6];
emscripten::val mJS{emscripten::typed_memory_view(6, m)};
call<void>("_getMatrix", mJS);
auto [l, t, r, b] = bbox(m, vtx->f32s(), vtx->count());
auto [l, t, r, b] = bbox(m, vtx->f32s(), vertexCount);

call<void>("_drawImageMesh", image, value, opacity, vtxJS, uvJS, indicesJS, l, t, r, b);
}
Expand Down Expand Up @@ -294,17 +299,11 @@ namespace rive

class C2DFactory : public Factory
{
rcp<RenderBuffer> makeBufferU16(Span<const uint16_t> data) override
{
return rive::DataRenderBuffer::Make(data);
}
rcp<RenderBuffer> makeBufferU32(Span<const uint32_t> data) override
{
return rive::DataRenderBuffer::Make(data);
}
rcp<RenderBuffer> makeBufferF32(Span<const float> data) override
rcp<RenderBuffer> makeRenderBuffer(RenderBufferType type,
RenderBufferFlags flags,
size_t sizeInBytes)
{
return rive::DataRenderBuffer::Make(data);
return make_rcp<DataRenderBuffer>(type, flags, sizeInBytes);
}

rcp<RenderShader> makeLinearGradient(float sx,
Expand Down
2 changes: 1 addition & 1 deletion wasm/submodules/rive-cpp

0 comments on commit 9ae1356

Please sign in to comment.