1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebGPUCommandEncoder.h"
28
29#if ENABLE(WEBGPU)
30
31#include "GPUComputePassEncoder.h"
32#include "GPURenderPassDescriptor.h"
33#include "GPURenderPassEncoder.h"
34#include "WebGPUBuffer.h"
35#include "WebGPUComputePassEncoder.h"
36#include "WebGPURenderPassDescriptor.h"
37#include "WebGPURenderPassEncoder.h"
38#include "WebGPUTexture.h"
39#include <wtf/Optional.h>
40
41namespace WebCore {
42
43Optional<GPUBufferCopyView> WebGPUBufferCopyView::tryCreateGPUBufferCopyView() const
44{
45 if (!buffer || !buffer->buffer()) {
46 LOG(WebGPU, "WebGPUCommandEncoder: Invalid buffer for copy!");
47 return WTF::nullopt;
48 }
49
50 // FIXME: Add Web GPU validation.
51
52 return GPUBufferCopyView { makeRef(*buffer->buffer()), *this };
53}
54
55Optional<GPUTextureCopyView> WebGPUTextureCopyView::tryCreateGPUTextureCopyView() const
56{
57 if (!texture || !texture->texture()) {
58 LOG(WebGPU, "WebGPUCommandEncoder: Invalid texture for copy!");
59 return WTF::nullopt;
60 }
61
62 // FIXME: Add Web GPU validation.
63
64 return GPUTextureCopyView { makeRef(*texture->texture()), *this };
65}
66
67Ref<WebGPUCommandEncoder> WebGPUCommandEncoder::create(RefPtr<GPUCommandBuffer>&& buffer)
68{
69 return adoptRef(*new WebGPUCommandEncoder(WTFMove(buffer)));
70}
71
72WebGPUCommandEncoder::WebGPUCommandEncoder(RefPtr<GPUCommandBuffer>&& buffer)
73 : m_commandBuffer(WTFMove(buffer))
74{
75}
76
77Ref<WebGPURenderPassEncoder> WebGPUCommandEncoder::beginRenderPass(const WebGPURenderPassDescriptor& descriptor)
78{
79 if (!m_commandBuffer) {
80 LOG(WebGPU, "WebGPUCommandEncoder::beginRenderPass(): Invalid operation!");
81 return WebGPURenderPassEncoder::create(nullptr);
82 }
83 auto gpuDescriptor = descriptor.tryCreateGPURenderPassDescriptor();
84 if (!gpuDescriptor)
85 return WebGPURenderPassEncoder::create(nullptr);
86
87 auto encoder = GPURenderPassEncoder::tryCreate(makeRef(*m_commandBuffer), WTFMove(*gpuDescriptor));
88 return WebGPURenderPassEncoder::create(WTFMove(encoder));
89}
90
91Ref<WebGPUComputePassEncoder> WebGPUCommandEncoder::beginComputePass()
92{
93 if (!m_commandBuffer) {
94 LOG(WebGPU, "WebGPUCommandEncoder::beginComputePass(): Invalid operation!");
95 return WebGPUComputePassEncoder::create(nullptr);
96 }
97 auto encoder = GPUComputePassEncoder::tryCreate(makeRef(*m_commandBuffer));
98 return WebGPUComputePassEncoder::create(WTFMove(encoder));
99}
100
101void WebGPUCommandEncoder::copyBufferToBuffer(WebGPUBuffer& src, uint64_t srcOffset, WebGPUBuffer& dst, uint64_t dstOffset, uint64_t size)
102{
103 if (!m_commandBuffer) {
104 LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToBuffer(): Invalid operation!");
105 return;
106 }
107 if (!src.buffer() || !dst.buffer()) {
108 LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToBuffer(): Invalid GPUBuffer!");
109 return;
110 }
111
112 // FIXME: Add Web GPU validation.
113
114 m_commandBuffer->copyBufferToBuffer(makeRef(*src.buffer()), srcOffset, makeRef(*dst.buffer()), dstOffset, size);
115}
116
117void WebGPUCommandEncoder::copyBufferToTexture(const WebGPUBufferCopyView& srcBuffer, const WebGPUTextureCopyView& dstTexture, const GPUExtent3D& size)
118{
119 if (!m_commandBuffer) {
120 LOG(WebGPU, "WebGPUCommandEncoder::copyBufferToTexture(): Invalid operation!");
121 return;
122 }
123 auto gpuBufferView = srcBuffer.tryCreateGPUBufferCopyView();
124 auto gpuTextureView = dstTexture.tryCreateGPUTextureCopyView();
125
126 if (!gpuBufferView || !gpuTextureView)
127 return;
128
129 // FIXME: Add Web GPU validation.
130
131 m_commandBuffer->copyBufferToTexture(WTFMove(*gpuBufferView), WTFMove(*gpuTextureView), size);
132}
133
134void WebGPUCommandEncoder::copyTextureToBuffer(const WebGPUTextureCopyView& srcTexture, const WebGPUBufferCopyView& dstBuffer, const GPUExtent3D& size)
135{
136 if (!m_commandBuffer) {
137 LOG(WebGPU, "WebGPUCommandEncoder::copyTextureToBuffer(): Invalid operation!");
138 return;
139 }
140 auto gpuTextureView = srcTexture.tryCreateGPUTextureCopyView();
141 auto gpuBufferView = dstBuffer.tryCreateGPUBufferCopyView();
142
143 if (!gpuTextureView || !gpuBufferView)
144 return;
145
146 // FIXME: Add Web GPU validation.
147
148 m_commandBuffer->copyTextureToBuffer(WTFMove(*gpuTextureView), WTFMove(*gpuBufferView), size);
149}
150
151void WebGPUCommandEncoder::copyTextureToTexture(const WebGPUTextureCopyView& src, const WebGPUTextureCopyView& dst, const GPUExtent3D& size)
152{
153 if (!m_commandBuffer) {
154 LOG(WebGPU, "WebGPUCommandEncoder::copyTextureToTexture(): Invalid operation!");
155 return;
156 }
157 auto gpuSrcView = src.tryCreateGPUTextureCopyView();
158 auto gpuDstView = dst.tryCreateGPUTextureCopyView();
159
160 if (!gpuSrcView || !gpuDstView)
161 return;
162
163 // FIXME: Add Web GPU validation.
164
165 m_commandBuffer->copyTextureToTexture(WTFMove(*gpuSrcView), WTFMove(*gpuDstView), size);
166}
167
168Ref<WebGPUCommandBuffer> WebGPUCommandEncoder::finish()
169{
170 if (!m_commandBuffer || m_commandBuffer->isEncodingPass()) {
171 LOG(WebGPU, "WebGPUCommandEncoder::finish(): Invalid operation!");
172 return WebGPUCommandBuffer::create(nullptr);
173 }
174 // Passes the referenced GPUCommandBuffer to the WebGPUCommandBuffer, invalidating this WebGPUCommandEncoder.
175 return WebGPUCommandBuffer::create(m_commandBuffer.releaseNonNull());
176}
177
178} // namespace WebCore
179
180#endif // ENABLE(WEBGPU)
181