From 06cc5c1acdf3f27266683900e4712690867704c9 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Mon, 13 Jun 2022 13:34:48 +0000 Subject: [PATCH] dawn.node: Implement reflection for container objects. Bug: dawn:1451 Change-Id: I54f2594c901c522de58ae337011be0c686c6e6dc Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/93140 Kokoro: Kokoro Reviewed-by: Ben Clayton Commit-Queue: Corentin Wallez --- src/dawn/node/binding/Converter.cpp | 334 ++++++++++++++++++++++++++ src/dawn/node/binding/Converter.h | 11 + src/dawn/node/binding/GPUBuffer.cpp | 15 +- src/dawn/node/binding/GPUQuerySet.cpp | 16 +- src/dawn/node/binding/GPUTexture.cpp | 49 +++- 5 files changed, 408 insertions(+), 17 deletions(-) diff --git a/src/dawn/node/binding/Converter.cpp b/src/dawn/node/binding/Converter.cpp index cad53f67e4..5355ed0395 100644 --- a/src/dawn/node/binding/Converter.cpp +++ b/src/dawn/node/binding/Converter.cpp @@ -459,11 +459,310 @@ bool Converter::Convert(wgpu::TextureFormat& out, const interop::GPUTextureForma return false; } +bool Converter::Convert(interop::GPUTextureFormat& out, wgpu::TextureFormat in) { + switch (in) { + case wgpu::TextureFormat::R8Unorm: + out = interop::GPUTextureFormat::kR8Unorm; + return true; + case wgpu::TextureFormat::R8Snorm: + out = interop::GPUTextureFormat::kR8Snorm; + return true; + case wgpu::TextureFormat::R8Uint: + out = interop::GPUTextureFormat::kR8Uint; + return true; + case wgpu::TextureFormat::R8Sint: + out = interop::GPUTextureFormat::kR8Sint; + return true; + case wgpu::TextureFormat::R16Uint: + out = interop::GPUTextureFormat::kR16Uint; + return true; + case wgpu::TextureFormat::R16Sint: + out = interop::GPUTextureFormat::kR16Sint; + return true; + case wgpu::TextureFormat::R16Float: + out = interop::GPUTextureFormat::kR16Float; + return true; + case wgpu::TextureFormat::RG8Unorm: + out = interop::GPUTextureFormat::kRg8Unorm; + return true; + case wgpu::TextureFormat::RG8Snorm: + out = interop::GPUTextureFormat::kRg8Snorm; + return true; + case wgpu::TextureFormat::RG8Uint: + out = interop::GPUTextureFormat::kRg8Uint; + return true; + case wgpu::TextureFormat::RG8Sint: + out = interop::GPUTextureFormat::kRg8Sint; + return true; + case wgpu::TextureFormat::R32Uint: + out = interop::GPUTextureFormat::kR32Uint; + return true; + case wgpu::TextureFormat::R32Sint: + out = interop::GPUTextureFormat::kR32Sint; + return true; + case wgpu::TextureFormat::R32Float: + out = interop::GPUTextureFormat::kR32Float; + return true; + case wgpu::TextureFormat::RG16Uint: + out = interop::GPUTextureFormat::kRg16Uint; + return true; + case wgpu::TextureFormat::RG16Sint: + out = interop::GPUTextureFormat::kRg16Sint; + return true; + case wgpu::TextureFormat::RG16Float: + out = interop::GPUTextureFormat::kRg16Float; + return true; + case wgpu::TextureFormat::RGBA8Unorm: + out = interop::GPUTextureFormat::kRgba8Unorm; + return true; + case wgpu::TextureFormat::RGBA8UnormSrgb: + out = interop::GPUTextureFormat::kRgba8UnormSrgb; + return true; + case wgpu::TextureFormat::RGBA8Snorm: + out = interop::GPUTextureFormat::kRgba8Snorm; + return true; + case wgpu::TextureFormat::RGBA8Uint: + out = interop::GPUTextureFormat::kRgba8Uint; + return true; + case wgpu::TextureFormat::RGBA8Sint: + out = interop::GPUTextureFormat::kRgba8Sint; + return true; + case wgpu::TextureFormat::BGRA8Unorm: + out = interop::GPUTextureFormat::kBgra8Unorm; + return true; + case wgpu::TextureFormat::BGRA8UnormSrgb: + out = interop::GPUTextureFormat::kBgra8UnormSrgb; + return true; + case wgpu::TextureFormat::RGB9E5Ufloat: + out = interop::GPUTextureFormat::kRgb9E5Ufloat; + return true; + case wgpu::TextureFormat::RGB10A2Unorm: + out = interop::GPUTextureFormat::kRgb10A2Unorm; + return true; + case wgpu::TextureFormat::RG11B10Ufloat: + out = interop::GPUTextureFormat::kRg11B10Ufloat; + return true; + case wgpu::TextureFormat::RG32Uint: + out = interop::GPUTextureFormat::kRg32Uint; + return true; + case wgpu::TextureFormat::RG32Sint: + out = interop::GPUTextureFormat::kRg32Sint; + return true; + case wgpu::TextureFormat::RG32Float: + out = interop::GPUTextureFormat::kRg32Float; + return true; + case wgpu::TextureFormat::RGBA16Uint: + out = interop::GPUTextureFormat::kRgba16Uint; + return true; + case wgpu::TextureFormat::RGBA16Sint: + out = interop::GPUTextureFormat::kRgba16Sint; + return true; + case wgpu::TextureFormat::RGBA16Float: + out = interop::GPUTextureFormat::kRgba16Float; + return true; + case wgpu::TextureFormat::RGBA32Uint: + out = interop::GPUTextureFormat::kRgba32Uint; + return true; + case wgpu::TextureFormat::RGBA32Sint: + out = interop::GPUTextureFormat::kRgba32Sint; + return true; + case wgpu::TextureFormat::RGBA32Float: + out = interop::GPUTextureFormat::kRgba32Float; + return true; + case wgpu::TextureFormat::Stencil8: + out = interop::GPUTextureFormat::kStencil8; + return true; + case wgpu::TextureFormat::Depth16Unorm: + out = interop::GPUTextureFormat::kDepth16Unorm; + return true; + case wgpu::TextureFormat::Depth24Plus: + out = interop::GPUTextureFormat::kDepth24Plus; + return true; + case wgpu::TextureFormat::Depth24PlusStencil8: + out = interop::GPUTextureFormat::kDepth24PlusStencil8; + return true; + case wgpu::TextureFormat::Depth32Float: + out = interop::GPUTextureFormat::kDepth32Float; + return true; + case wgpu::TextureFormat::Depth32FloatStencil8: + out = interop::GPUTextureFormat::kDepth32FloatStencil8; + return true; + case wgpu::TextureFormat::BC1RGBAUnorm: + out = interop::GPUTextureFormat::kBc1RgbaUnorm; + return true; + case wgpu::TextureFormat::BC1RGBAUnormSrgb: + out = interop::GPUTextureFormat::kBc1RgbaUnormSrgb; + return true; + case wgpu::TextureFormat::BC2RGBAUnorm: + out = interop::GPUTextureFormat::kBc2RgbaUnorm; + return true; + case wgpu::TextureFormat::BC2RGBAUnormSrgb: + out = interop::GPUTextureFormat::kBc2RgbaUnormSrgb; + return true; + case wgpu::TextureFormat::BC3RGBAUnorm: + out = interop::GPUTextureFormat::kBc3RgbaUnorm; + return true; + case wgpu::TextureFormat::BC3RGBAUnormSrgb: + out = interop::GPUTextureFormat::kBc3RgbaUnormSrgb; + return true; + case wgpu::TextureFormat::BC4RUnorm: + out = interop::GPUTextureFormat::kBc4RUnorm; + return true; + case wgpu::TextureFormat::BC4RSnorm: + out = interop::GPUTextureFormat::kBc4RSnorm; + return true; + case wgpu::TextureFormat::BC5RGUnorm: + out = interop::GPUTextureFormat::kBc5RgUnorm; + return true; + case wgpu::TextureFormat::BC5RGSnorm: + out = interop::GPUTextureFormat::kBc5RgSnorm; + return true; + case wgpu::TextureFormat::BC6HRGBUfloat: + out = interop::GPUTextureFormat::kBc6HRgbUfloat; + return true; + case wgpu::TextureFormat::BC6HRGBFloat: + out = interop::GPUTextureFormat::kBc6HRgbFloat; + return true; + case wgpu::TextureFormat::BC7RGBAUnorm: + out = interop::GPUTextureFormat::kBc7RgbaUnorm; + return true; + case wgpu::TextureFormat::BC7RGBAUnormSrgb: + out = interop::GPUTextureFormat::kBc7RgbaUnormSrgb; + return true; + case wgpu::TextureFormat::ETC2RGB8Unorm: + out = interop::GPUTextureFormat::kEtc2Rgb8Unorm; + return true; + case wgpu::TextureFormat::ETC2RGB8UnormSrgb: + out = interop::GPUTextureFormat::kEtc2Rgb8UnormSrgb; + return true; + case wgpu::TextureFormat::ETC2RGB8A1Unorm: + out = interop::GPUTextureFormat::kEtc2Rgb8A1Unorm; + return true; + case wgpu::TextureFormat::ETC2RGB8A1UnormSrgb: + out = interop::GPUTextureFormat::kEtc2Rgb8A1UnormSrgb; + return true; + case wgpu::TextureFormat::ETC2RGBA8Unorm: + out = interop::GPUTextureFormat::kEtc2Rgba8Unorm; + return true; + case wgpu::TextureFormat::ETC2RGBA8UnormSrgb: + out = interop::GPUTextureFormat::kEtc2Rgba8UnormSrgb; + return true; + case wgpu::TextureFormat::EACR11Unorm: + out = interop::GPUTextureFormat::kEacR11Unorm; + return true; + case wgpu::TextureFormat::EACR11Snorm: + out = interop::GPUTextureFormat::kEacR11Snorm; + return true; + case wgpu::TextureFormat::EACRG11Unorm: + out = interop::GPUTextureFormat::kEacRg11Unorm; + return true; + case wgpu::TextureFormat::EACRG11Snorm: + out = interop::GPUTextureFormat::kEacRg11Snorm; + return true; + case wgpu::TextureFormat::ASTC4x4Unorm: + out = interop::GPUTextureFormat::kAstc4X4Unorm; + return true; + case wgpu::TextureFormat::ASTC4x4UnormSrgb: + out = interop::GPUTextureFormat::kAstc4X4UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC5x4Unorm: + out = interop::GPUTextureFormat::kAstc5X4Unorm; + return true; + case wgpu::TextureFormat::ASTC5x4UnormSrgb: + out = interop::GPUTextureFormat::kAstc5X4UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC5x5Unorm: + out = interop::GPUTextureFormat::kAstc5X5Unorm; + return true; + case wgpu::TextureFormat::ASTC5x5UnormSrgb: + out = interop::GPUTextureFormat::kAstc5X5UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC6x5Unorm: + out = interop::GPUTextureFormat::kAstc6X5Unorm; + return true; + case wgpu::TextureFormat::ASTC6x5UnormSrgb: + out = interop::GPUTextureFormat::kAstc6X5UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC6x6Unorm: + out = interop::GPUTextureFormat::kAstc6X6Unorm; + return true; + case wgpu::TextureFormat::ASTC6x6UnormSrgb: + out = interop::GPUTextureFormat::kAstc6X6UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC8x5Unorm: + out = interop::GPUTextureFormat::kAstc8X5Unorm; + return true; + case wgpu::TextureFormat::ASTC8x5UnormSrgb: + out = interop::GPUTextureFormat::kAstc8X5UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC8x6Unorm: + out = interop::GPUTextureFormat::kAstc8X6Unorm; + return true; + case wgpu::TextureFormat::ASTC8x6UnormSrgb: + out = interop::GPUTextureFormat::kAstc8X6UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC8x8Unorm: + out = interop::GPUTextureFormat::kAstc8X8Unorm; + return true; + case wgpu::TextureFormat::ASTC8x8UnormSrgb: + out = interop::GPUTextureFormat::kAstc8X8UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC10x5Unorm: + out = interop::GPUTextureFormat::kAstc10X5Unorm; + return true; + case wgpu::TextureFormat::ASTC10x5UnormSrgb: + out = interop::GPUTextureFormat::kAstc10X5UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC10x6Unorm: + out = interop::GPUTextureFormat::kAstc10X6Unorm; + return true; + case wgpu::TextureFormat::ASTC10x6UnormSrgb: + out = interop::GPUTextureFormat::kAstc10X6UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC10x8Unorm: + out = interop::GPUTextureFormat::kAstc10X8Unorm; + return true; + case wgpu::TextureFormat::ASTC10x8UnormSrgb: + out = interop::GPUTextureFormat::kAstc10X8UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC10x10Unorm: + out = interop::GPUTextureFormat::kAstc10X10Unorm; + return true; + case wgpu::TextureFormat::ASTC10x10UnormSrgb: + out = interop::GPUTextureFormat::kAstc10X10UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC12x10Unorm: + out = interop::GPUTextureFormat::kAstc12X10Unorm; + return true; + case wgpu::TextureFormat::ASTC12x10UnormSrgb: + out = interop::GPUTextureFormat::kAstc12X10UnormSrgb; + return true; + case wgpu::TextureFormat::ASTC12x12Unorm: + out = interop::GPUTextureFormat::kAstc12X12Unorm; + return true; + case wgpu::TextureFormat::ASTC12x12UnormSrgb: + out = interop::GPUTextureFormat::kAstc12X12UnormSrgb; + return true; + + case wgpu::TextureFormat::Undefined: + case wgpu::TextureFormat::Depth24UnormStencil8: + case wgpu::TextureFormat::R8BG8Biplanar420Unorm: + return false; + } + + return false; +} + bool Converter::Convert(wgpu::TextureUsage& out, const interop::GPUTextureUsageFlags& in) { out = static_cast(in.value); return true; } +bool Converter::Convert(interop::GPUTextureUsageFlags& out, wgpu::TextureUsage in) { + out = interop::GPUTextureUsageFlags(static_cast(out)); + return true; +} + bool Converter::Convert(wgpu::ColorWriteMask& out, const interop::GPUColorWriteFlags& in) { out = static_cast(in.value); return true; @@ -474,6 +773,11 @@ bool Converter::Convert(wgpu::BufferUsage& out, const interop::GPUBufferUsageFla return true; } +bool Converter::Convert(interop::GPUBufferUsageFlags& out, wgpu::BufferUsage in) { + out = interop::GPUBufferUsageFlags(static_cast(out)); + return true; +} + bool Converter::Convert(wgpu::MapMode& out, const interop::GPUMapModeFlags& in) { out = static_cast(in.value); return true; @@ -501,6 +805,21 @@ bool Converter::Convert(wgpu::TextureDimension& out, const interop::GPUTextureDi return false; } +bool Converter::Convert(interop::GPUTextureDimension& out, wgpu::TextureDimension in) { + switch (in) { + case wgpu::TextureDimension::e1D: + out = interop::GPUTextureDimension::k1D; + return true; + case wgpu::TextureDimension::e2D: + out = interop::GPUTextureDimension::k2D; + return true; + case wgpu::TextureDimension::e3D: + out = interop::GPUTextureDimension::k3D; + return true; + } + return false; +} + bool Converter::Convert(wgpu::TextureViewDimension& out, const interop::GPUTextureViewDimension& in) { out = wgpu::TextureViewDimension::Undefined; @@ -1150,6 +1469,21 @@ bool Converter::Convert(wgpu::QueryType& out, const interop::GPUQueryType& in) { return false; } +bool Converter::Convert(interop::GPUQueryType& out, wgpu::QueryType in) { + switch (in) { + case wgpu::QueryType::Occlusion: + out = interop::GPUQueryType::kOcclusion; + return true; + case wgpu::QueryType::Timestamp: + out = interop::GPUQueryType::kTimestamp; + return true; + case wgpu::QueryType::PipelineStatistics: + // TODO(dawn:1123): Add support for pipeline statistics if they are in WebGPU one day. + return false; + } + 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 e0ef58c017..0c8a232a87 100644 --- a/src/dawn/node/binding/Converter.h +++ b/src/dawn/node/binding/Converter.h @@ -245,6 +245,17 @@ class Converter { [[nodiscard]] bool Convert(wgpu::PipelineLayout& out, const interop::GPUAutoLayoutMode& in); + // Below are the various overloads of Convert() used to convert the Dawn types -> interop. + [[nodiscard]] bool Convert(interop::GPUTextureDimension& out, wgpu::TextureDimension in); + + [[nodiscard]] bool Convert(interop::GPUTextureFormat& out, wgpu::TextureFormat in); + + [[nodiscard]] bool Convert(interop::GPUTextureUsageFlags& out, wgpu::TextureUsage in); + + [[nodiscard]] bool Convert(interop::GPUBufferUsageFlags& out, wgpu::BufferUsage in); + + [[nodiscard]] bool Convert(interop::GPUQueryType& out, wgpu::QueryType in); + // std::string to C string inline bool Convert(const char*& out, const std::string& in) { out = in.c_str(); diff --git a/src/dawn/node/binding/GPUBuffer.cpp b/src/dawn/node/binding/GPUBuffer.cpp index afe0735f68..0d9718d574 100644 --- a/src/dawn/node/binding/GPUBuffer.cpp +++ b/src/dawn/node/binding/GPUBuffer.cpp @@ -163,11 +163,20 @@ void GPUBuffer::destroy(Napi::Env) { } interop::GPUSize64 GPUBuffer::getSize(Napi::Env) { - UNIMPLEMENTED(); + return buffer_.GetSize(); } -interop::GPUBufferUsageFlags GPUBuffer::getUsage(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUBufferUsageFlags GPUBuffer::getUsage(Napi::Env env) { + interop::GPUBufferUsageFlags result; + + Converter conv(env); + if (!conv(result, buffer_.GetUsage())) { + Napi::Error::New(env, "Couldn't convert usage to a JavaScript value.") + .ThrowAsJavaScriptException(); + return {0u}; // Doesn't get used. + } + + return result; } void GPUBuffer::DetachMappings() { diff --git a/src/dawn/node/binding/GPUQuerySet.cpp b/src/dawn/node/binding/GPUQuerySet.cpp index 08a8a4700b..c02bdb6b6a 100644 --- a/src/dawn/node/binding/GPUQuerySet.cpp +++ b/src/dawn/node/binding/GPUQuerySet.cpp @@ -16,6 +16,7 @@ #include +#include "src/dawn/node/binding/Converter.h" #include "src/dawn/node/utils/Debug.h" namespace wgpu::binding { @@ -29,12 +30,21 @@ void GPUQuerySet::destroy(Napi::Env) { query_set_.Destroy(); } -interop::GPUQueryType GPUQuerySet::getType(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUQueryType GPUQuerySet::getType(Napi::Env env) { + interop::GPUQueryType result; + + Converter conv(env); + if (!conv(result, query_set_.GetType())) { + Napi::Error::New(env, "Couldn't convert type to a JavaScript value.") + .ThrowAsJavaScriptException(); + return interop::GPUQueryType::kOcclusion; // Doesn't get used. + } + + return result; } interop::GPUSize32 GPUQuerySet::getCount(Napi::Env) { - UNIMPLEMENTED(); + return query_set_.GetCount(); } std::string GPUQuerySet::getLabel(Napi::Env) { diff --git a/src/dawn/node/binding/GPUTexture.cpp b/src/dawn/node/binding/GPUTexture.cpp index 38aaac598d..24c682d87d 100644 --- a/src/dawn/node/binding/GPUTexture.cpp +++ b/src/dawn/node/binding/GPUTexture.cpp @@ -55,35 +55,62 @@ void GPUTexture::destroy(Napi::Env) { } interop::GPUIntegerCoordinate GPUTexture::getWidth(Napi::Env) { - UNIMPLEMENTED(); + return texture_.GetWidth(); } interop::GPUIntegerCoordinate GPUTexture::getHeight(Napi::Env) { - UNIMPLEMENTED(); + return texture_.GetHeight(); } interop::GPUIntegerCoordinate GPUTexture::getDepthOrArrayLayers(Napi::Env) { - UNIMPLEMENTED(); + return texture_.GetDepthOrArrayLayers(); } interop::GPUIntegerCoordinate GPUTexture::getMipLevelCount(Napi::Env) { - UNIMPLEMENTED(); + return texture_.GetMipLevelCount(); } interop::GPUSize32 GPUTexture::getSampleCount(Napi::Env) { - UNIMPLEMENTED(); + return texture_.GetSampleCount(); } -interop::GPUTextureDimension GPUTexture::getDimension(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUTextureDimension GPUTexture::getDimension(Napi::Env env) { + interop::GPUTextureDimension result; + + Converter conv(env); + if (!conv(result, texture_.GetDimension())) { + Napi::Error::New(env, "Couldn't convert dimension to a JavaScript value.") + .ThrowAsJavaScriptException(); + return interop::GPUTextureDimension::k1D; // Doesn't get used. + } + + return result; } -interop::GPUTextureFormat GPUTexture::getFormat(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUTextureFormat GPUTexture::getFormat(Napi::Env env) { + interop::GPUTextureFormat result; + + Converter conv(env); + if (!conv(result, texture_.GetFormat())) { + Napi::Error::New(env, "Couldn't convert format to a JavaScript value.") + .ThrowAsJavaScriptException(); + return interop::GPUTextureFormat::kR32Float; // Doesn't get used. + } + + return result; } -interop::GPUTextureUsageFlags GPUTexture::getUsage(Napi::Env) { - UNIMPLEMENTED(); +interop::GPUTextureUsageFlags GPUTexture::getUsage(Napi::Env env) { + interop::GPUTextureUsageFlags result; + + Converter conv(env); + if (!conv(result, texture_.GetUsage())) { + Napi::Error::New(env, "Couldn't convert usage to a JavaScript value.") + .ThrowAsJavaScriptException(); + return {0u}; // Doesn't get used. + } + + return result; } std::string GPUTexture::getLabel(Napi::Env) {