import_envmap.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. let import_envmap_pipeline: gpu_pipeline_t = null;
  2. let import_envmap_params_loc: i32;
  3. let import_envmap_params: vec4_t = vec4_create();
  4. let import_envmap_n: vec4_t = vec4_create();
  5. let import_envmap_radiance_loc: i32;
  6. let import_envmap_radiance: gpu_texture_t = null;
  7. let import_envmap_mips: gpu_texture_t[] = null;
  8. function import_envmap_run(path: string, image: gpu_texture_t) {
  9. // Init
  10. if (import_envmap_pipeline == null) {
  11. import_envmap_pipeline = gpu_create_pipeline();
  12. import_envmap_pipeline.vertex_shader = sys_get_shader("prefilter_envmap.vert");
  13. import_envmap_pipeline.fragment_shader = sys_get_shader("prefilter_envmap.frag");
  14. let vs: gpu_vertex_structure_t = {};
  15. gpu_vertex_struct_add(vs, "pos", vertex_data_t.F32_2X);
  16. import_envmap_pipeline.input_layout = vs;
  17. import_envmap_pipeline.color_attachment_count = 1;
  18. ARRAY_ACCESS(import_envmap_pipeline.color_attachment, 0) = tex_format_t.RGBA128;
  19. gpu_pipeline_compile(import_envmap_pipeline);
  20. import_envmap_params_loc = 0;
  21. import_envmap_radiance_loc = 0;
  22. import_envmap_radiance = gpu_create_render_target(1024, 512, tex_format_t.RGBA128);
  23. import_envmap_mips = [];
  24. let w: i32 = 512;
  25. for (let i: i32 = 0; i < 5; ++i) {
  26. array_push(import_envmap_mips, gpu_create_render_target(w, w > 1 ? math_floor(w / 2) : 1, tex_format_t.RGBA128));
  27. w = math_floor(w / 2);
  28. }
  29. }
  30. // Down-scale to 1024x512
  31. draw_begin(import_envmap_radiance);
  32. draw_set_pipeline(pipes_copy128);
  33. draw_scaled_image(image, 0, 0, 1024, 512);
  34. draw_set_pipeline(null);
  35. draw_end();
  36. // Radiance
  37. for (let i: i32 = 0; i < import_envmap_mips.length; ++i) {
  38. import_envmap_get_radiance_mip(import_envmap_mips[i], i, import_envmap_radiance);
  39. }
  40. // Irradiance
  41. let radiance_pixels: buffer_t = gpu_get_texture_pixels(import_envmap_radiance);
  42. scene_world._.irradiance = import_envmap_get_spherical_harmonics(radiance_pixels, import_envmap_radiance.width, import_envmap_radiance.height);
  43. // World
  44. scene_world.strength = 1.0;
  45. scene_world.radiance_mipmaps = import_envmap_mips.length - 2;
  46. scene_world._.envmap = image;
  47. scene_world.envmap = path;
  48. scene_world._.radiance = import_envmap_radiance;
  49. scene_world._.radiance_mipmaps = import_envmap_mips;
  50. context_raw.saved_envmap = image;
  51. if (context_raw.show_envmap_blur) {
  52. scene_world._.envmap = scene_world._.radiance_mipmaps[0];
  53. }
  54. context_raw.ddirty = 2;
  55. project_raw.envmap = path;
  56. }
  57. function import_envmap_get_radiance_mip(mip: gpu_texture_t, level: i32, radiance: gpu_texture_t) {
  58. _gpu_begin(mip);
  59. gpu_set_vertex_buffer(const_data_screen_aligned_vb);
  60. gpu_set_index_buffer(const_data_screen_aligned_ib);
  61. gpu_set_pipeline(import_envmap_pipeline);
  62. import_envmap_params.x = 0.1 + level / 8;
  63. ///if (arm_macos || arm_ios)
  64. import_envmap_params.y = 1024 * 2; // Prevent gpu hang
  65. ///else
  66. import_envmap_params.y = 1024 * 16;
  67. ///end
  68. gpu_set_float4(import_envmap_params_loc, import_envmap_params.x, import_envmap_params.y, import_envmap_params.z, import_envmap_params.w);
  69. gpu_set_texture(import_envmap_radiance_loc, radiance);
  70. gpu_draw();
  71. gpu_end();
  72. }
  73. function import_envmap_reverse_equirect(x: f32, y: f32): vec4_t {
  74. let theta: f32 = x * math_pi() * 2 - math_pi();
  75. let phi: f32 = y * math_pi();
  76. // return n.set(math_sin(phi) * math_cos(theta), -(math_sin(phi) * math_sin(theta)), math_cos(phi));
  77. import_envmap_n = vec4_create(-math_cos(phi), math_sin(phi) * math_cos(theta), -(math_sin(phi) * math_sin(theta)));
  78. return import_envmap_n;
  79. }
  80. // https://ndotl.wordpress.com/2015/03/07/pbr-cubemap-filtering
  81. // https://seblagarde.wordpress.com/2012/06/10/amd-cubemapgen-for-physically-based-rendering
  82. function import_envmap_get_spherical_harmonics(source: buffer_t, source_width: i32, source_height: i32): f32_array_t {
  83. let sh: f32_array_t = f32_array_create(9 * 3 + 1); // Align to mult of 4 - 27->28
  84. let accum: f32 = 0.0;
  85. let weight: f32 = 1.0;
  86. let weight1: f32 = weight * 4 / 17;
  87. let weight2: f32 = weight * 8 / 17;
  88. let weight3: f32 = weight * 15 / 17;
  89. let weight4: f32 = weight * 5 / 68;
  90. let weight5: f32 = weight * 15 / 68;
  91. for (let x: i32 = 0; x < source_width; ++x) {
  92. for (let y: i32 = 0; y < source_height; ++y) {
  93. import_envmap_n = import_envmap_reverse_equirect(x / source_width, y / source_height);
  94. for (let i: i32 = 0; i < 3; ++i) {
  95. let value: f32 = buffer_get_f32(source, ((x + y * source_width) * 16 + i * 4));
  96. value = math_pow(value, 1.0 / 2.2);
  97. sh[0 + i] += value * weight1;
  98. sh[3 + i] += value * weight2 * import_envmap_n.x;
  99. sh[6 + i] += value * weight2 * import_envmap_n.y;
  100. sh[9 + i] += value * weight2 * import_envmap_n.z;
  101. sh[12 + i] += value * weight3 * import_envmap_n.x * import_envmap_n.z;
  102. sh[15 + i] += value * weight3 * import_envmap_n.z * import_envmap_n.y;
  103. sh[18 + i] += value * weight3 * import_envmap_n.y * import_envmap_n.x;
  104. sh[21 + i] += value * weight4 * (3.0 * import_envmap_n.z * import_envmap_n.z - 1.0);
  105. sh[24 + i] += value * weight5 * (import_envmap_n.x * import_envmap_n.x - import_envmap_n.y * import_envmap_n.y);
  106. accum += weight;
  107. }
  108. }
  109. }
  110. for (let i: i32 = 0; i < sh.length; ++i) {
  111. sh[i] /= accum / 16;
  112. }
  113. return sh;
  114. }