texture-switch-performance.html (2914B)
1 <!-- 2 Copyright (c) 2019 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. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL 2 Texture Switch Conformance Tests</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 </head> 16 <body> 17 <div id="description"></div> 18 <div id="console"></div> 19 <script> 20 "use strict"; 21 description("Ensures that switching the texture referenced by a sampler uniform performs reasonably well."); 22 var wtu = WebGLTestUtils; 23 var canvas = document.createElement('canvas'); 24 canvas.width = 32; 25 canvas.height = 32; 26 var gl = wtu.create3DContext(canvas, undefined, 2); 27 if (!gl) { 28 testFailed("context does not exist"); 29 finishTest(); 30 } else { 31 var program = wtu.setupTexturedQuad(gl); 32 var tex = gl.createTexture(); 33 gl.activeTexture(gl.TEXTURE0); 34 gl.bindTexture(gl.TEXTURE_2D, tex); 35 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 36 37 var tex2 = gl.createTexture(); 38 gl.activeTexture(gl.TEXTURE1); 39 gl.bindTexture(gl.TEXTURE_2D, tex); 40 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); 41 42 var loc = gl.getUniformLocation(program, "tex"); 43 44 var RUNTIME = 2000; 45 var THRESHOLD = 0.8; 46 var baseStartTime = 0; 47 var baseFrameCount = 0; 48 var testStartTime = 0; 49 var testFrameCount = 0; 50 51 baseStartTime = Date.now(); 52 function drawBaseline() { 53 for (var i = 0; i < 400; ++i) { 54 gl.uniform1i(loc, 0); 55 gl.drawArrays(gl.TRIANGLES, 0, 6); 56 gl.uniform1i(loc, 0); 57 gl.drawArrays(gl.TRIANGLES, 0, 6); 58 } 59 60 ++baseFrameCount; 61 62 if (Date.now() - baseStartTime > RUNTIME) { 63 testStartTime = Date.now(); 64 requestAnimationFrame(drawTest); 65 } else { 66 requestAnimationFrame(drawBaseline); 67 } 68 } 69 70 function drawTest() { 71 for (var i = 0; i < 400; ++i) { 72 gl.uniform1i(loc, 0); 73 gl.drawArrays(gl.TRIANGLES, 0, 6); 74 gl.uniform1i(loc, 1); 75 gl.drawArrays(gl.TRIANGLES, 0, 6); 76 } 77 78 ++testFrameCount; 79 80 if (Date.now() - testStartTime > RUNTIME) { 81 var perfString = " - achieved " + testFrameCount + " frames in " + ((Date.now() - testStartTime) / 1000.0) + 82 " seconds (" + (testFrameCount / baseFrameCount).toFixed(2) + " times baseline performance)"; 83 if (testFrameCount > baseFrameCount * THRESHOLD) { 84 testPassed("Texture switching did not significantly hurt performance" + perfString); 85 } else { 86 testFailed("Texture switching significantly hurt performance" + perfString); 87 } 88 console.log(testFrameCount); 89 finishTest(); 90 } else { 91 requestAnimationFrame(drawTest); 92 } 93 } 94 95 requestAnimationFrame(drawBaseline); 96 } 97 var successfullyParsed = true; 98 </script> 99 </body> 100 </html>