checkFloatTexturePrecision.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import PixelFormat from '../Core/PixelFormat.js';
  2. import CheckFloatTexturePrecisionFS from '../Shaders/CheckFloatTexturePrecisionFS.js';
  3. import ComputeCommand from './ComputeCommand.js';
  4. import ComputeEngine from './ComputeEngine.js';
  5. import Framebuffer from './Framebuffer.js';
  6. import PixelDatatype from './PixelDatatype.js';
  7. import Texture from './Texture.js';
  8. /**
  9. * Checks if the context's floating point textures support 6 decimal places of precision.
  10. *
  11. * @param {Context} context A context wrapping a gl implementation.
  12. * @returns {Boolean} Whether or not the context's floating point textures support 6 decimal places of precision
  13. *
  14. * @private
  15. */
  16. function checkFloatTexturePrecision(context) {
  17. if (!context.floatingPointTexture) {
  18. return false;
  19. }
  20. var computeEngine = new ComputeEngine(context);
  21. var outputTexture = new Texture({
  22. context : context,
  23. width : 1,
  24. height : 1,
  25. pixelFormat : PixelFormat.RGBA
  26. });
  27. var floatTexture = new Texture({
  28. context : context,
  29. width : 1,
  30. height : 1,
  31. pixelFormat : PixelFormat.RGBA,
  32. pixelDatatype : checkFloatTexturePrecision._getFloatPixelType(),
  33. source : {
  34. width : 1,
  35. height : 1,
  36. arrayBufferView : checkFloatTexturePrecision._getArray([123456, 0, 0, 0])
  37. }
  38. });
  39. var framebuffer = new Framebuffer({
  40. context : context,
  41. colorTextures : [outputTexture],
  42. destroyAttachments : false
  43. });
  44. var readState = {
  45. framebuffer : framebuffer,
  46. x : 0,
  47. y : 0,
  48. width : 1,
  49. height : 1
  50. };
  51. var sixPlaces = false;
  52. var computeCommand = new ComputeCommand({
  53. fragmentShaderSource : CheckFloatTexturePrecisionFS,
  54. outputTexture : outputTexture,
  55. uniformMap : {
  56. u_floatTexture : function() {
  57. return floatTexture;
  58. }
  59. },
  60. persists : false,
  61. postExecute : function() {
  62. var pixel = context.readPixels(readState);
  63. sixPlaces = pixel[0] === 0;
  64. }
  65. });
  66. computeCommand.execute(computeEngine);
  67. computeEngine.destroy();
  68. framebuffer.destroy();
  69. return sixPlaces;
  70. }
  71. checkFloatTexturePrecision._getFloatPixelType = function() {
  72. return PixelDatatype.FLOAT;
  73. };
  74. checkFloatTexturePrecision._getArray = function(array) {
  75. return new Float32Array(array);
  76. };
  77. export default checkFloatTexturePrecision;