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-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-06 23:10:03 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
RefPtr<CommandBuffer> comb = new CommandBuffer(mParent, id);
|
|
|
|
return comb.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|