Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <script type="vertex" id="vs">
  4. attribute float vertexID;
  5. varying vec4 vColor;
  6. void main(void) {
  7. gl_Position = vec4(0., 0., 0., 1.); // Failed on windows chrome/firefox, passed on firefox with disable-angle
  8. // gl_Position = vec4(-1., -1., 0., 1.); // Failed on widnows chrome/firefox, passed on firefox with disable-angle
  9. // gl_Position = vec4(0.5, 0.5, 0., 1.); // Failed on widnows chrome/firefox, passed on firefox with disable-angle
  10. vColor = vec4(1, 1, 1, vertexID);
  11. }
  12. </script>
  13. <script type="fragment" id="fs">
  14. precision highp float;
  15. varying vec4 vColor;
  16. void main(void) {
  17. gl_FragColor = vColor;
  18. }
  19. </script>
  20. <canvas id="webgl-canvas"></canvas>
  21. <script>
  22. /*
  23. Setup a frame buffer with 1X1 texture, and render N points to the same pixel position.
  24. Clear the render target with (0, 0, 0) and render (1, 1, 1) and peform readPixels to verify the value
  25. */
  26. const WIDTH = 1;
  27. const HEIGHT = 1;
  28. const POINT_COUNT = 10;
  29. const canvas = document.getElementById("webgl-canvas");
  30. canvas.width = window.innerWidth;
  31. canvas.height = window.innerHeight;
  32. const gl = canvas.getContext("webgl");
  33. const width = WIDTH;
  34. const height = HEIGHT;
  35. // set up Program
  36. const vsSource = document.getElementById("vs").text.trim();
  37. const fsSource = document.getElementById("fs").text.trim();
  38. const vs = gl.createShader(gl.VERTEX_SHADER);
  39. const fs = gl.createShader(gl.FRAGMENT_SHADER);
  40. gl.shaderSource(vs, vsSource);
  41. gl.compileShader(vs);
  42. if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
  43. let i, lines;
  44.  
  45. console.error(gl.getShaderInfoLog(vs));
  46. lines = vsSource.split("\n");
  47. for (i = 0; i < lines.length; ++i) {
  48. console.error(`${i + 1}: ${lines[i]}`);
  49. }
  50. }
  51.  
  52. gl.shaderSource(fs, fsSource);
  53. gl.compileShader(fs);
  54. if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
  55. let i, lines;
  56.  
  57. console.error(gl.getShaderInfoLog(fs));
  58. lines = vsSource.split("\n");
  59. for (i = 0; i < lines.length; ++i) {
  60. console.error(`${i + 1}: ${lines[i]}`);
  61. }
  62. }
  63.  
  64.  
  65. const program = gl.createProgram();
  66. gl.attachShader(program, vs);
  67. gl.attachShader(program, fs);
  68. gl.linkProgram(program);
  69. gl.useProgram(program);
  70.  
  71. // set up Framebuffer to render to 1X1 texture
  72. const framebuffer = gl.createFramebuffer();
  73. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  74. gl.activeTexture(gl.TEXTURE0);
  75. const colorTarget = gl.createTexture();
  76. gl.bindTexture(gl.TEXTURE_2D, colorTarget);
  77. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  78. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  79. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  80. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  81. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  82. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTarget, 0);
  83. // GEOMETRY
  84. // var ext = gl.getExtension("OES_vertex_array_object");
  85. // let quadArray;
  86. // if (!ext) {
  87. // console.error('This test will fail, OES_vertex_array_object is not supported');
  88. // } else {
  89. // quadArray = ext.createVertexArrayOES();
  90. // }
  91. // const quadArray = gl.createVertexArrayOES();
  92. // gl.bindVertexArray(quadArray);
  93. const vertexIDBuffer = gl.createBuffer();
  94. const vertexIDs = new Float32Array(POINT_COUNT).map((_, index) => index);
  95. gl.bindBuffer(gl.ARRAY_BUFFER, vertexIDBuffer);
  96. gl.bufferData(gl.ARRAY_BUFFER, vertexIDs, gl.STATIC_DRAW);
  97. gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
  98. gl.enableVertexAttribArray(0);
  99. gl.viewport(0, 0, width, height);
  100. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  101. gl.clearColor(0, 0, 0, 0);
  102. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  103. gl.disable(gl.BLEND);
  104. gl.disable(gl.DEPTH_TEST);
  105.  
  106. gl.drawArrays(gl.POINTS, 0, POINT_COUNT);
  107. const data = new Uint8Array(4 * width * height);
  108. gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, data);
  109. let failed = false;
  110. console.log('--- Testing Rendering to 1X1 Texture ---');
  111. const expectedColor = [255, 255, 255]; // alpha value depends on which fragment rendered last not checking for it.
  112. const EPSILON = 0.000001;
  113. if (Math.abs(data[0] - expectedColor[0]) > EPSILON) {
  114. console.log(`Red channel: expected ${expectedColor[0]} actual: ${data[0]}`);
  115. failed = true;
  116. }
  117. if (Math.abs(data[1] - expectedColor[1]) > EPSILON) {
  118. console.log(`Green channel: expected ${expectedColor[1]} actual: ${data[1]}`);
  119. failed = true;
  120. }
  121. if (Math.abs(data[2] - expectedColor[2]) > EPSILON) {
  122. console.log(`Blue channel: expected ${expectedColor[2]} actual: ${data[2]}`);
  123. failed = true;
  124. }
  125.  
  126. console.log(`Expected color : ${expectedColor.slice(0, 3)}`);
  127. console.log(`Received color : ${data.slice(0, 3)}`);
  128. if (failed) {
  129. console.log('TEST FAILED');
  130. } else {
  131. console.log('TEST PASSED');
  132. }
  133. </script>
  134. </body>
  135. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement