summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/samplers/sampler-drawing-test.html
blob: ec6aa515efcf7520b1d9a05210928b2d44ae0881 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!--
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">
<title>WebGL Sampler Drawing Test</title>
<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>
</head>
<body>
<canvas id="canvas_drawing" width="12" height="12"></canvas>
<canvas id="canvas_texture" width="2" height="2"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("Tests drawing with sampler works as expected");
debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas_drawing", null, 2);
var canvas_texture = null;
var samplerParam = [
    gl.REPEAT,
    gl.CLAMP_TO_EDGE,
    gl.MIRRORED_REPEAT,
];
var color = [200, 0, 254, 255];

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    wtu.setupTexturedQuadWithTexCoords(gl, [-2.5, -2.5], [3.5, 3.5]);

    setupCanvasTexture();
    for (var ii = 0; ii < samplerParam.length; ++ii) {
        runDrawingTest(samplerParam[ii]);
    }

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

function setupCanvasTexture() {
    canvas_texture = document.getElementById("canvas_texture");
    var ctx2d = canvas_texture.getContext("2d");
    ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")";
    ctx2d.fillRect(0, 0, 1, 1);
}

function runDrawingTest(param) {
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas_texture);

    var sampler = gl.createSampler();
    gl.samplerParameteri(sampler, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.samplerParameteri(sampler, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_S, param);
    gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_T, param);

    gl.clearColor(1,1,1,1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.bindSampler(0, sampler);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 6);

    checkPixels(param);
}

function checkPixels(param) {
    var buf = new Uint8Array(12 * 12 * 4);
    gl.readPixels(0, 0, 12, 12, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var passed = true;
    for (var yy = 0; yy < 12; ++yy) {
        for (var xx = 0; xx < 12; ++xx) {
            var ec = [0, 0, 0, 0];
            switch (param) {
                case gl.REPEAT:
                    if (xx % 2 == 1 && yy % 2 == 1) {
                        ec = color;
                    }
                    break;
                case gl.CLAMP_TO_EDGE:
                    if (xx < 6 && yy < 6) {
                        ec = color;
                    }
                    break;
                case gl.MIRRORED_REPEAT:
                    if (xx % 4 < 2 && yy % 4 < 2) {
                        ec = color;
                    }
                    break;
            }
            var off = (yy * 12 + xx) * 4;
            if (buf[off + 0] != ec[0] || buf[off + 1] != ec[1] ||
                buf[off + 2] != ec[2] || buf[off + 3] != ec[3]) {
                var msg = 'at (' + xx + ', ' + yy + ') expected: ' +
                    ec[0] + ', ' + ec[1] + ', ' + ec[2] + ', ' + ec[3] + ' found: ' +
                    buf[off + 0] + ', ' + buf[off + 1] + ', ' + buf[off + 2] + ', ' + buf[off + 3];
                testFailed(msg);
                passed = false;
            }
        }
    }
    if (passed) {
        testPassed("Drawing with wrap " + wtu.glEnumToString(gl, param) + " as expected");
    }
}

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

</body>
</html>