diff --git a/src/dawn_node/binding/CMakeLists.txt b/src/dawn_node/binding/CMakeLists.txt index 4287e13206..e1f7de4018 100644 --- a/src/dawn_node/binding/CMakeLists.txt +++ b/src/dawn_node/binding/CMakeLists.txt @@ -46,6 +46,7 @@ add_library(dawn_node_binding STATIC "GPURenderPipeline.h" "GPUSampler.cpp" "GPUSampler.h" + "GPUShaderModule.cpp" "GPUShaderModule.h" "GPUSupportedLimits.cpp" "GPUSupportedLimits.h" diff --git a/src/dawn_node/binding/GPUShaderModule.cpp b/src/dawn_node/binding/GPUShaderModule.cpp new file mode 100644 index 0000000000..3323ba5c83 --- /dev/null +++ b/src/dawn_node/binding/GPUShaderModule.cpp @@ -0,0 +1,125 @@ +// 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/GPUShaderModule.h" + +#include + +#include "src/dawn_node/utils/Debug.h" + +namespace wgpu { namespace binding { + + //////////////////////////////////////////////////////////////////////////////// + // wgpu::bindings::GPUShaderModule + //////////////////////////////////////////////////////////////////////////////// + GPUShaderModule::GPUShaderModule(wgpu::ShaderModule shader, std::shared_ptr async) + : shader_(std::move(shader)), async_(std::move(async)) { + } + + interop::Promise> + GPUShaderModule::compilationInfo(Napi::Env env) { + struct GPUCompilationMessage : public interop::GPUCompilationMessage { + WGPUCompilationMessage message; + + GPUCompilationMessage(const WGPUCompilationMessage& m) : message(m) { + } + std::string getMessage(Napi::Env) override { + return message.message; + } + interop::GPUCompilationMessageType getType(Napi::Env) override { + switch (message.type) { + case WGPUCompilationMessageType_Error: + return interop::GPUCompilationMessageType::kError; + case WGPUCompilationMessageType_Warning: + return interop::GPUCompilationMessageType::kWarning; + case WGPUCompilationMessageType_Info: + return interop::GPUCompilationMessageType::kInfo; + default: + UNIMPLEMENTED(); + } + } + uint64_t getLineNum(Napi::Env) override { + return message.lineNum; + } + uint64_t getLinePos(Napi::Env) override { + return message.linePos; + } + uint64_t getOffset(Napi::Env) override { + return message.offset; + } + uint64_t getLength(Napi::Env) override { + return message.length; + } + }; + + using Messages = std::vector>; + + struct GPUCompilationInfo : public interop::GPUCompilationInfo { + std::vector messages; + + GPUCompilationInfo(Napi::Env env, Messages msgs) { + messages.reserve(msgs.size()); + for (auto& msg : msgs) { + messages.emplace_back(Napi::Persistent(Napi::Object(env, msg))); + } + } + Messages getMessages(Napi::Env) override { + Messages out; + out.reserve(messages.size()); + for (auto& msg : messages) { + out.emplace_back(msg.Value()); + } + return out; + } + }; + + using Promise = interop::Promise>; + + struct Context { + Napi::Env env; + Promise promise; + AsyncTask task; + }; + auto ctx = new Context{env, env, async_}; + auto promise = ctx->promise; + + shader_.GetCompilationInfo( + [](WGPUCompilationInfoRequestStatus status, WGPUCompilationInfo const* compilationInfo, + void* userdata) { + auto c = std::unique_ptr(static_cast(userdata)); + + Messages messages(compilationInfo->messageCount); + for (uint32_t i = 0; i < compilationInfo->messageCount; i++) { + auto& msg = compilationInfo->messages[i]; + messages[i] = + interop::GPUCompilationMessage::Create(c->env, msg); + } + + c->promise.Resolve(interop::GPUCompilationInfo::Create( + c->env, c->env, std::move(messages))); + }, + ctx); + + return promise; + } + + std::optional GPUShaderModule::getLabel(Napi::Env) { + UNIMPLEMENTED(); + } + + void GPUShaderModule::setLabel(Napi::Env, std::optional value) { + UNIMPLEMENTED(); + } + +}} // namespace wgpu::binding