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 #include "mozilla/dom/WebGPUBinding.h"
7 #include "RenderBundleEncoder.h"
8 
9 #include "BindGroup.h"
10 #include "Buffer.h"
11 #include "RenderBundle.h"
12 #include "RenderPipeline.h"
13 #include "ipc/WebGPUChild.h"
14 #include "mozilla/webgpu/ffi/wgpu.h"
15 
16 namespace mozilla {
17 namespace webgpu {
18 
GPU_IMPL_CYCLE_COLLECTION(RenderBundleEncoder,mParent,mUsedBindGroups,mUsedBuffers,mUsedPipelines,mUsedTextureViews)19 GPU_IMPL_CYCLE_COLLECTION(RenderBundleEncoder, mParent, mUsedBindGroups,
20                           mUsedBuffers, mUsedPipelines, mUsedTextureViews)
21 GPU_IMPL_JS_WRAP(RenderBundleEncoder)
22 
23 ffi::WGPURenderBundleEncoder* ScopedFfiBundleTraits::empty() { return nullptr; }
24 
release(ffi::WGPURenderBundleEncoder * raw)25 void ScopedFfiBundleTraits::release(ffi::WGPURenderBundleEncoder* raw) {
26   if (raw) {
27     ffi::wgpu_render_bundle_encoder_destroy(raw);
28   }
29 }
30 
CreateRenderBundleEncoder(RawId aDeviceId,const dom::GPURenderBundleEncoderDescriptor & aDesc)31 ffi::WGPURenderBundleEncoder* CreateRenderBundleEncoder(
32     RawId aDeviceId, const dom::GPURenderBundleEncoderDescriptor& aDesc) {
33   ffi::WGPURenderBundleEncoderDescriptor desc = {};
34   desc.sample_count = aDesc.mSampleCount;
35 
36   nsCString label;
37   if (aDesc.mLabel.WasPassed()) {
38     LossyCopyUTF16toASCII(aDesc.mLabel.Value(), label);
39     desc.label = label.get();
40   }
41 
42   ffi::WGPUTextureFormat depthStencilFormat = ffi::WGPUTextureFormat_Sentinel;
43   if (aDesc.mDepthStencilFormat.WasPassed()) {
44     WebGPUChild::ConvertTextureFormatRef(aDesc.mDepthStencilFormat.Value(),
45                                          depthStencilFormat);
46     desc.depth_stencil_format = &depthStencilFormat;
47   }
48 
49   std::vector<ffi::WGPUTextureFormat> colorFormats = {};
50   for (const auto i : IntegerRange(aDesc.mColorFormats.Length())) {
51     ffi::WGPUTextureFormat format = ffi::WGPUTextureFormat_Sentinel;
52     WebGPUChild::ConvertTextureFormatRef(aDesc.mColorFormats[i], format);
53     colorFormats.push_back(format);
54   }
55 
56   desc.color_formats = colorFormats.data();
57   desc.color_formats_length = colorFormats.size();
58 
59   return ffi::wgpu_device_create_render_bundle_encoder(aDeviceId, &desc);
60 }
61 
RenderBundleEncoder(Device * const aParent,WebGPUChild * const aBridge,const dom::GPURenderBundleEncoderDescriptor & aDesc)62 RenderBundleEncoder::RenderBundleEncoder(
63     Device* const aParent, WebGPUChild* const aBridge,
64     const dom::GPURenderBundleEncoderDescriptor& aDesc)
65     : ChildOf(aParent),
66       mEncoder(CreateRenderBundleEncoder(aParent->mId, aDesc)) {}
67 
~RenderBundleEncoder()68 RenderBundleEncoder::~RenderBundleEncoder() { Cleanup(); }
69 
Cleanup()70 void RenderBundleEncoder::Cleanup() {
71   if (mValid) {
72     mValid = false;
73   }
74 }
75 
SetBindGroup(uint32_t aSlot,const BindGroup & aBindGroup,const dom::Sequence<uint32_t> & aDynamicOffsets)76 void RenderBundleEncoder::SetBindGroup(
77     uint32_t aSlot, const BindGroup& aBindGroup,
78     const dom::Sequence<uint32_t>& aDynamicOffsets) {
79   if (mValid) {
80     mUsedBindGroups.AppendElement(&aBindGroup);
81     ffi::wgpu_render_bundle_set_bind_group(mEncoder, aSlot, aBindGroup.mId,
82                                            aDynamicOffsets.Elements(),
83                                            aDynamicOffsets.Length());
84   }
85 }
86 
SetPipeline(const RenderPipeline & aPipeline)87 void RenderBundleEncoder::SetPipeline(const RenderPipeline& aPipeline) {
88   if (mValid) {
89     mUsedPipelines.AppendElement(&aPipeline);
90     ffi::wgpu_render_bundle_set_pipeline(mEncoder, aPipeline.mId);
91   }
92 }
93 
SetIndexBuffer(const Buffer & aBuffer,const dom::GPUIndexFormat & aIndexFormat,uint64_t aOffset,uint64_t aSize)94 void RenderBundleEncoder::SetIndexBuffer(
95     const Buffer& aBuffer, const dom::GPUIndexFormat& aIndexFormat,
96     uint64_t aOffset, uint64_t aSize) {
97   if (mValid) {
98     mUsedBuffers.AppendElement(&aBuffer);
99     const auto iformat = aIndexFormat == dom::GPUIndexFormat::Uint32
100                              ? ffi::WGPUIndexFormat_Uint32
101                              : ffi::WGPUIndexFormat_Uint16;
102     ffi::wgpu_render_bundle_set_index_buffer(mEncoder, aBuffer.mId, iformat,
103                                              aOffset, aSize);
104   }
105 }
106 
SetVertexBuffer(uint32_t aSlot,const Buffer & aBuffer,uint64_t aOffset,uint64_t aSize)107 void RenderBundleEncoder::SetVertexBuffer(uint32_t aSlot, const Buffer& aBuffer,
108                                           uint64_t aOffset, uint64_t aSize) {
109   if (mValid) {
110     mUsedBuffers.AppendElement(&aBuffer);
111     ffi::wgpu_render_bundle_set_vertex_buffer(mEncoder, aSlot, aBuffer.mId,
112                                               aOffset, aSize);
113   }
114 }
115 
Draw(uint32_t aVertexCount,uint32_t aInstanceCount,uint32_t aFirstVertex,uint32_t aFirstInstance)116 void RenderBundleEncoder::Draw(uint32_t aVertexCount, uint32_t aInstanceCount,
117                                uint32_t aFirstVertex, uint32_t aFirstInstance) {
118   if (mValid) {
119     ffi::wgpu_render_bundle_draw(mEncoder, aVertexCount, aInstanceCount,
120                                  aFirstVertex, aFirstInstance);
121   }
122 }
123 
DrawIndexed(uint32_t aIndexCount,uint32_t aInstanceCount,uint32_t aFirstIndex,int32_t aBaseVertex,uint32_t aFirstInstance)124 void RenderBundleEncoder::DrawIndexed(uint32_t aIndexCount,
125                                       uint32_t aInstanceCount,
126                                       uint32_t aFirstIndex, int32_t aBaseVertex,
127                                       uint32_t aFirstInstance) {
128   if (mValid) {
129     ffi::wgpu_render_bundle_draw_indexed(mEncoder, aIndexCount, aInstanceCount,
130                                          aFirstIndex, aBaseVertex,
131                                          aFirstInstance);
132   }
133 }
134 
DrawIndirect(const Buffer & aIndirectBuffer,uint64_t aIndirectOffset)135 void RenderBundleEncoder::DrawIndirect(const Buffer& aIndirectBuffer,
136                                        uint64_t aIndirectOffset) {
137   if (mValid) {
138     ffi::wgpu_render_bundle_draw_indirect(mEncoder, aIndirectBuffer.mId,
139                                           aIndirectOffset);
140   }
141 }
142 
DrawIndexedIndirect(const Buffer & aIndirectBuffer,uint64_t aIndirectOffset)143 void RenderBundleEncoder::DrawIndexedIndirect(const Buffer& aIndirectBuffer,
144                                               uint64_t aIndirectOffset) {
145   if (mValid) {
146     ffi::wgpu_render_bundle_draw_indexed_indirect(mEncoder, aIndirectBuffer.mId,
147                                                   aIndirectOffset);
148   }
149 }
150 
Finish(const dom::GPURenderBundleDescriptor & aDesc)151 already_AddRefed<RenderBundle> RenderBundleEncoder::Finish(
152     const dom::GPURenderBundleDescriptor& aDesc) {
153   RawId id = 0;
154   if (mValid) {
155     mValid = false;
156     auto bridge = mParent->GetBridge();
157     if (bridge && bridge->IsOpen()) {
158       auto* encoder = mEncoder.forget();
159       MOZ_ASSERT(encoder);
160       id = bridge->RenderBundleEncoderFinish(*encoder, mParent->mId, aDesc);
161     }
162   }
163   RefPtr<RenderBundle> bundle = new RenderBundle(mParent, id);
164   return bundle.forget();
165 }
166 
167 }  // namespace webgpu
168 }  // namespace mozilla
169