dawn_node: Use wgpu::SupportedLimits

Query the Adapter / Device for limits instead of hardcoding them.

Bug: dawn:1143
Change-Id: Ib6d47bdb81df8e5e6eb767d35d346bbf34c5877a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/65722
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/dawn_node/binding/GPUAdapter.cpp b/src/dawn_node/binding/GPUAdapter.cpp
index 82fc7b1..82378d8 100644
--- a/src/dawn_node/binding/GPUAdapter.cpp
+++ b/src/dawn_node/binding/GPUAdapter.cpp
@@ -92,7 +92,43 @@
     }
 
     interop::Interface<interop::GPUSupportedLimits> GPUAdapter::getLimits(Napi::Env env) {
-        return interop::GPUSupportedLimits::Create<GPUSupportedLimits>(env);
+        WGPUSupportedLimits limits{};
+        if (!adapter_.GetLimits(&limits)) {
+            Napi::Error::New(env, "failed to get adapter limits").ThrowAsJavaScriptException();
+        }
+
+        wgpu::SupportedLimits wgpuLimits{};
+
+#define COPY_LIMIT(LIMIT) wgpuLimits.limits.LIMIT = limits.limits.LIMIT
+        COPY_LIMIT(maxTextureDimension1D);
+        COPY_LIMIT(maxTextureDimension2D);
+        COPY_LIMIT(maxTextureDimension3D);
+        COPY_LIMIT(maxTextureArrayLayers);
+        COPY_LIMIT(maxBindGroups);
+        COPY_LIMIT(maxDynamicUniformBuffersPerPipelineLayout);
+        COPY_LIMIT(maxDynamicStorageBuffersPerPipelineLayout);
+        COPY_LIMIT(maxSampledTexturesPerShaderStage);
+        COPY_LIMIT(maxSamplersPerShaderStage);
+        COPY_LIMIT(maxStorageBuffersPerShaderStage);
+        COPY_LIMIT(maxStorageTexturesPerShaderStage);
+        COPY_LIMIT(maxUniformBuffersPerShaderStage);
+        COPY_LIMIT(maxUniformBufferBindingSize);
+        COPY_LIMIT(maxStorageBufferBindingSize);
+        COPY_LIMIT(minUniformBufferOffsetAlignment);
+        COPY_LIMIT(minStorageBufferOffsetAlignment);
+        COPY_LIMIT(maxVertexBuffers);
+        COPY_LIMIT(maxVertexAttributes);
+        COPY_LIMIT(maxVertexBufferArrayStride);
+        COPY_LIMIT(maxInterStageShaderComponents);
+        COPY_LIMIT(maxComputeWorkgroupStorageSize);
+        COPY_LIMIT(maxComputeInvocationsPerWorkgroup);
+        COPY_LIMIT(maxComputeWorkgroupSizeX);
+        COPY_LIMIT(maxComputeWorkgroupSizeY);
+        COPY_LIMIT(maxComputeWorkgroupSizeZ);
+        COPY_LIMIT(maxComputeWorkgroupsPerDimension);
+#undef COPY_LIMIT
+
+        return interop::GPUSupportedLimits::Create<GPUSupportedLimits>(env, wgpuLimits);
     }
 
     bool GPUAdapter::getIsFallbackAdapter(Napi::Env) {
diff --git a/src/dawn_node/binding/GPUDevice.cpp b/src/dawn_node/binding/GPUDevice.cpp
index 73b5e18..0870307 100644
--- a/src/dawn_node/binding/GPUDevice.cpp
+++ b/src/dawn_node/binding/GPUDevice.cpp
@@ -126,7 +126,11 @@
     }
 
     interop::Interface<interop::GPUSupportedLimits> GPUDevice::getLimits(Napi::Env env) {
-        return interop::GPUSupportedLimits::Create<GPUSupportedLimits>(env);
+        wgpu::SupportedLimits limits{};
+        if (!device_.GetLimits(&limits)) {
+            Napi::Error::New(env, "failed to get device limits").ThrowAsJavaScriptException();
+        }
+        return interop::GPUSupportedLimits::Create<GPUSupportedLimits>(env, limits);
     }
 
     interop::Interface<interop::GPUQueue> GPUDevice::getQueue(Napi::Env env) {
diff --git a/src/dawn_node/binding/GPUSupportedLimits.cpp b/src/dawn_node/binding/GPUSupportedLimits.cpp
index 8587031..4a8399b 100644
--- a/src/dawn_node/binding/GPUSupportedLimits.cpp
+++ b/src/dawn_node/binding/GPUSupportedLimits.cpp
@@ -20,112 +20,112 @@
     // wgpu::bindings::GPUSupportedLimits
     ////////////////////////////////////////////////////////////////////////////////
 
-    // Values taken from.
-    // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webgpu/gpu_supported_limits.h
-    // TODO(crbug.com/dawn/1131): Actually use limits reported by the device / adapter.
+    GPUSupportedLimits::GPUSupportedLimits(wgpu::SupportedLimits limits)
+        : limits_(std::move(limits)) {
+    }
 
     uint32_t GPUSupportedLimits::getMaxTextureDimension1D(Napi::Env) {
-        return 8192;
+        return limits_.limits.maxTextureDimension1D;
     }
 
     uint32_t GPUSupportedLimits::getMaxTextureDimension2D(Napi::Env) {
-        return 8192;
+        return limits_.limits.maxTextureDimension2D;
     }
 
     uint32_t GPUSupportedLimits::getMaxTextureDimension3D(Napi::Env) {
-        return 2048;
+        return limits_.limits.maxTextureDimension3D;
     }
 
     uint32_t GPUSupportedLimits::getMaxTextureArrayLayers(Napi::Env) {
-        return 2048;
+        return limits_.limits.maxTextureArrayLayers;
     }
 
     uint32_t GPUSupportedLimits::getMaxBindGroups(Napi::Env) {
-        return 4;
+        return limits_.limits.maxBindGroups;
     }
 
     uint32_t GPUSupportedLimits::getMaxDynamicUniformBuffersPerPipelineLayout(Napi::Env) {
-        return 8;
+        return limits_.limits.maxDynamicUniformBuffersPerPipelineLayout;
     }
 
     uint32_t GPUSupportedLimits::getMaxDynamicStorageBuffersPerPipelineLayout(Napi::Env) {
-        return 4;
+        return limits_.limits.maxDynamicStorageBuffersPerPipelineLayout;
     }
 
     uint32_t GPUSupportedLimits::getMaxSampledTexturesPerShaderStage(Napi::Env) {
-        return 16;
+        return limits_.limits.maxSampledTexturesPerShaderStage;
     }
 
     uint32_t GPUSupportedLimits::getMaxSamplersPerShaderStage(Napi::Env) {
-        return 16;
+        return limits_.limits.maxSamplersPerShaderStage;
     }
 
     uint32_t GPUSupportedLimits::getMaxStorageBuffersPerShaderStage(Napi::Env) {
-        return 4;
+        return limits_.limits.maxStorageBuffersPerShaderStage;
     }
 
     uint32_t GPUSupportedLimits::getMaxStorageTexturesPerShaderStage(Napi::Env) {
-        return 4;
+        return limits_.limits.maxStorageTexturesPerShaderStage;
     }
 
     uint32_t GPUSupportedLimits::getMaxUniformBuffersPerShaderStage(Napi::Env) {
-        return 12;
+        return limits_.limits.maxUniformBuffersPerShaderStage;
     }
 
     uint64_t GPUSupportedLimits::getMaxUniformBufferBindingSize(Napi::Env) {
-        return 16384;
+        return limits_.limits.maxUniformBufferBindingSize;
     }
 
     uint64_t GPUSupportedLimits::getMaxStorageBufferBindingSize(Napi::Env) {
-        return 134217728;
+        return limits_.limits.maxStorageBufferBindingSize;
     }
 
     uint32_t GPUSupportedLimits::getMinUniformBufferOffsetAlignment(Napi::Env) {
-        return 256;
+        return limits_.limits.minUniformBufferOffsetAlignment;
     }
 
     uint32_t GPUSupportedLimits::getMinStorageBufferOffsetAlignment(Napi::Env) {
-        return 256;
+        return limits_.limits.minStorageBufferOffsetAlignment;
     }
 
     uint32_t GPUSupportedLimits::getMaxVertexBuffers(Napi::Env) {
-        return 8;
+        return limits_.limits.maxVertexBuffers;
     }
 
     uint32_t GPUSupportedLimits::getMaxVertexAttributes(Napi::Env) {
-        return 16;
+        return limits_.limits.maxVertexAttributes;
     }
 
     uint32_t GPUSupportedLimits::getMaxVertexBufferArrayStride(Napi::Env) {
-        return 2048;
+        return limits_.limits.maxVertexBufferArrayStride;
     }
 
     uint32_t GPUSupportedLimits::getMaxInterStageShaderComponents(Napi::Env) {
-        return 60;
+        return limits_.limits.maxInterStageShaderComponents;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeWorkgroupStorageSize(Napi::Env) {
-        return 16352;
+        return limits_.limits.maxComputeWorkgroupStorageSize;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeInvocationsPerWorkgroup(Napi::Env) {
-        return 256;
+        return limits_.limits.maxComputeInvocationsPerWorkgroup;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeWorkgroupSizeX(Napi::Env) {
-        return 256;
+        return limits_.limits.maxComputeWorkgroupSizeX;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeWorkgroupSizeY(Napi::Env) {
-        return 256;
+        return limits_.limits.maxComputeWorkgroupSizeY;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeWorkgroupSizeZ(Napi::Env) {
-        return 64;
+        return limits_.limits.maxComputeWorkgroupSizeZ;
     }
 
     uint32_t GPUSupportedLimits::getMaxComputeWorkgroupsPerDimension(Napi::Env) {
-        return 65535;
+        return limits_.limits.maxComputeWorkgroupsPerDimension;
     }
 
 }}  // namespace wgpu::binding
diff --git a/src/dawn_node/binding/GPUSupportedLimits.h b/src/dawn_node/binding/GPUSupportedLimits.h
index b50753d..faed813 100644
--- a/src/dawn_node/binding/GPUSupportedLimits.h
+++ b/src/dawn_node/binding/GPUSupportedLimits.h
@@ -25,6 +25,8 @@
     // GPUSupportedLimits is an implementation of interop::GPUSupportedLimits.
     class GPUSupportedLimits final : public interop::GPUSupportedLimits {
       public:
+        GPUSupportedLimits(wgpu::SupportedLimits);
+
         // interop::GPUSupportedLimits interface compliance
         uint32_t getMaxTextureDimension1D(Napi::Env) override;
         uint32_t getMaxTextureDimension2D(Napi::Env) override;
@@ -52,6 +54,9 @@
         uint32_t getMaxComputeWorkgroupSizeY(Napi::Env) override;
         uint32_t getMaxComputeWorkgroupSizeZ(Napi::Env) override;
         uint32_t getMaxComputeWorkgroupsPerDimension(Napi::Env) override;
+
+      private:
+        wgpu::SupportedLimits limits_;
     };
 
 }}  // namespace wgpu::binding