Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
michael@0 | 3 | |
michael@0 | 4 | <title>WebGL test: Basic drawing</title> |
michael@0 | 5 | |
michael@0 | 6 | <script src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 7 | <link rel="stylesheet" href="/tests/SimpleTest/test.css"> |
michael@0 | 8 | <script src="driver-info.js"></script> |
michael@0 | 9 | <script src="webgl-util.js"></script> |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | <script id="vs" type="x-shader/x-vertex"> |
michael@0 | 13 | |
michael@0 | 14 | attribute vec2 aVertCoord; |
michael@0 | 15 | |
michael@0 | 16 | void main(void) { |
michael@0 | 17 | gl_Position = vec4(aVertCoord, 0.0, 1.0); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | </script> |
michael@0 | 21 | <script id="fs" type="x-shader/x-fragment"> |
michael@0 | 22 | |
michael@0 | 23 | precision mediump float; |
michael@0 | 24 | |
michael@0 | 25 | void main(void) { |
michael@0 | 26 | gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | </script> |
michael@0 | 30 | <body> |
michael@0 | 31 | <canvas id="c" width="64" height="64"></canvas> |
michael@0 | 32 | <script> |
michael@0 | 33 | |
michael@0 | 34 | // Give ourselves a scope to return early from: |
michael@0 | 35 | (function() { |
michael@0 | 36 | var gl = WebGLUtil.getWebGL('c'); |
michael@0 | 37 | if (!gl) { |
michael@0 | 38 | todo(false, 'WebGL is unavailable.'); |
michael@0 | 39 | return; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function errorFunc(str) { |
michael@0 | 43 | ok(false, 'Error: ' + str); |
michael@0 | 44 | } |
michael@0 | 45 | WebGLUtil.setErrorFunc(errorFunc); |
michael@0 | 46 | WebGLUtil.setWarningFunc(errorFunc); |
michael@0 | 47 | |
michael@0 | 48 | gl.disable(gl.DEPTH_TEST); |
michael@0 | 49 | |
michael@0 | 50 | var prog = WebGLUtil.createProgramByIds(gl, 'vs', 'fs'); |
michael@0 | 51 | if (!prog) { |
michael@0 | 52 | ok(false, 'Program linking should succeed.'); |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | prog.aVertCoord = gl.getAttribLocation(prog, "aVertCoord"); |
michael@0 | 57 | ok(prog.aVertCoord >= 0, '`aVertCoord` should be valid.'); |
michael@0 | 58 | |
michael@0 | 59 | function checkGLError(func, info, refValue) { |
michael@0 | 60 | if (!refValue) |
michael@0 | 61 | refValue = 0; |
michael@0 | 62 | |
michael@0 | 63 | var error = gl.getError(); |
michael@0 | 64 | func(error == refValue, |
michael@0 | 65 | '[' + info + '] gl.getError should be 0x' + refValue.toString(16) + |
michael@0 | 66 | ', was 0x' + error.toString(16) + '.'); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | var vertCoordArr = new Float32Array([ |
michael@0 | 70 | -1, -1, |
michael@0 | 71 | 1, -1, |
michael@0 | 72 | -1, 1, |
michael@0 | 73 | 1, 1, |
michael@0 | 74 | ]); |
michael@0 | 75 | var vertCoordBuff = gl.createBuffer(); |
michael@0 | 76 | gl.bindBuffer(gl.ARRAY_BUFFER, vertCoordBuff); |
michael@0 | 77 | gl.bufferData(gl.ARRAY_BUFFER, vertCoordArr, gl.STATIC_DRAW); |
michael@0 | 78 | |
michael@0 | 79 | var indexArr = new Uint16Array([ |
michael@0 | 80 | 0, 1, 2, |
michael@0 | 81 | 3, |
michael@0 | 82 | ]); |
michael@0 | 83 | var indexBuff = gl.createBuffer(); |
michael@0 | 84 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuff); |
michael@0 | 85 | gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexArr, gl.STATIC_DRAW); |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | function testPixel(x, y, refData, func, infoString) { |
michael@0 | 89 | var pixel = new Uint8Array(4); |
michael@0 | 90 | gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel); |
michael@0 | 91 | |
michael@0 | 92 | var pixelMatches = pixel[0] == refData[0] && |
michael@0 | 93 | pixel[1] == refData[1] && |
michael@0 | 94 | pixel[2] == refData[2] && |
michael@0 | 95 | pixel[3] == refData[3]; |
michael@0 | 96 | func(pixelMatches, infoString); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | function preDraw(info) { |
michael@0 | 100 | gl.clearColor(1.0, 0.0, 0.0, 1.0); |
michael@0 | 101 | gl.clear(gl.COLOR_BUFFER_BIT); |
michael@0 | 102 | |
michael@0 | 103 | testPixel(0, 0, [255, 0, 0, 255], ok, '[' + info + '] Should be red before drawing.'); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function postDraw(info) { |
michael@0 | 107 | testPixel(0, 0, [0, 255, 0, 255], ok, '[' + info + '] Should be green before drawing.'); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | gl.useProgram(prog); |
michael@0 | 111 | gl.enableVertexAttribArray(prog.aVertCoord); |
michael@0 | 112 | gl.vertexAttribPointer(prog.aVertCoord, 2, gl.FLOAT, false, 0, 0); |
michael@0 | 113 | |
michael@0 | 114 | // Start drawing |
michael@0 | 115 | checkGLError(ok, 'after setup'); |
michael@0 | 116 | |
michael@0 | 117 | preDraw('DrawArrays'); |
michael@0 | 118 | gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); |
michael@0 | 119 | postDraw('DrawArrays'); |
michael@0 | 120 | checkGLError(ok, 'after DrawArrays'); |
michael@0 | 121 | |
michael@0 | 122 | preDraw('DrawElements'); |
michael@0 | 123 | gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_SHORT, 0); |
michael@0 | 124 | postDraw('DrawElements'); |
michael@0 | 125 | checkGLError(ok, 'after DrawElements'); |
michael@0 | 126 | |
michael@0 | 127 | ok(true, 'Test complete.'); |
michael@0 | 128 | })(); |
michael@0 | 129 | |
michael@0 | 130 | </script> |
michael@0 | 131 |