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 "WebGPUDevice.h" |
28 | |
29 | #if ENABLE(WEBGPU) |
30 | |
31 | #include "GPUBindGroup.h" |
32 | #include "GPUBindGroupBinding.h" |
33 | #include "GPUBindGroupDescriptor.h" |
34 | #include "GPUBindGroupLayoutDescriptor.h" |
35 | #include "GPUBufferBinding.h" |
36 | #include "GPUBufferDescriptor.h" |
37 | #include "GPUCommandBuffer.h" |
38 | #include "GPUPipelineStageDescriptor.h" |
39 | #include "GPURenderPipelineDescriptor.h" |
40 | #include "GPUSampler.h" |
41 | #include "GPUSamplerDescriptor.h" |
42 | #include "GPUShaderModuleDescriptor.h" |
43 | #include "GPUTextureDescriptor.h" |
44 | #include "JSDOMConvertBufferSource.h" |
45 | #include "JSWebGPUBuffer.h" |
46 | #include "Logging.h" |
47 | #include "WebGPUBindGroup.h" |
48 | #include "WebGPUBindGroupBinding.h" |
49 | #include "WebGPUBindGroupDescriptor.h" |
50 | #include "WebGPUBindGroupLayout.h" |
51 | #include "WebGPUBufferBinding.h" |
52 | #include "WebGPUCommandEncoder.h" |
53 | #include "WebGPUComputePipeline.h" |
54 | #include "WebGPUComputePipelineDescriptor.h" |
55 | #include "WebGPUPipelineLayout.h" |
56 | #include "WebGPUPipelineLayoutDescriptor.h" |
57 | #include "WebGPUPipelineStageDescriptor.h" |
58 | #include "WebGPUQueue.h" |
59 | #include "WebGPURenderPipeline.h" |
60 | #include "WebGPURenderPipelineDescriptor.h" |
61 | #include "WebGPUSampler.h" |
62 | #include "WebGPUShaderModule.h" |
63 | #include "WebGPUShaderModuleDescriptor.h" |
64 | #include "WebGPUSwapChain.h" |
65 | #include "WebGPUTexture.h" |
66 | |
67 | namespace WebCore { |
68 | |
69 | RefPtr<WebGPUDevice> WebGPUDevice::tryCreate(Ref<const WebGPUAdapter>&& adapter) |
70 | { |
71 | if (auto device = GPUDevice::tryCreate(adapter->options())) |
72 | return adoptRef(new WebGPUDevice(WTFMove(adapter), device.releaseNonNull())); |
73 | return nullptr; |
74 | } |
75 | |
76 | WebGPUDevice::WebGPUDevice(Ref<const WebGPUAdapter>&& adapter, Ref<GPUDevice>&& device) |
77 | : m_adapter(WTFMove(adapter)) |
78 | , m_device(WTFMove(device)) |
79 | { |
80 | } |
81 | |
82 | Ref<WebGPUBuffer> WebGPUDevice::createBuffer(const GPUBufferDescriptor& descriptor) const |
83 | { |
84 | auto buffer = m_device->tryCreateBuffer(descriptor); |
85 | return WebGPUBuffer::create(WTFMove(buffer)); |
86 | } |
87 | |
88 | Vector<JSC::JSValue> WebGPUDevice::createBufferMapped(JSC::ExecState& state, const GPUBufferDescriptor& descriptor) const |
89 | { |
90 | JSC::JSValue wrappedArrayBuffer = JSC::jsNull(); |
91 | |
92 | auto buffer = m_device->tryCreateBuffer(descriptor, true); |
93 | if (buffer) { |
94 | auto arrayBuffer = buffer->mapOnCreation(); |
95 | wrappedArrayBuffer = toJS(&state, JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), arrayBuffer); |
96 | } |
97 | |
98 | auto webBuffer = WebGPUBuffer::create(WTFMove(buffer)); |
99 | auto wrappedWebBuffer = toJS(&state, JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), webBuffer); |
100 | |
101 | return { wrappedWebBuffer, wrappedArrayBuffer }; |
102 | } |
103 | |
104 | Ref<WebGPUTexture> WebGPUDevice::createTexture(const GPUTextureDescriptor& descriptor) const |
105 | { |
106 | auto texture = m_device->tryCreateTexture(descriptor); |
107 | return WebGPUTexture::create(WTFMove(texture)); |
108 | } |
109 | |
110 | Ref<WebGPUSampler> WebGPUDevice::createSampler(const GPUSamplerDescriptor& descriptor) const |
111 | { |
112 | auto sampler = m_device->tryCreateSampler(descriptor); |
113 | return WebGPUSampler::create(WTFMove(sampler)); |
114 | } |
115 | |
116 | Ref<WebGPUBindGroupLayout> WebGPUDevice::createBindGroupLayout(const GPUBindGroupLayoutDescriptor& descriptor) const |
117 | { |
118 | auto layout = m_device->tryCreateBindGroupLayout(descriptor); |
119 | return WebGPUBindGroupLayout::create(WTFMove(layout)); |
120 | } |
121 | |
122 | Ref<WebGPUPipelineLayout> WebGPUDevice::createPipelineLayout(const WebGPUPipelineLayoutDescriptor& descriptor) const |
123 | { |
124 | auto gpuDescriptor = descriptor.tryCreateGPUPipelineLayoutDescriptor(); |
125 | if (!gpuDescriptor) |
126 | return WebGPUPipelineLayout::create(nullptr); |
127 | |
128 | auto layout = m_device->createPipelineLayout(WTFMove(*gpuDescriptor)); |
129 | return WebGPUPipelineLayout::create(WTFMove(layout)); |
130 | } |
131 | |
132 | Ref<WebGPUBindGroup> WebGPUDevice::createBindGroup(const WebGPUBindGroupDescriptor& descriptor) const |
133 | { |
134 | auto gpuDescriptor = descriptor.tryCreateGPUBindGroupDescriptor(); |
135 | if (!gpuDescriptor) |
136 | return WebGPUBindGroup::create(nullptr); |
137 | |
138 | auto bindGroup = GPUBindGroup::tryCreate(*gpuDescriptor); |
139 | return WebGPUBindGroup::create(WTFMove(bindGroup)); |
140 | } |
141 | |
142 | Ref<WebGPUShaderModule> WebGPUDevice::createShaderModule(const WebGPUShaderModuleDescriptor& descriptor) const |
143 | { |
144 | // FIXME: What can be validated here? |
145 | auto module = m_device->tryCreateShaderModule(GPUShaderModuleDescriptor { descriptor.code, descriptor.isWHLSL }); |
146 | return WebGPUShaderModule::create(WTFMove(module)); |
147 | } |
148 | |
149 | Ref<WebGPURenderPipeline> WebGPUDevice::createRenderPipeline(const WebGPURenderPipelineDescriptor& descriptor) const |
150 | { |
151 | auto gpuDescriptor = descriptor.tryCreateGPURenderPipelineDescriptor(); |
152 | if (!gpuDescriptor) |
153 | return WebGPURenderPipeline::create(nullptr); |
154 | |
155 | auto pipeline = m_device->tryCreateRenderPipeline(*gpuDescriptor); |
156 | return WebGPURenderPipeline::create(WTFMove(pipeline)); |
157 | } |
158 | |
159 | Ref<WebGPUComputePipeline> WebGPUDevice::createComputePipeline(const WebGPUComputePipelineDescriptor& descriptor) const |
160 | { |
161 | auto gpuDescriptor = descriptor.tryCreateGPUComputePipelineDescriptor(); |
162 | if (!gpuDescriptor) |
163 | return WebGPUComputePipeline::create(nullptr); |
164 | |
165 | auto pipeline = m_device->tryCreateComputePipeline(*gpuDescriptor); |
166 | return WebGPUComputePipeline::create(WTFMove(pipeline)); |
167 | } |
168 | |
169 | Ref<WebGPUCommandEncoder> WebGPUDevice::createCommandEncoder() const |
170 | { |
171 | auto commandBuffer = m_device->tryCreateCommandBuffer(); |
172 | return WebGPUCommandEncoder::create(WTFMove(commandBuffer)); |
173 | } |
174 | |
175 | Ref<WebGPUQueue> WebGPUDevice::getQueue() const |
176 | { |
177 | if (!m_queue) |
178 | m_queue = WebGPUQueue::create(m_device->tryGetQueue()); |
179 | |
180 | return makeRef(*m_queue.get()); |
181 | } |
182 | |
183 | } // namespace WebCore |
184 | |
185 | #endif // ENABLE(WEBGPU) |
186 | |