webgpuTextureHelper.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /* eslint-disable babylonjs/available */
  2. // eslint-disable-next-line @typescript-eslint/naming-convention
  3. import * as WebGPUConstants from "./webgpuConstants.js";
  4. import { Scalar } from "../../Maths/math.scalar.js";
  5. /** @internal */
  6. export class WebGPUTextureHelper {
  7. static ComputeNumMipmapLevels(width, height) {
  8. return Scalar.ILog2(Math.max(width, height)) + 1;
  9. }
  10. static GetTextureTypeFromFormat(format) {
  11. switch (format) {
  12. // One Component = 8 bits
  13. case WebGPUConstants.TextureFormat.R8Unorm:
  14. case WebGPUConstants.TextureFormat.R8Snorm:
  15. case WebGPUConstants.TextureFormat.R8Uint:
  16. case WebGPUConstants.TextureFormat.R8Sint:
  17. case WebGPUConstants.TextureFormat.RG8Unorm:
  18. case WebGPUConstants.TextureFormat.RG8Snorm:
  19. case WebGPUConstants.TextureFormat.RG8Uint:
  20. case WebGPUConstants.TextureFormat.RG8Sint:
  21. case WebGPUConstants.TextureFormat.RGBA8Unorm:
  22. case WebGPUConstants.TextureFormat.RGBA8UnormSRGB:
  23. case WebGPUConstants.TextureFormat.RGBA8Snorm:
  24. case WebGPUConstants.TextureFormat.RGBA8Uint:
  25. case WebGPUConstants.TextureFormat.RGBA8Sint:
  26. case WebGPUConstants.TextureFormat.BGRA8Unorm:
  27. case WebGPUConstants.TextureFormat.BGRA8UnormSRGB:
  28. case WebGPUConstants.TextureFormat.RGB10A2UINT: // composite format - let's say it's byte...
  29. case WebGPUConstants.TextureFormat.RGB10A2Unorm: // composite format - let's say it's byte...
  30. case WebGPUConstants.TextureFormat.RGB9E5UFloat: // composite format - let's say it's byte...
  31. case WebGPUConstants.TextureFormat.RG11B10UFloat: // composite format - let's say it's byte...
  32. case WebGPUConstants.TextureFormat.BC7RGBAUnorm:
  33. case WebGPUConstants.TextureFormat.BC7RGBAUnormSRGB:
  34. case WebGPUConstants.TextureFormat.BC6HRGBUFloat:
  35. case WebGPUConstants.TextureFormat.BC6HRGBFloat:
  36. case WebGPUConstants.TextureFormat.BC5RGUnorm:
  37. case WebGPUConstants.TextureFormat.BC5RGSnorm:
  38. case WebGPUConstants.TextureFormat.BC3RGBAUnorm:
  39. case WebGPUConstants.TextureFormat.BC3RGBAUnormSRGB:
  40. case WebGPUConstants.TextureFormat.BC2RGBAUnorm:
  41. case WebGPUConstants.TextureFormat.BC2RGBAUnormSRGB:
  42. case WebGPUConstants.TextureFormat.BC4RUnorm:
  43. case WebGPUConstants.TextureFormat.BC4RSnorm:
  44. case WebGPUConstants.TextureFormat.BC1RGBAUnorm:
  45. case WebGPUConstants.TextureFormat.BC1RGBAUnormSRGB:
  46. case WebGPUConstants.TextureFormat.ETC2RGB8Unorm:
  47. case WebGPUConstants.TextureFormat.ETC2RGB8UnormSRGB:
  48. case WebGPUConstants.TextureFormat.ETC2RGB8A1Unorm:
  49. case WebGPUConstants.TextureFormat.ETC2RGB8A1UnormSRGB:
  50. case WebGPUConstants.TextureFormat.ETC2RGBA8Unorm:
  51. case WebGPUConstants.TextureFormat.ETC2RGBA8UnormSRGB:
  52. case WebGPUConstants.TextureFormat.EACR11Unorm:
  53. case WebGPUConstants.TextureFormat.EACR11Snorm:
  54. case WebGPUConstants.TextureFormat.EACRG11Unorm:
  55. case WebGPUConstants.TextureFormat.EACRG11Snorm:
  56. case WebGPUConstants.TextureFormat.ASTC4x4Unorm:
  57. case WebGPUConstants.TextureFormat.ASTC4x4UnormSRGB:
  58. case WebGPUConstants.TextureFormat.ASTC5x4Unorm:
  59. case WebGPUConstants.TextureFormat.ASTC5x4UnormSRGB:
  60. case WebGPUConstants.TextureFormat.ASTC5x5Unorm:
  61. case WebGPUConstants.TextureFormat.ASTC5x5UnormSRGB:
  62. case WebGPUConstants.TextureFormat.ASTC6x5Unorm:
  63. case WebGPUConstants.TextureFormat.ASTC6x5UnormSRGB:
  64. case WebGPUConstants.TextureFormat.ASTC6x6Unorm:
  65. case WebGPUConstants.TextureFormat.ASTC6x6UnormSRGB:
  66. case WebGPUConstants.TextureFormat.ASTC8x5Unorm:
  67. case WebGPUConstants.TextureFormat.ASTC8x5UnormSRGB:
  68. case WebGPUConstants.TextureFormat.ASTC8x6Unorm:
  69. case WebGPUConstants.TextureFormat.ASTC8x6UnormSRGB:
  70. case WebGPUConstants.TextureFormat.ASTC8x8Unorm:
  71. case WebGPUConstants.TextureFormat.ASTC8x8UnormSRGB:
  72. case WebGPUConstants.TextureFormat.ASTC10x5Unorm:
  73. case WebGPUConstants.TextureFormat.ASTC10x5UnormSRGB:
  74. case WebGPUConstants.TextureFormat.ASTC10x6Unorm:
  75. case WebGPUConstants.TextureFormat.ASTC10x6UnormSRGB:
  76. case WebGPUConstants.TextureFormat.ASTC10x8Unorm:
  77. case WebGPUConstants.TextureFormat.ASTC10x8UnormSRGB:
  78. case WebGPUConstants.TextureFormat.ASTC10x10Unorm:
  79. case WebGPUConstants.TextureFormat.ASTC10x10UnormSRGB:
  80. case WebGPUConstants.TextureFormat.ASTC12x10Unorm:
  81. case WebGPUConstants.TextureFormat.ASTC12x10UnormSRGB:
  82. case WebGPUConstants.TextureFormat.ASTC12x12Unorm:
  83. case WebGPUConstants.TextureFormat.ASTC12x12UnormSRGB:
  84. case WebGPUConstants.TextureFormat.Stencil8:
  85. return 0;
  86. // One component = 16 bits
  87. case WebGPUConstants.TextureFormat.R16Uint:
  88. case WebGPUConstants.TextureFormat.R16Sint:
  89. case WebGPUConstants.TextureFormat.RG16Uint:
  90. case WebGPUConstants.TextureFormat.RG16Sint:
  91. case WebGPUConstants.TextureFormat.RGBA16Uint:
  92. case WebGPUConstants.TextureFormat.RGBA16Sint:
  93. case WebGPUConstants.TextureFormat.Depth16Unorm:
  94. return 5;
  95. case WebGPUConstants.TextureFormat.R16Float:
  96. case WebGPUConstants.TextureFormat.RG16Float:
  97. case WebGPUConstants.TextureFormat.RGBA16Float:
  98. return 2;
  99. // One component = 32 bits
  100. case WebGPUConstants.TextureFormat.R32Uint:
  101. case WebGPUConstants.TextureFormat.R32Sint:
  102. case WebGPUConstants.TextureFormat.RG32Uint:
  103. case WebGPUConstants.TextureFormat.RG32Sint:
  104. case WebGPUConstants.TextureFormat.RGBA32Uint:
  105. case WebGPUConstants.TextureFormat.RGBA32Sint:
  106. return 7;
  107. case WebGPUConstants.TextureFormat.R32Float:
  108. case WebGPUConstants.TextureFormat.RG32Float:
  109. case WebGPUConstants.TextureFormat.RGBA32Float:
  110. case WebGPUConstants.TextureFormat.Depth32Float:
  111. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  112. case WebGPUConstants.TextureFormat.Depth24Plus:
  113. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static GetBlockInformationFromFormat(format) {
  119. switch (format) {
  120. // 8 bits formats
  121. case WebGPUConstants.TextureFormat.R8Unorm:
  122. case WebGPUConstants.TextureFormat.R8Snorm:
  123. case WebGPUConstants.TextureFormat.R8Uint:
  124. case WebGPUConstants.TextureFormat.R8Sint:
  125. return { width: 1, height: 1, length: 1 };
  126. // 16 bits formats
  127. case WebGPUConstants.TextureFormat.R16Uint:
  128. case WebGPUConstants.TextureFormat.R16Sint:
  129. case WebGPUConstants.TextureFormat.R16Float:
  130. case WebGPUConstants.TextureFormat.RG8Unorm:
  131. case WebGPUConstants.TextureFormat.RG8Snorm:
  132. case WebGPUConstants.TextureFormat.RG8Uint:
  133. case WebGPUConstants.TextureFormat.RG8Sint:
  134. return { width: 1, height: 1, length: 2 };
  135. // 32 bits formats
  136. case WebGPUConstants.TextureFormat.R32Uint:
  137. case WebGPUConstants.TextureFormat.R32Sint:
  138. case WebGPUConstants.TextureFormat.R32Float:
  139. case WebGPUConstants.TextureFormat.RG16Uint:
  140. case WebGPUConstants.TextureFormat.RG16Sint:
  141. case WebGPUConstants.TextureFormat.RG16Float:
  142. case WebGPUConstants.TextureFormat.RGBA8Unorm:
  143. case WebGPUConstants.TextureFormat.RGBA8UnormSRGB:
  144. case WebGPUConstants.TextureFormat.RGBA8Snorm:
  145. case WebGPUConstants.TextureFormat.RGBA8Uint:
  146. case WebGPUConstants.TextureFormat.RGBA8Sint:
  147. case WebGPUConstants.TextureFormat.BGRA8Unorm:
  148. case WebGPUConstants.TextureFormat.BGRA8UnormSRGB:
  149. case WebGPUConstants.TextureFormat.RGB9E5UFloat:
  150. case WebGPUConstants.TextureFormat.RGB10A2UINT:
  151. case WebGPUConstants.TextureFormat.RGB10A2Unorm:
  152. case WebGPUConstants.TextureFormat.RG11B10UFloat:
  153. return { width: 1, height: 1, length: 4 };
  154. // 64 bits formats
  155. case WebGPUConstants.TextureFormat.RG32Uint:
  156. case WebGPUConstants.TextureFormat.RG32Sint:
  157. case WebGPUConstants.TextureFormat.RG32Float:
  158. case WebGPUConstants.TextureFormat.RGBA16Uint:
  159. case WebGPUConstants.TextureFormat.RGBA16Sint:
  160. case WebGPUConstants.TextureFormat.RGBA16Float:
  161. return { width: 1, height: 1, length: 8 };
  162. // 128 bits formats
  163. case WebGPUConstants.TextureFormat.RGBA32Uint:
  164. case WebGPUConstants.TextureFormat.RGBA32Sint:
  165. case WebGPUConstants.TextureFormat.RGBA32Float:
  166. return { width: 1, height: 1, length: 16 };
  167. // Depth and stencil formats
  168. case WebGPUConstants.TextureFormat.Stencil8:
  169. // eslint-disable-next-line no-throw-literal
  170. throw "No fixed size for Stencil8 format!";
  171. case WebGPUConstants.TextureFormat.Depth16Unorm:
  172. return { width: 1, height: 1, length: 2 };
  173. case WebGPUConstants.TextureFormat.Depth24Plus:
  174. // eslint-disable-next-line no-throw-literal
  175. throw "No fixed size for Depth24Plus format!";
  176. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  177. // eslint-disable-next-line no-throw-literal
  178. throw "No fixed size for Depth24PlusStencil8 format!";
  179. case WebGPUConstants.TextureFormat.Depth32Float:
  180. return { width: 1, height: 1, length: 4 };
  181. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  182. return { width: 1, height: 1, length: 5 };
  183. // BC compressed formats usable if "texture-compression-bc" is both
  184. // supported by the device/user agent and enabled in requestDevice.
  185. case WebGPUConstants.TextureFormat.BC7RGBAUnorm:
  186. case WebGPUConstants.TextureFormat.BC7RGBAUnormSRGB:
  187. case WebGPUConstants.TextureFormat.BC6HRGBUFloat:
  188. case WebGPUConstants.TextureFormat.BC6HRGBFloat:
  189. case WebGPUConstants.TextureFormat.BC5RGUnorm:
  190. case WebGPUConstants.TextureFormat.BC5RGSnorm:
  191. case WebGPUConstants.TextureFormat.BC3RGBAUnorm:
  192. case WebGPUConstants.TextureFormat.BC3RGBAUnormSRGB:
  193. case WebGPUConstants.TextureFormat.BC2RGBAUnorm:
  194. case WebGPUConstants.TextureFormat.BC2RGBAUnormSRGB:
  195. return { width: 4, height: 4, length: 16 };
  196. case WebGPUConstants.TextureFormat.BC4RUnorm:
  197. case WebGPUConstants.TextureFormat.BC4RSnorm:
  198. case WebGPUConstants.TextureFormat.BC1RGBAUnorm:
  199. case WebGPUConstants.TextureFormat.BC1RGBAUnormSRGB:
  200. return { width: 4, height: 4, length: 8 };
  201. // ETC2 compressed formats usable if "texture-compression-etc2" is both
  202. // supported by the device/user agent and enabled in requestDevice.
  203. case WebGPUConstants.TextureFormat.ETC2RGB8Unorm:
  204. case WebGPUConstants.TextureFormat.ETC2RGB8UnormSRGB:
  205. case WebGPUConstants.TextureFormat.ETC2RGB8A1Unorm:
  206. case WebGPUConstants.TextureFormat.ETC2RGB8A1UnormSRGB:
  207. case WebGPUConstants.TextureFormat.EACR11Unorm:
  208. case WebGPUConstants.TextureFormat.EACR11Snorm:
  209. return { width: 4, height: 4, length: 8 };
  210. case WebGPUConstants.TextureFormat.ETC2RGBA8Unorm:
  211. case WebGPUConstants.TextureFormat.ETC2RGBA8UnormSRGB:
  212. case WebGPUConstants.TextureFormat.EACRG11Unorm:
  213. case WebGPUConstants.TextureFormat.EACRG11Snorm:
  214. return { width: 4, height: 4, length: 16 };
  215. // ASTC compressed formats usable if "texture-compression-astc" is both
  216. // supported by the device/user agent and enabled in requestDevice.
  217. case WebGPUConstants.TextureFormat.ASTC4x4Unorm:
  218. case WebGPUConstants.TextureFormat.ASTC4x4UnormSRGB:
  219. return { width: 4, height: 4, length: 16 };
  220. case WebGPUConstants.TextureFormat.ASTC5x4Unorm:
  221. case WebGPUConstants.TextureFormat.ASTC5x4UnormSRGB:
  222. return { width: 5, height: 4, length: 16 };
  223. case WebGPUConstants.TextureFormat.ASTC5x5Unorm:
  224. case WebGPUConstants.TextureFormat.ASTC5x5UnormSRGB:
  225. return { width: 5, height: 5, length: 16 };
  226. case WebGPUConstants.TextureFormat.ASTC6x5Unorm:
  227. case WebGPUConstants.TextureFormat.ASTC6x5UnormSRGB:
  228. return { width: 6, height: 5, length: 16 };
  229. case WebGPUConstants.TextureFormat.ASTC6x6Unorm:
  230. case WebGPUConstants.TextureFormat.ASTC6x6UnormSRGB:
  231. return { width: 6, height: 6, length: 16 };
  232. case WebGPUConstants.TextureFormat.ASTC8x5Unorm:
  233. case WebGPUConstants.TextureFormat.ASTC8x5UnormSRGB:
  234. return { width: 8, height: 5, length: 16 };
  235. case WebGPUConstants.TextureFormat.ASTC8x6Unorm:
  236. case WebGPUConstants.TextureFormat.ASTC8x6UnormSRGB:
  237. return { width: 8, height: 6, length: 16 };
  238. case WebGPUConstants.TextureFormat.ASTC8x8Unorm:
  239. case WebGPUConstants.TextureFormat.ASTC8x8UnormSRGB:
  240. return { width: 8, height: 8, length: 16 };
  241. case WebGPUConstants.TextureFormat.ASTC10x5Unorm:
  242. case WebGPUConstants.TextureFormat.ASTC10x5UnormSRGB:
  243. return { width: 10, height: 5, length: 16 };
  244. case WebGPUConstants.TextureFormat.ASTC10x6Unorm:
  245. case WebGPUConstants.TextureFormat.ASTC10x6UnormSRGB:
  246. return { width: 10, height: 6, length: 16 };
  247. case WebGPUConstants.TextureFormat.ASTC10x8Unorm:
  248. case WebGPUConstants.TextureFormat.ASTC10x8UnormSRGB:
  249. return { width: 10, height: 8, length: 16 };
  250. case WebGPUConstants.TextureFormat.ASTC10x10Unorm:
  251. case WebGPUConstants.TextureFormat.ASTC10x10UnormSRGB:
  252. return { width: 10, height: 10, length: 16 };
  253. case WebGPUConstants.TextureFormat.ASTC12x10Unorm:
  254. case WebGPUConstants.TextureFormat.ASTC12x10UnormSRGB:
  255. return { width: 12, height: 10, length: 16 };
  256. case WebGPUConstants.TextureFormat.ASTC12x12Unorm:
  257. case WebGPUConstants.TextureFormat.ASTC12x12UnormSRGB:
  258. return { width: 12, height: 12, length: 16 };
  259. }
  260. return { width: 1, height: 1, length: 4 };
  261. }
  262. static IsHardwareTexture(texture) {
  263. return !!texture.release;
  264. }
  265. static IsInternalTexture(texture) {
  266. return !!texture.dispose;
  267. }
  268. static IsImageBitmap(imageBitmap) {
  269. return imageBitmap.close !== undefined;
  270. }
  271. static IsImageBitmapArray(imageBitmap) {
  272. return Array.isArray(imageBitmap) && imageBitmap[0].close !== undefined;
  273. }
  274. static IsCompressedFormat(format) {
  275. switch (format) {
  276. case WebGPUConstants.TextureFormat.BC7RGBAUnormSRGB:
  277. case WebGPUConstants.TextureFormat.BC7RGBAUnorm:
  278. case WebGPUConstants.TextureFormat.BC6HRGBFloat:
  279. case WebGPUConstants.TextureFormat.BC6HRGBUFloat:
  280. case WebGPUConstants.TextureFormat.BC5RGSnorm:
  281. case WebGPUConstants.TextureFormat.BC5RGUnorm:
  282. case WebGPUConstants.TextureFormat.BC4RSnorm:
  283. case WebGPUConstants.TextureFormat.BC4RUnorm:
  284. case WebGPUConstants.TextureFormat.BC3RGBAUnormSRGB:
  285. case WebGPUConstants.TextureFormat.BC3RGBAUnorm:
  286. case WebGPUConstants.TextureFormat.BC2RGBAUnormSRGB:
  287. case WebGPUConstants.TextureFormat.BC2RGBAUnorm:
  288. case WebGPUConstants.TextureFormat.BC1RGBAUnormSRGB:
  289. case WebGPUConstants.TextureFormat.BC1RGBAUnorm:
  290. case WebGPUConstants.TextureFormat.ETC2RGB8Unorm:
  291. case WebGPUConstants.TextureFormat.ETC2RGB8UnormSRGB:
  292. case WebGPUConstants.TextureFormat.ETC2RGB8A1Unorm:
  293. case WebGPUConstants.TextureFormat.ETC2RGB8A1UnormSRGB:
  294. case WebGPUConstants.TextureFormat.ETC2RGBA8Unorm:
  295. case WebGPUConstants.TextureFormat.ETC2RGBA8UnormSRGB:
  296. case WebGPUConstants.TextureFormat.EACR11Unorm:
  297. case WebGPUConstants.TextureFormat.EACR11Snorm:
  298. case WebGPUConstants.TextureFormat.EACRG11Unorm:
  299. case WebGPUConstants.TextureFormat.EACRG11Snorm:
  300. case WebGPUConstants.TextureFormat.ASTC4x4Unorm:
  301. case WebGPUConstants.TextureFormat.ASTC4x4UnormSRGB:
  302. case WebGPUConstants.TextureFormat.ASTC5x4Unorm:
  303. case WebGPUConstants.TextureFormat.ASTC5x4UnormSRGB:
  304. case WebGPUConstants.TextureFormat.ASTC5x5Unorm:
  305. case WebGPUConstants.TextureFormat.ASTC5x5UnormSRGB:
  306. case WebGPUConstants.TextureFormat.ASTC6x5Unorm:
  307. case WebGPUConstants.TextureFormat.ASTC6x5UnormSRGB:
  308. case WebGPUConstants.TextureFormat.ASTC6x6Unorm:
  309. case WebGPUConstants.TextureFormat.ASTC6x6UnormSRGB:
  310. case WebGPUConstants.TextureFormat.ASTC8x5Unorm:
  311. case WebGPUConstants.TextureFormat.ASTC8x5UnormSRGB:
  312. case WebGPUConstants.TextureFormat.ASTC8x6Unorm:
  313. case WebGPUConstants.TextureFormat.ASTC8x6UnormSRGB:
  314. case WebGPUConstants.TextureFormat.ASTC8x8Unorm:
  315. case WebGPUConstants.TextureFormat.ASTC8x8UnormSRGB:
  316. case WebGPUConstants.TextureFormat.ASTC10x5Unorm:
  317. case WebGPUConstants.TextureFormat.ASTC10x5UnormSRGB:
  318. case WebGPUConstants.TextureFormat.ASTC10x6Unorm:
  319. case WebGPUConstants.TextureFormat.ASTC10x6UnormSRGB:
  320. case WebGPUConstants.TextureFormat.ASTC10x8Unorm:
  321. case WebGPUConstants.TextureFormat.ASTC10x8UnormSRGB:
  322. case WebGPUConstants.TextureFormat.ASTC10x10Unorm:
  323. case WebGPUConstants.TextureFormat.ASTC10x10UnormSRGB:
  324. case WebGPUConstants.TextureFormat.ASTC12x10Unorm:
  325. case WebGPUConstants.TextureFormat.ASTC12x10UnormSRGB:
  326. case WebGPUConstants.TextureFormat.ASTC12x12Unorm:
  327. case WebGPUConstants.TextureFormat.ASTC12x12UnormSRGB:
  328. return true;
  329. }
  330. return false;
  331. }
  332. static GetWebGPUTextureFormat(type, format, useSRGBBuffer = false) {
  333. switch (format) {
  334. case 15:
  335. return WebGPUConstants.TextureFormat.Depth16Unorm;
  336. case 16:
  337. return WebGPUConstants.TextureFormat.Depth24Plus;
  338. case 13:
  339. return WebGPUConstants.TextureFormat.Depth24PlusStencil8;
  340. case 14:
  341. return WebGPUConstants.TextureFormat.Depth32Float;
  342. case 18:
  343. return WebGPUConstants.TextureFormat.Depth32FloatStencil8;
  344. case 19:
  345. return WebGPUConstants.TextureFormat.Stencil8;
  346. case 36492:
  347. return useSRGBBuffer ? WebGPUConstants.TextureFormat.BC7RGBAUnormSRGB : WebGPUConstants.TextureFormat.BC7RGBAUnorm;
  348. case 36495:
  349. return WebGPUConstants.TextureFormat.BC6HRGBUFloat;
  350. case 36494:
  351. return WebGPUConstants.TextureFormat.BC6HRGBFloat;
  352. case 33779:
  353. return useSRGBBuffer ? WebGPUConstants.TextureFormat.BC3RGBAUnormSRGB : WebGPUConstants.TextureFormat.BC3RGBAUnorm;
  354. case 33778:
  355. return useSRGBBuffer ? WebGPUConstants.TextureFormat.BC2RGBAUnormSRGB : WebGPUConstants.TextureFormat.BC2RGBAUnorm;
  356. case 33777:
  357. case 33776:
  358. return useSRGBBuffer ? WebGPUConstants.TextureFormat.BC1RGBAUnormSRGB : WebGPUConstants.TextureFormat.BC1RGBAUnorm;
  359. case 37808:
  360. return useSRGBBuffer ? WebGPUConstants.TextureFormat.ASTC4x4UnormSRGB : WebGPUConstants.TextureFormat.ASTC4x4Unorm;
  361. case 36196:
  362. case 37492:
  363. return useSRGBBuffer ? WebGPUConstants.TextureFormat.ETC2RGB8UnormSRGB : WebGPUConstants.TextureFormat.ETC2RGB8Unorm;
  364. case 37496:
  365. return useSRGBBuffer ? WebGPUConstants.TextureFormat.ETC2RGBA8UnormSRGB : WebGPUConstants.TextureFormat.ETC2RGBA8Unorm;
  366. }
  367. switch (type) {
  368. case 3:
  369. switch (format) {
  370. case 6:
  371. return WebGPUConstants.TextureFormat.R8Snorm;
  372. case 7:
  373. return WebGPUConstants.TextureFormat.RG8Snorm;
  374. case 4:
  375. // eslint-disable-next-line no-throw-literal
  376. throw "RGB format not supported in WebGPU";
  377. case 8:
  378. return WebGPUConstants.TextureFormat.R8Sint;
  379. case 9:
  380. return WebGPUConstants.TextureFormat.RG8Sint;
  381. case 10:
  382. // eslint-disable-next-line no-throw-literal
  383. throw "RGB_INTEGER format not supported in WebGPU";
  384. case 11:
  385. return WebGPUConstants.TextureFormat.RGBA8Sint;
  386. default:
  387. return WebGPUConstants.TextureFormat.RGBA8Snorm;
  388. }
  389. case 0:
  390. switch (format) {
  391. case 6:
  392. return WebGPUConstants.TextureFormat.R8Unorm;
  393. case 7:
  394. return WebGPUConstants.TextureFormat.RG8Unorm;
  395. case 4:
  396. // eslint-disable-next-line no-throw-literal
  397. throw "TEXTUREFORMAT_RGB format not supported in WebGPU";
  398. case 5:
  399. return useSRGBBuffer ? WebGPUConstants.TextureFormat.RGBA8UnormSRGB : WebGPUConstants.TextureFormat.RGBA8Unorm;
  400. case 12:
  401. return useSRGBBuffer ? WebGPUConstants.TextureFormat.BGRA8UnormSRGB : WebGPUConstants.TextureFormat.BGRA8Unorm;
  402. case 8:
  403. return WebGPUConstants.TextureFormat.R8Uint;
  404. case 9:
  405. return WebGPUConstants.TextureFormat.RG8Uint;
  406. case 10:
  407. // eslint-disable-next-line no-throw-literal
  408. throw "RGB_INTEGER format not supported in WebGPU";
  409. case 11:
  410. return WebGPUConstants.TextureFormat.RGBA8Uint;
  411. case 0:
  412. // eslint-disable-next-line no-throw-literal
  413. throw "TEXTUREFORMAT_ALPHA format not supported in WebGPU";
  414. case 1:
  415. // eslint-disable-next-line no-throw-literal
  416. throw "TEXTUREFORMAT_LUMINANCE format not supported in WebGPU";
  417. case 2:
  418. // eslint-disable-next-line no-throw-literal
  419. throw "TEXTUREFORMAT_LUMINANCE_ALPHA format not supported in WebGPU";
  420. default:
  421. return WebGPUConstants.TextureFormat.RGBA8Unorm;
  422. }
  423. case 4:
  424. switch (format) {
  425. case 8:
  426. return WebGPUConstants.TextureFormat.R16Sint;
  427. case 9:
  428. return WebGPUConstants.TextureFormat.RG16Sint;
  429. case 10:
  430. // eslint-disable-next-line no-throw-literal
  431. throw "TEXTUREFORMAT_RGB_INTEGER format not supported in WebGPU";
  432. case 11:
  433. return WebGPUConstants.TextureFormat.RGBA16Sint;
  434. default:
  435. return WebGPUConstants.TextureFormat.RGBA16Sint;
  436. }
  437. case 5:
  438. switch (format) {
  439. case 8:
  440. return WebGPUConstants.TextureFormat.R16Uint;
  441. case 9:
  442. return WebGPUConstants.TextureFormat.RG16Uint;
  443. case 10:
  444. // eslint-disable-next-line no-throw-literal
  445. throw "TEXTUREFORMAT_RGB_INTEGER format not supported in WebGPU";
  446. case 11:
  447. return WebGPUConstants.TextureFormat.RGBA16Uint;
  448. default:
  449. return WebGPUConstants.TextureFormat.RGBA16Uint;
  450. }
  451. case 6:
  452. switch (format) {
  453. case 8:
  454. return WebGPUConstants.TextureFormat.R32Sint;
  455. case 9:
  456. return WebGPUConstants.TextureFormat.RG32Sint;
  457. case 10:
  458. // eslint-disable-next-line no-throw-literal
  459. throw "TEXTUREFORMAT_RGB_INTEGER format not supported in WebGPU";
  460. case 11:
  461. return WebGPUConstants.TextureFormat.RGBA32Sint;
  462. default:
  463. return WebGPUConstants.TextureFormat.RGBA32Sint;
  464. }
  465. case 7: // Refers to UNSIGNED_INT
  466. switch (format) {
  467. case 8:
  468. return WebGPUConstants.TextureFormat.R32Uint;
  469. case 9:
  470. return WebGPUConstants.TextureFormat.RG32Uint;
  471. case 10:
  472. // eslint-disable-next-line no-throw-literal
  473. throw "TEXTUREFORMAT_RGB_INTEGER format not supported in WebGPU";
  474. case 11:
  475. return WebGPUConstants.TextureFormat.RGBA32Uint;
  476. default:
  477. return WebGPUConstants.TextureFormat.RGBA32Uint;
  478. }
  479. case 1:
  480. switch (format) {
  481. case 6:
  482. return WebGPUConstants.TextureFormat.R32Float; // By default. Other possibility is R16Float.
  483. case 7:
  484. return WebGPUConstants.TextureFormat.RG32Float; // By default. Other possibility is RG16Float.
  485. case 4:
  486. // eslint-disable-next-line no-throw-literal
  487. throw "TEXTUREFORMAT_RGB format not supported in WebGPU";
  488. case 5:
  489. return WebGPUConstants.TextureFormat.RGBA32Float; // By default. Other possibility is RGBA16Float.
  490. default:
  491. return WebGPUConstants.TextureFormat.RGBA32Float;
  492. }
  493. case 2:
  494. switch (format) {
  495. case 6:
  496. return WebGPUConstants.TextureFormat.R16Float;
  497. case 7:
  498. return WebGPUConstants.TextureFormat.RG16Float;
  499. case 4:
  500. // eslint-disable-next-line no-throw-literal
  501. throw "TEXTUREFORMAT_RGB format not supported in WebGPU";
  502. case 5:
  503. return WebGPUConstants.TextureFormat.RGBA16Float;
  504. default:
  505. return WebGPUConstants.TextureFormat.RGBA16Float;
  506. }
  507. case 10:
  508. // eslint-disable-next-line no-throw-literal
  509. throw "TEXTURETYPE_UNSIGNED_SHORT_5_6_5 format not supported in WebGPU";
  510. case 13:
  511. switch (format) {
  512. case 5:
  513. return WebGPUConstants.TextureFormat.RG11B10UFloat;
  514. case 11:
  515. // eslint-disable-next-line no-throw-literal
  516. throw "TEXTUREFORMAT_RGBA_INTEGER format not supported in WebGPU when type is TEXTURETYPE_UNSIGNED_INT_10F_11F_11F_REV";
  517. default:
  518. return WebGPUConstants.TextureFormat.RG11B10UFloat;
  519. }
  520. case 14:
  521. switch (format) {
  522. case 5:
  523. return WebGPUConstants.TextureFormat.RGB9E5UFloat;
  524. case 11:
  525. // eslint-disable-next-line no-throw-literal
  526. throw "TEXTUREFORMAT_RGBA_INTEGER format not supported in WebGPU when type is TEXTURETYPE_UNSIGNED_INT_5_9_9_9_REV";
  527. default:
  528. return WebGPUConstants.TextureFormat.RGB9E5UFloat;
  529. }
  530. case 8:
  531. // eslint-disable-next-line no-throw-literal
  532. throw "TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4 format not supported in WebGPU";
  533. case 9:
  534. // eslint-disable-next-line no-throw-literal
  535. throw "TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1 format not supported in WebGPU";
  536. case 11:
  537. switch (format) {
  538. case 5:
  539. return WebGPUConstants.TextureFormat.RGB10A2Unorm;
  540. case 11:
  541. return WebGPUConstants.TextureFormat.RGB10A2UINT;
  542. default:
  543. return WebGPUConstants.TextureFormat.RGB10A2Unorm;
  544. }
  545. }
  546. return useSRGBBuffer ? WebGPUConstants.TextureFormat.RGBA8UnormSRGB : WebGPUConstants.TextureFormat.RGBA8Unorm;
  547. }
  548. static GetNumChannelsFromWebGPUTextureFormat(format) {
  549. switch (format) {
  550. case WebGPUConstants.TextureFormat.R8Unorm:
  551. case WebGPUConstants.TextureFormat.R8Snorm:
  552. case WebGPUConstants.TextureFormat.R8Uint:
  553. case WebGPUConstants.TextureFormat.R8Sint:
  554. case WebGPUConstants.TextureFormat.BC4RUnorm:
  555. case WebGPUConstants.TextureFormat.BC4RSnorm:
  556. case WebGPUConstants.TextureFormat.R16Uint:
  557. case WebGPUConstants.TextureFormat.R16Sint:
  558. case WebGPUConstants.TextureFormat.Depth16Unorm:
  559. case WebGPUConstants.TextureFormat.R16Float:
  560. case WebGPUConstants.TextureFormat.R32Uint:
  561. case WebGPUConstants.TextureFormat.R32Sint:
  562. case WebGPUConstants.TextureFormat.R32Float:
  563. case WebGPUConstants.TextureFormat.Depth32Float:
  564. case WebGPUConstants.TextureFormat.Stencil8:
  565. case WebGPUConstants.TextureFormat.Depth24Plus:
  566. case WebGPUConstants.TextureFormat.EACR11Unorm:
  567. case WebGPUConstants.TextureFormat.EACR11Snorm:
  568. return 1;
  569. case WebGPUConstants.TextureFormat.RG8Unorm:
  570. case WebGPUConstants.TextureFormat.RG8Snorm:
  571. case WebGPUConstants.TextureFormat.RG8Uint:
  572. case WebGPUConstants.TextureFormat.RG8Sint:
  573. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  574. case WebGPUConstants.TextureFormat.BC5RGUnorm:
  575. case WebGPUConstants.TextureFormat.BC5RGSnorm:
  576. case WebGPUConstants.TextureFormat.RG16Uint:
  577. case WebGPUConstants.TextureFormat.RG16Sint:
  578. case WebGPUConstants.TextureFormat.RG16Float:
  579. case WebGPUConstants.TextureFormat.RG32Uint:
  580. case WebGPUConstants.TextureFormat.RG32Sint:
  581. case WebGPUConstants.TextureFormat.RG32Float:
  582. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  583. case WebGPUConstants.TextureFormat.EACRG11Unorm:
  584. case WebGPUConstants.TextureFormat.EACRG11Snorm:
  585. return 2;
  586. case WebGPUConstants.TextureFormat.RGB9E5UFloat:
  587. case WebGPUConstants.TextureFormat.RG11B10UFloat:
  588. case WebGPUConstants.TextureFormat.BC6HRGBUFloat:
  589. case WebGPUConstants.TextureFormat.BC6HRGBFloat:
  590. case WebGPUConstants.TextureFormat.ETC2RGB8Unorm:
  591. case WebGPUConstants.TextureFormat.ETC2RGB8UnormSRGB:
  592. return 3;
  593. case WebGPUConstants.TextureFormat.RGBA8Unorm:
  594. case WebGPUConstants.TextureFormat.RGBA8UnormSRGB:
  595. case WebGPUConstants.TextureFormat.RGBA8Snorm:
  596. case WebGPUConstants.TextureFormat.RGBA8Uint:
  597. case WebGPUConstants.TextureFormat.RGBA8Sint:
  598. case WebGPUConstants.TextureFormat.BGRA8Unorm:
  599. case WebGPUConstants.TextureFormat.BGRA8UnormSRGB:
  600. case WebGPUConstants.TextureFormat.RGB10A2UINT:
  601. case WebGPUConstants.TextureFormat.RGB10A2Unorm:
  602. case WebGPUConstants.TextureFormat.BC7RGBAUnorm:
  603. case WebGPUConstants.TextureFormat.BC7RGBAUnormSRGB:
  604. case WebGPUConstants.TextureFormat.BC3RGBAUnorm:
  605. case WebGPUConstants.TextureFormat.BC3RGBAUnormSRGB:
  606. case WebGPUConstants.TextureFormat.BC2RGBAUnorm:
  607. case WebGPUConstants.TextureFormat.BC2RGBAUnormSRGB:
  608. case WebGPUConstants.TextureFormat.BC1RGBAUnorm:
  609. case WebGPUConstants.TextureFormat.BC1RGBAUnormSRGB:
  610. case WebGPUConstants.TextureFormat.RGBA16Uint:
  611. case WebGPUConstants.TextureFormat.RGBA16Sint:
  612. case WebGPUConstants.TextureFormat.RGBA16Float:
  613. case WebGPUConstants.TextureFormat.RGBA32Uint:
  614. case WebGPUConstants.TextureFormat.RGBA32Sint:
  615. case WebGPUConstants.TextureFormat.RGBA32Float:
  616. case WebGPUConstants.TextureFormat.ETC2RGB8A1Unorm:
  617. case WebGPUConstants.TextureFormat.ETC2RGB8A1UnormSRGB:
  618. case WebGPUConstants.TextureFormat.ETC2RGBA8Unorm:
  619. case WebGPUConstants.TextureFormat.ETC2RGBA8UnormSRGB:
  620. case WebGPUConstants.TextureFormat.ASTC4x4Unorm:
  621. case WebGPUConstants.TextureFormat.ASTC4x4UnormSRGB:
  622. case WebGPUConstants.TextureFormat.ASTC5x4Unorm:
  623. case WebGPUConstants.TextureFormat.ASTC5x4UnormSRGB:
  624. case WebGPUConstants.TextureFormat.ASTC5x5Unorm:
  625. case WebGPUConstants.TextureFormat.ASTC5x5UnormSRGB:
  626. case WebGPUConstants.TextureFormat.ASTC6x5Unorm:
  627. case WebGPUConstants.TextureFormat.ASTC6x5UnormSRGB:
  628. case WebGPUConstants.TextureFormat.ASTC6x6Unorm:
  629. case WebGPUConstants.TextureFormat.ASTC6x6UnormSRGB:
  630. case WebGPUConstants.TextureFormat.ASTC8x5Unorm:
  631. case WebGPUConstants.TextureFormat.ASTC8x5UnormSRGB:
  632. case WebGPUConstants.TextureFormat.ASTC8x6Unorm:
  633. case WebGPUConstants.TextureFormat.ASTC8x6UnormSRGB:
  634. case WebGPUConstants.TextureFormat.ASTC8x8Unorm:
  635. case WebGPUConstants.TextureFormat.ASTC8x8UnormSRGB:
  636. case WebGPUConstants.TextureFormat.ASTC10x5Unorm:
  637. case WebGPUConstants.TextureFormat.ASTC10x5UnormSRGB:
  638. case WebGPUConstants.TextureFormat.ASTC10x6Unorm:
  639. case WebGPUConstants.TextureFormat.ASTC10x6UnormSRGB:
  640. case WebGPUConstants.TextureFormat.ASTC10x8Unorm:
  641. case WebGPUConstants.TextureFormat.ASTC10x8UnormSRGB:
  642. case WebGPUConstants.TextureFormat.ASTC10x10Unorm:
  643. case WebGPUConstants.TextureFormat.ASTC10x10UnormSRGB:
  644. case WebGPUConstants.TextureFormat.ASTC12x10Unorm:
  645. case WebGPUConstants.TextureFormat.ASTC12x10UnormSRGB:
  646. case WebGPUConstants.TextureFormat.ASTC12x12Unorm:
  647. case WebGPUConstants.TextureFormat.ASTC12x12UnormSRGB:
  648. return 4;
  649. }
  650. // eslint-disable-next-line no-throw-literal
  651. throw `Unknown format ${format}!`;
  652. }
  653. static HasStencilAspect(format) {
  654. switch (format) {
  655. case WebGPUConstants.TextureFormat.Stencil8:
  656. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  657. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  658. return true;
  659. }
  660. return false;
  661. }
  662. static HasDepthAndStencilAspects(format) {
  663. switch (format) {
  664. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  665. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  666. return true;
  667. }
  668. return false;
  669. }
  670. static GetDepthFormatOnly(format) {
  671. switch (format) {
  672. case WebGPUConstants.TextureFormat.Depth16Unorm:
  673. return WebGPUConstants.TextureFormat.Depth16Unorm;
  674. case WebGPUConstants.TextureFormat.Depth24Plus:
  675. return WebGPUConstants.TextureFormat.Depth24Plus;
  676. case WebGPUConstants.TextureFormat.Depth24PlusStencil8:
  677. return WebGPUConstants.TextureFormat.Depth24Plus;
  678. case WebGPUConstants.TextureFormat.Depth32Float:
  679. return WebGPUConstants.TextureFormat.Depth32Float;
  680. case WebGPUConstants.TextureFormat.Depth32FloatStencil8:
  681. return WebGPUConstants.TextureFormat.Depth32Float;
  682. }
  683. return format;
  684. }
  685. static GetSample(sampleCount) {
  686. // WebGPU only supports 1 or 4
  687. return sampleCount > 1 ? 4 : 1;
  688. }
  689. }
  690. //# sourceMappingURL=webgpuTextureHelper.js.map