1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef GPU_DEVICE_H_ 7 #define GPU_DEVICE_H_ 8 9 #include "mozilla/MozPromise.h" 10 #include "mozilla/RefPtr.h" 11 #include "mozilla/webgpu/WebGPUTypes.h" 12 #include "mozilla/webrender/WebRenderAPI.h" 13 #include "mozilla/DOMEventTargetHelper.h" 14 15 namespace mozilla { 16 namespace dom { 17 struct GPUExtensions; 18 struct GPUFeatures; 19 struct GPULimits; 20 struct GPUExtent3DDict; 21 22 struct GPUBufferDescriptor; 23 struct GPUTextureDescriptor; 24 struct GPUSamplerDescriptor; 25 struct GPUBindGroupLayoutDescriptor; 26 struct GPUPipelineLayoutDescriptor; 27 struct GPUBindGroupDescriptor; 28 struct GPUBlendStateDescriptor; 29 struct GPUDepthStencilStateDescriptor; 30 struct GPUInputStateDescriptor; 31 struct GPUShaderModuleDescriptor; 32 struct GPUAttachmentStateDescriptor; 33 struct GPUComputePipelineDescriptor; 34 struct GPURenderBundleEncoderDescriptor; 35 struct GPURenderPipelineDescriptor; 36 struct GPUCommandEncoderDescriptor; 37 struct GPUSwapChainDescriptor; 38 39 class EventHandlerNonNull; 40 class Promise; 41 template <typename T> 42 class Sequence; 43 class GPUBufferOrGPUTexture; 44 enum class GPUErrorFilter : uint8_t; 45 class GPULogCallback; 46 } // namespace dom 47 namespace ipc { 48 enum class ResponseRejectReason; 49 class Shmem; 50 } // namespace ipc 51 52 namespace webgpu { 53 class Adapter; 54 class BindGroup; 55 class BindGroupLayout; 56 class Buffer; 57 class CommandEncoder; 58 class ComputePipeline; 59 class Fence; 60 class InputState; 61 class PipelineLayout; 62 class Queue; 63 class RenderBundleEncoder; 64 class RenderPipeline; 65 class Sampler; 66 class ShaderModule; 67 class Texture; 68 class WebGPUChild; 69 70 typedef MozPromise<ipc::Shmem, ipc::ResponseRejectReason, true> MappingPromise; 71 72 class Device final : public DOMEventTargetHelper { 73 public: 74 NS_DECL_ISUPPORTS_INHERITED 75 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Device, DOMEventTargetHelper) 76 GPU_DECL_JS_WRAP(Device) 77 78 explicit Device(Adapter* const aParent, RawId aId); 79 80 RefPtr<WebGPUChild> GetBridge(); 81 static JSObject* CreateExternalArrayBuffer(JSContext* aCx, size_t aSize, 82 ipc::Shmem& aShmem); 83 RefPtr<MappingPromise> MapBufferForReadAsync(RawId aId, size_t aSize, 84 ErrorResult& aRv); 85 void UnmapBuffer(RawId aId, UniquePtr<ipc::Shmem> aShmem, bool aFlush); 86 already_AddRefed<Texture> InitSwapChain( 87 const dom::GPUSwapChainDescriptor& aDesc, 88 const dom::GPUExtent3DDict& aExtent3D, 89 wr::ExternalImageId aExternalImageId, gfx::SurfaceFormat aFormat); 90 91 private: 92 ~Device(); 93 void Cleanup(); 94 95 RefPtr<WebGPUChild> mBridge; 96 const RawId mId; 97 bool mValid = true; 98 nsString mLabel; 99 RefPtr<Queue> mQueue; 100 101 public: 102 void GetLabel(nsAString& aValue) const; 103 void SetLabel(const nsAString& aLabel); 104 105 Queue* DefaultQueue() const; 106 107 already_AddRefed<Buffer> CreateBuffer(const dom::GPUBufferDescriptor& aDesc); 108 void CreateBufferMapped(JSContext* aCx, const dom::GPUBufferDescriptor& aDesc, 109 nsTArray<JS::Value>& aSequence, ErrorResult& aRv); 110 111 already_AddRefed<Texture> CreateTexture( 112 const dom::GPUTextureDescriptor& aDesc); 113 already_AddRefed<Sampler> CreateSampler( 114 const dom::GPUSamplerDescriptor& aDesc); 115 116 already_AddRefed<CommandEncoder> CreateCommandEncoder( 117 const dom::GPUCommandEncoderDescriptor& aDesc); 118 119 already_AddRefed<BindGroupLayout> CreateBindGroupLayout( 120 const dom::GPUBindGroupLayoutDescriptor& aDesc); 121 already_AddRefed<PipelineLayout> CreatePipelineLayout( 122 const dom::GPUPipelineLayoutDescriptor& aDesc); 123 already_AddRefed<BindGroup> CreateBindGroup( 124 const dom::GPUBindGroupDescriptor& aDesc); 125 126 already_AddRefed<ShaderModule> CreateShaderModule( 127 const dom::GPUShaderModuleDescriptor& aDesc); 128 already_AddRefed<ComputePipeline> CreateComputePipeline( 129 const dom::GPUComputePipelineDescriptor& aDesc); 130 already_AddRefed<RenderPipeline> CreateRenderPipeline( 131 const dom::GPURenderPipelineDescriptor& aDesc); 132 133 // IMPL_EVENT_HANDLER(uncapturederror) 134 }; 135 136 } // namespace webgpu 137 } // namespace mozilla 138 139 #endif // GPU_DEVICE_H_ 140