2 Copyright (c) 2023 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <title>gl.texSubImage2D() test to Wasm Memory
4GB in size.
</title>
11 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
12 <script src=
"../../js/js-test-pre.js"></script>
13 <script src=
"../../js/webgl-test-utils.js"> </script>
16 <canvas id=
"canvas" width=
"2" height=
"2" style=
"width: 40px; height: 40px;"></canvas>
17 <div id=
"description"></div>
18 <div id=
"console"></div>
21 description(document
.title
);
22 debug("Tests that gl.texSubImage2D() can be called on WebAssembly Memory 4GB in size.");
24 let wtu
= WebGLTestUtils
;
25 let gl
= wtu
.create3DContext("canvas", undefined, 2);
28 const SIZE
= 4*1024*1024*1024 - PAGE
; // when uint32_t size is max, we can only reach one page short of full 4GB
29 let view
= new Uint8Array(new WebAssembly
.Memory({ initial
: SIZE
/PAGE
}).buffer
);
31 function compileShader(type
, src
) {
32 let shader
= gl
.createShader(type
);
33 gl
.shaderSource(shader
, src
);
34 gl
.compileShader(shader
);
35 let log
= gl
.getShaderInfoLog(shader
);
40 function createProgram(vs
, fs
) {
41 let program
= gl
.createProgram();
42 gl
.attachShader(program
, vs
);
43 gl
.attachShader(program
, fs
);
44 gl
.bindAttribLocation(program
, 0, 'pos');
45 gl
.linkProgram(program
);
46 gl
.useProgram(program
);
50 let program
= createProgram(
51 compileShader(gl
.VERTEX_SHADER
, `
54 void main() { uv = pos; gl_Position = vec4(pos*2.0-vec2(1.0,1.0),0,1); }`),
55 compileShader(gl
.FRAGMENT_SHADER
, `
57 uniform sampler2D tex;
59 void main() { gl_FragColor = texture2D(tex,uv); }`));
61 gl
.bindBuffer(gl
.ARRAY_BUFFER
, gl
.createBuffer());
62 gl
.bufferData(gl
.ARRAY_BUFFER
, new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]), gl
.STATIC_DRAW
);
63 gl
.vertexAttribPointer(0, 2, gl
.FLOAT
, gl
.FALSE
, 0, 0);
64 gl
.enableVertexAttribArray(0);
66 let texture
= gl
.createTexture();
67 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
69 // Test uploading an image
70 const expectedColor
= [42, 84, 128, 255];
71 const offset
= SIZE
- 4;
72 view
.set(expectedColor
, offset
);
73 gl
.texStorage2D(gl
.TEXTURE_2D
, 1, gl
.RGBA8
, 1, 1);
74 gl
.texSubImage2D(gl
.TEXTURE_2D
, 0, 0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, view
, offset
);
75 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
);
77 // Test rendering with that image
78 gl
.drawArrays(gl
.TRIANGLE_STRIP
, 0, 4);
80 // Verify that we rendered what we expected
81 wtu
.checkCanvasRect(gl
, 0, 0, 1, 1, expectedColor
, "texSubImage2D produced expected color");
82 var successfullyParsed
= true;
84 <script src=
"../../js/js-test-post.js"></script>