diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 295c66f2953..4c77e7ad6fd 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -825,6 +825,7 @@ set(SOURCES WebDriver/TimeoutsConfiguration.cpp WebGL/Extensions/ANGLEInstancedArrays.cpp WebGL/Extensions/OESVertexArrayObject.cpp + WebGL/Extensions/WebGLDrawBuffers.cpp WebGL/Extensions/WebGLVertexArrayObjectOES.cpp WebGL/EventNames.cpp WebGL/OpenGLContext.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index a611792d26f..b909958aeb7 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -863,6 +863,7 @@ class WebGLVertexArrayObject; namespace Web::WebGL::Extensions { class ANGLEInstancedArrays; class OESVertexArrayObject; +class WebGLDrawBuffers; class WebGLVertexArrayObjectOES; } diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.cpp b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.cpp new file mode 100644 index 00000000000..47a25c85aea --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.cpp @@ -0,0 +1,52 @@ +/* + * 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::Extensions { + +GC_DEFINE_ALLOCATOR(WebGLDrawBuffers); + +JS::ThrowCompletionOr> WebGLDrawBuffers::create(JS::Realm& realm, GC::Ref context) +{ + return realm.create(realm, context); +} + +WebGLDrawBuffers::WebGLDrawBuffers(JS::Realm& realm, GC::Ref context) + : PlatformObject(realm) + , m_context(context) +{ + m_context->context().request_extension("GL_EXT_draw_buffers"); +} + +void WebGLDrawBuffers::draw_buffers_webgl(Vector buffers) +{ + m_context->context().make_current(); + glDrawBuffersEXT(buffers.size(), buffers.data()); +} + +void WebGLDrawBuffers::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLDrawBuffers); +} + +void WebGLDrawBuffers::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_context); +} + +} diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.h b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.h new file mode 100644 index 00000000000..a63087de6ba --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::WebGL::Extensions { + +class WebGLDrawBuffers : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(WebGLDrawBuffers, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(WebGLDrawBuffers); + +public: + static JS::ThrowCompletionOr> create(JS::Realm&, GC::Ref); + + void draw_buffers_webgl(Vector buffers); + +protected: + void initialize(JS::Realm&) override; + void visit_edges(Visitor&) override; + +private: + WebGLDrawBuffers(JS::Realm&, GC::Ref); + + GC::Ref m_context; +}; + +} diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.idl b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.idl new file mode 100644 index 00000000000..514a9f55eb9 --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLDrawBuffers.idl @@ -0,0 +1,49 @@ +#import + +// https://registry.khronos.org/webgl/extensions/WEBGL_draw_buffers/ +// NOTE: Original WEBGL_draw_buffers 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 WebGLDrawBuffers { + const GLenum COLOR_ATTACHMENT0_WEBGL = 0x8CE0; + const GLenum COLOR_ATTACHMENT1_WEBGL = 0x8CE1; + const GLenum COLOR_ATTACHMENT2_WEBGL = 0x8CE2; + const GLenum COLOR_ATTACHMENT3_WEBGL = 0x8CE3; + const GLenum COLOR_ATTACHMENT4_WEBGL = 0x8CE4; + const GLenum COLOR_ATTACHMENT5_WEBGL = 0x8CE5; + const GLenum COLOR_ATTACHMENT6_WEBGL = 0x8CE6; + const GLenum COLOR_ATTACHMENT7_WEBGL = 0x8CE7; + const GLenum COLOR_ATTACHMENT8_WEBGL = 0x8CE8; + const GLenum COLOR_ATTACHMENT9_WEBGL = 0x8CE9; + const GLenum COLOR_ATTACHMENT10_WEBGL = 0x8CEA; + const GLenum COLOR_ATTACHMENT11_WEBGL = 0x8CEB; + const GLenum COLOR_ATTACHMENT12_WEBGL = 0x8CEC; + const GLenum COLOR_ATTACHMENT13_WEBGL = 0x8CED; + const GLenum COLOR_ATTACHMENT14_WEBGL = 0x8CEE; + const GLenum COLOR_ATTACHMENT15_WEBGL = 0x8CEF; + + const GLenum DRAW_BUFFER0_WEBGL = 0x8825; + const GLenum DRAW_BUFFER1_WEBGL = 0x8826; + const GLenum DRAW_BUFFER2_WEBGL = 0x8827; + const GLenum DRAW_BUFFER3_WEBGL = 0x8828; + const GLenum DRAW_BUFFER4_WEBGL = 0x8829; + const GLenum DRAW_BUFFER5_WEBGL = 0x882A; + const GLenum DRAW_BUFFER6_WEBGL = 0x882B; + const GLenum DRAW_BUFFER7_WEBGL = 0x882C; + const GLenum DRAW_BUFFER8_WEBGL = 0x882D; + const GLenum DRAW_BUFFER9_WEBGL = 0x882E; + const GLenum DRAW_BUFFER10_WEBGL = 0x882F; + const GLenum DRAW_BUFFER11_WEBGL = 0x8830; + const GLenum DRAW_BUFFER12_WEBGL = 0x8831; + const GLenum DRAW_BUFFER13_WEBGL = 0x8832; + const GLenum DRAW_BUFFER14_WEBGL = 0x8833; + const GLenum DRAW_BUFFER15_WEBGL = 0x8834; + + const GLenum MAX_COLOR_ATTACHMENTS_WEBGL = 0x8CDF; + const GLenum MAX_DRAW_BUFFERS_WEBGL = 0x8824; + + undefined drawBuffersWEBGL(sequence buffers); +}; diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index 6351c8b2641..36cae79fe3e 100644 --- a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp +++ b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -92,6 +93,7 @@ void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor) visitor.visit(m_canvas_element); visitor.visit(m_angle_instanced_arrays_extension); visitor.visit(m_oes_vertex_array_object_extension); + visitor.visit(m_webgl_draw_buffers_extension); } void WebGLRenderingContext::present() @@ -207,6 +209,15 @@ JS::Object* WebGLRenderingContext::get_extension(String const& name) return m_oes_vertex_array_object_extension; } + if (Infra::is_ascii_case_insensitive_match(name, "WEBGL_draw_buffers"sv)) { + if (!m_webgl_draw_buffers_extension) { + m_webgl_draw_buffers_extension = MUST(Extensions::WebGLDrawBuffers::create(realm(), *this)); + } + + VERIFY(m_webgl_draw_buffers_extension); + return m_webgl_draw_buffers_extension; + } + return nullptr; } diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.h b/Libraries/LibWeb/WebGL/WebGLRenderingContext.h index dc7d3372c9b..3da42ee436b 100644 --- a/Libraries/LibWeb/WebGL/WebGLRenderingContext.h +++ b/Libraries/LibWeb/WebGL/WebGLRenderingContext.h @@ -83,6 +83,7 @@ private: // "Multiple calls to getExtension with the same extension string, taking into account case-insensitive comparison, must return the same object as long as the extension is enabled." GC::Ptr m_angle_instanced_arrays_extension; GC::Ptr m_oes_vertex_array_object_extension; + GC::Ptr m_webgl_draw_buffers_extension; virtual void set_error(GLenum error) override; }; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index ba26f69874f..8e6610a549a 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -382,6 +382,7 @@ libweb_js_bindings(WebAudio/PeriodicWave) libweb_js_bindings(WebAudio/StereoPannerNode) libweb_js_bindings(WebGL/Extensions/ANGLEInstancedArrays) libweb_js_bindings(WebGL/Extensions/OESVertexArrayObject) +libweb_js_bindings(WebGL/Extensions/WebGLDrawBuffers) libweb_js_bindings(WebGL/Extensions/WebGLVertexArrayObjectOES) libweb_js_bindings(WebGL/WebGL2RenderingContext) libweb_js_bindings(WebGL/WebGLActiveInfo)