import_envmap.ts 6.0 KB

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