From 442f0b9a13b98a9f117b8bca7740f0a5f88f3e68 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Thu, 9 Jan 2025 19:17:19 +0000 Subject: [PATCH] LibWeb/WebGL: Implement OES_vertex_array_object extension --- Libraries/LibWeb/CMakeLists.txt | 2 + .../LibWeb/WebGL/OESVertexArrayObject.cpp | 105 ++++++++++++++++++ Libraries/LibWeb/WebGL/OESVertexArrayObject.h | 38 +++++++ .../LibWeb/WebGL/OESVertexArrayObject.idl | 21 ++++ .../LibWeb/WebGL/WebGLRenderingContext.cpp | 4 + .../WebGL/WebGLVertexArrayObjectOES.cpp | 35 ++++++ .../LibWeb/WebGL/WebGLVertexArrayObjectOES.h | 30 +++++ .../WebGL/WebGLVertexArrayObjectOES.idl | 6 + Libraries/LibWeb/idl_files.cmake | 2 + .../BindingsGenerator/IDLGenerators.cpp | 1 + 10 files changed, 244 insertions(+) create mode 100644 Libraries/LibWeb/WebGL/OESVertexArrayObject.cpp create mode 100644 Libraries/LibWeb/WebGL/OESVertexArrayObject.h create mode 100644 Libraries/LibWeb/WebGL/OESVertexArrayObject.idl create mode 100644 Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.cpp create mode 100644 Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.h create mode 100644 Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.idl diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 97bcb85204a..c5badacecf1 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -825,6 +825,7 @@ set(SOURCES WebDriver/TimeoutsConfiguration.cpp WebGL/ANGLEInstancedArrays.cpp WebGL/EventNames.cpp + WebGL/OESVertexArrayObject.cpp WebGL/OpenGLContext.cpp WebGL/WebGL2RenderingContext.cpp WebGL/WebGLActiveInfo.cpp @@ -845,6 +846,7 @@ set(SOURCES WebGL/WebGLTransformFeedback.cpp WebGL/WebGLUniformLocation.cpp WebGL/WebGLVertexArrayObject.cpp + WebGL/WebGLVertexArrayObjectOES.cpp WebIDL/AbstractOperations.cpp WebIDL/Buffers.cpp WebIDL/CallbackType.cpp diff --git a/Libraries/LibWeb/WebGL/OESVertexArrayObject.cpp b/Libraries/LibWeb/WebGL/OESVertexArrayObject.cpp new file mode 100644 index 00000000000..49951e21b0e --- /dev/null +++ b/Libraries/LibWeb/WebGL/OESVertexArrayObject.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +#define GL_GLEXT_PROTOTYPES 1 +#include +#include + +namespace Web::WebGL { + +GC_DEFINE_ALLOCATOR(OESVertexArrayObject); + +JS::ThrowCompletionOr> OESVertexArrayObject::create(JS::Realm& realm, GC::Ref context) +{ + return realm.create(realm, context); +} + +OESVertexArrayObject::OESVertexArrayObject(JS::Realm& realm, GC::Ref context) + : PlatformObject(realm) + , m_context(context) +{ + m_context->context().request_extension("GL_OES_vertex_array_object"); +} + +GC::Ref OESVertexArrayObject::create_vertex_array_oes() +{ + m_context->context().make_current(); + + GLuint handle = 0; + glGenVertexArraysOES(1, &handle); + return WebGLVertexArrayObjectOES::create(realm(), m_context, handle); +} + +void OESVertexArrayObject::delete_vertex_array_oes(GC::Root array_object) +{ + m_context->context().make_current(); + + GLuint vertex_array_handle = 0; + if (array_object) { + auto handle_or_error = array_object->handle(m_context.ptr()); + if (handle_or_error.is_error()) { + // FIXME: m_context->set_error(GL_INVALID_OPERATION); + return; + } + vertex_array_handle = handle_or_error.release_value(); + } + + glDeleteVertexArraysOES(1, &vertex_array_handle); +} + +bool OESVertexArrayObject::is_vertex_array_oes(GC::Root array_object) +{ + m_context->context().make_current(); + + GLuint vertex_array_handle = 0; + if (array_object) { + auto handle_or_error = array_object->handle(m_context.ptr()); + if (handle_or_error.is_error()) { + return false; + } + vertex_array_handle = handle_or_error.release_value(); + } + + return glIsVertexArrayOES(vertex_array_handle) == GL_TRUE; +} + +void OESVertexArrayObject::bind_vertex_array_oes(GC::Root array_object) +{ + m_context->context().make_current(); + + GLuint vertex_array_handle = 0; + if (array_object) { + auto handle_or_error = array_object->handle(m_context.ptr()); + if (handle_or_error.is_error()) { + // FIXME: m_context->set_error(GL_INVALID_OPERATION); + return; + } + vertex_array_handle = handle_or_error.release_value(); + } + + glBindVertexArrayOES(vertex_array_handle); +} + +void OESVertexArrayObject::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(OESVertexArrayObject); +} + +void OESVertexArrayObject::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_context); +} + +} diff --git a/Libraries/LibWeb/WebGL/OESVertexArrayObject.h b/Libraries/LibWeb/WebGL/OESVertexArrayObject.h new file mode 100644 index 00000000000..ef34332518c --- /dev/null +++ b/Libraries/LibWeb/WebGL/OESVertexArrayObject.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::WebGL { + +class OESVertexArrayObject : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(OESVertexArrayObject, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(OESVertexArrayObject); + +public: + static JS::ThrowCompletionOr> create(JS::Realm&, GC::Ref); + + GC::Ref create_vertex_array_oes(); + void delete_vertex_array_oes(GC::Root array_object); + bool is_vertex_array_oes(GC::Root array_object); + void bind_vertex_array_oes(GC::Root array_object); + +protected: + void initialize(JS::Realm&) override; + void visit_edges(Visitor&) override; + +private: + OESVertexArrayObject(JS::Realm&, GC::Ref); + + GC::Ref m_context; +}; + +} diff --git a/Libraries/LibWeb/WebGL/OESVertexArrayObject.idl b/Libraries/LibWeb/WebGL/OESVertexArrayObject.idl new file mode 100644 index 00000000000..177f2ce9126 --- /dev/null +++ b/Libraries/LibWeb/WebGL/OESVertexArrayObject.idl @@ -0,0 +1,21 @@ +#import +#import + +// https://registry.khronos.org/webgl/extensions/OES_vertex_array_object/ +// NOTE: Original OES_vertex_array_object name is changed to title case, +// so it matches corresponding C++ class name, and does not require +// IDL generator to handle snake_case to TitleCase conversion. +// Having a different name is totally fine, because LegacyNoInterfaceObject +// prevents the name from being exposed to JavaScript. +[Exposed=(Window,Worker), LegacyNoInterfaceObject] +interface OESVertexArrayObject { + // Same note as WebGLRenderingContextBase: + // Functions that have the [WebGLHandlesContextLoss] extended attribute in the spec do not have them here. + + const GLenum VERTEX_ARRAY_BINDING_OES = 0x85B5; + + WebGLVertexArrayObjectOES createVertexArrayOES(); + undefined deleteVertexArrayOES(WebGLVertexArrayObjectOES? arrayObject); + GLboolean isVertexArrayOES(WebGLVertexArrayObjectOES? arrayObject); + undefined bindVertexArrayOES(WebGLVertexArrayObjectOES? arrayObject); +}; diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index b03f58263c0..a88b4179220 100644 --- a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp +++ b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,9 @@ JS::Object* WebGLRenderingContext::get_extension(String const& name) if (name == "ANGLE_instanced_arrays"sv) { return MUST(ANGLEInstancedArrays::create(realm())); } + if (name == "OES_vertex_array_object"sv) { + return MUST(OESVertexArrayObject::create(realm(), *this)); + } return nullptr; } diff --git a/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.cpp b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.cpp new file mode 100644 index 00000000000..a047ddb76bd --- /dev/null +++ b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::WebGL { + +GC_DEFINE_ALLOCATOR(WebGLVertexArrayObjectOES); + +GC::Ref WebGLVertexArrayObjectOES::create(JS::Realm& realm, WebGLRenderingContextBase& context, GLuint handle) +{ + return realm.create(realm, context, handle); +} + +WebGLVertexArrayObjectOES::WebGLVertexArrayObjectOES(JS::Realm& realm, WebGLRenderingContextBase& context, GLuint handle) + : WebGLObject(realm, context, handle) +{ +} + +WebGLVertexArrayObjectOES::~WebGLVertexArrayObjectOES() = default; + +void WebGLVertexArrayObjectOES::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLVertexArrayObjectOES); +} + +} diff --git a/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.h b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.h new file mode 100644 index 00000000000..8cfc7d0d562 --- /dev/null +++ b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::WebGL { + +class WebGLVertexArrayObjectOES : public WebGLObject { + WEB_PLATFORM_OBJECT(WebGLVertexArrayObjectOES, WebGLObject); + GC_DECLARE_ALLOCATOR(WebGLVertexArrayObjectOES); + +public: + static GC::Ref create(JS::Realm& realm, WebGLRenderingContextBase&, GLuint handle); + + virtual ~WebGLVertexArrayObjectOES() override; + +protected: + explicit WebGLVertexArrayObjectOES(JS::Realm&, WebGLRenderingContextBase&, GLuint handle); + + virtual void initialize(JS::Realm&) override; +}; + +} diff --git a/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.idl b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.idl new file mode 100644 index 00000000000..6694fa6b37e --- /dev/null +++ b/Libraries/LibWeb/WebGL/WebGLVertexArrayObjectOES.idl @@ -0,0 +1,6 @@ +#import + +// https://registry.khronos.org/webgl/extensions/OES_vertex_array_object/ +[Exposed=(Window,Worker), LegacyNoInterfaceObject] +interface WebGLVertexArrayObjectOES : WebGLObject { +}; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 0a36712793a..f929040d7e4 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -381,6 +381,7 @@ libweb_js_bindings(WebAudio/PannerNode) libweb_js_bindings(WebAudio/PeriodicWave) libweb_js_bindings(WebAudio/StereoPannerNode) libweb_js_bindings(WebGL/ANGLEInstancedArrays) +libweb_js_bindings(WebGL/OESVertexArrayObject) libweb_js_bindings(WebGL/WebGL2RenderingContext) libweb_js_bindings(WebGL/WebGLActiveInfo) libweb_js_bindings(WebGL/WebGLBuffer) @@ -399,6 +400,7 @@ libweb_js_bindings(WebGL/WebGLTexture) libweb_js_bindings(WebGL/WebGLTransformFeedback) libweb_js_bindings(WebGL/WebGLUniformLocation) libweb_js_bindings(WebGL/WebGLVertexArrayObject) +libweb_js_bindings(WebGL/WebGLVertexArrayObjectOES) libweb_js_bindings(WebIDL/DOMException) libweb_js_bindings(WebSockets/WebSocket) libweb_js_bindings(WebVTT/VTTCue) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 57f5751f6e5..df69f22cf8e 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -123,6 +123,7 @@ static bool is_platform_object(Type const& type) "WebGLTexture"sv, "WebGLUniformLocation"sv, "WebGLVertexArrayObject"sv, + "WebGLVertexArrayObjectOES"sv, "Window"sv, "WindowProxy"sv, "WritableStream"sv,