• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/dawn_node/binding/GPURenderBundleEncoder.h"
16 
17 #include "src/dawn_node/binding/Converter.h"
18 #include "src/dawn_node/binding/GPUBindGroup.h"
19 #include "src/dawn_node/binding/GPUBuffer.h"
20 #include "src/dawn_node/binding/GPURenderBundle.h"
21 #include "src/dawn_node/binding/GPURenderPipeline.h"
22 #include "src/dawn_node/utils/Debug.h"
23 
24 namespace wgpu { namespace binding {
25 
26     ////////////////////////////////////////////////////////////////////////////////
27     // wgpu::bindings::GPURenderBundleEncoder
28     ////////////////////////////////////////////////////////////////////////////////
GPURenderBundleEncoder(wgpu::RenderBundleEncoder enc)29     GPURenderBundleEncoder::GPURenderBundleEncoder(wgpu::RenderBundleEncoder enc)
30         : enc_(std::move(enc)) {
31     }
32 
finish(Napi::Env env,interop::GPURenderBundleDescriptor descriptor)33     interop::Interface<interop::GPURenderBundle> GPURenderBundleEncoder::finish(
34         Napi::Env env,
35         interop::GPURenderBundleDescriptor descriptor) {
36         wgpu::RenderBundleDescriptor desc{};
37 
38         return interop::GPURenderBundle::Create<GPURenderBundle>(env, enc_.Finish(&desc));
39     }
40 
setBindGroup(Napi::Env env,interop::GPUIndex32 index,interop::Interface<interop::GPUBindGroup> bindGroup,std::vector<interop::GPUBufferDynamicOffset> dynamicOffsets)41     void GPURenderBundleEncoder::setBindGroup(
42         Napi::Env env,
43         interop::GPUIndex32 index,
44         interop::Interface<interop::GPUBindGroup> bindGroup,
45         std::vector<interop::GPUBufferDynamicOffset> dynamicOffsets) {
46         Converter conv(env);
47 
48         wgpu::BindGroup bg{};
49         uint32_t* offsets = nullptr;
50         uint32_t num_offsets = 0;
51         if (!conv(bg, bindGroup) || !conv(offsets, num_offsets, dynamicOffsets)) {
52             return;
53         }
54 
55         enc_.SetBindGroup(index, bg, num_offsets, offsets);
56     }
57 
setBindGroup(Napi::Env env,interop::GPUIndex32 index,interop::Interface<interop::GPUBindGroup> bindGroup,interop::Uint32Array dynamicOffsetsData,interop::GPUSize64 dynamicOffsetsDataStart,interop::GPUSize32 dynamicOffsetsDataLength)58     void GPURenderBundleEncoder::setBindGroup(Napi::Env env,
59                                               interop::GPUIndex32 index,
60                                               interop::Interface<interop::GPUBindGroup> bindGroup,
61                                               interop::Uint32Array dynamicOffsetsData,
62                                               interop::GPUSize64 dynamicOffsetsDataStart,
63                                               interop::GPUSize32 dynamicOffsetsDataLength) {
64         Converter conv(env);
65 
66         wgpu::BindGroup bg{};
67         if (!conv(bg, bindGroup)) {
68             return;
69         }
70 
71         enc_.SetBindGroup(index, bg, dynamicOffsetsDataLength,
72                           dynamicOffsetsData.Data() + dynamicOffsetsDataStart);
73     }
74 
pushDebugGroup(Napi::Env,std::string groupLabel)75     void GPURenderBundleEncoder::pushDebugGroup(Napi::Env, std::string groupLabel) {
76         enc_.PushDebugGroup(groupLabel.c_str());
77     }
78 
popDebugGroup(Napi::Env)79     void GPURenderBundleEncoder::popDebugGroup(Napi::Env) {
80         enc_.PopDebugGroup();
81     }
82 
insertDebugMarker(Napi::Env,std::string markerLabel)83     void GPURenderBundleEncoder::insertDebugMarker(Napi::Env, std::string markerLabel) {
84         enc_.InsertDebugMarker(markerLabel.c_str());
85     }
86 
setPipeline(Napi::Env env,interop::Interface<interop::GPURenderPipeline> pipeline)87     void GPURenderBundleEncoder::setPipeline(
88         Napi::Env env,
89         interop::Interface<interop::GPURenderPipeline> pipeline) {
90         Converter conv(env);
91 
92         wgpu::RenderPipeline p{};
93         if (!conv(p, pipeline)) {
94             return;
95         }
96 
97         enc_.SetPipeline(p);
98     }
99 
setIndexBuffer(Napi::Env env,interop::Interface<interop::GPUBuffer> buffer,interop::GPUIndexFormat indexFormat,interop::GPUSize64 offset,std::optional<interop::GPUSize64> size)100     void GPURenderBundleEncoder::setIndexBuffer(Napi::Env env,
101                                                 interop::Interface<interop::GPUBuffer> buffer,
102                                                 interop::GPUIndexFormat indexFormat,
103                                                 interop::GPUSize64 offset,
104                                                 std::optional<interop::GPUSize64> size) {
105         Converter conv(env);
106 
107         wgpu::Buffer b{};
108         wgpu::IndexFormat f{};
109         uint64_t o = 0;
110         uint64_t s = wgpu::kWholeSize;
111         if (!conv(b, buffer) ||       //
112             !conv(f, indexFormat) ||  //
113             !conv(o, offset) ||       //
114             !conv(s, size)) {
115             return;
116         }
117 
118         enc_.SetIndexBuffer(b, f, o, s);
119     }
120 
setVertexBuffer(Napi::Env env,interop::GPUIndex32 slot,interop::Interface<interop::GPUBuffer> buffer,interop::GPUSize64 offset,std::optional<interop::GPUSize64> size)121     void GPURenderBundleEncoder::setVertexBuffer(Napi::Env env,
122                                                  interop::GPUIndex32 slot,
123                                                  interop::Interface<interop::GPUBuffer> buffer,
124                                                  interop::GPUSize64 offset,
125                                                  std::optional<interop::GPUSize64> size) {
126         Converter conv(env);
127 
128         wgpu::Buffer b{};
129         uint64_t s = wgpu::kWholeSize;
130         if (!conv(b, buffer) || !conv(s, size)) {
131             return;
132         }
133         enc_.SetVertexBuffer(slot, b, offset, s);
134     }
135 
draw(Napi::Env env,interop::GPUSize32 vertexCount,interop::GPUSize32 instanceCount,interop::GPUSize32 firstVertex,interop::GPUSize32 firstInstance)136     void GPURenderBundleEncoder::draw(Napi::Env env,
137                                       interop::GPUSize32 vertexCount,
138                                       interop::GPUSize32 instanceCount,
139                                       interop::GPUSize32 firstVertex,
140                                       interop::GPUSize32 firstInstance) {
141         enc_.Draw(vertexCount, instanceCount, firstVertex, firstInstance);
142     }
143 
drawIndexed(Napi::Env env,interop::GPUSize32 indexCount,interop::GPUSize32 instanceCount,interop::GPUSize32 firstIndex,interop::GPUSignedOffset32 baseVertex,interop::GPUSize32 firstInstance)144     void GPURenderBundleEncoder::drawIndexed(Napi::Env env,
145                                              interop::GPUSize32 indexCount,
146                                              interop::GPUSize32 instanceCount,
147                                              interop::GPUSize32 firstIndex,
148                                              interop::GPUSignedOffset32 baseVertex,
149                                              interop::GPUSize32 firstInstance) {
150         enc_.DrawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance);
151     }
152 
drawIndirect(Napi::Env env,interop::Interface<interop::GPUBuffer> indirectBuffer,interop::GPUSize64 indirectOffset)153     void GPURenderBundleEncoder::drawIndirect(Napi::Env env,
154                                               interop::Interface<interop::GPUBuffer> indirectBuffer,
155                                               interop::GPUSize64 indirectOffset) {
156         Converter conv(env);
157 
158         wgpu::Buffer b{};
159         uint32_t o = 0;
160 
161         if (!conv(b, indirectBuffer) ||  //
162             !conv(o, indirectOffset)) {
163             return;
164         }
165         enc_.DrawIndirect(b, o);
166     }
167 
drawIndexedIndirect(Napi::Env env,interop::Interface<interop::GPUBuffer> indirectBuffer,interop::GPUSize64 indirectOffset)168     void GPURenderBundleEncoder::drawIndexedIndirect(
169         Napi::Env env,
170         interop::Interface<interop::GPUBuffer> indirectBuffer,
171         interop::GPUSize64 indirectOffset) {
172         Converter conv(env);
173 
174         wgpu::Buffer b{};
175         uint32_t o = 0;
176 
177         if (!conv(b, indirectBuffer) ||  //
178             !conv(o, indirectOffset)) {
179             return;
180         }
181         enc_.DrawIndexedIndirect(b, o);
182     }
183 
getLabel(Napi::Env)184     std::optional<std::string> GPURenderBundleEncoder::getLabel(Napi::Env) {
185         UNIMPLEMENTED();
186     }
187 
setLabel(Napi::Env,std::optional<std::string> value)188     void GPURenderBundleEncoder::setLabel(Napi::Env, std::optional<std::string> value) {
189         UNIMPLEMENTED();
190     }
191 
192 }}  // namespace wgpu::binding
193