1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function SetupShaderProgram(gl) {
6 // Create Vertex Shader.
7 var vertex_shader = '' +
8 'attribute vec4 vPosition;\n' +
9 'attribute vec2 texCoord0;\n' +
10 'varying vec2 texCoord;\n' +
12 ' gl_Position = vPosition;\n' +
13 ' texCoord = texCoord0;\n' +
15 var vs = gl.createShader(gl.VERTEX_SHADER);
16 gl.shaderSource(vs, vertex_shader);
19 // Create Fragment Shader.
20 var fragment_shader = '' +
22 'precision mediump float;\n' +
24 'uniform sampler2D tex;\n' +
25 'varying vec2 texCoord;\n' +
27 ' gl_FragData[0] = texture2D(tex, texCoord);\n' +
29 var fs = gl.createShader(gl.FRAGMENT_SHADER);
30 gl.shaderSource(fs, fragment_shader);
34 var program = gl.createProgram();
35 gl.attachShader(program, vs);
36 gl.attachShader(program, fs);
38 gl.bindAttribLocation(program, 0, 'vPosition');
39 gl.bindAttribLocation(program, 1, 'texCoord0');
40 gl.linkProgram(program);
44 gl.useProgram(program);
47 function SetupQuad(gl) {
48 SetupShaderProgram(gl);
51 var vertexObject = gl.createBuffer();
52 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
53 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
59 1.0, -1.0, 0.0]), gl.STATIC_DRAW);
60 gl.enableVertexAttribArray(0);
61 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
63 var vertexObject = gl.createBuffer();
64 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
65 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
71 1.0, 0.0]), gl.STATIC_DRAW);
72 gl.enableVertexAttribArray(1);
73 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
76 function DoTextureUploadBenchmark(gl, dimension) {
77 SetupShaderProgram(gl);
80 var canvasTexture = gl.createTexture();
81 gl.bindTexture(gl.TEXTURE_2D, canvasTexture);
82 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
83 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
88 function update_texture() {
89 requestAnimationFrame(update_texture);
90 var pixels = new Uint8Array(dimension * dimension * 4);
91 if (((frame + delta) < 0) || (frame + delta) >= 256 * 3)
95 for (var i = 0; i < dimension * dimension; ++i) {
96 pixels[i*4] = Math.min(frame, 255);
97 pixels[i*4+1] = Math.max(256, Math.min(511, frame)) - 256;
98 pixels[i*4+2] = Math.max(512, frame) - 512;
102 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, dimension, dimension, 0,
103 gl.RGBA, gl.UNSIGNED_BYTE, pixels);
104 gl.drawArrays(gl.TRIANGLES, 0, 6);