import_envmap.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. let import_envmap_pipeline: pipeline_t = null;
  2. let import_envmap_params_loc: kinc_const_loc_t;
  3. let import_envmap_params: vec4_t = vec4_create();
  4. let import_envmap_n: vec4_t = vec4_create();
  5. let import_envmap_radiance_loc: kinc_tex_unit_t;
  6. let import_envmap_radiance: image_t = null;
  7. let import_envmap_radiance_cpu: image_t = null;
  8. let import_envmap_mips: image_t[] = null;
  9. let import_envmap_mips_cpu: image_t[] = null;
  10. function import_envmap_run(path: string, image: image_t) {
  11. // Init
  12. if (import_envmap_pipeline == null) {
  13. import_envmap_pipeline = g4_pipeline_create();
  14. import_envmap_pipeline.vertex_shader = sys_get_shader("pass.vert");
  15. import_envmap_pipeline.fragment_shader = sys_get_shader("prefilter_envmap.frag");
  16. let vs: vertex_struct_t = g4_vertex_struct_create();
  17. g4_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  18. import_envmap_pipeline.input_layout = [vs];
  19. import_envmap_pipeline.color_attachment_count = 1;
  20. import_envmap_pipeline.color_attachments[0] = tex_format_t.RGBA128;
  21. g4_pipeline_compile(import_envmap_pipeline);
  22. import_envmap_params_loc = g4_pipeline_get_const_loc(import_envmap_pipeline, "params");
  23. import_envmap_radiance_loc = g4_pipeline_get_tex_unit(import_envmap_pipeline, "radiance");
  24. import_envmap_radiance = image_create_render_target(1024, 512, tex_format_t.RGBA128);
  25. import_envmap_mips = [];
  26. let w: i32 = 512;
  27. for (let i: i32 = 0; i < 10; ++i) {
  28. array_push(import_envmap_mips, image_create_render_target(w, w > 1 ? math_floor(w / 2) : 1, tex_format_t.RGBA128));
  29. w = math_floor(w / 2);
  30. }
  31. if (const_data_screen_aligned_vb == null) {
  32. const_data_create_screen_aligned_data();
  33. }
  34. }
  35. // Down-scale to 1024x512
  36. g2_begin(import_envmap_radiance);
  37. g2_set_pipeline(base_pipe_copy128);
  38. g2_draw_scaled_image(image, 0, 0, 1024, 512);
  39. g2_set_pipeline(null);
  40. g2_end();
  41. let radiance_pixels: buffer_t = image_get_pixels(import_envmap_radiance);
  42. if (import_envmap_radiance_cpu != null) {
  43. let _radiance_cpu: image_t = import_envmap_radiance_cpu;
  44. app_notify_on_next_frame(function (_radiance_cpu: image_t) {
  45. image_unload(_radiance_cpu);
  46. }, _radiance_cpu);
  47. }
  48. import_envmap_radiance_cpu = image_from_bytes(radiance_pixels, import_envmap_radiance.width, import_envmap_radiance.height, tex_format_t.RGBA128);
  49. // Radiance
  50. if (import_envmap_mips_cpu != null) {
  51. for (let i: i32 = 0; i < import_envmap_mips_cpu.length; ++i) {
  52. let mip: image_t = import_envmap_mips_cpu[i];
  53. app_notify_on_next_frame(function (mip: image_t) {
  54. ///if (!krom_direct3d12) // TODO: crashes after 50+ imports
  55. image_unload(mip);
  56. ///end
  57. }, mip);
  58. }
  59. }
  60. import_envmap_mips_cpu = [];
  61. for (let i: i32 = 0; i < import_envmap_mips.length; ++i) {
  62. import_envmap_get_radiance_mip(import_envmap_mips[i], i, import_envmap_radiance);
  63. array_push(import_envmap_mips_cpu, image_from_bytes(image_get_pixels(import_envmap_mips[i]), import_envmap_mips[i].width, import_envmap_mips[i].height, tex_format_t.RGBA128));
  64. }
  65. image_set_mipmaps(import_envmap_radiance_cpu, import_envmap_mips_cpu);
  66. // Irradiance
  67. scene_world._.irradiance = import_envmap_get_spherical_harmonics(radiance_pixels, import_envmap_radiance.width, import_envmap_radiance.height);
  68. // World
  69. scene_world.strength = 1.0;
  70. scene_world.radiance_mipmaps = import_envmap_mips_cpu.length - 2;
  71. scene_world._.envmap = image;
  72. scene_world.envmap = path;
  73. scene_world._.radiance = import_envmap_radiance_cpu;
  74. scene_world._.radiance_mipmaps = import_envmap_mips_cpu;
  75. context_raw.saved_envmap = image;
  76. if (context_raw.show_envmap_blur) {
  77. scene_world._.envmap = scene_world._.radiance_mipmaps[0];
  78. }
  79. context_raw.ddirty = 2;
  80. project_raw.envmap = path;
  81. }
  82. function import_envmap_get_radiance_mip(mip: image_t, level: i32, radiance: image_t) {
  83. g4_begin(mip);
  84. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  85. g4_set_index_buffer(const_data_screen_aligned_ib);
  86. g4_set_pipeline(import_envmap_pipeline);
  87. import_envmap_params.x = 0.1 + level / 8;
  88. g4_set_float4(import_envmap_params_loc, import_envmap_params.x, import_envmap_params.y, import_envmap_params.z, import_envmap_params.w);
  89. g4_set_tex(import_envmap_radiance_loc, radiance);
  90. g4_draw();
  91. g4_end();
  92. }
  93. function import_envmap_reverse_equirect(x: f32, y: f32): vec4_t {
  94. let theta: f32 = x * math_pi() * 2 - math_pi();
  95. let phi: f32 = y * math_pi();
  96. // return n.set(math_sin(phi) * math_cos(theta), -(math_sin(phi) * math_sin(theta)), math_cos(phi));
  97. return vec4_set(import_envmap_n, -math_cos(phi), math_sin(phi) * math_cos(theta), -(math_sin(phi) * math_sin(theta)));
  98. }
  99. // https://ndotl.wordpress.com/2015/03/07/pbr-cubemap-filtering
  100. // https://seblagarde.wordpress.com/2012/06/10/amd-cubemapgen-for-physically-based-rendering
  101. function import_envmap_get_spherical_harmonics(source: buffer_t, source_width: i32, source_height: i32): f32_array_t {
  102. let sh: f32_array_t = f32_array_create(9 * 3 + 1); // Align to mult of 4 - 27->28
  103. let accum: f32 = 0.0;
  104. let weight: f32 = 1.0;
  105. let weight1: f32 = weight * 4 / 17;
  106. let weight2: f32 = weight * 8 / 17;
  107. let weight3: f32 = weight * 15 / 17;
  108. let weight4: f32 = weight * 5 / 68;
  109. let weight5: f32 = weight * 15 / 68;
  110. let view: buffer_view_t = buffer_view_create(source);
  111. for (let x: i32 = 0; x < source_width; ++x) {
  112. for (let y: i32 = 0; y < source_height; ++y) {
  113. import_envmap_n = import_envmap_reverse_equirect(x / source_width, y / source_height);
  114. for (let i: i32 = 0; i < 3; ++i) {
  115. let value: f32 = buffer_view_get_f32(view, ((x + y * source_width) * 16 + i * 4));
  116. value = math_pow(value, 1.0 / 2.2);
  117. sh[0 + i] += value * weight1;
  118. sh[3 + i] += value * weight2 * import_envmap_n.x;
  119. sh[6 + i] += value * weight2 * import_envmap_n.y;
  120. sh[9 + i] += value * weight2 * import_envmap_n.z;
  121. sh[12 + i] += value * weight3 * import_envmap_n.x * import_envmap_n.z;
  122. sh[15 + i] += value * weight3 * import_envmap_n.z * import_envmap_n.y;
  123. sh[18 + i] += value * weight3 * import_envmap_n.y * import_envmap_n.x;
  124. sh[21 + i] += value * weight4 * (3.0 * import_envmap_n.z * import_envmap_n.z - 1.0);
  125. sh[24 + i] += value * weight5 * (import_envmap_n.x * import_envmap_n.x - import_envmap_n.y * import_envmap_n.y);
  126. accum += weight;
  127. }
  128. }
  129. }
  130. for (let i: i32 = 0; i < sh.length; ++i) {
  131. sh[i] /= accum / 16;
  132. }
  133. return sh;
  134. }