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