Unreviewed, rolling out r250114.
[WebKit-https.git] / Source / WebCore / Modules / webgpu / WebGPUDevice.cpp
index ccd48d8..5ebf083 100644 (file)
 
 #if ENABLE(WEBGPU)
 
+#include "DOMWindow.h"
+#include "Document.h"
+#include "EventNames.h"
+#include "Exception.h"
+#include "GPUBindGroup.h"
+#include "GPUBindGroupBinding.h"
+#include "GPUBindGroupDescriptor.h"
+#include "GPUBindGroupLayoutDescriptor.h"
+#include "GPUBufferBinding.h"
+#include "GPUBufferDescriptor.h"
+#include "GPUCommandBuffer.h"
+#include "GPUComputePipelineDescriptor.h"
+#include "GPUProgrammableStageDescriptor.h"
+#include "GPURenderPipelineDescriptor.h"
+#include "GPUSampler.h"
+#include "GPUSamplerDescriptor.h"
+#include "GPUShaderModuleDescriptor.h"
+#include "GPUTextureDescriptor.h"
+#include "GPUUncapturedErrorEvent.h"
+#include "InspectorInstrumentation.h"
+#include "JSDOMConvertBufferSource.h"
+#include "JSGPUOutOfMemoryError.h"
+#include "JSGPUValidationError.h"
+#include "JSWebGPUBuffer.h"
+#include "Logging.h"
+#include "WebGPUBindGroup.h"
+#include "WebGPUBindGroupBinding.h"
+#include "WebGPUBindGroupDescriptor.h"
+#include "WebGPUBindGroupLayout.h"
+#include "WebGPUBufferBinding.h"
+#include "WebGPUCommandEncoder.h"
+#include "WebGPUComputePipeline.h"
+#include "WebGPUComputePipelineDescriptor.h"
+#include "WebGPUPipelineLayout.h"
+#include "WebGPUPipelineLayoutDescriptor.h"
+#include "WebGPUProgrammableStageDescriptor.h"
+#include "WebGPUQueue.h"
+#include "WebGPURenderPipeline.h"
+#include "WebGPURenderPipelineDescriptor.h"
+#include "WebGPUSampler.h"
+#include "WebGPUShaderModule.h"
+#include "WebGPUShaderModuleDescriptor.h"
+#include "WebGPUSwapChain.h"
+#include "WebGPUTexture.h"
+#include <JavaScriptCore/ConsoleMessage.h>
+#include <memory>
+#include <wtf/HashSet.h>
+#include <wtf/IsoMallocInlines.h>
+#include <wtf/Lock.h>
+#include <wtf/MainThread.h>
+#include <wtf/NeverDestroyed.h>
+#include <wtf/Optional.h>
+#include <wtf/Ref.h>
+#include <wtf/RefPtr.h>
+#include <wtf/Variant.h>
+#include <wtf/Vector.h>
+#include <wtf/text/WTFString.h>
+
 namespace WebCore {
 
-Ref<WebGPUDevice> WebGPUDevice::create(ScriptExecutionContext&, Ref<WebGPUAdapter>&& adapter)
+WTF_MAKE_ISO_ALLOCATED_IMPL(WebGPUDevice);
+
+RefPtr<WebGPUDevice> WebGPUDevice::tryCreate(ScriptExecutionContext& context, Ref<const WebGPUAdapter>&& adapter)
+{
+    if (auto device = GPUDevice::tryCreate(adapter->options()))
+        return adoptRef(new WebGPUDevice(context, WTFMove(adapter), device.releaseNonNull()));
+    return nullptr;
+}
+
+HashSet<WebGPUDevice*>& WebGPUDevice::instances(const LockHolder&)
 {
-    return adoptRef(*new WebGPUDevice(WTFMove(adapter)));
+    static NeverDestroyed<HashSet<WebGPUDevice*>> instances;
+    return instances;
 }
 
-WebGPUDevice::WebGPUDevice(Ref<WebGPUAdapter>&& adapter)
-    : m_adapter(WTFMove(adapter))
+Lock& WebGPUDevice::instancesMutex()
 {
-    UNUSED_PARAM(m_adapter);
+    static LazyNeverDestroyed<Lock> mutex;
+    static std::once_flag initializeMutex;
+    std::call_once(initializeMutex, [] {
+        mutex.construct();
+    });
+    return mutex.get();
+}
+
+WebGPUDevice::WebGPUDevice(ScriptExecutionContext& context, Ref<const WebGPUAdapter>&& adapter, Ref<GPUDevice>&& device)
+    : m_scriptExecutionContext(context)
+    , m_adapter(WTFMove(adapter))
+    , m_device(WTFMove(device))
+    , m_errorScopes(GPUErrorScopes::create([this, weakThis = makeWeakPtr(this)] (GPUError&& error) {
+        if (weakThis)
+            dispatchUncapturedError(WTFMove(error));
+    }))
+{
+    ASSERT(m_scriptExecutionContext.isDocument());
+
+    LockHolder lock(instancesMutex());
+    instances(lock).add(this);
+}
+
+WebGPUDevice::~WebGPUDevice()
+{
+    InspectorInstrumentation::willDestroyWebGPUDevice(*this);
+
+    LockHolder lock(instancesMutex());
+    ASSERT(instances(lock).contains(this));
+    instances(lock).remove(this);
+}
+
+Ref<WebGPUBuffer> WebGPUDevice::createBuffer(const GPUBufferDescriptor& descriptor) const
+{
+    m_errorScopes->setErrorPrefix("GPUDevice.createBuffer(): ");
+
+    auto buffer = m_device->tryCreateBuffer(descriptor, GPUBufferMappedOption::NotMapped, m_errorScopes);
+    return WebGPUBuffer::create(WTFMove(buffer), m_errorScopes);
+}
+
+Vector<JSC::JSValue> WebGPUDevice::createBufferMapped(JSC::ExecState& state, const GPUBufferDescriptor& descriptor) const
+{
+    m_errorScopes->setErrorPrefix("GPUDevice.createBufferMapped(): ");
+
+    JSC::JSValue wrappedArrayBuffer = JSC::jsNull();
+
+    auto buffer = m_device->tryCreateBuffer(descriptor, GPUBufferMappedOption::IsMapped, m_errorScopes);
+    if (buffer) {
+        auto arrayBuffer = buffer->mapOnCreation();
+        wrappedArrayBuffer = toJS(&state, JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), arrayBuffer);
+    }
+
+    auto webBuffer = WebGPUBuffer::create(WTFMove(buffer), m_errorScopes);
+    auto wrappedWebBuffer = toJS(&state, JSC::jsCast<JSDOMGlobalObject*>(state.lexicalGlobalObject()), webBuffer);
+
+    return { wrappedWebBuffer, wrappedArrayBuffer };
+}
+
+Ref<WebGPUTexture> WebGPUDevice::createTexture(const GPUTextureDescriptor& descriptor) const
+{
+    auto texture = m_device->tryCreateTexture(descriptor);
+    return WebGPUTexture::create(WTFMove(texture));
+}
+
+Ref<WebGPUSampler> WebGPUDevice::createSampler(const GPUSamplerDescriptor& descriptor) const
+{
+    auto sampler = m_device->tryCreateSampler(descriptor);
+    return WebGPUSampler::create(WTFMove(sampler));
+}
+
+Ref<WebGPUBindGroupLayout> WebGPUDevice::createBindGroupLayout(const GPUBindGroupLayoutDescriptor& descriptor) const
+{
+    auto layout = m_device->tryCreateBindGroupLayout(descriptor);
+    return WebGPUBindGroupLayout::create(WTFMove(layout));
+}
+
+Ref<WebGPUPipelineLayout> WebGPUDevice::createPipelineLayout(const WebGPUPipelineLayoutDescriptor& descriptor) const
+{
+    auto gpuDescriptor = descriptor.tryCreateGPUPipelineLayoutDescriptor();
+    if (!gpuDescriptor)
+        return WebGPUPipelineLayout::create(nullptr);
+    
+    auto layout = m_device->createPipelineLayout(WTFMove(*gpuDescriptor));
+    return WebGPUPipelineLayout::create(WTFMove(layout));
+}
+
+Ref<WebGPUBindGroup> WebGPUDevice::createBindGroup(const WebGPUBindGroupDescriptor& descriptor) const
+{
+    auto gpuDescriptor = descriptor.tryCreateGPUBindGroupDescriptor();
+    if (!gpuDescriptor)
+        return WebGPUBindGroup::create(nullptr);
+
+    auto bindGroup = m_device->tryCreateBindGroup(*gpuDescriptor, m_errorScopes);
+    return WebGPUBindGroup::create(WTFMove(bindGroup));
+}
+
+Ref<WebGPUShaderModule> WebGPUDevice::createShaderModule(const WebGPUShaderModuleDescriptor& descriptor) const
+{
+    // FIXME: What can be validated here?
+    auto module = m_device->tryCreateShaderModule(GPUShaderModuleDescriptor { descriptor.code });
+    return WebGPUShaderModule::create(WTFMove(module));
+}
+
+Ref<WebGPURenderPipeline> WebGPUDevice::createRenderPipeline(const WebGPURenderPipelineDescriptor& descriptor) const
+{
+    m_errorScopes->setErrorPrefix("GPUDevice.createRenderPipeline(): ");
+
+    auto gpuDescriptor = descriptor.tryCreateGPURenderPipelineDescriptor(m_errorScopes);
+    if (!gpuDescriptor)
+        return WebGPURenderPipeline::create(nullptr, m_errorScopes);
+
+    auto pipeline = m_device->tryCreateRenderPipeline(*gpuDescriptor, m_errorScopes);
+    return WebGPURenderPipeline::create(WTFMove(pipeline), m_errorScopes);
+}
+
+Ref<WebGPUComputePipeline> WebGPUDevice::createComputePipeline(const WebGPUComputePipelineDescriptor& descriptor) const
+{
+    m_errorScopes->setErrorPrefix("GPUDevice.createComputePipeline(): ");
+
+    auto gpuDescriptor = descriptor.tryCreateGPUComputePipelineDescriptor(m_errorScopes);
+    if (!gpuDescriptor)
+        return WebGPUComputePipeline::create(nullptr, m_errorScopes);
+
+    auto pipeline = m_device->tryCreateComputePipeline(*gpuDescriptor, m_errorScopes);
+    return WebGPUComputePipeline::create(WTFMove(pipeline), m_errorScopes);
+}
+
+Ref<WebGPUCommandEncoder> WebGPUDevice::createCommandEncoder() const
+{
+    auto commandBuffer = m_device->tryCreateCommandBuffer();
+    return WebGPUCommandEncoder::create(WTFMove(commandBuffer));
+}
+
+Ref<WebGPUQueue> WebGPUDevice::getQueue() const
+{
+    if (!m_queue)
+        m_queue = WebGPUQueue::create(m_device->tryGetQueue());
+
+    return makeRef(*m_queue.get());
+}
+
+void WebGPUDevice::popErrorScope(ErrorPromise&& promise)
+{
+    String failMessage;
+    Optional<GPUError> error = m_errorScopes->popErrorScope(failMessage);
+    if (failMessage.isEmpty())
+        promise.resolve(error);
+    else
+        promise.reject(Exception { OperationError, "GPUDevice::popErrorScope(): " + failMessage });
+}
+
+// Errors reported via the validation error event should also appear in the console as warnings.
+static void printValidationErrorToConsole(GPUError& error, ScriptExecutionContext& context)
+{
+    if (!WTF::holds_alternative<RefPtr<GPUValidationError>>(error))
+        return;
+
+    auto validationError = WTF::get<RefPtr<GPUValidationError>>(error);
+    if (!validationError)
+        return;
+
+    auto message = validationError->message();
+    auto consoleMessage = makeUnique<Inspector::ConsoleMessage>(MessageSource::Rendering, MessageType::Log, MessageLevel::Warning, message);
+
+    downcast<Document>(context).addConsoleMessage(WTFMove(consoleMessage));
+}
+
+void WebGPUDevice::dispatchUncapturedError(GPUError&& error)
+{
+    printValidationErrorToConsole(error, m_scriptExecutionContext);
+
+    callOnMainThread([error = WTFMove(error), this, protectedThis = makeRef(*this)] () mutable {
+        dispatchEvent(GPUUncapturedErrorEvent::create(eventNames().uncapturederrorEvent, WTFMove(error)));
+    });
 }
 
 } // namespace WebCore