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#pragma once
27
28#if ENABLE(WEBGPU)
29
30#include "GPUQueue.h"
31#include "GPUSwapChain.h"
32#include <wtf/RefCounted.h>
33#include <wtf/RetainPtr.h>
34#include <wtf/WeakPtr.h>
35
36OBJC_PROTOCOL(MTLDevice);
37
38namespace WebCore {
39
40class GPUBindGroupLayout;
41class GPUBuffer;
42class GPUCommandBuffer;
43class GPUComputePipeline;
44class GPUPipelineLayout;
45class GPURenderPipeline;
46class GPUSampler;
47class GPUShaderModule;
48class GPUTexture;
49
50struct GPUBindGroupLayoutDescriptor;
51struct GPUBufferDescriptor;
52struct GPUComputePipelineDescriptor;
53struct GPUPipelineLayoutDescriptor;
54struct GPURenderPipelineDescriptor;
55struct GPURequestAdapterOptions;
56struct GPUSamplerDescriptor;
57struct GPUShaderModuleDescriptor;
58struct GPUSwapChainDescriptor;
59struct GPUTextureDescriptor;
60
61using PlatformDevice = MTLDevice;
62using PlatformDeviceSmartPtr = RetainPtr<MTLDevice>;
63
64class GPUDevice : public RefCounted<GPUDevice>, public CanMakeWeakPtr<GPUDevice> {
65public:
66 static RefPtr<GPUDevice> tryCreate(const Optional<GPURequestAdapterOptions>&);
67
68 RefPtr<GPUBuffer> tryCreateBuffer(const GPUBufferDescriptor&);
69 RefPtr<GPUTexture> tryCreateTexture(const GPUTextureDescriptor&) const;
70 RefPtr<GPUSampler> tryCreateSampler(const GPUSamplerDescriptor&) const;
71
72 RefPtr<GPUBindGroupLayout> tryCreateBindGroupLayout(const GPUBindGroupLayoutDescriptor&) const;
73 Ref<GPUPipelineLayout> createPipelineLayout(GPUPipelineLayoutDescriptor&&) const;
74
75 RefPtr<GPUShaderModule> tryCreateShaderModule(const GPUShaderModuleDescriptor&) const;
76 RefPtr<GPURenderPipeline> tryCreateRenderPipeline(const GPURenderPipelineDescriptor&) const;
77 RefPtr<GPUComputePipeline> tryCreateComputePipeline(const GPUComputePipelineDescriptor&) const;
78
79 RefPtr<GPUCommandBuffer> tryCreateCommandBuffer() const;
80
81 RefPtr<GPUQueue> tryGetQueue() const;
82
83 PlatformDevice* platformDevice() const { return m_platformDevice.get(); }
84 GPUSwapChain* swapChain() const { return m_swapChain.get(); }
85 void setSwapChain(RefPtr<GPUSwapChain>&&);
86
87private:
88 explicit GPUDevice(PlatformDeviceSmartPtr&&);
89
90 PlatformDeviceSmartPtr m_platformDevice;
91 mutable RefPtr<GPUQueue> m_queue;
92 RefPtr<GPUSwapChain> m_swapChain;
93};
94
95} // namespace WebCore
96
97#endif // ENABLE(WEBGPU)
98