summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/js/tests/invalid-vertex-attrib-test.js
blob: b28c484843ab97c45921cea6d01cf7ef0cd780c0 (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
var createInvalidAttribTestFn = (function() {

async function testPreserveDrawingBufferTrue(gl, drawFn, clear) {
  debug('');
  debug(`test preserveDrawingBuffer: true with ${drawFn.name} ${clear ? 'with' : 'without'} clear`);

  if (clear) {
    gl.clearColor(0, 0, 0, 0);
    gl.clear(gl.COLOR_BUFFER_BIT);
  }

  const skipTest = drawFn(gl);
  if (skipTest) {
    debug('skipped: extension does not exist');
    return;
  }

  wtu.checkCanvas(gl, [255, 0, 0, 255], "canvas should be red");

  await waitForComposite();

  wtu.checkCanvas(gl, [255, 0, 0, 255], "canvas should be red");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

function setupWebGL({
    webglVersion,
    shadersFn,
    attribs,
}) {
    const positionBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuf);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
      -1, -1,
       1, -1,
      -1,  1,
      -1,  1,
       1, -1,
       1,  1,
    ]), gl.STATIC_DRAW);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    const indexBuf = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint8Array([0, 1, 2, 3, 4, 5]), gl.STATIC_DRAW);
    return gl;
}

function createInvalidAttribTestFn(gl) {
  const vs = `
  attribute vec4 vPosition;
  void main()
  {
      gl_Position = vPosition;
  }
  `;

  const fs = `
  precision mediump float;
  void main()
  {
      gl_FragColor = vec4(1, 0, 0, 1);
  }
  `

  const program = wtu.setupProgram(gl, [vs, fs], ["vPosition"]);
  if (!program) {
    debug(`program failed to compile: ${wtu.getLastError()}`);
  }

  const positionBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
    -1, -1,
     1, -1,
    -1,  1,
    -1,  1,
     1, -1,
     1,  1,
  ]), gl.STATIC_DRAW);

  const indexBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
                new Uint8Array([0, 1, 2, 3, 4, 5]),
                gl.STATIC_DRAW);

  return async function invalidAttribTestFn(drawFn) {
    debug('');

    // reset attribs
    gl.bindBuffer(gl.ARRAY_BUFFER, null);
    const numAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
    for (let i = 0; i < numAttribs; ++i) {
      gl.disableVertexAttribArray(i);
      gl.vertexAttribPointer(1, 1, gl.FLOAT, false, 0, 0);
    }

    debug(`test ${drawFn.name} draws with valid attributes`);
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");

    gl.clearColor(0, 0, 0, 0,);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.checkCanvas(gl, [0, 0, 0, 0], "canvas should be zero");

    drawFn(gl);

    wtu.checkCanvas(gl, [255, 0, 0, 255], "canvas should be red");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");

    debug(`test ${drawFn.name} generates INVALID_OPERATION draws with enabled attribute no buffer bound`);
    gl.enableVertexAttribArray(1);

    gl.clearColor(0, 0, 0, 0,);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.checkCanvas(gl, [0, 0, 0, 0], "canvas should be zero");

    drawFn(gl);

    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "should generate INVALID_OPERATION");
    wtu.checkCanvas(gl, [0, 0, 0, 0], "canvas should be zero");
  };
}

return createInvalidAttribTestFn;
}());