1 // Copyright 2021 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWN_NODE_BINDING_GPUDEVICE_H_ 16 #define DAWN_NODE_BINDING_GPUDEVICE_H_ 17 18 #include "dawn/webgpu_cpp.h" 19 #include "napi.h" 20 #include "src/dawn_node/binding/AsyncRunner.h" 21 #include "src/dawn_node/interop/WebGPU.h" 22 23 namespace wgpu { namespace binding { 24 // GPUDevice is an implementation of interop::GPUDevice that wraps a wgpu::Device. 25 class GPUDevice final : public interop::GPUDevice { 26 public: 27 GPUDevice(Napi::Env env, wgpu::Device device); 28 ~GPUDevice(); 29 30 // interop::GPUDevice interface compliance 31 interop::Interface<interop::GPUSupportedFeatures> getFeatures(Napi::Env) override; 32 interop::Interface<interop::GPUSupportedLimits> getLimits(Napi::Env) override; 33 interop::Interface<interop::GPUQueue> getQueue(Napi::Env env) override; 34 void destroy(Napi::Env) override; 35 interop::Interface<interop::GPUBuffer> createBuffer( 36 Napi::Env env, 37 interop::GPUBufferDescriptor descriptor) override; 38 interop::Interface<interop::GPUTexture> createTexture( 39 Napi::Env, 40 interop::GPUTextureDescriptor descriptor) override; 41 interop::Interface<interop::GPUSampler> createSampler( 42 Napi::Env, 43 interop::GPUSamplerDescriptor descriptor) override; 44 interop::Interface<interop::GPUExternalTexture> importExternalTexture( 45 Napi::Env, 46 interop::GPUExternalTextureDescriptor descriptor) override; 47 interop::Interface<interop::GPUBindGroupLayout> createBindGroupLayout( 48 Napi::Env, 49 interop::GPUBindGroupLayoutDescriptor descriptor) override; 50 interop::Interface<interop::GPUPipelineLayout> createPipelineLayout( 51 Napi::Env, 52 interop::GPUPipelineLayoutDescriptor descriptor) override; 53 interop::Interface<interop::GPUBindGroup> createBindGroup( 54 Napi::Env, 55 interop::GPUBindGroupDescriptor descriptor) override; 56 interop::Interface<interop::GPUShaderModule> createShaderModule( 57 Napi::Env, 58 interop::GPUShaderModuleDescriptor descriptor) override; 59 interop::Interface<interop::GPUComputePipeline> createComputePipeline( 60 Napi::Env, 61 interop::GPUComputePipelineDescriptor descriptor) override; 62 interop::Interface<interop::GPURenderPipeline> createRenderPipeline( 63 Napi::Env, 64 interop::GPURenderPipelineDescriptor descriptor) override; 65 interop::Promise<interop::Interface<interop::GPUComputePipeline>> 66 createComputePipelineAsync(Napi::Env env, 67 interop::GPUComputePipelineDescriptor descriptor) override; 68 interop::Promise<interop::Interface<interop::GPURenderPipeline>> createRenderPipelineAsync( 69 Napi::Env env, 70 interop::GPURenderPipelineDescriptor descriptor) override; 71 interop::Interface<interop::GPUCommandEncoder> createCommandEncoder( 72 Napi::Env env, 73 interop::GPUCommandEncoderDescriptor descriptor) override; 74 interop::Interface<interop::GPURenderBundleEncoder> createRenderBundleEncoder( 75 Napi::Env, 76 interop::GPURenderBundleEncoderDescriptor descriptor) override; 77 interop::Interface<interop::GPUQuerySet> createQuerySet( 78 Napi::Env, 79 interop::GPUQuerySetDescriptor descriptor) override; 80 interop::Promise<interop::Interface<interop::GPUDeviceLostInfo>> getLost( 81 Napi::Env env) override; 82 void pushErrorScope(Napi::Env, interop::GPUErrorFilter filter) override; 83 interop::Promise<std::optional<interop::GPUError>> popErrorScope(Napi::Env env) override; 84 std::optional<std::string> getLabel(Napi::Env) override; 85 void setLabel(Napi::Env, std::optional<std::string> value) override; 86 interop::Interface<interop::EventHandler> getOnuncapturederror(Napi::Env) override; 87 void setOnuncapturederror(Napi::Env, 88 interop::Interface<interop::EventHandler> value) override; 89 void addEventListener( 90 Napi::Env, 91 std::string type, 92 std::optional<interop::Interface<interop::EventListener>> callback, 93 std::optional<std::variant<interop::AddEventListenerOptions, bool>> options) override; 94 void removeEventListener( 95 Napi::Env, 96 std::string type, 97 std::optional<interop::Interface<interop::EventListener>> callback, 98 std::optional<std::variant<interop::EventListenerOptions, bool>> options) override; 99 bool dispatchEvent(Napi::Env, interop::Interface<interop::Event> event) override; 100 101 private: 102 void QueueTick(); 103 104 Napi::Env env_; 105 wgpu::Device device_; 106 std::shared_ptr<AsyncRunner> async_; 107 std::vector<interop::Promise<interop::Interface<interop::GPUDeviceLostInfo>>> 108 lost_promises_; 109 }; 110 111 }} // namespace wgpu::binding 112 113 #endif // DAWN_NODE_BINDING_GPUDEVICE_H_ 114