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 "mozilla/Scoped.h"
10 #include "mozilla/dom/TypedArray.h"
11 #include "ObjectModel.h"
12 
13 namespace mozilla {
14 class ErrorResult;
15 
16 namespace dom {
17 class DoubleSequenceOrGPUColorDict;
18 struct GPURenderPassDescriptor;
19 template <typename T>
20 class Sequence;
21 namespace binding_detail {
22 template <typename T>
23 class AutoSequence;
24 }  // namespace binding_detail
25 }  // namespace dom
26 namespace webgpu {
27 namespace ffi {
28 struct WGPURenderPass;
29 }  // namespace ffi
30 
31 class BindGroup;
32 class Buffer;
33 class CommandEncoder;
34 class RenderBundle;
35 class RenderPipeline;
36 class TextureView;
37 
38 struct ScopedFfiRenderTraits {
39   typedef ffi::WGPURenderPass* type;
40   static type empty();
41   static void release(type raw);
42 };
43 
44 class RenderPassEncoder final : 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,
51                     const dom::GPURenderPassDescriptor& aDesc);
52 
53  protected:
54   virtual ~RenderPassEncoder();
Cleanup()55   void Cleanup() {}
56 
57   Scoped<ScopedFfiRenderTraits> mPass;
58   // keep all the used objects alive while the pass is recorded
59   nsTArray<RefPtr<const BindGroup>> mUsedBindGroups;
60   nsTArray<RefPtr<const Buffer>> mUsedBuffers;
61   nsTArray<RefPtr<const RenderPipeline>> mUsedPipelines;
62   nsTArray<RefPtr<const TextureView>> mUsedTextureViews;
63   nsTArray<RefPtr<const RenderBundle>> mUsedRenderBundles;
64 
65  public:
66   // programmable pass encoder
67   void SetBindGroup(uint32_t aSlot, const BindGroup& aBindGroup,
68                     const dom::Sequence<uint32_t>& aDynamicOffsets);
69   // render encoder base
70   void SetPipeline(const RenderPipeline& aPipeline);
71   void SetIndexBuffer(const Buffer& aBuffer,
72                       const dom::GPUIndexFormat& aIndexFormat, uint64_t aOffset,
73                       uint64_t aSize);
74   void SetVertexBuffer(uint32_t aSlot, const Buffer& aBuffer, uint64_t aOffset,
75                        uint64_t aSize);
76   void Draw(uint32_t aVertexCount, uint32_t aInstanceCount,
77             uint32_t aFirstVertex, uint32_t aFirstInstance);
78   void DrawIndexed(uint32_t aIndexCount, uint32_t aInstanceCount,
79                    uint32_t aFirstIndex, int32_t aBaseVertex,
80                    uint32_t aFirstInstance);
81   void DrawIndirect(const Buffer& aIndirectBuffer, uint64_t aIndirectOffset);
82   void DrawIndexedIndirect(const Buffer& aIndirectBuffer,
83                            uint64_t aIndirectOffset);
84   // self
85   void SetViewport(float x, float y, float width, float height, float minDepth,
86                    float maxDepth);
87   void SetScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
88   void SetBlendConstant(const dom::DoubleSequenceOrGPUColorDict& color);
89   void SetStencilReference(uint32_t reference);
90   void ExecuteBundles(
91       const dom::Sequence<OwningNonNull<RenderBundle>>& aBundles);
92   void EndPass(ErrorResult& aRv);
93 };
94 
95 }  // namespace webgpu
96 }  // namespace mozilla
97 
98 #endif  // GPU_RenderPassEncoder_H_
99