summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/tex-sub-image-2d.html
blob: 0d72946613d32ca9ceef088968bf29d2f55cac4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"></script>
<script id="fshader" type="x-shader/x-fragment">
precision mediump float;

uniform sampler2D tex;
varying vec2 texCoord;

void main()
{
    float intensity = texture2D(tex, texCoord).a;
    gl_FragColor = vec4(intensity, intensity, intensity, 1.0);
}
</script>

</head>
<body>
<canvas id="example" width="256" height="1"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Tests texSubImage2D upload path from Uint8Array');

var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = wtu.create3DContext(canvas);
gl.disable(gl.DITHER);
var program = wtu.setupProgram(
    gl,
    [wtu.simpleTextureVertexShader, "fshader"],
    ['vPosition', 'texCoord0']);
wtu.setupUnitQuad(gl);
var textureWidth = 256;
var textureHeight = 1;

var textureLoc = gl.getUniformLocation(program, "tex");

var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// Allocate the texture object
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, textureWidth, textureHeight, 0, gl.ALPHA, gl.UNSIGNED_BYTE, null);
// Prepare the image data
var array = new Uint8Array(textureWidth);
for (var i = 0; i < textureWidth; i++)
    array[i] = i;
// Fill the texture object with data -- this is actually the code path being tested
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, gl.ALPHA, gl.UNSIGNED_BYTE, array);

// Clear and set up
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.useProgram(program);
gl.uniform1i(textureLoc, 0);
// Draw the texture to the frame buffer
gl.drawArrays(gl.TRIANGLES, 0, 6);

// Read back the frame buffer
var buf = new Uint8Array(textureWidth * textureHeight * 4);
gl.readPixels(0, 0, textureWidth, textureHeight, gl.RGBA, gl.UNSIGNED_BYTE, buf);

// Verify the frame buffer's contents
var passed = true;
for (var i = 0; i < textureWidth; i++) {
    var val = i;
    if (buf[4 * i + 0] != val ||
        buf[4 * i + 1] != val ||
        buf[4 * i + 2] != val) {
        testFailed("pixel at (" + i + ", 0) was (" +
                   buf[4 * i + 0] + ", " +
                   buf[4 * i + 1] + ", " +
                   buf[4 * i + 2] + "), should be (" +
                   val + ", " + val + ", " + val + ")");
        passed = false;
        break;
    }
}

if (passed)
    testPassed("");

var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>
</body>
</html>