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/GPUCommandEncoder.h" 16 17 #include "src/dawn_node/binding/Converter.h" 18 #include "src/dawn_node/binding/GPU.h" 19 #include "src/dawn_node/binding/GPUBuffer.h" 20 #include "src/dawn_node/binding/GPUCommandBuffer.h" 21 #include "src/dawn_node/binding/GPUComputePassEncoder.h" 22 #include "src/dawn_node/binding/GPUQuerySet.h" 23 #include "src/dawn_node/binding/GPURenderPassEncoder.h" 24 #include "src/dawn_node/binding/GPUTexture.h" 25 #include "src/dawn_node/utils/Debug.h" 26 27 namespace wgpu { namespace binding { 28 29 //////////////////////////////////////////////////////////////////////////////// 30 // wgpu::bindings::GPUCommandEncoder 31 //////////////////////////////////////////////////////////////////////////////// GPUCommandEncoder(wgpu::CommandEncoder enc)32 GPUCommandEncoder::GPUCommandEncoder(wgpu::CommandEncoder enc) : enc_(std::move(enc)) { 33 } 34 beginRenderPass(Napi::Env env,interop::GPURenderPassDescriptor descriptor)35 interop::Interface<interop::GPURenderPassEncoder> GPUCommandEncoder::beginRenderPass( 36 Napi::Env env, 37 interop::GPURenderPassDescriptor descriptor) { 38 Converter conv(env); 39 40 wgpu::RenderPassDescriptor desc{}; 41 if (!conv(desc.colorAttachments, desc.colorAttachmentCount, descriptor.colorAttachments) || 42 !conv(desc.depthStencilAttachment, descriptor.depthStencilAttachment) || 43 !conv(desc.label, descriptor.label) || 44 !conv(desc.occlusionQuerySet, descriptor.occlusionQuerySet)) { 45 return {}; 46 } 47 return interop::GPURenderPassEncoder::Create<GPURenderPassEncoder>( 48 env, enc_.BeginRenderPass(&desc)); 49 } 50 beginComputePass(Napi::Env env,interop::GPUComputePassDescriptor descriptor)51 interop::Interface<interop::GPUComputePassEncoder> GPUCommandEncoder::beginComputePass( 52 Napi::Env env, 53 interop::GPUComputePassDescriptor descriptor) { 54 wgpu::ComputePassDescriptor desc{}; 55 return interop::GPUComputePassEncoder::Create<GPUComputePassEncoder>( 56 env, enc_.BeginComputePass(&desc)); 57 } 58 copyBufferToBuffer(Napi::Env env,interop::Interface<interop::GPUBuffer> source,interop::GPUSize64 sourceOffset,interop::Interface<interop::GPUBuffer> destination,interop::GPUSize64 destinationOffset,interop::GPUSize64 size)59 void GPUCommandEncoder::copyBufferToBuffer(Napi::Env env, 60 interop::Interface<interop::GPUBuffer> source, 61 interop::GPUSize64 sourceOffset, 62 interop::Interface<interop::GPUBuffer> destination, 63 interop::GPUSize64 destinationOffset, 64 interop::GPUSize64 size) { 65 Converter conv(env); 66 67 wgpu::Buffer src{}; 68 wgpu::Buffer dst{}; 69 if (!conv(src, source) || // 70 !conv(dst, destination)) { 71 return; 72 } 73 74 enc_.CopyBufferToBuffer(src, sourceOffset, dst, destinationOffset, size); 75 } 76 copyBufferToTexture(Napi::Env env,interop::GPUImageCopyBuffer source,interop::GPUImageCopyTexture destination,interop::GPUExtent3D copySize)77 void GPUCommandEncoder::copyBufferToTexture(Napi::Env env, 78 interop::GPUImageCopyBuffer source, 79 interop::GPUImageCopyTexture destination, 80 interop::GPUExtent3D copySize) { 81 Converter conv(env); 82 83 wgpu::ImageCopyBuffer src{}; 84 wgpu::ImageCopyTexture dst{}; 85 wgpu::Extent3D size{}; 86 if (!conv(src, source) || // 87 !conv(dst, destination) || // 88 !conv(size, copySize)) { 89 return; 90 } 91 92 enc_.CopyBufferToTexture(&src, &dst, &size); 93 } 94 copyTextureToBuffer(Napi::Env env,interop::GPUImageCopyTexture source,interop::GPUImageCopyBuffer destination,interop::GPUExtent3D copySize)95 void GPUCommandEncoder::copyTextureToBuffer(Napi::Env env, 96 interop::GPUImageCopyTexture source, 97 interop::GPUImageCopyBuffer destination, 98 interop::GPUExtent3D copySize) { 99 Converter conv(env); 100 101 wgpu::ImageCopyTexture src{}; 102 wgpu::ImageCopyBuffer dst{}; 103 wgpu::Extent3D size{}; 104 if (!conv(src, source) || // 105 !conv(dst, destination) || // 106 !conv(size, copySize)) { 107 return; 108 } 109 110 enc_.CopyTextureToBuffer(&src, &dst, &size); 111 } 112 copyTextureToTexture(Napi::Env env,interop::GPUImageCopyTexture source,interop::GPUImageCopyTexture destination,interop::GPUExtent3D copySize)113 void GPUCommandEncoder::copyTextureToTexture(Napi::Env env, 114 interop::GPUImageCopyTexture source, 115 interop::GPUImageCopyTexture destination, 116 interop::GPUExtent3D copySize) { 117 Converter conv(env); 118 119 wgpu::ImageCopyTexture src{}; 120 wgpu::ImageCopyTexture dst{}; 121 wgpu::Extent3D size{}; 122 if (!conv(src, source) || // 123 !conv(dst, destination) || // 124 !conv(size, copySize)) { 125 return; 126 } 127 128 enc_.CopyTextureToTexture(&src, &dst, &size); 129 } 130 pushDebugGroup(Napi::Env,std::string groupLabel)131 void GPUCommandEncoder::pushDebugGroup(Napi::Env, std::string groupLabel) { 132 enc_.PushDebugGroup(groupLabel.c_str()); 133 } 134 popDebugGroup(Napi::Env)135 void GPUCommandEncoder::popDebugGroup(Napi::Env) { 136 enc_.PopDebugGroup(); 137 } 138 insertDebugMarker(Napi::Env,std::string markerLabel)139 void GPUCommandEncoder::insertDebugMarker(Napi::Env, std::string markerLabel) { 140 enc_.InsertDebugMarker(markerLabel.c_str()); 141 } 142 writeTimestamp(Napi::Env env,interop::Interface<interop::GPUQuerySet> querySet,interop::GPUSize32 queryIndex)143 void GPUCommandEncoder::writeTimestamp(Napi::Env env, 144 interop::Interface<interop::GPUQuerySet> querySet, 145 interop::GPUSize32 queryIndex) { 146 Converter conv(env); 147 148 wgpu::QuerySet q{}; 149 if (!conv(q, querySet)) { 150 return; 151 } 152 153 enc_.WriteTimestamp(q, queryIndex); 154 } 155 resolveQuerySet(Napi::Env env,interop::Interface<interop::GPUQuerySet> querySet,interop::GPUSize32 firstQuery,interop::GPUSize32 queryCount,interop::Interface<interop::GPUBuffer> destination,interop::GPUSize64 destinationOffset)156 void GPUCommandEncoder::resolveQuerySet(Napi::Env env, 157 interop::Interface<interop::GPUQuerySet> querySet, 158 interop::GPUSize32 firstQuery, 159 interop::GPUSize32 queryCount, 160 interop::Interface<interop::GPUBuffer> destination, 161 interop::GPUSize64 destinationOffset) { 162 Converter conv(env); 163 164 wgpu::QuerySet q{}; 165 uint32_t f = 0; 166 uint32_t c = 0; 167 wgpu::Buffer b{}; 168 uint64_t o = 0; 169 170 if (!conv(q, querySet) || // 171 !conv(f, firstQuery) || // 172 !conv(c, queryCount) || // 173 !conv(b, destination) || // 174 !conv(o, destinationOffset)) { 175 return; 176 } 177 178 enc_.ResolveQuerySet(q, f, c, b, o); 179 } 180 finish(Napi::Env env,interop::GPUCommandBufferDescriptor descriptor)181 interop::Interface<interop::GPUCommandBuffer> GPUCommandEncoder::finish( 182 Napi::Env env, 183 interop::GPUCommandBufferDescriptor descriptor) { 184 wgpu::CommandBufferDescriptor desc{}; 185 return interop::GPUCommandBuffer::Create<GPUCommandBuffer>(env, enc_.Finish(&desc)); 186 } 187 getLabel(Napi::Env)188 std::optional<std::string> GPUCommandEncoder::getLabel(Napi::Env) { 189 UNIMPLEMENTED(); 190 } 191 setLabel(Napi::Env,std::optional<std::string> value)192 void GPUCommandEncoder::setLabel(Napi::Env, std::optional<std::string> value) { 193 UNIMPLEMENTED(); 194 } 195 196 }} // namespace wgpu::binding 197