texture_layout_bench.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //! Run with `cargo run --release -p victory-formats --example texture_layout_bench`.
  2. use std::hint::black_box;
  3. use std::time::Instant;
  4. use victory_formats::texture::{Dimension, Swizzle, Texture, TextureDesc};
  5. fn main() {
  6. const CALLS: u64 = 50_000;
  7. let base = TextureDesc {
  8. dimension: Dimension::Tex2d,
  9. width: 512,
  10. height: 512,
  11. depth_or_array: 1,
  12. mips: 0,
  13. format: 98, // BC7
  14. samples: 1,
  15. swizzle: Swizzle::SW_64KB_D,
  16. };
  17. let cases = [
  18. ("BC7 64KB_D", base),
  19. (
  20. "BC1 64KB_S",
  21. TextureDesc {
  22. format: 71,
  23. swizzle: Swizzle::SW_64KB_S,
  24. ..base
  25. },
  26. ),
  27. (
  28. "RGBA8 4KB_D",
  29. TextureDesc {
  30. format: 28,
  31. swizzle: Swizzle::SW_4KB_D,
  32. ..base
  33. },
  34. ),
  35. (
  36. "R8 64KB_S",
  37. TextureDesc {
  38. format: 61,
  39. swizzle: Swizzle::SW_64KB_S,
  40. ..base
  41. },
  42. ),
  43. (
  44. "RGBA8 volume 64KB_S",
  45. TextureDesc {
  46. dimension: Dimension::Tex3d,
  47. width: 128,
  48. height: 128,
  49. depth_or_array: 64,
  50. format: 28,
  51. swizzle: Swizzle::SW_64KB_S,
  52. ..base
  53. },
  54. ),
  55. (
  56. "RGBA8 linear",
  57. TextureDesc {
  58. format: 28,
  59. swizzle: Swizzle::LINEAR,
  60. ..base
  61. },
  62. ),
  63. ];
  64. for (name, desc) in cases {
  65. let mut samples = [0.0; 7];
  66. black_box(Texture::new(black_box(&desc)).unwrap());
  67. let mut checksum = 0u64;
  68. for sample in &mut samples {
  69. let start = Instant::now();
  70. for i in 0..CALLS {
  71. let desc = TextureDesc {
  72. width: desc.width + (i % 32) as u32,
  73. ..desc
  74. };
  75. let texture = black_box(Texture::new(black_box(&desc)).unwrap());
  76. let layout = &texture.planes[0].layout;
  77. checksum =
  78. checksum.wrapping_add(texture.size + layout.offset(0, 7, 11, 0).unwrap());
  79. }
  80. *sample = start.elapsed().as_secs_f64() * 1e9 / CALLS as f64;
  81. }
  82. samples.sort_by(f64::total_cmp);
  83. println!("{name:22} {:8.1} ns/layout {checksum}", samples[3]);
  84. }
  85. }