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"
|
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-04-13 16:59:24 +03:00
|
|
|
static ffi::WGPUBufferCopyView ConvertBufferCopyView(
|
|
|
|
const dom::GPUBufferCopyView& aView) {
|
2020-04-21 02:50:04 +03:00
|
|
|
ffi::WGPUBufferCopyView view = {};
|
2020-04-13 16:59:24 +03:00
|
|
|
view.buffer = aView.mBuffer->mId;
|
|
|
|
view.offset = aView.mOffset;
|
|
|
|
view.bytes_per_row = aView.mBytesPerRow;
|
|
|
|
view.rows_per_image = aView.mRowsPerImage;
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ffi::WGPUTextureCopyView ConvertTextureCopyView(
|
|
|
|
const dom::GPUTextureCopyView& aView) {
|
|
|
|
ffi::WGPUTextureCopyView view = {};
|
|
|
|
view.texture = aView.mTexture->mId;
|
|
|
|
view.mip_level = aView.mMipLevel;
|
|
|
|
view.array_layer = aView.mArrayLayer;
|
|
|
|
if (aView.mOrigin.WasPassed()) {
|
|
|
|
const auto& origin = aView.mOrigin.Value();
|
|
|
|
if (origin.IsUnsignedLongSequence()) {
|
|
|
|
const auto& seq = origin.GetAsUnsignedLongSequence();
|
|
|
|
view.origin.x = seq.Length() > 0 ? seq[0] : 0;
|
|
|
|
view.origin.y = seq.Length() > 1 ? seq[1] : 0;
|
|
|
|
view.origin.z = seq.Length() > 2 ? seq[2] : 0;
|
|
|
|
} else if (origin.IsGPUOrigin3DDict()) {
|
|
|
|
const auto& dict = origin.GetAsGPUOrigin3DDict();
|
|
|
|
view.origin.x = dict.mX;
|
|
|
|
view.origin.y = dict.mY;
|
|
|
|
view.origin.z = dict.mZ;
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexpected origin type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ffi::WGPUExtent3d ConvertExtent(const dom::GPUExtent3D& aExtent) {
|
2020-04-21 02:50:04 +03:00
|
|
|
ffi::WGPUExtent3d extent = {};
|
2020-04-13 16:59:24 +03:00
|
|
|
if (aExtent.IsUnsignedLongSequence()) {
|
|
|
|
const auto& seq = aExtent.GetAsUnsignedLongSequence();
|
|
|
|
extent.width = seq.Length() > 0 ? seq[0] : 0;
|
|
|
|
extent.height = seq.Length() > 1 ? seq[1] : 0;
|
|
|
|
extent.depth = seq.Length() > 2 ? seq[2] : 0;
|
|
|
|
} else if (aExtent.IsGPUExtent3DDict()) {
|
|
|
|
const auto& dict = aExtent.GetAsGPUExtent3DDict();
|
|
|
|
extent.width = dict.mWidth;
|
|
|
|
extent.height = dict.mHeight;
|
|
|
|
extent.depth = dict.mDepth;
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexptected extent type");
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
mBridge->SendCommandEncoderCopyBufferToBuffer(
|
|
|
|
mId, aSource.mId, aSourceOffset, aDestination.mId, aDestinationOffset,
|
|
|
|
aSize);
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:59:24 +03:00
|
|
|
void CommandEncoder::CopyBufferToTexture(
|
|
|
|
const dom::GPUBufferCopyView& aSource,
|
|
|
|
const dom::GPUTextureCopyView& aDestination,
|
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
|
|
if (mValid) {
|
|
|
|
const auto source = ConvertBufferCopyView(aSource);
|
|
|
|
const auto destination = ConvertTextureCopyView(aDestination);
|
|
|
|
const auto size = ConvertExtent(aCopySize);
|
|
|
|
mBridge->SendCommandEncoderCopyBufferToTexture(mId, source, destination,
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::CopyTextureToBuffer(
|
|
|
|
const dom::GPUTextureCopyView& aSource,
|
|
|
|
const dom::GPUBufferCopyView& aDestination,
|
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
|
|
if (mValid) {
|
|
|
|
const auto source = ConvertTextureCopyView(aSource);
|
|
|
|
const auto destination = ConvertBufferCopyView(aDestination);
|
|
|
|
const auto size = ConvertExtent(aCopySize);
|
|
|
|
mBridge->SendCommandEncoderCopyTextureToBuffer(mId, source, destination,
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CommandEncoder::CopyTextureToTexture(
|
|
|
|
const dom::GPUTextureCopyView& aSource,
|
|
|
|
const dom::GPUTextureCopyView& aDestination,
|
|
|
|
const dom::GPUExtent3D& aCopySize) {
|
|
|
|
if (mValid) {
|
|
|
|
const auto source = ConvertTextureCopyView(aSource);
|
|
|
|
const auto destination = ConvertTextureCopyView(aDestination);
|
|
|
|
const auto size = ConvertExtent(aCopySize);
|
|
|
|
mBridge->SendCommandEncoderCopyTextureToTexture(mId, source, destination,
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
auto* targetCanvasElement = at.mAttachment->GetTargetCanvasElement();
|
|
|
|
if (targetCanvasElement) {
|
|
|
|
if (mTargetCanvasElement) {
|
|
|
|
NS_WARNING("Command encoder touches more than one canvas");
|
|
|
|
} else {
|
|
|
|
mTargetCanvasElement = targetCanvasElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:25:30 +03:00
|
|
|
RefPtr<RenderPassEncoder> pass = new RenderPassEncoder(this, aDesc);
|
|
|
|
return pass.forget();
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
void CommandEncoder::EndComputePass(Span<const uint8_t> aData,
|
|
|
|
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
|
|
|
}
|
|
|
|
ipc::Shmem shmem;
|
|
|
|
if (!mBridge->AllocShmem(aData.Length(), ipc::Shmem::SharedMemory::TYPE_BASIC,
|
|
|
|
&shmem)) {
|
2020-02-03 23:19:11 +03:00
|
|
|
return aRv.ThrowAbortError(nsPrintfCString(
|
|
|
|
"Unable to allocate shmem of size %zu", aData.Length()));
|
2020-01-22 10:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(shmem.get<uint8_t>(), aData.data(), aData.Length());
|
|
|
|
mBridge->SendCommandEncoderRunComputePass(mId, std::move(shmem));
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:25:30 +03:00
|
|
|
void CommandEncoder::EndRenderPass(Span<const uint8_t> aData,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!mValid) {
|
|
|
|
return aRv.ThrowInvalidStateError("Command encoder is not valid");
|
|
|
|
}
|
|
|
|
ipc::Shmem shmem;
|
|
|
|
if (!mBridge->AllocShmem(aData.Length(), ipc::Shmem::SharedMemory::TYPE_BASIC,
|
|
|
|
&shmem)) {
|
|
|
|
return aRv.ThrowAbortError(nsPrintfCString(
|
|
|
|
"Unable to allocate shmem of size %zu", aData.Length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(shmem.get<uint8_t>(), aData.data(), aData.Length());
|
|
|
|
mBridge->SendCommandEncoderRunRenderPass(mId, std::move(shmem));
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:31:51 +03:00
|
|
|
already_AddRefed<CommandBuffer> CommandEncoder::Finish(
|
|
|
|
const dom::GPUCommandBufferDescriptor& aDesc) {
|
|
|
|
RawId id = 0;
|
|
|
|
if (mValid) {
|
|
|
|
mValid = false;
|
|
|
|
id = mBridge->CommandEncoderFinish(mId, aDesc);
|
|
|
|
}
|
2020-04-21 02:50:04 +03:00
|
|
|
RefPtr<CommandBuffer> comb =
|
|
|
|
new CommandBuffer(mParent, id, mTargetCanvasElement);
|
2020-01-22 10:31:51 +03:00
|
|
|
return comb.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|