LibWeb/WebGL: Define Int32List exactly like in the spec

Same fix as d54cab60 but applied for Int32List.
This commit is contained in:
Aliaksandr Kalenik 2025-09-30 16:55:21 +02:00 committed by Jelle Raaijmakers
parent 655cd339a7
commit c75a8fab3b
7 changed files with 56 additions and 172 deletions

View File

@ -451,25 +451,13 @@ void WebGL2RenderingContextImpl::clear_bufferfv(WebIDL::UnsignedLong buffer, Web
needs_to_present();
}
void WebGL2RenderingContextImpl::clear_bufferiv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> values, WebIDL::UnsignedLongLong src_offset)
void WebGL2RenderingContextImpl::clear_bufferiv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Int32List values, WebIDL::UnsignedLongLong src_offset)
{
m_context->make_current();
m_context->notify_content_will_change();
int const* data = nullptr;
size_t count = 0;
if (values.has<Vector<int>>()) {
auto& vector = values.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (values.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*values.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
auto span = span_from_int32_list(values);
auto count = span.size();
switch (buffer) {
case GL_COLOR:
@ -491,8 +479,8 @@ void WebGL2RenderingContextImpl::clear_bufferiv(WebIDL::UnsignedLong buffer, Web
return;
}
data += src_offset;
glClearBufferiv(buffer, drawbuffer, data);
span = span.slice(src_offset);
glClearBufferiv(buffer, drawbuffer, span.data());
needs_to_present();
}
@ -1286,152 +1274,100 @@ void WebGL2RenderingContextImpl::uniform4fv(GC::Root<WebGLUniformLocation> locat
glUniform4fv(location->handle(), count / 4, span.data());
}
void WebGL2RenderingContextImpl::uniform1iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
void WebGL2RenderingContextImpl::uniform1iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
auto span = span_from_int32_list(v);
auto count = span.size();
if (src_offset + src_length > count) {
set_error(GL_INVALID_VALUE);
return;
}
data += src_offset;
span = span.slice(src_offset);
if (src_length == 0) {
count -= src_offset;
} else {
count = src_length;
}
glUniform1iv(location->handle(), count / 1, data);
glUniform1iv(location->handle(), count / 1, span.data());
}
void WebGL2RenderingContextImpl::uniform2iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
void WebGL2RenderingContextImpl::uniform2iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
auto span = span_from_int32_list(v);
auto count = span.size();
if (src_offset + src_length > count) {
set_error(GL_INVALID_VALUE);
return;
}
data += src_offset;
span = span.slice(src_offset);
if (src_length == 0) {
count -= src_offset;
} else {
count = src_length;
}
glUniform2iv(location->handle(), count / 2, data);
glUniform2iv(location->handle(), count / 2, span.data());
}
void WebGL2RenderingContextImpl::uniform3iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
void WebGL2RenderingContextImpl::uniform3iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
auto span = span_from_int32_list(v);
auto count = span.size();
if (src_offset + src_length > count) {
set_error(GL_INVALID_VALUE);
return;
}
data += src_offset;
span = span.slice(src_offset);
if (src_length == 0) {
count -= src_offset;
} else {
count = src_length;
}
glUniform3iv(location->handle(), count / 3, data);
glUniform3iv(location->handle(), count / 3, span.data());
}
void WebGL2RenderingContextImpl::uniform4iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
void WebGL2RenderingContextImpl::uniform4iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
auto span = span_from_int32_list(v);
auto count = span.size();
if (src_offset + src_length > count) {
set_error(GL_INVALID_VALUE);
return;
}
data += src_offset;
span = span.slice(src_offset);
if (src_length == 0) {
count -= src_offset;
} else {
count = src_length;
}
glUniform4iv(location->handle(), count / 4, data);
glUniform4iv(location->handle(), count / 4, span.data());
}
void WebGL2RenderingContextImpl::uniform_matrix2fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List data, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length)

View File

@ -49,7 +49,7 @@ public:
void draw_elements_instanced(WebIDL::UnsignedLong mode, WebIDL::Long count, WebIDL::UnsignedLong type, WebIDL::LongLong offset, WebIDL::Long instance_count);
void draw_buffers(Vector<WebIDL::UnsignedLong> buffers);
void clear_bufferfv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Float32List values, WebIDL::UnsignedLongLong src_offset);
void clear_bufferiv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> values, WebIDL::UnsignedLongLong src_offset);
void clear_bufferiv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Int32List values, WebIDL::UnsignedLongLong src_offset);
void clear_bufferuiv(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::UnsignedLong>> values, WebIDL::UnsignedLongLong src_offset);
void clear_bufferfi(WebIDL::UnsignedLong buffer, WebIDL::Long drawbuffer, float depth, WebIDL::Long stencil);
GC::Root<WebGLSampler> create_sampler();
@ -91,10 +91,10 @@ public:
void uniform2fv(GC::Root<WebGLUniformLocation> location, Float32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform3fv(GC::Root<WebGLUniformLocation> location, Float32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform4fv(GC::Root<WebGLUniformLocation> location, Float32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform1iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform2iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform3iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform4iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform1iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform2iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform3iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform4iv(GC::Root<WebGLUniformLocation> location, Int32List v, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform_matrix2fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List data, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform_matrix3fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List data, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);
void uniform_matrix4fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List data, WebIDL::UnsignedLongLong src_offset, WebIDL::UnsignedLong src_length);

View File

@ -20,6 +20,7 @@ using TexImageSource = Variant<GC::Root<HTML::ImageBitmap>, GC::Root<HTML::Image
class WebGLRenderingContextBase {
public:
using Float32List = Variant<GC::Root<JS::Float32Array>, Vector<float>>;
using Int32List = Variant<GC::Root<JS::Int32Array>, Vector<WebIDL::Long>>;
virtual GC::Cell const* gc_cell() const = 0;
virtual void visit_edges(JS::Cell::Visitor&) = 0;
@ -31,6 +32,13 @@ public:
return float32_list.get<Vector<float>>();
return float32_list.get<GC::Root<JS::Float32Array>>()->data();
}
static Span<int> span_from_int32_list(Int32List& int32_list)
{
if (int32_list.has<Vector<int>>())
return int32_list.get<Vector<int>>();
return int32_list.get<GC::Root<JS::Int32Array>>()->data();
}
};
}

View File

@ -402,104 +402,44 @@ void WebGLRenderingContextImpl::uniform4fv(GC::Root<WebGLUniformLocation> locati
glUniform4fv(location->handle(), span.size() / 4, span.data());
}
void WebGLRenderingContextImpl::uniform1iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v)
void WebGLRenderingContextImpl::uniform1iv(GC::Root<WebGLUniformLocation> location, Int32List v)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
glUniform1iv(location->handle(), count / 1, data);
auto span = span_from_int32_list(v);
glUniform1iv(location->handle(), span.size() / 1, span.data());
}
void WebGLRenderingContextImpl::uniform2iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v)
void WebGLRenderingContextImpl::uniform2iv(GC::Root<WebGLUniformLocation> location, Int32List v)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
glUniform2iv(location->handle(), count / 2, data);
auto span = span_from_int32_list(v);
glUniform2iv(location->handle(), span.size() / 2, span.data());
}
void WebGLRenderingContextImpl::uniform3iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v)
void WebGLRenderingContextImpl::uniform3iv(GC::Root<WebGLUniformLocation> location, Int32List v)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
glUniform3iv(location->handle(), count / 3, data);
auto span = span_from_int32_list(v);
glUniform3iv(location->handle(), span.size() / 3, span.data());
}
void WebGLRenderingContextImpl::uniform4iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v)
void WebGLRenderingContextImpl::uniform4iv(GC::Root<WebGLUniformLocation> location, Int32List v)
{
m_context->make_current();
if (!location)
return;
int const* data = nullptr;
size_t count = 0;
if (v.has<Vector<int>>()) {
auto& vector = v.get<Vector<int>>();
data = vector.data();
count = vector.size();
} else if (v.has<GC::Root<WebIDL::BufferSource>>()) {
auto& typed_array_base = static_cast<JS::TypedArrayBase&>(*v.get<GC::Root<WebIDL::BufferSource>>()->raw_object());
auto& typed_array = as<JS::Int32Array>(typed_array_base);
data = typed_array.data().data();
count = typed_array.array_length().length();
} else {
VERIFY_NOT_REACHED();
}
glUniform4iv(location->handle(), count / 4, data);
auto span = span_from_int32_list(v);
glUniform4iv(location->handle(), span.size() / 4, span.data());
}
void WebGLRenderingContextImpl::uniform_matrix2fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List value)

View File

@ -42,10 +42,10 @@ public:
void uniform2fv(GC::Root<WebGLUniformLocation> location, Float32List v);
void uniform3fv(GC::Root<WebGLUniformLocation> location, Float32List v);
void uniform4fv(GC::Root<WebGLUniformLocation> location, Float32List v);
void uniform1iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v);
void uniform2iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v);
void uniform3iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v);
void uniform4iv(GC::Root<WebGLUniformLocation> location, Variant<GC::Root<WebIDL::BufferSource>, Vector<WebIDL::Long>> v);
void uniform1iv(GC::Root<WebGLUniformLocation> location, Int32List v);
void uniform2iv(GC::Root<WebGLUniformLocation> location, Int32List v);
void uniform3iv(GC::Root<WebGLUniformLocation> location, Int32List v);
void uniform4iv(GC::Root<WebGLUniformLocation> location, Int32List v);
void uniform_matrix2fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List value);
void uniform_matrix3fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List value);
void uniform_matrix4fv(GC::Root<WebGLUniformLocation> location, bool transpose, Float32List value);

View File

@ -9,8 +9,7 @@ typedef (ImageBitmap or
// FIXME: VideoFrame
) TexImageSource;
// FIXME: BufferSource should be a Int32Array
typedef (BufferSource or sequence<GLint>) Int32List;
typedef (Int32Array or sequence<GLint>) Int32List;
// https://registry.khronos.org/webgl/specs/latest/1.0/#5.14
interface mixin WebGLRenderingContextOverloads {

View File

@ -181,6 +181,7 @@ static bool is_javascript_builtin(Type const& type)
"Float16Array"sv,
"Float32Array"sv,
"Float64Array"sv,
"Int32Array"sv,
"Uint8Array"sv,
"Uint8ClampedArray"sv,
};