WebGPUCommandEncoder.cpp [plain text]
#include "config.h"
#include "WebGPUCommandEncoder.h"
#if ENABLE(WEBGPU)
#include "GPUComputePassEncoder.h"
#include "GPURenderPassDescriptor.h"
#include "GPURenderPassEncoder.h"
#include "Logging.h"
#include "WebGPUBuffer.h"
#include "WebGPUComputePassEncoder.h"
#include "WebGPURenderPassDescriptor.h"
#include "WebGPURenderPassEncoder.h"
#include "WebGPUTexture.h"
#include <wtf/Optional.h>
namespace WebCore {
Optional<GPUBufferCopyView> WebGPUBufferCopyView::tryCreateGPUBufferCopyView() const
{
if (!buffer || !buffer->buffer()) {
LOG(WebGPU, "WebGPUCommandEncoder: Invalid buffer for copy!");
return WTF::nullopt;
}
return GPUBufferCopyView { makeRef(*buffer->buffer()), *this };
}
Optional<GPUTextureCopyView> WebGPUTextureCopyView::tryCreateGPUTextureCopyView() const
{
if (!texture || !texture->texture()) {
LOG(WebGPU, "WebGPUCommandEncoder: Invalid texture for copy!");
return WTF::nullopt;
}
return GPUTextureCopyView { makeRef(*texture->texture()), *this };
}
Ref<WebGPUCommandEncoder> WebGPUCommandEncoder::create(RefPtr<GPUCommandBuffer>&& buffer)
{
return adoptRef(*new WebGPUCommandEncoder(WTFMove(buffer)));
}
WebGPUCommandEncoder::WebGPUCommandEncoder(RefPtr<GPUCommandBuffer>&& buffer)
: m_commandBuffer(WTFMove(buffer))
{
}
Ref<WebGPURenderPassEncoder> WebGPUCommandEncoder::beginRenderPass(const WebGPURenderPassDescriptor& descriptor)
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::beginRenderPass(): Invalid operation!");
return WebGPURenderPassEncoder::create(nullptr);
}
auto gpuDescriptor = descriptor.tryCreateGPURenderPassDescriptor();
if (!gpuDescriptor)
return WebGPURenderPassEncoder::create(nullptr);
auto encoder = GPURenderPassEncoder::tryCreate(makeRef(*m_commandBuffer), WTFMove(*gpuDescriptor));
return WebGPURenderPassEncoder::create(WTFMove(encoder));
}
Ref<WebGPUComputePassEncoder> WebGPUCommandEncoder::beginComputePass()
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::beginComputePass(): Invalid operation!");
return WebGPUComputePassEncoder::create(nullptr);
}
auto encoder = GPUComputePassEncoder::tryCreate(makeRef(*m_commandBuffer));
return WebGPUComputePassEncoder::create(WTFMove(encoder));
}
void WebGPUCommandEncoder::copyBufferToBuffer(WebGPUBuffer& src, uint64_t srcOffset, WebGPUBuffer& dst, uint64_t dstOffset, uint64_t size)
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToBuffer(): Invalid operation!");
return;
}
if (!src.buffer() || !dst.buffer()) {
LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToBuffer(): Invalid GPUBuffer!");
return;
}
m_commandBuffer->copyBufferToBuffer(makeRef(*src.buffer()), srcOffset, makeRef(*dst.buffer()), dstOffset, size);
}
void WebGPUCommandEncoder::copyBufferToTexture(const WebGPUBufferCopyView& srcBuffer, const WebGPUTextureCopyView& dstTexture, const GPUExtent3D& size)
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToTexture(): Invalid operation!");
return;
}
auto gpuBufferView = srcBuffer.tryCreateGPUBufferCopyView();
auto gpuTextureView = dstTexture.tryCreateGPUTextureCopyView();
if (!gpuBufferView || !gpuTextureView)
return;
m_commandBuffer->copyBufferToTexture(WTFMove(*gpuBufferView), WTFMove(*gpuTextureView), size);
}
void WebGPUCommandEncoder::copyTextureToBuffer(const WebGPUTextureCopyView& srcTexture, const WebGPUBufferCopyView& dstBuffer, const GPUExtent3D& size)
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::copyTextureToBuffer(): Invalid operation!");
return;
}
auto gpuTextureView = srcTexture.tryCreateGPUTextureCopyView();
auto gpuBufferView = dstBuffer.tryCreateGPUBufferCopyView();
if (!gpuTextureView || !gpuBufferView)
return;
m_commandBuffer->copyTextureToBuffer(WTFMove(*gpuTextureView), WTFMove(*gpuBufferView), size);
}
void WebGPUCommandEncoder::copyTextureToTexture(const WebGPUTextureCopyView& src, const WebGPUTextureCopyView& dst, const GPUExtent3D& size)
{
if (!m_commandBuffer) {
LOG(WebGPU, "WebGPUCommandEncoder::copyTextureToTexture(): Invalid operation!");
return;
}
auto gpuSrcView = src.tryCreateGPUTextureCopyView();
auto gpuDstView = dst.tryCreateGPUTextureCopyView();
if (!gpuSrcView || !gpuDstView)
return;
m_commandBuffer->copyTextureToTexture(WTFMove(*gpuSrcView), WTFMove(*gpuDstView), size);
}
Ref<WebGPUCommandBuffer> WebGPUCommandEncoder::finish()
{
if (!m_commandBuffer || m_commandBuffer->isEncodingPass()) {
LOG(WebGPU, "WebGPUCommandEncoder::finish(): Invalid operation!");
return WebGPUCommandBuffer::create(nullptr);
}
return WebGPUCommandBuffer::create(m_commandBuffer.releaseNonNull());
}
}
#endif // ENABLE(WEBGPU)