2
0

RenderPathRaytrace.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package arm.render;
  2. import iron.RenderPath;
  3. import iron.Scene;
  4. #if kha_direct3d12
  5. class RenderPathRaytrace {
  6. public static var frame = 1.0;
  7. public static var ready = false;
  8. static var f32 = new kha.arrays.Float32Array(20);
  9. static var helpMat = iron.math.Mat4.identity();
  10. public static function init() {
  11. iron.data.Data.getBlob("raytrace.cso", function(shader:kha.Blob) {
  12. if (Context.mergedObject == null) arm.util.MeshUtil.mergeMesh();
  13. var mo = Context.mergedObject;
  14. var sc = mo.transform.scale;
  15. var md = mo.data;
  16. var geom = md.geom;
  17. var count = Std.int(geom.positions.length / 4);
  18. var vb = new kha.arrays.Float32Array((count + 4) * 8);
  19. for (i in 0...count) {
  20. vb[i * 8 ] = (geom.positions[i * 4 ] / 32767);
  21. vb[i * 8 + 1] = (geom.positions[i * 4 + 1] / 32767);
  22. vb[i * 8 + 2] = (geom.positions[i * 4 + 2] / 32767);
  23. vb[i * 8 + 3] = geom.normals [i * 2 ] / 32767;
  24. vb[i * 8 + 4] = geom.normals [i * 2 + 1] / 32767;
  25. vb[i * 8 + 5] = geom.positions[i * 4 + 3] / 32767;
  26. vb[i * 8 + 6] = (geom.uvs[i * 2 ] / 32767);
  27. vb[i * 8 + 7] = (geom.uvs[i * 2 + 1] / 32767);
  28. }
  29. vb[count * 8 ] = -1; // Light
  30. vb[count * 8 + 1] = 0.25;
  31. vb[count * 8 + 2] = 1;
  32. vb[count * 8 + 3] = 0;
  33. vb[count * 8 + 4] = 0;
  34. vb[count * 8 + 5] = -1;
  35. vb[count * 8 + 6] = 0;
  36. vb[count * 8 + 7] = 0;
  37. vb[count * 8 + 8] = 1;
  38. vb[count * 8 + 9] = -0.25;
  39. vb[count * 8 + 10] = 1;
  40. vb[count * 8 + 11] = 0;
  41. vb[count * 8 + 12] = 0;
  42. vb[count * 8 + 13] = -1;
  43. vb[count * 8 + 14] = 0;
  44. vb[count * 8 + 15] = 0;
  45. vb[count * 8 + 16] = -1;
  46. vb[count * 8 + 17] = -0.25;
  47. vb[count * 8 + 18] = 1;
  48. vb[count * 8 + 19] = 0;
  49. vb[count * 8 + 20] = 0;
  50. vb[count * 8 + 21] = -1;
  51. vb[count * 8 + 22] = 0;
  52. vb[count * 8 + 23] = 0;
  53. vb[count * 8 + 24] = 1;
  54. vb[count * 8 + 25] = 0.25;
  55. vb[count * 8 + 26] = 1;
  56. vb[count * 8 + 27] = 0;
  57. vb[count * 8 + 28] = 0;
  58. vb[count * 8 + 29] = -1;
  59. vb[count * 8 + 30] = 0;
  60. vb[count * 8 + 31] = 0;
  61. var indices = geom.indices[0];
  62. var ib = new kha.arrays.Uint32Array(indices.length + 6);
  63. for (i in 0...indices.length) ib[i] = indices[i];
  64. ib[indices.length ] = count ; // Light
  65. ib[indices.length + 1] = count + 1;
  66. ib[indices.length + 2] = count + 2;
  67. ib[indices.length + 3] = count ;
  68. ib[indices.length + 4] = count + 3;
  69. ib[indices.length + 5] = count + 1;
  70. var layer = Context.layer;
  71. var savedEnvmap = Scene.active.world.probe.radiance;
  72. untyped Krom.raytraceInit(
  73. shader.bytes.getData(), vb.buffer, ib.buffer, iron.App.w(), iron.App.h(),
  74. layer.texpaint.renderTarget_, layer.texpaint_nor.renderTarget_, layer.texpaint_pack.renderTarget_,
  75. savedEnvmap.texture_);
  76. });
  77. }
  78. public static function commands() {
  79. if (!ready) { ready = true; init(); return; }
  80. var cam = Scene.active.camera;
  81. var ct = cam.transform;
  82. helpMat.setFrom(cam.V);
  83. helpMat.multmat(cam.P);
  84. helpMat.getInverse(helpMat);
  85. f32[0] = ct.worldx();
  86. f32[1] = ct.worldy();
  87. f32[2] = ct.worldz();
  88. f32[3] = frame;
  89. frame += 1.0;
  90. f32[4] = helpMat._00;
  91. f32[5] = helpMat._01;
  92. f32[6] = helpMat._02;
  93. f32[7] = helpMat._03;
  94. f32[8] = helpMat._10;
  95. f32[9] = helpMat._11;
  96. f32[10] = helpMat._12;
  97. f32[11] = helpMat._13;
  98. f32[12] = helpMat._20;
  99. f32[13] = helpMat._21;
  100. f32[14] = helpMat._22;
  101. f32[15] = helpMat._23;
  102. f32[16] = helpMat._30;
  103. f32[17] = helpMat._31;
  104. f32[18] = helpMat._32;
  105. f32[19] = helpMat._33;
  106. var path = RenderPathDeferred.path;
  107. var framebuffer = path.renderTargets.get("taa").image;
  108. untyped Krom.raytraceDispatchRays(framebuffer.renderTarget_, f32.buffer);
  109. Context.ddirty = 1;
  110. // Context.ddirty--;
  111. Context.pdirty--;
  112. Context.rdirty--;
  113. }
  114. }
  115. #end