babylon.tools.dds.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Tools = BABYLON.Tools || {};
  4. // Based on https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js
  5. // All values and structures referenced from:
  6. // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
  7. var DDS_MAGIC = 0x20534444;
  8. var DDSD_CAPS = 0x1,
  9. DDSD_HEIGHT = 0x2,
  10. DDSD_WIDTH = 0x4,
  11. DDSD_PITCH = 0x8,
  12. DDSD_PIXELFORMAT = 0x1000,
  13. DDSD_MIPMAPCOUNT = 0x20000,
  14. DDSD_LINEARSIZE = 0x80000,
  15. DDSD_DEPTH = 0x800000;
  16. var DDSCAPS_COMPLEX = 0x8,
  17. DDSCAPS_MIPMAP = 0x400000,
  18. DDSCAPS_TEXTURE = 0x1000;
  19. var DDSCAPS2_CUBEMAP = 0x200,
  20. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
  21. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
  22. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
  23. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
  24. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
  25. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
  26. DDSCAPS2_VOLUME = 0x200000;
  27. var DDPF_ALPHAPIXELS = 0x1,
  28. DDPF_ALPHA = 0x2,
  29. DDPF_FOURCC = 0x4,
  30. DDPF_RGB = 0x40,
  31. DDPF_YUV = 0x200,
  32. DDPF_LUMINANCE = 0x20000;
  33. function fourCCToInt32(value) {
  34. return value.charCodeAt(0) +
  35. (value.charCodeAt(1) << 8) +
  36. (value.charCodeAt(2) << 16) +
  37. (value.charCodeAt(3) << 24);
  38. }
  39. function int32ToFourCC(value) {
  40. return String.fromCharCode(
  41. value & 0xff,
  42. (value >> 8) & 0xff,
  43. (value >> 16) & 0xff,
  44. (value >> 24) & 0xff
  45. );
  46. }
  47. var FOURCC_DXT1 = fourCCToInt32("DXT1");
  48. var FOURCC_DXT3 = fourCCToInt32("DXT3");
  49. var FOURCC_DXT5 = fourCCToInt32("DXT5");
  50. var headerLengthInt = 31; // The header length in 32 bit ints
  51. // Offsets into the header array
  52. var off_magic = 0;
  53. var off_size = 1;
  54. var off_flags = 2;
  55. var off_height = 3;
  56. var off_width = 4;
  57. var off_mipmapCount = 7;
  58. var off_pfFlags = 20;
  59. var off_pfFourCC = 21;
  60. function dxtToRgb565(src, src16Offset, width, height) {
  61. var c = new Uint16Array(4);
  62. var dst = new Uint16Array(width * height);
  63. var nWords = (width * height) / 4;
  64. var m = 0;
  65. var dstI = 0;
  66. var i = 0;
  67. var r0 = 0, g0 = 0, b0 = 0, r1 = 0, g1 = 0, b1 = 0;
  68. var blockWidth = width / 4;
  69. var blockHeight = height / 4;
  70. for (var blockY = 0; blockY < blockHeight; blockY++) {
  71. for (var blockX = 0; blockX < blockWidth; blockX++) {
  72. i = src16Offset + 4 * (blockY * blockWidth + blockX);
  73. c[0] = src[i];
  74. c[1] = src[i + 1];
  75. r0 = c[0] & 0x1f;
  76. g0 = c[0] & 0x7e0;
  77. b0 = c[0] & 0xf800;
  78. r1 = c[1] & 0x1f;
  79. g1 = c[1] & 0x7e0;
  80. b1 = c[1] & 0xf800;
  81. // Interpolate between c0 and c1 to get c2 and c3.
  82. // Note that we approximate 1/3 as 3/8 and 2/3 as 5/8 for
  83. // speed. This also appears to be what the hardware DXT
  84. // decoder in many GPUs does :)
  85. c[2] = ((5 * r0 + 3 * r1) >> 3)
  86. | (((5 * g0 + 3 * g1) >> 3) & 0x7e0)
  87. | (((5 * b0 + 3 * b1) >> 3) & 0xf800);
  88. c[3] = ((5 * r1 + 3 * r0) >> 3)
  89. | (((5 * g1 + 3 * g0) >> 3) & 0x7e0)
  90. | (((5 * b1 + 3 * b0) >> 3) & 0xf800);
  91. m = src[i + 2];
  92. dstI = (blockY * 4) * width + blockX * 4;
  93. dst[dstI] = c[m & 0x3];
  94. dst[dstI + 1] = c[(m >> 2) & 0x3];
  95. dst[dstI + 2] = c[(m >> 4) & 0x3];
  96. dst[dstI + 3] = c[(m >> 6) & 0x3];
  97. dstI += width;
  98. dst[dstI] = c[(m >> 8) & 0x3];
  99. dst[dstI + 1] = c[(m >> 10) & 0x3];
  100. dst[dstI + 2] = c[(m >> 12) & 0x3];
  101. dst[dstI + 3] = c[(m >> 14)];
  102. m = src[i + 3];
  103. dstI += width;
  104. dst[dstI] = c[m & 0x3];
  105. dst[dstI + 1] = c[(m >> 2) & 0x3];
  106. dst[dstI + 2] = c[(m >> 4) & 0x3];
  107. dst[dstI + 3] = c[(m >> 6) & 0x3];
  108. dstI += width;
  109. dst[dstI] = c[(m >> 8) & 0x3];
  110. dst[dstI + 1] = c[(m >> 10) & 0x3];
  111. dst[dstI + 2] = c[(m >> 12) & 0x3];
  112. dst[dstI + 3] = c[(m >> 14)];
  113. }
  114. }
  115. return dst;
  116. }
  117. function uploadDDSLevels(gl, ext, arrayBuffer) {
  118. var header = new Int32Array(arrayBuffer, 0, headerLengthInt),
  119. fourCC, blockBytes, internalFormat,
  120. width, height, dataLength, dataOffset,
  121. rgb565Data, byteArray, mipmapCount, i;
  122. if (header[off_magic] != DDS_MAGIC) {
  123. console.error("Invalid magic number in DDS header");
  124. return 0;
  125. }
  126. if (!header[off_pfFlags] & DDPF_FOURCC) {
  127. console.error("Unsupported format, must contain a FourCC code");
  128. return 0;
  129. }
  130. fourCC = header[off_pfFourCC];
  131. switch (fourCC) {
  132. case FOURCC_DXT1:
  133. blockBytes = 8;
  134. internalFormat = ext ? ext.COMPRESSED_RGB_S3TC_DXT1_EXT : null;
  135. break;
  136. case FOURCC_DXT3:
  137. blockBytes = 16;
  138. internalFormat = ext ? ext.COMPRESSED_RGBA_S3TC_DXT3_EXT : null;
  139. break;
  140. case FOURCC_DXT5:
  141. blockBytes = 16;
  142. internalFormat = ext ? ext.COMPRESSED_RGBA_S3TC_DXT5_EXT : null;
  143. break;
  144. default:
  145. console.error("Unsupported FourCC code:", int32ToFourCC(fourCC));
  146. return null;
  147. }
  148. mipmapCount = 1;
  149. if (header[off_flags] & DDSD_MIPMAPCOUNT && loadMipmaps !== false) {
  150. mipmapCount = Math.max(1, header[off_mipmapCount]);
  151. }
  152. width = header[off_width];
  153. height = header[off_height];
  154. dataOffset = header[off_size] + 4;
  155. if (ext) {
  156. for (i = 0; i < mipmapCount; ++i) {
  157. dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
  158. byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength);
  159. gl.compressedTexImage2D(gl.TEXTURE_2D, i, internalFormat, width, height, 0, byteArray);
  160. dataOffset += dataLength;
  161. width *= 0.5;
  162. height *= 0.5;
  163. }
  164. } else {
  165. if (fourCC == FOURCC_DXT1) {
  166. dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
  167. byteArray = new Uint16Array(arrayBuffer);
  168. rgb565Data = dxtToRgb565(byteArray, dataOffset / 2, width, height);
  169. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, rgb565Data);
  170. if (loadMipmaps) {
  171. gl.generateMipmap(gl.TEXTURE_2D);
  172. }
  173. } else {
  174. console.error("No manual decoder for", int32ToFourCC(fourCC), "and no native support");
  175. return 0;
  176. }
  177. }
  178. return mipmapCount;
  179. }
  180. BABYLON.Tools.LoadDDSTexture = function(gl, ext, data) {
  181. return uploadDDSLevels(gl, ext, data);
  182. };
  183. })();