RenderPassEncoder.h (4521B)
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_RenderPassEncoder_H_ 7 #define GPU_RenderPassEncoder_H_ 8 9 #include "CanvasContext.h" 10 #include "ObjectModel.h" 11 #include "mozilla/dom/TypedArray.h" 12 13 namespace mozilla { 14 class ErrorResult; 15 16 namespace dom { 17 class DoubleSequenceOrGPUColorDict; 18 enum class GPUIndexFormat : uint8_t; 19 struct GPURenderPassDescriptor; 20 template <typename T> 21 class Sequence; 22 namespace binding_detail { 23 template <typename T> 24 class AutoSequence; 25 } // namespace binding_detail 26 } // namespace dom 27 namespace webgpu { 28 namespace ffi { 29 struct WGPURecordedRenderPass; 30 } // namespace ffi 31 32 class BindGroup; 33 class Buffer; 34 class CommandEncoder; 35 class RenderBundle; 36 class RenderPipeline; 37 class TextureView; 38 39 struct ffiWGPURenderPassDeleter { 40 void operator()(ffi::WGPURecordedRenderPass*); 41 }; 42 43 class RenderPassEncoder final : public nsWrapperCache, 44 public ObjectBase, 45 public ChildOf<CommandEncoder> { 46 public: 47 GPU_DECL_CYCLE_COLLECTION(RenderPassEncoder) 48 GPU_DECL_JS_WRAP(RenderPassEncoder) 49 50 RenderPassEncoder(CommandEncoder* const aParent, RawId aId, 51 const dom::GPURenderPassDescriptor& aDesc); 52 53 protected: 54 virtual ~RenderPassEncoder(); 55 56 std::unique_ptr<ffi::WGPURecordedRenderPass, ffiWGPURenderPassDeleter> mPass; 57 // keep all the used objects alive while the pass is recorded 58 nsTArray<RefPtr<const BindGroup>> mUsedBindGroups; 59 nsTArray<RefPtr<const Buffer>> mUsedBuffers; 60 nsTArray<RefPtr<const RenderPipeline>> mUsedPipelines; 61 nsTArray<RefPtr<const TextureView>> mUsedTextureViews; 62 nsTArray<RefPtr<const RenderBundle>> mUsedRenderBundles; 63 64 // The canvas contexts of any canvas textures used in bind groups of this 65 // render pass. 66 CanvasContextArray mUsedCanvasContexts; 67 68 // programmable pass encoder 69 private: 70 bool mValid = true; 71 72 void SetBindGroup(uint32_t aSlot, BindGroup* const aBindGroup, 73 const uint32_t* aDynamicOffsets, 74 size_t aDynamicOffsetsLength); 75 76 public: 77 void Invalidate() { mValid = false; } 78 79 void SetBindGroup(uint32_t aSlot, BindGroup* const aBindGroup, 80 const dom::Sequence<uint32_t>& aDynamicOffsets, 81 ErrorResult& aRv); 82 void SetBindGroup(uint32_t aSlot, BindGroup* const aBindGroup, 83 const dom::Uint32Array& aDynamicOffsetsData, 84 uint64_t aDynamicOffsetsDataStart, 85 uint64_t aDynamicOffsetsDataLength, ErrorResult& aRv); 86 // render encoder base 87 void SetPipeline(const RenderPipeline& aPipeline); 88 void SetIndexBuffer(const Buffer& aBuffer, 89 const dom::GPUIndexFormat& aIndexFormat, uint64_t aOffset, 90 const dom::Optional<uint64_t>& aSize); 91 void SetVertexBuffer(uint32_t aSlot, const Buffer& aBuffer, uint64_t aOffset, 92 const dom::Optional<uint64_t>& aSize); 93 void Draw(uint32_t aVertexCount, uint32_t aInstanceCount, 94 uint32_t aFirstVertex, uint32_t aFirstInstance); 95 void DrawIndexed(uint32_t aIndexCount, uint32_t aInstanceCount, 96 uint32_t aFirstIndex, int32_t aBaseVertex, 97 uint32_t aFirstInstance); 98 void DrawIndirect(const Buffer& aIndirectBuffer, uint64_t aIndirectOffset); 99 void DrawIndexedIndirect(const Buffer& aIndirectBuffer, 100 uint64_t aIndirectOffset); 101 // self 102 void SetViewport(float x, float y, float width, float height, float minDepth, 103 float maxDepth); 104 void SetScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height); 105 void SetBlendConstant(const dom::DoubleSequenceOrGPUColorDict& color); 106 void SetStencilReference(uint32_t reference); 107 108 void BeginOcclusionQuery(uint32_t queryIndex); 109 void EndOcclusionQuery(); 110 111 void PushDebugGroup(const nsAString& aString); 112 void PopDebugGroup(); 113 void InsertDebugMarker(const nsAString& aString); 114 115 void ExecuteBundles( 116 const dom::Sequence<OwningNonNull<RenderBundle>>& aBundles); 117 118 void End(); 119 120 // helpers not defined by WebGPU 121 mozilla::Span<const WeakPtr<CanvasContext>> GetCanvasContexts() const { 122 return mUsedCanvasContexts; 123 } 124 }; 125 126 } // namespace webgpu 127 } // namespace mozilla 128 129 #endif // GPU_RenderPassEncoder_H_