From 958938655d5e0175f35b89bae2e13434a4ef7da6 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Tue, 28 Jan 2025 18:25:26 +0000 Subject: [PATCH] LibWeb/WebGL: Implement WEBGL_compressed_texture_s3tc extension --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/Forward.h | 1 + .../Extensions/WebGLCompressedTextureS3tc.cpp | 43 +++++++++++++++++++ .../Extensions/WebGLCompressedTextureS3tc.h | 33 ++++++++++++++ .../Extensions/WebGLCompressedTextureS3tc.idl | 16 +++++++ .../LibWeb/WebGL/WebGL2RenderingContext.cpp | 26 ++++++++++- .../LibWeb/WebGL/WebGL2RenderingContext.h | 4 ++ .../LibWeb/WebGL/WebGLRenderingContext.cpp | 11 +++++ .../LibWeb/WebGL/WebGLRenderingContext.h | 1 + Libraries/LibWeb/idl_files.cmake | 1 + .../LibWeb/GenerateWebGLRenderingContext.cpp | 2 +- 11 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.cpp create mode 100644 Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.h create mode 100644 Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.idl diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 511718e5fd6..ed20adac3d9 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -834,6 +834,7 @@ set(SOURCES WebDriver/UserPrompt.cpp WebGL/Extensions/ANGLEInstancedArrays.cpp WebGL/Extensions/OESVertexArrayObject.cpp + WebGL/Extensions/WebGLCompressedTextureS3tc.cpp WebGL/Extensions/WebGLDrawBuffers.cpp WebGL/Extensions/WebGLVertexArrayObjectOES.cpp WebGL/EventNames.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 0ec9512e140..01106a0a39f 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -883,6 +883,7 @@ class WebGLVertexArrayObject; namespace Web::WebGL::Extensions { class ANGLEInstancedArrays; class OESVertexArrayObject; +class WebGLCompressedTextureS3tc; class WebGLDrawBuffers; class WebGLVertexArrayObjectOES; } diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.cpp b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.cpp new file mode 100644 index 00000000000..87cb8e1b060 --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Web::WebGL::Extensions { + +GC_DEFINE_ALLOCATOR(WebGLCompressedTextureS3tc); + +JS::ThrowCompletionOr> WebGLCompressedTextureS3tc::create(JS::Realm& realm, WebGLRenderingContextBase* context) +{ + return realm.create(realm, context); +} + +WebGLCompressedTextureS3tc::WebGLCompressedTextureS3tc(JS::Realm& realm, WebGLRenderingContextBase* context) + : PlatformObject(realm) + , m_context(context) +{ + m_context->context().request_extension("GL_EXT_texture_compression_dxt1"); + m_context->context().request_extension("GL_ANGLE_texture_compression_dxt3"); + m_context->context().request_extension("GL_ANGLE_texture_compression_dxt5"); +} + +void WebGLCompressedTextureS3tc::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(WebGLCompressedTextureS3tc); +} + +void WebGLCompressedTextureS3tc::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_context->gc_cell()); +} + +} diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.h b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.h new file mode 100644 index 00000000000..6fa2dca31f3 --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025, Luke Wilde + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::WebGL::Extensions { + +class WebGLCompressedTextureS3tc : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(WebGLCompressedTextureS3tc, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(WebGLCompressedTextureS3tc); + +public: + static JS::ThrowCompletionOr> create(JS::Realm&, WebGLRenderingContextBase*); + +protected: + void initialize(JS::Realm&) override; + void visit_edges(Visitor&) override; + +private: + WebGLCompressedTextureS3tc(JS::Realm&, WebGLRenderingContextBase*); + + // FIXME: It should be GC::Ptr instead of raw pointer, but we need to make WebGLRenderingContextBase inherit from PlatformObject first. + WebGLRenderingContextBase* m_context; +}; + +} diff --git a/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.idl b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.idl new file mode 100644 index 00000000000..2a798fc5b8c --- /dev/null +++ b/Libraries/LibWeb/WebGL/Extensions/WebGLCompressedTextureS3tc.idl @@ -0,0 +1,16 @@ +#import + +// https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_s3tc/ +// NOTE: Original WEBGL_compressed_texture_s3tc 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 WebGLCompressedTextureS3tc { + // Compressed Texture Formats + const GLenum COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0; + const GLenum COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1; + const GLenum COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2; + const GLenum COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3; +}; diff --git a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.cpp b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.cpp index 847129e76e0..1b4d5b156d3 100644 --- a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.cpp +++ b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.cpp @@ -12,8 +12,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -71,6 +73,7 @@ void WebGL2RenderingContext::visit_edges(Cell::Visitor& visitor) Base::visit_edges(visitor); WebGL2RenderingContextImpl::visit_edges(visitor); visitor.visit(m_canvas_element); + visitor.visit(m_webgl_compressed_texture_s3tc_extension); } void WebGL2RenderingContext::present() @@ -154,8 +157,29 @@ Optional> WebGL2RenderingContext::get_supported_extensions() return context().get_supported_extensions(); } -JS::Object* WebGL2RenderingContext::get_extension(String const&) +JS::Object* WebGL2RenderingContext::get_extension(String const& name) { + // Returns an object if, and only if, name is an ASCII case-insensitive match [HTML] for one of the names returned + // from getSupportedExtensions; otherwise, returns null. The object returned from getExtension contains any constants + // or functions provided by the extension. A returned object may have no constants or functions if the extension does + // not define any, but a unique object must still be returned. That object is used to indicate that the extension has + // been enabled. + auto supported_extensions = get_supported_extensions(); + auto supported_extension_iterator = supported_extensions->find_if([&name](String const& supported_extension) { + return Infra::is_ascii_case_insensitive_match(supported_extension, name); + }); + if (supported_extension_iterator == supported_extensions->end()) + return nullptr; + + if (Infra::is_ascii_case_insensitive_match(name, "WEBGL_compressed_texture_s3tc"sv)) { + if (!m_webgl_compressed_texture_s3tc_extension) { + m_webgl_compressed_texture_s3tc_extension = MUST(Extensions::WebGLCompressedTextureS3tc::create(realm(), this)); + } + + VERIFY(m_webgl_compressed_texture_s3tc_extension); + return m_webgl_compressed_texture_s3tc_extension; + } + return nullptr; } diff --git a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h index 1c6613b84cd..994d0b23abd 100644 --- a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h +++ b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h @@ -80,6 +80,10 @@ private: GLenum m_error { 0 }; + // Extensions + // "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_webgl_compressed_texture_s3tc_extension; + virtual void set_error(GLenum error) override; }; diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp index 36cae79fe3e..b3a9dfa77d4 100644 --- a/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp +++ b/Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,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_compressed_texture_s3tc_extension); visitor.visit(m_webgl_draw_buffers_extension); } @@ -209,6 +211,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_compressed_texture_s3tc"sv)) { + if (!m_webgl_compressed_texture_s3tc_extension) { + m_webgl_compressed_texture_s3tc_extension = MUST(Extensions::WebGLCompressedTextureS3tc::create(realm(), this)); + } + + VERIFY(m_webgl_compressed_texture_s3tc_extension); + return m_webgl_compressed_texture_s3tc_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)); diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.h b/Libraries/LibWeb/WebGL/WebGLRenderingContext.h index 3da42ee436b..c9cc8ece980 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_compressed_texture_s3tc_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 37151936225..1187bac6011 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -388,6 +388,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/WebGLCompressedTextureS3tc) libweb_js_bindings(WebGL/Extensions/WebGLDrawBuffers) libweb_js_bindings(WebGL/Extensions/WebGLVertexArrayObjectOES) libweb_js_bindings(WebGL/WebGL2RenderingContext) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWebGLRenderingContext.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWebGLRenderingContext.cpp index 9fda256e541..c4fe49d20a7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWebGLRenderingContext.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWebGLRenderingContext.cpp @@ -832,7 +832,7 @@ class @class_name@ : public WebGLRenderingContextBase { public: @class_name@(JS::Realm&, NonnullOwnPtr); - OpenGLContext& context() { return *m_context; } + virtual OpenGLContext& context() override { return *m_context; } virtual void present() = 0; virtual void needs_to_present() = 0;