babylon.tools.dds.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. module BABYLON.Internals {
  2. // Based on demo done by Brandon Jones - http://media.tojicode.com/webgl-samples/dds.html
  3. // All values and structures referenced from:
  4. // http://msdn.microsoft.com/en-us/library/bb943991.aspx/
  5. var DDS_MAGIC = 0x20534444;
  6. var DDSD_CAPS = 0x1,
  7. DDSD_HEIGHT = 0x2,
  8. DDSD_WIDTH = 0x4,
  9. DDSD_PITCH = 0x8,
  10. DDSD_PIXELFORMAT = 0x1000,
  11. DDSD_MIPMAPCOUNT = 0x20000,
  12. DDSD_LINEARSIZE = 0x80000,
  13. DDSD_DEPTH = 0x800000;
  14. var DDSCAPS_COMPLEX = 0x8,
  15. DDSCAPS_MIPMAP = 0x400000,
  16. DDSCAPS_TEXTURE = 0x1000;
  17. var DDSCAPS2_CUBEMAP = 0x200,
  18. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400,
  19. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800,
  20. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000,
  21. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000,
  22. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000,
  23. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000,
  24. DDSCAPS2_VOLUME = 0x200000;
  25. var DDPF_ALPHAPIXELS = 0x1,
  26. DDPF_ALPHA = 0x2,
  27. DDPF_FOURCC = 0x4,
  28. DDPF_RGB = 0x40,
  29. DDPF_YUV = 0x200,
  30. DDPF_LUMINANCE = 0x20000;
  31. function FourCCToInt32(value) {
  32. return value.charCodeAt(0) +
  33. (value.charCodeAt(1) << 8) +
  34. (value.charCodeAt(2) << 16) +
  35. (value.charCodeAt(3) << 24);
  36. }
  37. function Int32ToFourCC(value) {
  38. return String.fromCharCode(
  39. value & 0xff,
  40. (value >> 8) & 0xff,
  41. (value >> 16) & 0xff,
  42. (value >> 24) & 0xff
  43. );
  44. }
  45. var FOURCC_DXT1 = FourCCToInt32("DXT1");
  46. var FOURCC_DXT3 = FourCCToInt32("DXT3");
  47. var FOURCC_DXT5 = FourCCToInt32("DXT5");
  48. var headerLengthInt = 31; // The header length in 32 bit ints
  49. // Offsets into the header array
  50. var off_magic = 0;
  51. var off_size = 1;
  52. var off_flags = 2;
  53. var off_height = 3;
  54. var off_width = 4;
  55. var off_mipmapCount = 7;
  56. var off_pfFlags = 20;
  57. var off_pfFourCC = 21;
  58. var off_RGBbpp = 22;
  59. var off_RMask = 23;
  60. var off_GMask = 24;
  61. var off_BMask = 25;
  62. var off_AMask = 26;
  63. var off_caps1 = 27;
  64. var off_caps2 = 28;
  65. export interface DDSInfo {
  66. width: number;
  67. height: number;
  68. mipmapCount: number;
  69. isFourCC: boolean;
  70. isRGB: boolean;
  71. isLuminance: boolean;
  72. isCube: boolean
  73. };
  74. export class DDSTools {
  75. public static GetDDSInfo(arrayBuffer: any): DDSInfo {
  76. var header = new Int32Array(arrayBuffer, 0, headerLengthInt);
  77. var mipmapCount = 1;
  78. if (header[off_flags] & DDSD_MIPMAPCOUNT) {
  79. mipmapCount = Math.max(1, header[off_mipmapCount]);
  80. }
  81. return {
  82. width: header[off_width],
  83. height: header[off_height],
  84. mipmapCount: mipmapCount,
  85. isFourCC: (header[off_pfFlags] & DDPF_FOURCC) === DDPF_FOURCC,
  86. isRGB: (header[off_pfFlags] & DDPF_RGB) === DDPF_RGB,
  87. isLuminance: (header[off_pfFlags] & DDPF_LUMINANCE) === DDPF_LUMINANCE,
  88. isCube: (header[off_caps2] & DDSCAPS2_CUBEMAP) === DDSCAPS2_CUBEMAP
  89. };
  90. }
  91. private static GetRGBAArrayBuffer(width: number, height: number, dataOffset: number, dataLength: number, arrayBuffer: ArrayBuffer): Uint8Array {
  92. var byteArray = new Uint8Array(dataLength);
  93. var srcData = new Uint8Array(arrayBuffer);
  94. var index = 0;
  95. for (var y = height - 1; y >= 0; y--) {
  96. for (var x = 0; x < width; x++) {
  97. var srcPos = dataOffset + (x + y * width) * 4;
  98. byteArray[index + 2] = srcData[srcPos];
  99. byteArray[index + 1] = srcData[srcPos + 1];
  100. byteArray[index] = srcData[srcPos + 2];
  101. byteArray[index + 3] = srcData[srcPos + 3];
  102. index += 4;
  103. }
  104. }
  105. return byteArray;
  106. }
  107. private static GetRGBArrayBuffer(width: number, height: number, dataOffset:number, dataLength: number, arrayBuffer: ArrayBuffer): Uint8Array {
  108. var byteArray = new Uint8Array(dataLength);
  109. var srcData = new Uint8Array(arrayBuffer);
  110. var index = 0;
  111. for (var y = height - 1; y >= 0; y--) {
  112. for (var x = 0; x < width; x++) {
  113. var srcPos = dataOffset + (x + y * width) * 3;
  114. byteArray[index + 2] = srcData[srcPos];
  115. byteArray[index + 1] = srcData[srcPos + 1];
  116. byteArray[index] = srcData[srcPos + 2];
  117. index += 3;
  118. }
  119. }
  120. return byteArray;
  121. }
  122. private static GetLuminanceArrayBuffer(width: number, height: number, dataOffset: number, dataLength: number, arrayBuffer: ArrayBuffer): Uint8Array {
  123. var byteArray = new Uint8Array(dataLength);
  124. var srcData = new Uint8Array(arrayBuffer);
  125. var index = 0;
  126. for (var y = height - 1; y >= 0; y--) {
  127. for (var x = 0; x < width; x++) {
  128. var srcPos = dataOffset + (x + y * width);
  129. byteArray[index] = srcData[srcPos];
  130. index++;
  131. }
  132. }
  133. return byteArray;
  134. }
  135. public static UploadDDSLevels(gl: WebGLRenderingContext, ext: any, arrayBuffer: any, info: DDSInfo, loadMipmaps: boolean, faces: number): void {
  136. var header = new Int32Array(arrayBuffer, 0, headerLengthInt),
  137. fourCC, blockBytes, internalFormat,
  138. width, height, dataLength, dataOffset,
  139. byteArray, mipmapCount, i;
  140. if (header[off_magic] != DDS_MAGIC) {
  141. Tools.Error("Invalid magic number in DDS header");
  142. return;
  143. }
  144. if (!info.isFourCC && !info.isRGB && !info.isLuminance) {
  145. Tools.Error("Unsupported format, must contain a FourCC, RGB or LUMINANCE code");
  146. return;
  147. }
  148. if (info.isFourCC) {
  149. fourCC = header[off_pfFourCC];
  150. switch (fourCC) {
  151. case FOURCC_DXT1:
  152. blockBytes = 8;
  153. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  154. break;
  155. case FOURCC_DXT3:
  156. blockBytes = 16;
  157. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  158. break;
  159. case FOURCC_DXT5:
  160. blockBytes = 16;
  161. internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  162. break;
  163. default:
  164. console.error("Unsupported FourCC code:", Int32ToFourCC(fourCC));
  165. return;
  166. }
  167. }
  168. mipmapCount = 1;
  169. if (header[off_flags] & DDSD_MIPMAPCOUNT && loadMipmaps !== false) {
  170. mipmapCount = Math.max(1, header[off_mipmapCount]);
  171. }
  172. var bpp = header[off_RGBbpp];
  173. for (var face = 0; face < faces; face++) {
  174. var sampler = faces == 1 ? gl.TEXTURE_2D : (gl.TEXTURE_CUBE_MAP_POSITIVE_X + face);
  175. width = header[off_width];
  176. height = header[off_height];
  177. dataOffset = header[off_size] + 4;
  178. for (i = 0; i < mipmapCount; ++i) {
  179. if (info.isRGB) {
  180. if (bpp == 24) {
  181. dataLength = width * height * 3;
  182. byteArray = DDSTools.GetRGBArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  183. gl.texImage2D(sampler, i, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_BYTE, byteArray);
  184. } else { // 32
  185. dataLength = width * height * 4;
  186. byteArray = DDSTools.GetRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  187. gl.texImage2D(sampler, i, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, byteArray);
  188. }
  189. } else if (info.isLuminance) {
  190. var unpackAlignment = gl.getParameter(gl.UNPACK_ALIGNMENT);
  191. var unpaddedRowSize = width;
  192. var paddedRowSize = Math.floor((width + unpackAlignment - 1) / unpackAlignment) * unpackAlignment;
  193. dataLength = paddedRowSize * (height - 1) + unpaddedRowSize;
  194. byteArray = DDSTools.GetLuminanceArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
  195. gl.texImage2D(sampler, i, gl.LUMINANCE, width, height, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, byteArray);
  196. } else {
  197. dataLength = Math.max(4, width) / 4 * Math.max(4, height) / 4 * blockBytes;
  198. byteArray = new Uint8Array(arrayBuffer, dataOffset, dataLength);
  199. gl.compressedTexImage2D(sampler, i, internalFormat, width, height, 0, byteArray);
  200. }
  201. dataOffset += dataLength;
  202. width *= 0.5;
  203. height *= 0.5;
  204. width = Math.max(1.0, width);
  205. height = Math.max(1.0, height);
  206. }
  207. }
  208. }
  209. }
  210. }