2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebGPUDevice.h"
31 #include "GPUCommandBuffer.h"
32 #include "GPUPipelineStageDescriptor.h"
33 #include "GPURenderPipelineDescriptor.h"
34 #include "GPUShaderModuleDescriptor.h"
36 #include "WebGPUCommandBuffer.h"
37 #include "WebGPUPipelineStageDescriptor.h"
38 #include "WebGPURenderPipeline.h"
39 #include "WebGPURenderPipelineDescriptor.h"
40 #include "WebGPUShaderModule.h"
41 #include "WebGPUShaderModuleDescriptor.h"
42 #include "WebGPUShaderStage.h"
46 RefPtr<WebGPUDevice> WebGPUDevice::create(Ref<WebGPUAdapter>&& adapter)
48 auto device = GPUDevice::create(); // FIXME: Take adapter into account when creating m_device.
52 return adoptRef(new WebGPUDevice(WTFMove(adapter), WTFMove(device)));
55 WebGPUDevice::WebGPUDevice(Ref<WebGPUAdapter>&& adapter, RefPtr<GPUDevice>&& device)
56 : m_adapter(WTFMove(adapter))
59 UNUSED_PARAM(m_adapter);
62 RefPtr<WebGPUShaderModule> WebGPUDevice::createShaderModule(WebGPUShaderModuleDescriptor&& descriptor) const
64 return WebGPUShaderModule::create(m_device->createShaderModule(GPUShaderModuleDescriptor { descriptor.code }));
67 RefPtr<WebGPURenderPipeline> WebGPUDevice::createRenderPipeline(WebGPURenderPipelineDescriptor&& descriptor) const
69 const char* const functionName = "WebGPUDevice::createRenderPipeline()";
71 UNUSED_PARAM(functionName);
74 if (descriptor.stages.isEmpty()) {
75 LOG(WebGPU, "%s: No stages in WebGPURenderPipelineDescriptor!", functionName);
79 GPUPipelineStageDescriptor vertexStage;
80 GPUPipelineStageDescriptor fragmentStage;
82 for (const auto& stageDescriptor : descriptor.stages) {
83 if (!stageDescriptor.module || !stageDescriptor.module->module() || stageDescriptor.entryPoint.isEmpty()) {
84 LOG(WebGPU, "%s: Invalid WebGPUPipelineStageDescriptor!", functionName);
88 switch (stageDescriptor.stage) {
89 case WebGPUShaderStage::VERTEX:
90 if (vertexStage.module) {
91 LOG(WebGPU, "%s: Multiple vertex stages in WebGPURenderPipelineDescriptor!", functionName);
95 vertexStage.module = stageDescriptor.module->module();
96 vertexStage.entryPoint = stageDescriptor.entryPoint;
98 case WebGPUShaderStage::FRAGMENT:
99 if (fragmentStage.module) {
100 LOG(WebGPU, "%s: Multiple fragment stages in WebGPURenderPipelineDescriptor!", functionName);
104 fragmentStage.module = stageDescriptor.module->module();
105 fragmentStage.entryPoint = stageDescriptor.entryPoint;
108 LOG(WebGPU, "%s: Invalid shader stage in WebGPURenderPipelineDescriptor!", functionName);
113 // Metal (if not other APIs) requires at least the vertex shader.
114 if (!vertexStage.module || vertexStage.entryPoint.isEmpty()) {
115 LOG(WebGPU, "%s: Invalid vertex stage in WebGPURenderPipelineDescriptor!", functionName);
119 return WebGPURenderPipeline::create(m_device->createRenderPipeline(GPURenderPipelineDescriptor { WTFMove(vertexStage), WTFMove(fragmentStage), descriptor.primitiveTopology }));
122 RefPtr<WebGPUCommandBuffer> WebGPUDevice::createCommandBuffer() const
124 return WebGPUCommandBuffer::create(m_device->createCommandBuffer());
127 } // namespace WebCore
129 #endif // ENABLE(WEBGPU)