Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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(2., 22., 222., 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. Setup blenind to calculate max value in Red channel and number of points in Alpha channel.
  25. */
  26. const MAX_32_BIT_FLOAT = 3.402823466e38;
  27. const WIDTH = 1;
  28. const HEIGHT = 1;
  29. const POINT_COUNT = 10;
  30. const canvas = document.getElementById("webgl-canvas");
  31. canvas.width = window.innerWidth;
  32. canvas.height = window.innerHeight;
  33. const gl = canvas.getContext("webgl2");
  34. if (!gl || gl.getExtension('EXT_color_buffer_float') === null) {
  35. console.log('Not a WebGL2 context or EXT_color_buffer_float not supported, this test will fail');
  36. }
  37. const width = WIDTH;
  38. const height = HEIGHT;
  39. // set up Program
  40. const vsSource = document.getElementById("vs").text.trim();
  41. const fsSource = document.getElementById("fs").text.trim();
  42. const vs = gl.createShader(gl.VERTEX_SHADER);
  43. const fs = gl.createShader(gl.FRAGMENT_SHADER);
  44. gl.shaderSource(vs, vsSource);
  45. gl.compileShader(vs);
  46. if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
  47. let i, lines;
  48.  
  49. console.error(gl.getShaderInfoLog(vs));
  50. lines = vsSource.split("\n");
  51. for (i = 0; i < lines.length; ++i) {
  52. console.error(`${i + 1}: ${lines[i]}`);
  53. }
  54. }
  55.  
  56. gl.shaderSource(fs, fsSource);
  57. gl.compileShader(fs);
  58.  
  59. if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
  60. let i, lines;
  61.  
  62. console.error(gl.getShaderInfoLog(fs));
  63. lines = vsSource.split("\n");
  64. for (i = 0; i < lines.length; ++i) {
  65. console.error(`${i + 1}: ${lines[i]}`);
  66. }
  67. }
  68.  
  69.  
  70. const program = gl.createProgram();
  71. gl.attachShader(program, vs);
  72. gl.attachShader(program, fs);
  73. gl.linkProgram(program);
  74. gl.useProgram(program);
  75.  
  76. // set up Framebuffer to render to 1X1 texture
  77. const framebuffer = gl.createFramebuffer();
  78. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  79. gl.activeTexture(gl.TEXTURE0);
  80. const colorTarget = gl.createTexture();
  81. gl.bindTexture(gl.TEXTURE_2D, colorTarget);
  82. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  83. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  84. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  85. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  86. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, width, height, 0, gl.RGBA, gl.FLOAT, null);
  87. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTarget, 0);
  88. // GEOMETRY
  89. const quadArray = gl.createVertexArray();
  90. gl.bindVertexArray(quadArray);
  91. const vertexIDBuffer = gl.createBuffer();
  92. const vertexIDs = new Float32Array(POINT_COUNT).map((_, index) => index);
  93. console.log('Using 0, 0 for position');
  94. console.log(`VertexIDs: ${vertexIDs}`);
  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.DRAW_FRAMEBUFFER, framebuffer);
  101. gl.clearColor(1, 11, 111, 0);
  102. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  103. gl.disable(gl.BLEND);
  104. gl.disable(gl.DEPTH_TEST);
  105. // gl.blendFunc(gl.ONE, gl.ONE);
  106. // gl.blendEquationSeparate(gl.MAX, gl.FUNC_ADD);
  107.  
  108. gl.drawArrays(gl.POINTS, 0, POINT_COUNT);
  109. const data = new Float32Array(4 * width * height);
  110. gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.FLOAT, data);
  111. let failed = false;
  112. console.log('Testing Additive Blending');
  113. const expectedColor = [2, 22, 222, 0]; // alpha value depends on which fragment rendered last not checking for it.
  114. if (data[0] !== expectedColor[0]) {
  115. console.log(`Red channel: expected ${expectedColor[0]} acutal: ${data[0]}`);
  116. failed = true;
  117. }
  118. if (data[1] !== expectedColor[1]) {
  119. console.log(`Green channel: expected ${expectedColor[1]} acutal: ${data[1]}`);
  120. failed = true;
  121. }
  122. if (data[2] !== expectedColor[2]) {
  123. console.log(`Blue channel: expected ${expectedColor[2]} acutal: ${data[2]}`);
  124. failed = true;
  125. }
  126. //if (data[3] !== expectedColor[3]) {
  127. // console.log(`Alpha channel: expected ${expectedColor[3]} acutal: ${data[3]}`);
  128. // failed = true;
  129. //}
  130. console.log(`Recevied expected color : ${data.slice(0, 4)}`);
  131. if (failed) {
  132. console.log('TEST FAILED');
  133. } else {
  134. console.log('TEST PASSED');
  135. }
  136. </script>
  137. </body>
  138. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement