LibWeb/WebGL: Implement WEBGL_draw_buffers extension

This commit is contained in:
Luke Wilde 2025-01-10 16:36:00 +00:00 committed by Andreas Kling
parent de77a5e3ea
commit 55f7cb4f10
Notes: github-actions[bot] 2025-01-21 20:37:38 +00:00
8 changed files with 150 additions and 0 deletions

View file

@ -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

View file

@ -863,6 +863,7 @@ class WebGLVertexArrayObject;
namespace Web::WebGL::Extensions {
class ANGLEInstancedArrays;
class OESVertexArrayObject;
class WebGLDrawBuffers;
class WebGLVertexArrayObjectOES;
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/WebGLDrawBuffersPrototype.h>
#include <LibWeb/WebGL/Extensions/WebGLDrawBuffers.h>
#include <LibWeb/WebGL/OpenGLContext.h>
#include <LibWeb/WebGL/WebGLRenderingContext.h>
#define GL_GLEXT_PROTOTYPES 1
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
namespace Web::WebGL::Extensions {
GC_DEFINE_ALLOCATOR(WebGLDrawBuffers);
JS::ThrowCompletionOr<GC::Ptr<WebGLDrawBuffers>> WebGLDrawBuffers::create(JS::Realm& realm, GC::Ref<WebGLRenderingContext> context)
{
return realm.create<WebGLDrawBuffers>(realm, context);
}
WebGLDrawBuffers::WebGLDrawBuffers(JS::Realm& realm, GC::Ref<WebGLRenderingContext> context)
: PlatformObject(realm)
, m_context(context)
{
m_context->context().request_extension("GL_EXT_draw_buffers");
}
void WebGLDrawBuffers::draw_buffers_webgl(Vector<GLenum> 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);
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebGL/Types.h>
namespace Web::WebGL::Extensions {
class WebGLDrawBuffers : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(WebGLDrawBuffers, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(WebGLDrawBuffers);
public:
static JS::ThrowCompletionOr<GC::Ptr<WebGLDrawBuffers>> create(JS::Realm&, GC::Ref<WebGLRenderingContext>);
void draw_buffers_webgl(Vector<GLenum> buffers);
protected:
void initialize(JS::Realm&) override;
void visit_edges(Visitor&) override;
private:
WebGLDrawBuffers(JS::Realm&, GC::Ref<WebGLRenderingContext>);
GC::Ref<WebGLRenderingContext> m_context;
};
}

View file

@ -0,0 +1,49 @@
#import <WebGL/Types.idl>
// 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<GLenum> buffers);
};

View file

@ -16,6 +16,7 @@
#include <LibWeb/WebGL/EventNames.h>
#include <LibWeb/WebGL/Extensions/ANGLEInstancedArrays.h>
#include <LibWeb/WebGL/Extensions/OESVertexArrayObject.h>
#include <LibWeb/WebGL/Extensions/WebGLDrawBuffers.h>
#include <LibWeb/WebGL/OpenGLContext.h>
#include <LibWeb/WebGL/WebGLContextEvent.h>
#include <LibWeb/WebGL/WebGLRenderingContext.h>
@ -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;
}

View file

@ -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<Extensions::ANGLEInstancedArrays> m_angle_instanced_arrays_extension;
GC::Ptr<Extensions::OESVertexArrayObject> m_oes_vertex_array_object_extension;
GC::Ptr<Extensions::WebGLDrawBuffers> m_webgl_draw_buffers_extension;
virtual void set_error(GLenum error) override;
};

View file

@ -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)