Skip to content

Commit a48f9f0

Browse files
authored
feat(webgl): implement bufferData(target, size, usage) support in WebGL bindings (#248)
1 parent 1347577 commit a48f9f0

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>WebGL bufferData(target, size, usage) Test</title>
6+
<style>
7+
body {
8+
font-family: monospace;
9+
padding: 20px;
10+
}
11+
12+
.test-result {
13+
margin: 10px 0;
14+
}
15+
16+
.pass {
17+
color: green;
18+
}
19+
20+
.fail {
21+
color: red;
22+
}
23+
</style>
24+
</head>
25+
26+
<body>
27+
<h1>WebGL bufferData(target, size, usage) Test</h1>
28+
<div id="results"></div>
29+
30+
<script>
31+
const results = document.getElementById('results');
32+
33+
function addResult(message, isPass) {
34+
const div = document.createElement('div');
35+
div.className = `test-result ${isPass ? 'pass' : 'fail'}`;
36+
div.textContent = `${isPass ? '✅' : '❌'} ${message}`;
37+
results.appendChild(div);
38+
}
39+
40+
function runTests() {
41+
const gl = navigator.gl;
42+
if (!gl) {
43+
addResult('WebGL not supported', false);
44+
return;
45+
}
46+
47+
try {
48+
// Test 1: Basic bufferData with size
49+
const buffer1 = gl.createBuffer();
50+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer1);
51+
gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
52+
addResult(`bufferData(ARRAY_BUFFER, 1024, STATIC_DRAW) - Size.`, true);
53+
54+
// Test 5: Verify we can still use the old bufferData with TypedArray
55+
const buffer5 = gl.createBuffer();
56+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer5);
57+
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
58+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
59+
addResult(`bufferData(ARRAY_BUFFER, Float32Array[4], STATIC_DRAW).`, true);
60+
addResult('All tests completed successfully!', true);
61+
62+
} catch (error) {
63+
addResult(`Error during testing: ${error.message}`, false);
64+
}
65+
}
66+
67+
// Run tests when page loads
68+
runTests();
69+
</script>
70+
</body>
71+
72+
</html>

src/bindings/webgl/rendering_context-inl.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,19 @@ namespace webgl
620620
return env.Undefined();
621621
}
622622

623-
/**
624-
* TODO: support bufferData() with size
625-
*/
626-
if (info[1].IsNumber())
627-
{
628-
Napi::TypeError::New(env, "bufferData(target, size, usage) is not supported yet.")
629-
.ThrowAsJavaScriptException();
630-
return env.Undefined();
631-
}
632-
633623
auto targetInt = info[0].As<Napi::Number>().Int32Value();
634624
auto target = static_cast<client_graphics::WebGLBufferBindingTarget>(targetInt);
635625
auto usageInt = info[2].As<Napi::Number>().Int32Value();
636626
auto usage = static_cast<client_graphics::WebGLBufferUsage>(usageInt);
637627

628+
// Handle bufferData(target, size, usage) - allocate buffer with given size
629+
if (info[1].IsNumber())
630+
{
631+
auto size = info[1].As<Napi::Number>().Int64Value();
632+
glContext_->bufferData(target, size, usage);
633+
return env.Undefined();
634+
}
635+
638636
auto jsBuffer = info[1];
639637
void *bufferData = nullptr;
640638
size_t bufferSize = 0;

0 commit comments

Comments
 (0)