summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-mochitest/test_implicit_color_buffer_float.html
blob: bbabef3e8b0f36ef89f0dea10706ecbcda4bcaad (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>

var RGBA32F_EXT = 0x8814;
var RGBA16F_EXT = 0x881A; // Yep, it's really 4 and A.
var HALF_FLOAT_OES = 0x8D61;

function IsFormatValidForRB(gl, format) {
    ok(!gl.getError(), 'Should have no errors here.');

    var rb = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rb);
    gl.renderbufferStorage(gl.RENDERBUFFER, format, 4, 4);

    var error = gl.getError();
    if (error == gl.INVALID_ENUM)
        return false;

    ok(error == gl.NO_ERROR, 'Error should be INVALID_ENUM or NO_ERROR.');
    return error == gl.NO_ERROR;
}

function IsFormatValidForTex(gl, format, type) {
    ok(!gl.getError(), 'Should have no errors here.');

    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, format, 4, 4, 0, format, type, null);

    var error = gl.getError();
    if (error == gl.INVALID_ENUM || error == gl.INVALID_OPERATION)
        return false;

    ok(error == gl.NO_ERROR, 'Error should be INVALID_{ENUM,OPERATION} or NO_ERROR.');
    return error == gl.NO_ERROR;
}

function IsFormatValidForTexFB(gl, format, type) {
    ok(!gl.getError(), 'Should have no errors here.');

    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, format, 4, 4, 0, format, type, null);

    var error = gl.getError();
    if (error == gl.INVALID_ENUM || error == gl.INVALID_OPERATION)
        return false;

    ok(error == gl.NO_ERROR, 'Error should be INVALID_{ENUM,OPERATION} or NO_ERROR.');

    var fb = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D,
                            tex, 0);
    error = gl.getError();
    ok(error == gl.NO_ERROR, 'Error should be NO_ERROR.');

    var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
    return status == gl.FRAMEBUFFER_COMPLETE;
}

function IsFormatValidForTexFBRead(gl, texFormat, texType, readType) {
    ok(!gl.getError(), 'Should have no errors here.');

    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, texFormat, 4, 4, 0, texFormat, texType,
                  null);

    var error = gl.getError();
    if (error == gl.INVALID_ENUM || error == gl.INVALID_OPERATION)
        return false;

    ok(error == gl.NO_ERROR, 'Error should be INVALID_{ENUM,OPERATION} or NO_ERROR.');

    var fb = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D,
                            tex, 0);
    error = gl.getError();
    ok(error == gl.NO_ERROR, 'Error should be NO_ERROR.');

    var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
    if (status != gl.FRAMEBUFFER_COMPLETE)
        return false;

    var data;
    switch (readType) {
    case gl.UNSIGNED_BYTE:
        data = new Uint8Array(4);
        break;
    case HALF_FLOAT_OES:
        data = new Uint16Array(4);
        break;
    case gl.FLOAT:
        data = new Float32Array(4);
        break;
    default:
        throw 'Bad `readType`.';
    }
    gl.readPixels(0, 0, 1, 1, gl.RGBA, readType, data);

    error = gl.getError();
    return error == gl.NO_ERROR;
}

function TestColorBufferExt(gl, rbFormat, texFormat, texType, readType)
{
    var isTexFBValid = IsFormatValidForTexFB(gl, texFormat, texType);
    var isTexFBReadValid = IsFormatValidForTexFBRead(gl, texFormat, texType,
                                                     readType);
    var isRBValid = IsFormatValidForRB(gl, rbFormat);

    var validSubsetCount = isTexFBValid + isTexFBReadValid + isRBValid;

    if (validSubsetCount) {
        ok(isTexFBValid, 'If active, texture-fbs should work.');
        ok(isTexFBReadValid, 'If active, reading texture-fbs should work.');
        ok(isRBValid, 'If active, renderbuffers should work.');
    }

    return validSubsetCount == 3;
}

function TestImpliedExtension(gl, baseExtName, impliedExtName, rbFormat,
                              texFormat, texType, readType)
{
    ok(true, '========');
    ok(true, 'Testing if ' + baseExtName + ' implies ' + impliedExtName + '.');
    ok(true, '--------');

    var baseExt = gl.getExtension(baseExtName);
    if (!baseExt) {
        ok(!baseExt, 'Ext \'' + baseExtName + '\' can be unsupported.');
        return;
    }

    var isTexValid = IsFormatValidForTex(gl, texFormat, texType);
    ok(isTexValid, baseExtName + ' should allow float textures.');
    if (!isTexValid)
        return;

    var isImplicitlyActive = TestColorBufferExt(gl, rbFormat, texFormat,
                                                texType, readType);

    if (isImplicitlyActive) {
        ok(true, 'Activating ' + baseExtName + ' has implicitly activated ' +
           impliedExtName + '.');

        var impliedExt = gl.getExtension(impliedExtName);
        ok(impliedExt, 'If ' + impliedExtName + ' is supported implicitly, it' +
           ' must be supported explicitly as well.');
        return;
    }

    ok(true, 'Activating ' + baseExtName + ' has not implicitly activated ' +
       impliedExtName + '.');
    ok(true, '--------');

    var impliedExt = gl.getExtension(impliedExtName);
    if (!impliedExt) {
        ok(true, impliedExtName + ' can be unsupported.');
        return;
    }
    ok(true, 'Explicit activation of ' + impliedExtName + ' successful.');

    var isFunctional = TestColorBufferExt(gl, rbFormat, texFormat, texType,
                                          readType);
    ok(isFunctional, impliedExtName + ' should be fully functional.');
}

(function() {
    var canvas = document.createElement('canvas');
    var gl = canvas.getContext('experimental-webgl');
    if (!gl) {
        ok(!gl, 'WebGL can be unsupported.');
        return;
    }

    TestImpliedExtension(gl, 'OES_texture_float', 'WEBGL_color_buffer_float',
                         RGBA32F_EXT, gl.RGBA, gl.FLOAT, gl.FLOAT);
    TestImpliedExtension(gl, 'OES_texture_half_float',
                         'EXT_color_buffer_half_float', RGBA16F_EXT, gl.RGBA,
                         HALF_FLOAT_OES, gl.FLOAT);
    ok(true, '========');
    ok(true, 'TEST COMPLETE');
})();

</script>

</body>
</html>