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 "WebGPUQueue.h"
39 #include "WebGPURenderPipeline.h"
40 #include "WebGPURenderPipelineDescriptor.h"
41 #include "WebGPUShaderModule.h"
42 #include "WebGPUShaderModuleDescriptor.h"
43 #include "WebGPUShaderStage.h"
47 RefPtr<WebGPUDevice> WebGPUDevice::create(Ref<WebGPUAdapter>&& adapter)
49 auto device = GPUDevice::create(); // FIXME: Take adapter into account when creating m_device.
53 return adoptRef(new WebGPUDevice(WTFMove(adapter), WTFMove(device)));
56 WebGPUDevice::WebGPUDevice(Ref<WebGPUAdapter>&& adapter, RefPtr<GPUDevice>&& device)
57 : m_adapter(WTFMove(adapter))
60 UNUSED_PARAM(m_adapter);
63 RefPtr<WebGPUShaderModule> WebGPUDevice::createShaderModule(WebGPUShaderModuleDescriptor&& descriptor) const
65 return WebGPUShaderModule::create(m_device->createShaderModule(GPUShaderModuleDescriptor { descriptor.code }));
68 RefPtr<WebGPURenderPipeline> WebGPUDevice::createRenderPipeline(WebGPURenderPipelineDescriptor&& descriptor) const
70 const char* const functionName = "WebGPUDevice::createRenderPipeline()";
72 UNUSED_PARAM(functionName);
75 if (descriptor.stages.isEmpty()) {
76 LOG(WebGPU, "%s: No stages in WebGPURenderPipelineDescriptor!", functionName);
80 GPUPipelineStageDescriptor vertexStage;
81 GPUPipelineStageDescriptor fragmentStage;
83 for (const auto& stageDescriptor : descriptor.stages) {
84 if (!stageDescriptor.module || !stageDescriptor.module->module() || stageDescriptor.entryPoint.isEmpty()) {
85 LOG(WebGPU, "%s: Invalid WebGPUPipelineStageDescriptor!", functionName);
89 switch (stageDescriptor.stage) {
90 case WebGPUShaderStage::VERTEX:
91 if (vertexStage.module) {
92 LOG(WebGPU, "%s: Multiple vertex stages in WebGPURenderPipelineDescriptor!", functionName);
96 vertexStage.module = stageDescriptor.module->module();
97 vertexStage.entryPoint = stageDescriptor.entryPoint;
99 case WebGPUShaderStage::FRAGMENT:
100 if (fragmentStage.module) {
101 LOG(WebGPU, "%s: Multiple fragment stages in WebGPURenderPipelineDescriptor!", functionName);
105 fragmentStage.module = stageDescriptor.module->module();
106 fragmentStage.entryPoint = stageDescriptor.entryPoint;
109 LOG(WebGPU, "%s: Invalid shader stage in WebGPURenderPipelineDescriptor!", functionName);
114 // Metal (if not other APIs) requires at least the vertex shader.
115 if (!vertexStage.module || vertexStage.entryPoint.isEmpty()) {
116 LOG(WebGPU, "%s: Invalid vertex stage in WebGPURenderPipelineDescriptor!", functionName);
120 return WebGPURenderPipeline::create(m_device->createRenderPipeline(GPURenderPipelineDescriptor { WTFMove(vertexStage), WTFMove(fragmentStage), descriptor.primitiveTopology }));
123 RefPtr<WebGPUCommandBuffer> WebGPUDevice::createCommandBuffer() const
125 return WebGPUCommandBuffer::create(m_device->createCommandBuffer());
128 RefPtr<WebGPUQueue> WebGPUDevice::getQueue()
131 m_queue = WebGPUQueue::create(m_device->getQueue());
136 } // namespace WebCore
138 #endif // ENABLE(WEBGPU)