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_RenderBundleEncoder_H_
7 #define GPU_RenderBundleEncoder_H_
8 
9 #include "mozilla/Scoped.h"
10 #include "mozilla/dom/TypedArray.h"
11 #include "ObjectModel.h"
12 
13 namespace mozilla {
14 namespace webgpu {
15 namespace ffi {
16 struct WGPURenderBundleEncoder;
17 }  // namespace ffi
18 
19 class Device;
20 class RenderBundle;
21 
22 struct ScopedFfiBundleTraits {
23   typedef ffi::WGPURenderBundleEncoder* type;
24   static type empty();
25   static void release(type raw);
26 };
27 
28 class RenderBundleEncoder final : public ObjectBase, public ChildOf<Device> {
29  public:
30   GPU_DECL_CYCLE_COLLECTION(RenderBundleEncoder)
31   GPU_DECL_JS_WRAP(RenderBundleEncoder)
32 
33   RenderBundleEncoder(Device* const aParent, WebGPUChild* const aBridge,
34                       const dom::GPURenderBundleEncoderDescriptor& aDesc);
35 
36  private:
37   ~RenderBundleEncoder();
38   void Cleanup();
39 
40   Scoped<ScopedFfiBundleTraits> mEncoder;
41   // keep all the used objects alive while the encoder is finished
42   nsTArray<RefPtr<const BindGroup>> mUsedBindGroups;
43   nsTArray<RefPtr<const Buffer>> mUsedBuffers;
44   nsTArray<RefPtr<const RenderPipeline>> mUsedPipelines;
45   nsTArray<RefPtr<const TextureView>> mUsedTextureViews;
46 
47  public:
48   // programmable pass encoder
49   void SetBindGroup(uint32_t aSlot, const BindGroup& aBindGroup,
50                     const dom::Sequence<uint32_t>& aDynamicOffsets);
51   // render encoder base
52   void SetPipeline(const RenderPipeline& aPipeline);
53   void SetIndexBuffer(const Buffer& aBuffer,
54                       const dom::GPUIndexFormat& aIndexFormat, uint64_t aOffset,
55                       uint64_t aSize);
56   void SetVertexBuffer(uint32_t aSlot, const Buffer& aBuffer, uint64_t aOffset,
57                        uint64_t aSize);
58   void Draw(uint32_t aVertexCount, uint32_t aInstanceCount,
59             uint32_t aFirstVertex, uint32_t aFirstInstance);
60   void DrawIndexed(uint32_t aIndexCount, uint32_t aInstanceCount,
61                    uint32_t aFirstIndex, int32_t aBaseVertex,
62                    uint32_t aFirstInstance);
63   void DrawIndirect(const Buffer& aIndirectBuffer, uint64_t aIndirectOffset);
64   void DrawIndexedIndirect(const Buffer& aIndirectBuffer,
65                            uint64_t aIndirectOffset);
66   // self
67   already_AddRefed<RenderBundle> Finish(
68       const dom::GPURenderBundleDescriptor& aDesc);
69 };
70 
71 }  // namespace webgpu
72 }  // namespace mozilla
73 
74 #endif  // GPU_RenderBundleEncoder_H_
75