2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-06-13 20:43:48 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
#include "mozilla/dom/WebGPUBinding.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "CommandEncoder.h"
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
#include "CommandBuffer.h"
|
2020-01-24 19:27:09 +03:00
|
|
|
#include "Buffer.h"
|
2020-01-22 10:31:51 +03:00
|
|
|
#include "ComputePassEncoder.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "Device.h"
|
2020-02-19 22:25:30 +03:00
|
|
|
#include "RenderPassEncoder.h"
|
2022-02-18 18:59:13 +03:00
|
|
|
#include "mozilla/webgpu/CanvasContext.h"
|
2020-11-16 22:57:04 +03:00
|
|
|
#include "mozilla/webgpu/ffi/wgpu.h"
|
|
|
|
#include "ipc/WebGPUChild.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2022-05-09 23:41:19 +03:00
|
|
|
namespace mozilla::webgpu {
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
GPU_IMPL_CYCLE_COLLECTION(CommandEncoder, mParent, mBridge)
|
2019-10-02 19:46:03 +03:00
|
|
|
GPU_IMPL_JS_WRAP(CommandEncoder)
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2020-07-29 01:26:21 +03:00
|
|
|
void CommandEncoder::ConvertTextureDataLayoutToFFI(
|
2021-04-08 17:35:42 +03:00
|
|
|
const dom::GPUImageDataLayout& aLayout,
|
|
|
|
ffi::WGPUImageDataLayout* aLayoutFFI) {
|
2020-07-29 01:26:21 +03:00
|
|
|
*aLayoutFFI = {};
|
|
|
|
aLayoutFFI->offset = aLayout.mOffset;
|
|
|
|
aLayoutFFI->bytes_per_row = aLayout.mBytesPerRow;
|
|
|
|
aLayoutFFI->rows_per_image = aLayout.mRowsPerImage;
|
2020-04-13 16:59:24 +03:00
|
|
|
}
|
|
|
|
|
2020-07-29 01:26:21 +03:00
|
|
|
void CommandEncoder::ConvertTextureCopyViewToFFI(
|
2021-04-08 17:35:42 +03:00
|
|
|
const dom::GPUImageCopyTexture& aCopy,
|
|
|
|
ffi::WGPUImageCopyTexture* aViewFFI) {
|
2020-07-29 01:26:21 +03:00
|
|
|
*aViewFFI = {};
|
2021-04-08 17:35:42 +03:00
|
|
|
aViewFFI->texture = aCopy.mTexture->mId;
|
|
|
|
aViewFFI->mip_level = aCopy.mMipLevel;
|
|
|
|
if (aCopy.mOrigin.WasPassed()) {
|
|
|
|
const auto& origin = aCopy.mOrigin.Value();
|
2020-07-21 00:07:03 +03:00
|
|
|
if (origin.IsRangeEnforcedUnsignedLongSequence()) {
|
|
|
|
const auto& seq = origin.GetAsRangeEnforcedUnsignedLongSequence();
|
2020-07-29 01:26:21 +03:00
|
|
|
aViewFFI->origin.x = seq.Length() > 0 ? seq[0] : 0;
|
|
|
|
aViewFFI->origin.y = seq.Length() > 1 ? seq[1] : 0;
|
|
|
|
aViewFFI->origin.z = seq.Length() > 2 ? seq[2] : 0;
|
2020-04-13 16:59:24 +03:00
|
|
|
} else if (origin.IsGPUOrigin3DDict()) {
|
|
|
|
const auto& dict = origin.GetAsGPUOrigin3DDict();
|
2020-07-29 01:26:21 +03:00
|
|
|
aViewFFI->origin.x = dict.mX;
|
|
|
|
aViewFFI->origin.y = dict.mY;
|
|
|
|
aViewFFI->origin.z = dict.mZ;
|
2020-04-13 16:59:24 +03:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexpected origin type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:26:21 +03:00
|
|
|
void CommandEncoder::ConvertExtent3DToFFI(const dom::GPUExtent3D& aExtent,
|
|
|
|
ffi::WGPUExtent3d* aExtentFFI) {
|
|
|
|
*aExtentFFI = {};
|
2020-07-21 00:07:03 +03:00
|
|
|
if (aExtent.IsRangeEnforcedUnsignedLongSequence()) {
|
|
|
|
const auto& seq = aExtent.GetAsRangeEnforcedUnsignedLongSequence();
|
2020-07-29 01:26:21 +03:00
|
|
|
aExtentFFI->width = seq.Length() > 0 ? seq[0] : 0;
|
|
|
|
aExtentFFI->height = seq.Length() > 1 ? seq[1] : 0;
|
2021-03-05 00:25:46 +03:00
|
|
|
aExtentFFI->depth_or_array_layers = seq.Length() > 2 ? seq[2] : 0;
|
2020-04-13 16:59:24 +03:00
|
|
|
} else if (aExtent.IsGPUExtent3DDict()) {
|
|
|
|
const auto& dict = aExtent.GetAsGPUExtent3DDict();
|
2020-07-29 01:26:21 +03:00
|
|
|
aExtentFFI->width = dict.mWidth;
|
|
|
|
aExtentFFI->height = dict.mHeight;
|
2021-03-05 00:25:46 +03:00
|
|
|
aExtentFFI->depth_or_array_layers = dict.mDepthOrArrayLayers;
|
2020-04-13 16:59:24 +03:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexptected extent type");
|
|
|
|
}
|
2020-07-29 01:26:21 +03:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:35:42 +03:00
|
|
|
static ffi::WGPUImageCopyBuffer ConvertBufferCopyView(
|
|
|
|
const dom::GPUImageCopyBuffer& aCopy) {
|
|
|
|
ffi::WGPUImageCopyBuffer view = {};
|
|
|
|
view.buffer = aCopy.mBuffer->mId;
|
|
|
|
CommandEncoder::ConvertTextureDataLayoutToFFI(aCopy, &view.layout);
|
2020-07-29 01:26:21 +03:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2021-04-08 17:35:42 +03:00
|
|
|
static ffi::WGPUImageCopyTexture ConvertTextureCopyView(
|
|
|
|
const dom::GPUImageCopyTexture& aCopy) {
|
|
|
|
ffi::WGPUImageCopyTexture view = {};
|
|
|
|
CommandEncoder::ConvertTextureCopyViewToFFI(aCopy, &view);
|
2020-07-29 01:26:21 +03:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ffi::WGPUExtent3d ConvertExtent(const dom::GPUExtent3D& aExtent) {
|
|
|
|
ffi::WGPUExtent3d extent = {};
|
|
|
|
CommandEncoder::ConvertExtent3DToFFI(aExtent, &extent);
|
2020-04-13 16:59:24 +03:00
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
CommandEncoder::CommandEncoder(Device* const aParent,
|
|
|
|
WebGPUChild* const aBridge, RawId aId)
|
2020-01-24 19:27:09 +03:00
|
|
|
: ChildOf(aParent), mId(aId), mBridge(aBridge) {}
|
2020-01-22 10:31:51 +03:00
|
|
|
|
|
|
|
CommandEncoder::~CommandEncoder() { Cleanup(); }
|
|
|
|
|
|
|
|
void CommandEncoder::Cleanup() {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid) {
|
2020-01-22 10:31:51 +03:00
|
|
|
mValid = false;
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mBridge->IsOpen()) {
|
|
|
|
mBridge->SendCommandEncoderDestroy(mId);
|
2020-01-24 19:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandEncoder::CopyBufferToBuffer(const Buffer& aSource,
|
|
|
|
BufferAddress aSourceOffset,
|
|
|
|
const Buffer& aDestination,
|
|
|
|
BufferAddress aDestinationOffset,
|
|
|
|
BufferAddress aSize) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf bb;
|
|
|
|
ffi::wgpu_command_encoder_copy_buffer_to_buffer(
|
|
|
|
aSource.mId, aSourceOffset, aDestination.mId, aDestinationOffset, aSize,
|
|
|
|
ToFFI(&bb));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:59:24 +03:00
|
|
|
void CommandEncoder::CopyBufferToTexture(
|
2021-04-08 17:35:42 +03:00
|
|
|
const dom::GPUImageCopyBuffer& aSource,
|
|
|
|
const dom::GPUImageCopyTexture& aDestination,
|
2020-04-13 16:59:24 +03:00
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf bb;
|
|
|
|
ffi::wgpu_command_encoder_copy_buffer_to_texture(
|
|
|
|
ConvertBufferCopyView(aSource), ConvertTextureCopyView(aDestination),
|
|
|
|
ConvertExtent(aCopySize), ToFFI(&bb));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
2021-05-27 20:20:28 +03:00
|
|
|
|
2022-02-18 18:59:13 +03:00
|
|
|
const auto& targetContext = aDestination.mTexture->mTargetContext;
|
|
|
|
if (targetContext) {
|
|
|
|
mTargetContexts.AppendElement(targetContext);
|
2021-05-27 20:20:28 +03:00
|
|
|
}
|
2020-04-13 16:59:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::CopyTextureToBuffer(
|
2021-04-08 17:35:42 +03:00
|
|
|
const dom::GPUImageCopyTexture& aSource,
|
|
|
|
const dom::GPUImageCopyBuffer& aDestination,
|
2020-04-13 16:59:24 +03:00
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf bb;
|
|
|
|
ffi::wgpu_command_encoder_copy_texture_to_buffer(
|
|
|
|
ConvertTextureCopyView(aSource), ConvertBufferCopyView(aDestination),
|
|
|
|
ConvertExtent(aCopySize), ToFFI(&bb));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
2020-04-13 16:59:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::CopyTextureToTexture(
|
2021-04-08 17:35:42 +03:00
|
|
|
const dom::GPUImageCopyTexture& aSource,
|
|
|
|
const dom::GPUImageCopyTexture& aDestination,
|
2020-04-13 16:59:24 +03:00
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf bb;
|
|
|
|
ffi::wgpu_command_encoder_copy_texture_to_texture(
|
|
|
|
ConvertTextureCopyView(aSource), ConvertTextureCopyView(aDestination),
|
|
|
|
ConvertExtent(aCopySize), ToFFI(&bb));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
2021-05-27 20:20:28 +03:00
|
|
|
|
2022-02-18 18:59:13 +03:00
|
|
|
const auto& targetContext = aDestination.mTexture->mTargetContext;
|
|
|
|
if (targetContext) {
|
|
|
|
mTargetContexts.AppendElement(targetContext);
|
2021-05-27 20:20:28 +03:00
|
|
|
}
|
2020-04-13 16:59:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:51:16 +03:00
|
|
|
void CommandEncoder::PushDebugGroup(const nsAString& aString) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2021-12-21 17:51:16 +03:00
|
|
|
ipc::ByteBuf bb;
|
2022-07-08 11:11:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8 marker(aString);
|
|
|
|
ffi::wgpu_command_encoder_push_debug_group(&marker, ToFFI(&bb));
|
2021-12-21 17:51:16 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::PopDebugGroup() {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2021-12-21 17:51:16 +03:00
|
|
|
ipc::ByteBuf bb;
|
|
|
|
ffi::wgpu_command_encoder_pop_debug_group(ToFFI(&bb));
|
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::InsertDebugMarker(const nsAString& aString) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2021-12-21 17:51:16 +03:00
|
|
|
ipc::ByteBuf bb;
|
2022-07-08 11:11:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8 marker(aString);
|
|
|
|
ffi::wgpu_command_encoder_insert_debug_marker(&marker, ToFFI(&bb));
|
2021-12-21 17:51:16 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(bb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
already_AddRefed<ComputePassEncoder> CommandEncoder::BeginComputePass(
|
|
|
|
const dom::GPUComputePassDescriptor& aDesc) {
|
|
|
|
RefPtr<ComputePassEncoder> pass = new ComputePassEncoder(this, aDesc);
|
|
|
|
return pass.forget();
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:25:30 +03:00
|
|
|
already_AddRefed<RenderPassEncoder> CommandEncoder::BeginRenderPass(
|
|
|
|
const dom::GPURenderPassDescriptor& aDesc) {
|
2020-04-21 02:50:04 +03:00
|
|
|
for (const auto& at : aDesc.mColorAttachments) {
|
2022-02-18 18:59:13 +03:00
|
|
|
auto* targetContext = at.mView->GetTargetContext();
|
|
|
|
if (targetContext) {
|
|
|
|
mTargetContexts.AppendElement(targetContext);
|
2021-05-11 08:42:20 +03:00
|
|
|
}
|
2021-05-20 22:20:03 +03:00
|
|
|
if (at.mResolveTarget.WasPassed()) {
|
2022-02-18 18:59:13 +03:00
|
|
|
targetContext = at.mResolveTarget.Value().GetTargetContext();
|
|
|
|
mTargetContexts.AppendElement(targetContext);
|
2020-04-21 02:50:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:25:30 +03:00
|
|
|
RefPtr<RenderPassEncoder> pass = new RenderPassEncoder(this, aDesc);
|
|
|
|
return pass.forget();
|
|
|
|
}
|
|
|
|
|
2020-11-07 05:43:09 +03:00
|
|
|
void CommandEncoder::EndComputePass(ffi::WGPUComputePass& aPass,
|
2020-01-22 10:31:51 +03:00
|
|
|
ErrorResult& aRv) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (!mValid || !mBridge->IsOpen()) {
|
2020-02-03 23:19:11 +03:00
|
|
|
return aRv.ThrowInvalidStateError("Command encoder is not valid");
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf byteBuf;
|
|
|
|
ffi::wgpu_compute_pass_finish(&aPass, ToFFI(&byteBuf));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(byteBuf));
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-07 05:43:09 +03:00
|
|
|
void CommandEncoder::EndRenderPass(ffi::WGPURenderPass& aPass,
|
2020-02-19 22:25:30 +03:00
|
|
|
ErrorResult& aRv) {
|
2022-04-28 00:13:21 +03:00
|
|
|
if (!mValid || !mBridge->IsOpen()) {
|
2020-02-19 22:25:30 +03:00
|
|
|
return aRv.ThrowInvalidStateError("Command encoder is not valid");
|
|
|
|
}
|
|
|
|
|
2020-11-07 05:43:09 +03:00
|
|
|
ipc::ByteBuf byteBuf;
|
|
|
|
ffi::wgpu_render_pass_finish(&aPass, ToFFI(&byteBuf));
|
2020-12-18 23:07:47 +03:00
|
|
|
mBridge->SendCommandEncoderAction(mId, mParent->mId, std::move(byteBuf));
|
2020-02-19 22:25:30 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
already_AddRefed<CommandBuffer> CommandEncoder::Finish(
|
|
|
|
const dom::GPUCommandBufferDescriptor& aDesc) {
|
|
|
|
RawId id = 0;
|
2022-04-28 00:13:21 +03:00
|
|
|
if (mValid && mBridge->IsOpen()) {
|
2020-01-22 10:31:51 +03:00
|
|
|
mValid = false;
|
2020-12-18 23:07:47 +03:00
|
|
|
id = mBridge->CommandEncoderFinish(mId, mParent->mId, aDesc);
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
2020-04-21 02:50:04 +03:00
|
|
|
RefPtr<CommandBuffer> comb =
|
2022-02-18 18:59:13 +03:00
|
|
|
new CommandBuffer(mParent, id, std::move(mTargetContexts));
|
2020-01-22 10:31:51 +03:00
|
|
|
return comb.forget();
|
|
|
|
}
|
|
|
|
|
2022-05-09 23:41:19 +03:00
|
|
|
} // namespace mozilla::webgpu
|