From 6104800416a4020fc2180d7ef826f983da3a0f79 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 28 Sep 2021 16:07:32 +0000 Subject: [PATCH] dawn_node: Add binding/GPUQueue.cpp Bug: dawn:1123 Change-Id: I629008de8b096da537737df18d7173e96d433394 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/64913 Commit-Queue: Ben Clayton Reviewed-by: Austin Eng --- src/dawn_node/binding/CMakeLists.txt | 1 + src/dawn_node/binding/GPUQueue.cpp | 134 +++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/dawn_node/binding/GPUQueue.cpp diff --git a/src/dawn_node/binding/CMakeLists.txt b/src/dawn_node/binding/CMakeLists.txt index 6461f49597..db7cb1434a 100644 --- a/src/dawn_node/binding/CMakeLists.txt +++ b/src/dawn_node/binding/CMakeLists.txt @@ -37,6 +37,7 @@ add_library(dawn_node_binding STATIC "GPUPipelineLayout.h" "GPUQuerySet.cpp" "GPUQuerySet.h" + "GPUQueue.cpp" "GPUQueue.h" "GPURenderBundle.cpp" "GPURenderBundle.h" diff --git a/src/dawn_node/binding/GPUQueue.cpp b/src/dawn_node/binding/GPUQueue.cpp new file mode 100644 index 0000000000..92ad948e7d --- /dev/null +++ b/src/dawn_node/binding/GPUQueue.cpp @@ -0,0 +1,134 @@ +// Copyright 2021 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/dawn_node/binding/GPUQueue.h" + +#include + +#include "src/dawn_node/binding/Converter.h" +#include "src/dawn_node/binding/GPUBuffer.h" +#include "src/dawn_node/binding/GPUCommandBuffer.h" +#include "src/dawn_node/utils/Debug.h" + +namespace wgpu { namespace binding { + + //////////////////////////////////////////////////////////////////////////////// + // wgpu::bindings::GPUQueue + //////////////////////////////////////////////////////////////////////////////// + GPUQueue::GPUQueue(wgpu::Queue queue, std::shared_ptr async) + : queue_(std::move(queue)), async_(std::move(async)) { + } + + void GPUQueue::submit( + Napi::Env env, + std::vector> commandBuffers) { + std::vector bufs(commandBuffers.size()); + for (size_t i = 0; i < commandBuffers.size(); i++) { + bufs[i] = *commandBuffers[i].As(); + } + Converter conv(env); + uint32_t bufs_size; + if (!conv(bufs_size, bufs.size())) { + return; + } + queue_.Submit(bufs_size, bufs.data()); + } + + interop::Promise GPUQueue::onSubmittedWorkDone(Napi::Env env) { + struct Context { + Napi::Env env; + interop::Promise promise; + AsyncTask task; + }; + auto ctx = new Context{env, interop::Promise(env), async_}; + auto promise = ctx->promise; + + queue_.OnSubmittedWorkDone( + 0, + [](WGPUQueueWorkDoneStatus status, void* userdata) { + auto c = std::unique_ptr(static_cast(userdata)); + if (status != WGPUQueueWorkDoneStatus::WGPUQueueWorkDoneStatus_Success) { + Napi::Error::New(c->env, "onSubmittedWorkDone() failed") + .ThrowAsJavaScriptException(); + } + c->promise.Resolve(); + }, + ctx); + + return promise; + } + + void GPUQueue::writeBuffer(Napi::Env env, + interop::Interface buffer, + interop::GPUSize64 bufferOffset, + interop::BufferSource data, + std::optional dataOffset, + std::optional size) { + wgpu::Buffer buf = *buffer.As(); + Converter::BufferSource src{}; + Converter conv(env); + if (!conv(src, data)) { + return; + } + + // TODO(crbug.com/dawn/1132): Bounds check + if (dataOffset.has_value()) { + if (src.data) { + src.data = reinterpret_cast(src.data) + dataOffset.value(); + } + src.size -= dataOffset.value(); + } + if (size.has_value()) { + src.size = size.value(); + } + + queue_.WriteBuffer(buf, bufferOffset, src.data, src.size); + } + + void GPUQueue::writeTexture(Napi::Env env, + interop::GPUImageCopyTexture destination, + interop::BufferSource data, + interop::GPUImageDataLayout dataLayout, + interop::GPUExtent3D size) { + wgpu::ImageCopyTexture dst{}; + Converter::BufferSource src{}; + wgpu::TextureDataLayout layout{}; + wgpu::Extent3D sz{}; + Converter conv(env); + if (!conv(dst, destination) || // + !conv(src, data) || // + !conv(layout, dataLayout) || // + !conv(sz, size)) { + return; + } + + queue_.WriteTexture(&dst, src.data, src.size, &layout, &sz); + } + + void GPUQueue::copyExternalImageToTexture(Napi::Env, + interop::GPUImageCopyExternalImage source, + interop::GPUImageCopyTextureTagged destination, + interop::GPUExtent3D copySize) { + UNIMPLEMENTED(); + } + + std::optional GPUQueue::getLabel(Napi::Env) { + UNIMPLEMENTED(); + } + + void GPUQueue::setLabel(Napi::Env, std::optional value) { + UNIMPLEMENTED(); + } + +}} // namespace wgpu::binding