Skip to content

Commit

Permalink
Add a memory-mapped GpuBuffer constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Nov 1, 2023
1 parent 38b402f commit a96266e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
4 changes: 0 additions & 4 deletions src/gpu_buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "gpu_buffer.hpp"

#include <cassert>
#include <format>
#include <stdexcept>

namespace pt
{
namespace
Expand Down
50 changes: 50 additions & 0 deletions src/gpu_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

#include <webgpu/webgpu.h>

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <format>
#include <span>
#include <stdexcept>

namespace pt
{
Expand All @@ -24,6 +29,14 @@ class GpuBuffer
const char* label,
std::size_t byteSize,
WGPUBufferUsageFlags usage);

template<typename T>
GpuBuffer(
WGPUDevice device,
const char* label,
WGPUBufferUsageFlags usage,
std::span<const T> data);

~GpuBuffer();

// Raw access
Expand All @@ -43,4 +56,41 @@ class GpuBuffer
std::size_t mByteSize = 0;
WGPUBufferUsageFlags mUsage = WGPUBufferUsage_None;
};

template<typename T>
GpuBuffer::GpuBuffer(
const WGPUDevice device,
const char* const label,
const WGPUBufferUsageFlags usage,
const std::span<const T> data)
: mBuffer(nullptr),
mByteSize(0),
mUsage(usage)
{
assert(device != nullptr);

mByteSize = sizeof(T) * data.size();

const WGPUBufferDescriptor bufferDesc{
.nextInChain = nullptr,
.label = label,
.usage = mUsage,
.size = mByteSize,
.mappedAtCreation = true,
};
mBuffer = wgpuDeviceCreateBuffer(device, &bufferDesc);

if (!mBuffer)
{
throw std::runtime_error(std::format("Failed to create buffer: {}", label));
}

// It's legal to set mappedAtCreation = true and use the mapped range even if the usage doesn't
// include MAP_READ or MAP_WRITE.
// https://www.w3.org/TR/webgpu/#dom-gpubufferdescriptor-mappedatcreation

void* const mappedData = wgpuBufferGetMappedRange(mBuffer, 0, mByteSize);
std::memcpy(mappedData, data.data(), mByteSize);
wgpuBufferUnmap(mBuffer);
}
} // namespace pt
33 changes: 12 additions & 21 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdint>
#include <cstdio>
#include <limits>
#include <span>
#include <tuple>

namespace pt
Expand Down Expand Up @@ -224,37 +225,27 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
Vertex{{-0.5f, 0.5f}, {0.0f, 1.0f}},
Vertex{{-0.5f, -0.5f}, {0.0f, 0.0f}},
};
const std::size_t vertexDataNumBytes = sizeof(Vertex) * vertexData.size();

// TODO: figure out a way to do memory-mapped buffers
vertexBuffer = GpuBuffer(
gpuContext.device,
"Vertex buffer",
vertexDataNumBytes,
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex);
wgpuQueueWriteBuffer(
gpuContext.queue, vertexBuffer.handle(), 0, vertexData.data(), vertexDataNumBytes);
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
std::span<const Vertex>(vertexData));
}

{
// DirectX, Metal, wgpu share the same left-handed coordinate system
// for their normalized device coordinates:
// https://github.com/gfx-rs/gfx/tree/master/src/backend/dx12
const glm::mat4 viewProjectionMatrix = glm::orthoLH(-0.5f, 0.5f, -0.5f, 0.5f, -1.f, 1.f);

uniformsBuffer = GpuBuffer(
gpuContext.device,
"uniforms buffer",
sizeof(glm::mat4),
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform);

{
// DirectX, Metal, wgpu share the same left-handed coordinate system
// for their normalized device coordinates:
// https://github.com/gfx-rs/gfx/tree/master/src/backend/dx12
glm::mat4 viewProjectionMatrix = glm::orthoLH(-0.5f, 0.5f, -0.5f, 0.5f, -1.f, 1.f);
wgpuQueueWriteBuffer(
gpuContext.queue,
uniformsBuffer.handle(),
0,
&viewProjectionMatrix[0],
sizeof(viewProjectionMatrix));
}
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
std::span<const std::uint8_t>(
reinterpret_cast<const std::uint8_t*>(&viewProjectionMatrix[0]),
sizeof(glm::mat4)));
}

{
Expand Down

0 comments on commit a96266e

Please sign in to comment.