KTX2Exporter.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import {
  2. ColorManagement,
  3. FloatType,
  4. HalfFloatType,
  5. UnsignedByteType,
  6. RGBAFormat,
  7. RGFormat,
  8. RGIntegerFormat,
  9. RedFormat,
  10. RedIntegerFormat,
  11. NoColorSpace,
  12. LinearSRGBColorSpace,
  13. SRGBColorSpace,
  14. SRGBTransfer,
  15. DataTexture,
  16. REVISION,
  17. } from 'three';
  18. import {
  19. write,
  20. KTX2Container,
  21. KHR_DF_CHANNEL_RGBSDA_ALPHA,
  22. KHR_DF_CHANNEL_RGBSDA_BLUE,
  23. KHR_DF_CHANNEL_RGBSDA_GREEN,
  24. KHR_DF_CHANNEL_RGBSDA_RED,
  25. KHR_DF_MODEL_RGBSDA,
  26. KHR_DF_PRIMARIES_BT709,
  27. KHR_DF_PRIMARIES_UNSPECIFIED,
  28. KHR_DF_SAMPLE_DATATYPE_FLOAT,
  29. KHR_DF_SAMPLE_DATATYPE_LINEAR,
  30. KHR_DF_SAMPLE_DATATYPE_SIGNED,
  31. KHR_DF_TRANSFER_LINEAR,
  32. KHR_DF_TRANSFER_SRGB,
  33. VK_FORMAT_R16_SFLOAT,
  34. VK_FORMAT_R16G16_SFLOAT,
  35. VK_FORMAT_R16G16B16A16_SFLOAT,
  36. VK_FORMAT_R32_SFLOAT,
  37. VK_FORMAT_R32G32_SFLOAT,
  38. VK_FORMAT_R32G32B32A32_SFLOAT,
  39. VK_FORMAT_R8_SRGB,
  40. VK_FORMAT_R8_UNORM,
  41. VK_FORMAT_R8G8_SRGB,
  42. VK_FORMAT_R8G8_UNORM,
  43. VK_FORMAT_R8G8B8A8_SRGB,
  44. VK_FORMAT_R8G8B8A8_UNORM,
  45. } from '../libs/ktx-parse.module.js';
  46. /**
  47. * References:
  48. * - https://github.khronos.org/KTX-Specification/ktxspec.v2.html
  49. * - https://registry.khronos.org/DataFormat/specs/1.3/dataformat.1.3.html
  50. * - https://github.com/donmccurdy/KTX-Parse
  51. */
  52. const VK_FORMAT_MAP = {
  53. [ RGBAFormat ]: {
  54. [ FloatType ]: {
  55. [ NoColorSpace ]: VK_FORMAT_R32G32B32A32_SFLOAT,
  56. [ LinearSRGBColorSpace ]: VK_FORMAT_R32G32B32A32_SFLOAT,
  57. },
  58. [ HalfFloatType ]: {
  59. [ NoColorSpace ]: VK_FORMAT_R16G16B16A16_SFLOAT,
  60. [ LinearSRGBColorSpace ]: VK_FORMAT_R16G16B16A16_SFLOAT,
  61. },
  62. [ UnsignedByteType ]: {
  63. [ NoColorSpace ]: VK_FORMAT_R8G8B8A8_UNORM,
  64. [ LinearSRGBColorSpace ]: VK_FORMAT_R8G8B8A8_UNORM,
  65. [ SRGBColorSpace ]: VK_FORMAT_R8G8B8A8_SRGB,
  66. },
  67. },
  68. [ RGFormat ]: {
  69. [ FloatType ]: {
  70. [ NoColorSpace ]: VK_FORMAT_R32G32_SFLOAT,
  71. [ LinearSRGBColorSpace ]: VK_FORMAT_R32G32_SFLOAT,
  72. },
  73. [ HalfFloatType ]: {
  74. [ NoColorSpace ]: VK_FORMAT_R16G16_SFLOAT,
  75. [ LinearSRGBColorSpace ]: VK_FORMAT_R16G16_SFLOAT,
  76. },
  77. [ UnsignedByteType ]: {
  78. [ NoColorSpace ]: VK_FORMAT_R8G8_UNORM,
  79. [ LinearSRGBColorSpace ]: VK_FORMAT_R8G8_UNORM,
  80. [ SRGBColorSpace ]: VK_FORMAT_R8G8_SRGB,
  81. },
  82. },
  83. [ RedFormat ]: {
  84. [ FloatType ]: {
  85. [ NoColorSpace ]: VK_FORMAT_R32_SFLOAT,
  86. [ LinearSRGBColorSpace ]: VK_FORMAT_R32_SFLOAT,
  87. },
  88. [ HalfFloatType ]: {
  89. [ NoColorSpace ]: VK_FORMAT_R16_SFLOAT,
  90. [ LinearSRGBColorSpace ]: VK_FORMAT_R16_SFLOAT,
  91. },
  92. [ UnsignedByteType ]: {
  93. [ NoColorSpace ]: VK_FORMAT_R8_UNORM,
  94. [ LinearSRGBColorSpace ]: VK_FORMAT_R8_UNORM,
  95. [ SRGBColorSpace ]: VK_FORMAT_R8_SRGB,
  96. },
  97. },
  98. };
  99. const KHR_DF_CHANNEL_MAP = [
  100. KHR_DF_CHANNEL_RGBSDA_RED,
  101. KHR_DF_CHANNEL_RGBSDA_GREEN,
  102. KHR_DF_CHANNEL_RGBSDA_BLUE,
  103. KHR_DF_CHANNEL_RGBSDA_ALPHA,
  104. ];
  105. // TODO: sampleLower and sampleUpper may change based on color space.
  106. const KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER = {
  107. [ FloatType ]: [ 0xbf800000, 0x3f800000 ],
  108. [ HalfFloatType ]: [ 0xbf800000, 0x3f800000 ],
  109. [ UnsignedByteType ]: [ 0, 255 ],
  110. };
  111. const ERROR_INPUT = 'THREE.KTX2Exporter: Supported inputs are DataTexture, Data3DTexture, or WebGLRenderer and WebGLRenderTarget.';
  112. const ERROR_FORMAT = 'THREE.KTX2Exporter: Supported formats are RGBAFormat, RGFormat, or RedFormat.';
  113. const ERROR_TYPE = 'THREE.KTX2Exporter: Supported types are FloatType, HalfFloatType, or UnsignedByteType."';
  114. const ERROR_COLOR_SPACE = 'THREE.KTX2Exporter: Supported color spaces are SRGBColorSpace (UnsignedByteType only), LinearSRGBColorSpace, or NoColorSpace.';
  115. /**
  116. * An exporter for KTX2.
  117. *
  118. * ```js
  119. * const exporter = new KTX2Exporter();
  120. * const result = await exporter.parse( dataTexture );
  121. * ```
  122. *
  123. * @three_import import { KTX2Exporter } from 'three/addons/exporters/KTX2Exporter.js';
  124. */
  125. export class KTX2Exporter {
  126. /**
  127. * This method has two variants.
  128. *
  129. * - When exporting a data texture, it receives one parameter. The data or 3D data texture.
  130. * - When exporting a render target (e.g. a PMREM), it receives two parameters. The renderer and the
  131. * render target.
  132. *
  133. * @async
  134. * @param {(DataTexture|Data3DTexture|WebGPURenderer|WebGLRenderer)} arg1 - The data texture to export or a renderer.
  135. * @param {RenderTarget} [arg2] - The render target that should be exported
  136. * @return {Promise<Uint8Array>} A Promise that resolves with the exported KTX2.
  137. */
  138. async parse( arg1, arg2 ) {
  139. let texture;
  140. if ( arg1.isDataTexture || arg1.isData3DTexture ) {
  141. texture = arg1;
  142. } else if ( ( arg1.isWebGLRenderer || arg1.isWebGPURenderer ) && arg2.isRenderTarget ) {
  143. texture = await toDataTexture( arg1, arg2 );
  144. } else {
  145. throw new Error( ERROR_INPUT );
  146. }
  147. if ( VK_FORMAT_MAP[ texture.format ] === undefined ) {
  148. throw new Error( ERROR_FORMAT );
  149. }
  150. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ] === undefined ) {
  151. throw new Error( ERROR_TYPE );
  152. }
  153. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ] === undefined ) {
  154. throw new Error( ERROR_COLOR_SPACE );
  155. }
  156. //
  157. const array = texture.image.data;
  158. const channelCount = getChannelCount( texture );
  159. const container = new KTX2Container();
  160. container.vkFormat = VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ];
  161. container.typeSize = array.BYTES_PER_ELEMENT;
  162. container.pixelWidth = texture.image.width;
  163. container.pixelHeight = texture.image.height;
  164. if ( texture.isData3DTexture ) {
  165. container.pixelDepth = texture.image.depth;
  166. }
  167. //
  168. const basicDesc = container.dataFormatDescriptor[ 0 ];
  169. basicDesc.colorModel = KHR_DF_MODEL_RGBSDA;
  170. basicDesc.colorPrimaries = texture.colorSpace === NoColorSpace
  171. ? KHR_DF_PRIMARIES_UNSPECIFIED
  172. : KHR_DF_PRIMARIES_BT709;
  173. basicDesc.transferFunction = ColorManagement.getTransfer( texture.colorSpace ) === SRGBTransfer
  174. ? KHR_DF_TRANSFER_SRGB
  175. : KHR_DF_TRANSFER_LINEAR;
  176. basicDesc.texelBlockDimension = [ 0, 0, 0, 0 ];
  177. basicDesc.bytesPlane = [
  178. container.typeSize * channelCount, 0, 0, 0, 0, 0, 0, 0,
  179. ];
  180. for ( let i = 0; i < channelCount; ++ i ) {
  181. let channelType = KHR_DF_CHANNEL_MAP[ i ];
  182. // Assign KHR_DF_SAMPLE_DATATYPE_LINEAR if the channel is linear _and_ differs from the transfer function.
  183. if ( channelType === KHR_DF_CHANNEL_RGBSDA_ALPHA && basicDesc.transferFunction !== KHR_DF_TRANSFER_LINEAR ) {
  184. channelType |= KHR_DF_SAMPLE_DATATYPE_LINEAR;
  185. }
  186. if ( texture.type === FloatType || texture.type === HalfFloatType ) {
  187. channelType |= KHR_DF_SAMPLE_DATATYPE_FLOAT;
  188. channelType |= KHR_DF_SAMPLE_DATATYPE_SIGNED;
  189. }
  190. basicDesc.samples.push( {
  191. channelType: channelType,
  192. bitOffset: i * array.BYTES_PER_ELEMENT * 8,
  193. bitLength: array.BYTES_PER_ELEMENT * 8 - 1,
  194. samplePosition: [ 0, 0, 0, 0 ],
  195. sampleLower: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 0 ],
  196. sampleUpper: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 1 ],
  197. } );
  198. }
  199. //
  200. container.levels = [ {
  201. levelData: new Uint8Array( array.buffer, array.byteOffset, array.byteLength ),
  202. uncompressedByteLength: array.byteLength,
  203. } ];
  204. //
  205. container.keyValue[ 'KTXwriter' ] = `three.js ${ REVISION }`;
  206. //
  207. return write( container, { keepWriter: true } );
  208. }
  209. }
  210. async function toDataTexture( renderer, rtt ) {
  211. const channelCount = getChannelCount( rtt.texture );
  212. let view;
  213. if ( renderer.isWebGLRenderer ) {
  214. if ( rtt.texture.type === FloatType ) {
  215. view = new Float32Array( rtt.width * rtt.height * channelCount );
  216. } else if ( rtt.texture.type === HalfFloatType ) {
  217. view = new Uint16Array( rtt.width * rtt.height * channelCount );
  218. } else if ( rtt.texture.type === UnsignedByteType ) {
  219. view = new Uint8Array( rtt.width * rtt.height * channelCount );
  220. } else {
  221. throw new Error( ERROR_TYPE );
  222. }
  223. await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height, view );
  224. } else {
  225. view = await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height );
  226. }
  227. const texture = new DataTexture( view, rtt.width, rtt.height, rtt.texture.format, rtt.texture.type );
  228. texture.colorSpace = rtt.texture.colorSpace;
  229. return texture;
  230. }
  231. function getChannelCount( texture ) {
  232. switch ( texture.format ) {
  233. case RGBAFormat:
  234. return 4;
  235. case RGFormat:
  236. case RGIntegerFormat:
  237. return 2;
  238. case RedFormat:
  239. case RedIntegerFormat:
  240. return 1;
  241. default:
  242. throw new Error( ERROR_FORMAT );
  243. }
  244. }