CommandEncoder.h (5316B)
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_CommandEncoder_H_ 7 #define GPU_CommandEncoder_H_ 8 9 #include "CanvasContext.h" 10 #include "ObjectModel.h" 11 #include "QuerySet.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/Span.h" 14 #include "mozilla/WeakPtr.h" 15 #include "mozilla/dom/TypedArray.h" 16 #include "mozilla/webgpu/WebGPUTypes.h" 17 #include "mozilla/webgpu/ffi/wgpu.h" 18 #include "nsTArrayForwardDeclare.h" 19 #include "nsWrapperCache.h" 20 21 namespace mozilla { 22 class ErrorResult; 23 24 namespace dom { 25 struct GPUComputePassDescriptor; 26 template <typename T> 27 class Sequence; 28 struct GPUCommandBufferDescriptor; 29 class GPUComputePipelineOrGPURenderPipeline; 30 class RangeEnforcedUnsignedLongSequenceOrGPUExtent3DDict; 31 struct GPUTexelCopyBufferInfo; 32 struct GPUTexelCopyTextureInfo; 33 struct GPUImageBitmapCopyView; 34 struct GPUTexelCopyBufferLayout; 35 struct GPURenderPassDescriptor; 36 using GPUExtent3D = RangeEnforcedUnsignedLongSequenceOrGPUExtent3DDict; 37 } // namespace dom 38 namespace webgpu { 39 40 class BindGroup; 41 class Buffer; 42 class CanvasContext; 43 class CommandBuffer; 44 class ComputePassEncoder; 45 class Device; 46 class ExternalTexture; 47 class RenderPassEncoder; 48 class WebGPUChild; 49 50 enum class CommandEncoderState { Open, Locked, Ended }; 51 52 class CommandEncoder final : public nsWrapperCache, 53 public ObjectBase, 54 public ChildOf<Device> { 55 public: 56 GPU_DECL_CYCLE_COLLECTION(CommandEncoder) 57 GPU_DECL_JS_WRAP(CommandEncoder) 58 59 CommandEncoder(Device* const aParent, RawId aId); 60 61 static void ConvertTextureDataLayoutToFFI( 62 const dom::GPUTexelCopyBufferLayout& aLayout, 63 ffi::WGPUTexelCopyBufferLayout* aLayoutFFI); 64 static void ConvertTextureCopyViewToFFI( 65 const dom::GPUTexelCopyTextureInfo& aCopy, 66 ffi::WGPUTexelCopyTextureInfo_TextureId* aViewFFI); 67 68 private: 69 virtual ~CommandEncoder(); 70 71 CommandEncoderState mState; 72 73 CanvasContextArray mPresentationContexts; 74 nsTArray<RefPtr<ExternalTexture>> mExternalTextures; 75 76 void TrackPresentationContext(WeakPtr<CanvasContext> aTargetContext); 77 78 public: 79 const auto& GetDevice() const { return mParent; }; 80 81 CommandEncoderState GetState() const { return mState; }; 82 83 void EndComputePass(ffi::WGPURecordedComputePass& aPass, 84 CanvasContextArray& aCanvasContexts, 85 Span<RefPtr<ExternalTexture>> aExternalTextures); 86 void EndRenderPass(ffi::WGPURecordedRenderPass& aPass, 87 CanvasContextArray& aCanvasContexts, 88 Span<RefPtr<ExternalTexture>> aExternalTextures); 89 90 void CopyBufferToBuffer(const Buffer& aSource, const Buffer& aDestination, 91 const dom::Optional<BufferAddress>& aSize) { 92 this->CopyBufferToBuffer(aSource, 0, aDestination, 0, aSize); 93 } 94 void CopyBufferToBuffer(const Buffer& aSource, BufferAddress aSourceOffset, 95 const Buffer& aDestination, 96 BufferAddress aDestinationOffset, 97 const dom::Optional<BufferAddress>& aSize); 98 void CopyBufferToTexture(const dom::GPUTexelCopyBufferInfo& aSource, 99 const dom::GPUTexelCopyTextureInfo& aDestination, 100 const dom::GPUExtent3D& aCopySize); 101 void CopyTextureToBuffer(const dom::GPUTexelCopyTextureInfo& aSource, 102 const dom::GPUTexelCopyBufferInfo& aDestination, 103 const dom::GPUExtent3D& aCopySize); 104 void CopyTextureToTexture(const dom::GPUTexelCopyTextureInfo& aSource, 105 const dom::GPUTexelCopyTextureInfo& aDestination, 106 const dom::GPUExtent3D& aCopySize); 107 void ClearBuffer(const Buffer& aBuffer, const uint64_t aOffset, 108 const dom::Optional<uint64_t>& aSize); 109 110 void PushDebugGroup(const nsAString& aString); 111 void PopDebugGroup(); 112 void InsertDebugMarker(const nsAString& aString); 113 114 already_AddRefed<ComputePassEncoder> BeginComputePass( 115 const dom::GPUComputePassDescriptor& aDesc); 116 already_AddRefed<RenderPassEncoder> BeginRenderPass( 117 const dom::GPURenderPassDescriptor& aDesc); 118 void ResolveQuerySet(QuerySet& aQuerySet, uint32_t aFirstQuery, 119 uint32_t aQueryCount, webgpu::Buffer& aDestination, 120 uint64_t aDestinationOffset); 121 already_AddRefed<CommandBuffer> Finish( 122 const dom::GPUCommandBufferDescriptor& aDesc); 123 }; 124 125 template <typename T> 126 void AssignPassTimestampWrites(const T& src, 127 ffi::WGPUPassTimestampWrites& dest) { 128 if (src.mBeginningOfPassWriteIndex.WasPassed()) { 129 dest.beginning_of_pass_write_index = 130 &src.mBeginningOfPassWriteIndex.Value(); 131 } else { 132 dest.beginning_of_pass_write_index = nullptr; 133 } 134 135 if (src.mEndOfPassWriteIndex.WasPassed()) { 136 dest.end_of_pass_write_index = &src.mEndOfPassWriteIndex.Value(); 137 } else { 138 dest.end_of_pass_write_index = nullptr; 139 } 140 141 dest.query_set = src.mQuerySet->GetId(); 142 } 143 144 } // namespace webgpu 145 } // namespace mozilla 146 147 #endif // GPU_CommandEncoder_H_