Unravel Engine C++ Reference
Loading...
Searching...
No Matches
hiz_pass.cpp
Go to the documentation of this file.
1#include "hiz_pass.h"
4#include <graphics/texture.h>
6
7namespace unravel
8{
9
10auto hiz_pass::init(rtti::context& ctx) -> bool
11{
12 // Get the asset manager for loading compute shaders
13 auto& am = ctx.get_cached<asset_manager>();
14
15 // Load compute shaders
16 auto cs_hiz_generate = am.get_asset<gfx::shader>("engine:/data/shaders/ssr/cs_hiz_generate.sc");
17 auto cs_hiz_downsample = am.get_asset<gfx::shader>("engine:/data/shaders/ssr/cs_hiz_downsample.sc");
18
19 if (!cs_hiz_generate || !cs_hiz_downsample)
20 {
21 return false;
22 }
23
24 // Create compute programs using gpu_program for compute shaders
25 hiz_generate_.program = std::make_shared<gpu_program>(cs_hiz_generate);
26 hiz_downsample_.program = std::make_shared<gpu_program>(cs_hiz_downsample);
27
28 // Cache uniforms using the uniforms_cache pattern
29 hiz_generate_.cache_uniforms();
30 hiz_downsample_.cache_uniforms();
31
32 return hiz_generate_.program->is_valid() && hiz_downsample_.program->is_valid();
33}
34
35void hiz_pass::run(gfx::render_view& rview, const run_params& params)
36{
37 // Validate input parameters
38 const auto& depth_buffer = params.depth_buffer;
39 const auto& output_hiz = params.output_hiz;
40 if (!depth_buffer || !output_hiz || !params.cam)
41 {
42 return;
43 }
44
45 const uint32_t hiz_width = output_hiz->info.width;
46 const uint32_t hiz_height = output_hiz->info.height;
47 const uint32_t num_mips = output_hiz->info.numMips;
48
49 // 1) Generate Hi-Z mip 0 from depth buffer
50 {
51 gfx::render_pass pass("hiz_generate_compute_pass");
52
53 // Begin the compute program
54 hiz_generate_.program->begin();
55
56 // Set input depth texture using gfx utilities
57 gfx::set_texture(hiz_generate_.s_depth, 0, depth_buffer);
58
59 // Set output Hi-Z mip 0 as image
60 gfx::set_image(1, output_hiz->native_handle(), 0, bgfx::Access::Write);
61
62 // Set parameters for the compute shader
63 math::vec4 hiz_params(float(hiz_width), float(hiz_height), 0.0f, 0.0f);
64 gfx::set_uniform(hiz_generate_.u_hiz_params, hiz_params);
65
66 // Dispatch compute shader with 8x8 thread groups
67 uint32_t num_groups_x = (hiz_width + 7) / 8;
68 uint32_t num_groups_y = (hiz_height + 7) / 8;
69 bgfx::dispatch(pass.id, hiz_generate_.program->native_handle(), num_groups_x, num_groups_y, 1);
70
71 hiz_generate_.program->end();
72 }
73
74 // 2) Generate remaining mip levels using compute shaders - optimized batching
75 if (num_mips > 1)
76 {
77 gfx::render_pass pass("hiz_downsample_compute_pass");
78
79 // Begin the downsampling compute program once for all mips
80 hiz_downsample_.program->begin();
81
82 for (uint32_t mip = 1; mip < num_mips; ++mip)
83 {
84 const uint32_t current_mip_width = hiz_width >> mip;
85 const uint32_t current_mip_height = hiz_height >> mip;
86
87 // Ensure minimum size of 1x1
88 if (current_mip_width == 0 || current_mip_height == 0)
89 {
90 break;
91 }
92
93 // Set input (previous mip level) as read-only image
94 gfx::set_image(0, output_hiz->native_handle(), mip - 1, bgfx::Access::Read);
95
96 // Set output (current mip level) as write-only image
97 gfx::set_image(1, output_hiz->native_handle(), mip, bgfx::Access::Write);
98
99 // Set parameters for the downsampling compute shader
100 math::vec4 hiz_params(float(current_mip_width), float(current_mip_height), 2.0f, float(mip));
101 gfx::set_uniform(hiz_downsample_.u_hiz_params, hiz_params);
102
103 // Dispatch compute shader with 8x8 thread groups
104 uint32_t num_groups_x = (current_mip_width + 7) / 8;
105 uint32_t num_groups_y = (current_mip_height + 7) / 8;
106 bgfx::dispatch(pass.id, hiz_downsample_.program->native_handle(), num_groups_x, num_groups_y, 1);
107 }
108
109 // End the program once after all dispatches
110 hiz_downsample_.program->end();
111 }
112
113 // Memory barrier to ensure all compute operations are complete
114 // This is handled automatically by bgfx between frame boundaries,
115 // but we add it for safety if the Hi-Z buffer is used immediately
116}
117
118} // namespace unravel
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard) -> asset_handle< T >
Gets an asset by its key.
void run(gfx::render_view &rview, const run_params &params)
Executes the Hi-Z generation: generates mip chain from depth buffer using compute shaders.
Definition hiz_pass.cpp:35
auto init(rtti::context &ctx) -> bool
Must be called once (after bgfx::init() and after asset_manager is registered in context).
Definition hiz_pass.cpp:10
void set_image(uint8_t _stage, texture_handle _handle, uint8_t _mip, access _access, texture_format _format)
Definition graphics.cpp:924
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:803
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
Definition graphics.cpp:889
gfx::view_id id
Definition render_pass.h:98
gfx::texture::ptr depth_buffer
Source depth buffer.
Definition hiz_pass.h:19
const camera * cam
Camera for near/far plane information.
Definition hiz_pass.h:21
gfx::texture::ptr output_hiz
Output Hi-Z texture (must be R32F or R16F format with mips)
Definition hiz_pass.h:20