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"
|
2020-11-16 22:57:04 +03:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
|
|
|
#include "mozilla/webgpu/ffi/wgpu.h"
|
|
|
|
#include "ipc/WebGPUChild.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace webgpu {
|
|
|
|
|
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() {
|
|
|
|
if (mValid && mParent) {
|
|
|
|
mValid = false;
|
2020-04-07 01:29:18 +03:00
|
|
|
auto bridge = mParent->GetBridge();
|
2020-01-24 19:27:09 +03:00
|
|
|
if (bridge && bridge->IsOpen()) {
|
2020-03-23 10:54:08 +03:00
|
|
|
bridge->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) {
|
|
|
|
if (mValid) {
|
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) {
|
|
|
|
if (mValid) {
|
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
|
|
|
|
|
|
|
const auto& targetCanvas = aDestination.mTexture->mTargetCanvasElement;
|
|
|
|
if (targetCanvas) {
|
|
|
|
mTargetCanvases.AppendElement(targetCanvas);
|
|
|
|
}
|
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) {
|
|
|
|
if (mValid) {
|
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) {
|
|
|
|
if (mValid) {
|
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
|
|
|
|
|
|
|
const auto& targetCanvas = aDestination.mTexture->mTargetCanvasElement;
|
|
|
|
if (targetCanvas) {
|
|
|
|
mTargetCanvases.AppendElement(targetCanvas);
|
|
|
|
}
|
2020-04-13 16:59:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-03-08 19:20:34 +03:00
|
|
|
auto* targetCanvasElement = at.mView->GetTargetCanvasElement();
|
2021-05-20 22:20:03 +03:00
|
|
|
if (targetCanvasElement) {
|
|
|
|
mTargetCanvases.AppendElement(targetCanvasElement);
|
2021-05-11 08:42:20 +03:00
|
|
|
}
|
2021-05-20 22:20:03 +03:00
|
|
|
if (at.mResolveTarget.WasPassed()) {
|
|
|
|
targetCanvasElement = at.mResolveTarget.Value().GetTargetCanvasElement();
|
|
|
|
mTargetCanvases.AppendElement(targetCanvasElement);
|
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) {
|
|
|
|
if (!mValid) {
|
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) {
|
|
|
|
if (!mValid) {
|
|
|
|
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;
|
|
|
|
if (mValid) {
|
|
|
|
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 =
|
2021-05-20 22:20:03 +03:00
|
|
|
new CommandBuffer(mParent, id, std::move(mTargetCanvases));
|
2020-01-22 10:31:51 +03:00
|
|
|
return comb.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|