From f4b9f95b68942a0fec8414c735c112756b93922c Mon Sep 17 00:00:00 2001 From: Jiawei Shao Date: Wed, 19 Apr 2023 01:47:52 +0000 Subject: [PATCH] dawn/node: Implement GPUBuffer.getMapState() Bug: dawn:1761 Change-Id: Id01789a2df8afd5726f6fc776e7d825153fd0f85 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/127722 Kokoro: Kokoro Reviewed-by: Corentin Wallez Commit-Queue: Jiawei Shao --- src/dawn/node/binding/Converter.cpp | 15 +++++++++++++++ src/dawn/node/binding/Converter.h | 2 ++ src/dawn/node/binding/GPUBuffer.cpp | 13 +++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/dawn/node/binding/Converter.cpp b/src/dawn/node/binding/Converter.cpp index 98c1619410..dd116ebb35 100644 --- a/src/dawn/node/binding/Converter.cpp +++ b/src/dawn/node/binding/Converter.cpp @@ -1722,6 +1722,21 @@ bool Converter::Convert(interop::GPUQueryType& out, wgpu::QueryType in) { return false; } +bool Convert(interop::GPUBufferMapState& out, wgpu::BufferMapState in) { + switch (in) { + case wgpu::BufferMapState::Unmapped: + out = interop::GPUBufferMapState::kUnmapped; + return true; + case wgpu::BufferMapState::Pending: + out = interop::GPUBufferMapState::kPending; + return true; + case wgpu::BufferMapState::Mapped: + out = interop::GPUBufferMapState::kMapped; + return true; + } + return false; +} + bool Converter::Convert(wgpu::AddressMode& out, const interop::GPUAddressMode& in) { out = wgpu::AddressMode::Repeat; switch (in) { diff --git a/src/dawn/node/binding/Converter.h b/src/dawn/node/binding/Converter.h index 16ad9b95a4..715cb18f5a 100644 --- a/src/dawn/node/binding/Converter.h +++ b/src/dawn/node/binding/Converter.h @@ -270,6 +270,8 @@ class Converter { [[nodiscard]] bool Convert(interop::GPUQueryType& out, wgpu::QueryType in); + [[nodiscard]] bool Convert(interop::GPUBufferMapState& out, wgpu::BufferMapState in); + // The two conversion methods don't generate an error when false is returned. That // responsibility is left to the caller if it is needed (it isn't always needed, see // https://gpuweb.github.io/gpuweb/#gpu-supportedfeatures) diff --git a/src/dawn/node/binding/GPUBuffer.cpp b/src/dawn/node/binding/GPUBuffer.cpp index 9d4a4cbc95..d92c840be4 100644 --- a/src/dawn/node/binding/GPUBuffer.cpp +++ b/src/dawn/node/binding/GPUBuffer.cpp @@ -166,8 +166,17 @@ interop::GPUSize64 GPUBuffer::getSize(Napi::Env) { return buffer_.GetSize(); } -interop::GPUBufferMapState GPUBuffer::getMapState(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUBufferMapState GPUBuffer::getMapState(Napi::Env env) { + interop::GPUBufferMapState result; + + Converter conv(env); + if (!conv(result, buffer_.GetMapState())) { + Napi::Error::New(env, "Couldn't convert usage to a JavaScript value.") + .ThrowAsJavaScriptException(); + return interop::GPUBufferMapState::kUnmapped; + } + + return result; } interop::GPUBufferUsageFlags GPUBuffer::getUsage(Napi::Env env) {