Przeglądaj źródła

LibWeb/WebGL: Add support for ANGLEInstancedArrays extension

This is only WebGL 1.0 extension. It's enabled in ANGLE by default.

Progress on https://ciechanow.ski/curves-and-surfaces/ that relies on
this extension.
Aliaksandr Kalenik 7 miesięcy temu
rodzic
commit
c9105955f6

+ 1 - 0
Libraries/LibWeb/CMakeLists.txt

@@ -804,6 +804,7 @@ set(SOURCES
     WebDriver/Response.cpp
     WebDriver/Screenshot.cpp
     WebDriver/TimeoutsConfiguration.cpp
+    WebGL/ANGLEInstancedArrays.cpp
     WebGL/EventNames.cpp
     WebGL/OpenGLContext.cpp
     WebGL/WebGL2RenderingContext.cpp

+ 51 - 0
Libraries/LibWeb/WebGL/ANGLEInstancedArrays.cpp

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/Realm.h>
+#include <LibWeb/Bindings/ANGLEInstancedArraysPrototype.h>
+#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/WebGL/ANGLEInstancedArrays.h>
+
+#define GL_GLEXT_PROTOTYPES 1
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+
+namespace Web::WebGL {
+
+GC_DEFINE_ALLOCATOR(ANGLEInstancedArrays);
+
+JS::ThrowCompletionOr<GC::Ptr<ANGLEInstancedArrays>> ANGLEInstancedArrays::create(JS::Realm& realm)
+{
+    return realm.create<ANGLEInstancedArrays>(realm);
+}
+
+ANGLEInstancedArrays::ANGLEInstancedArrays(JS::Realm& realm)
+    : PlatformObject(realm)
+{
+}
+
+void ANGLEInstancedArrays::vertex_attrib_divisor_angle(GLuint index, GLuint divisor)
+{
+    glVertexAttribDivisorANGLE(index, divisor);
+}
+
+void ANGLEInstancedArrays::draw_arrays_instanced_angle(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
+{
+    glDrawArraysInstancedANGLE(mode, first, count, primcount);
+}
+
+void ANGLEInstancedArrays::draw_elements_instanced_angle(GLenum mode, GLsizei count, GLenum type, GLintptr offset, GLsizei primcount)
+{
+    glDrawElementsInstancedANGLE(mode, count, type, reinterpret_cast<void*>(offset), primcount);
+}
+
+void ANGLEInstancedArrays::initialize(JS::Realm& realm)
+{
+    Base::initialize(realm);
+    WEB_SET_PROTOTYPE_FOR_INTERFACE(ANGLEInstancedArrays);
+}
+
+}

+ 33 - 0
Libraries/LibWeb/WebGL/ANGLEInstancedArrays.h

@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
+ *
+ * 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 {
+
+class ANGLEInstancedArrays : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(ANGLEInstancedArrays, Bindings::PlatformObject);
+    GC_DECLARE_ALLOCATOR(ANGLEInstancedArrays);
+
+public:
+    static JS::ThrowCompletionOr<GC::Ptr<ANGLEInstancedArrays>> create(JS::Realm&);
+
+    void draw_arrays_instanced_angle(GLenum mode, GLint first, GLsizei count, GLsizei primcount);
+    void draw_elements_instanced_angle(GLenum mode, GLsizei count, GLenum type, GLintptr offset, GLsizei primcount);
+    void vertex_attrib_divisor_angle(GLuint index, GLuint divisor);
+
+protected:
+    void initialize(JS::Realm&) override;
+
+private:
+    ANGLEInstancedArrays(JS::Realm&);
+};
+
+}

+ 15 - 0
Libraries/LibWeb/WebGL/ANGLEInstancedArrays.idl

@@ -0,0 +1,15 @@
+#import <WebGL/Types.idl>
+
+// https://registry.khronos.org/webgl/extensions/ANGLE_instanced_arrays/
+// NOTE: Original ANGLE_instanced_arrays 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 ANGLEInstancedArrays {
+    const GLenum VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88FE;
+    undefined drawArraysInstancedANGLE(GLenum mode, GLint first, GLsizei count, GLsizei primcount);
+    undefined drawElementsInstancedANGLE(GLenum mode, GLsizei count, GLenum type, GLintptr offset, GLsizei primcount);
+    undefined vertexAttribDivisorANGLE(GLuint index, GLuint divisor);
+};

+ 2 - 0
Libraries/LibWeb/WebGL/Types.h

@@ -14,6 +14,8 @@ namespace Web::WebGL {
 using GLenum = unsigned int;
 using GLuint = unsigned int;
 using GLint = int;
+using GLsizei = int;
+using GLintptr = int;
 
 // FIXME: This should really be "struct __GLsync*", but the linker doesn't recognise it.
 //        Since this conflicts with the original definition of GLsync, the suffix "Internal" has been added.

+ 5 - 1
Libraries/LibWeb/WebGL/WebGLRenderingContext.cpp

@@ -12,6 +12,7 @@
 #include <LibWeb/HTML/HTMLCanvasElement.h>
 #include <LibWeb/HTML/TraversableNavigable.h>
 #include <LibWeb/Painting/Paintable.h>
+#include <LibWeb/WebGL/ANGLEInstancedArrays.h>
 #include <LibWeb/WebGL/EventNames.h>
 #include <LibWeb/WebGL/OpenGLContext.h>
 #include <LibWeb/WebGL/WebGLContextEvent.h>
@@ -166,8 +167,11 @@ Optional<Vector<String>> WebGLRenderingContext::get_supported_extensions()
     return context().get_supported_extensions();
 }
 
-JS::Object* WebGLRenderingContext::get_extension(String const&)
+JS::Object* WebGLRenderingContext::get_extension(String const& name)
 {
+    if (name == "ANGLE_instanced_arrays"sv) {
+        return MUST(ANGLEInstancedArrays::create(realm()));
+    }
     return nullptr;
 }
 

+ 1 - 0
Libraries/LibWeb/idl_files.cmake

@@ -370,6 +370,7 @@ libweb_js_bindings(WebAudio/OfflineAudioContext)
 libweb_js_bindings(WebAudio/OscillatorNode)
 libweb_js_bindings(WebAudio/PannerNode)
 libweb_js_bindings(WebAudio/PeriodicWave)
+libweb_js_bindings(WebGL/ANGLEInstancedArrays)
 libweb_js_bindings(WebGL/WebGL2RenderingContext)
 libweb_js_bindings(WebGL/WebGLActiveInfo)
 libweb_js_bindings(WebGL/WebGLBuffer)