Unravel Engine C++ Reference
Loading...
Searching...
No Matches
prefilter_pass.cpp
Go to the documentation of this file.
1// prefilter_pass.cpp
2#include "prefilter_pass.h"
3#include <bgfx/bgfx.h>
4#include <bx/math.h>
7namespace unravel
8{
9
11{
12 auto& am = ctx.get_cached<asset_manager>();
13
14
15 // Load compute shader version
16 auto cs = am.get_asset<gfx::shader>("engine:/data/shaders/prefilter/cs_prefilter.sc");
17 cs_.program = std::make_unique<gpu_program>(cs);
18 cs_.cache_uniforms();
19
20 return cs_.program->is_valid();
21}
22
24{
25 return run_compute(params);
26}
27
29{
30 // Prepare output cubemap
31 const auto& ti = params.output_cube->info;
32 uint8_t max_mips = ti.numMips;
33
34 // Simple copy if disabled
35 {
36 APP_SCOPE_PERF("Rendering/Env Blit Pass");
37
38 auto output_cube = params.output_cube;
39
40 if(!params.apply_prefilter)
41 {
42 output_cube = params.output_cube_prefiltered;
43 }
44 gfx::render_pass pass("blit_faces_to_cubemap_pass");
45 for(uint8_t face = 0; face < 6; ++face)
46 {
47 auto src = params.input_faces[face]->native_handle();
48 for(uint8_t mip = 0; mip < max_mips; ++mip)
49 {
50 uint16_t dim = ti.width >> mip;
51 bgfx::blit(pass.id, output_cube->native_handle(), mip, 0, 0, face, src, mip, 0, 0, 0, dim, dim, 1);
52 }
53 }
54
55 if(!params.apply_prefilter)
56 {
57 return params.output_cube;
58 }
59 }
60
61 // Compute shader prefiltering
62 {
63 APP_SCOPE_PERF("Rendering/Env Compute Prefilter Pass");
64
65 const auto& input_cube = params.output_cube;
66 const auto& output_cube = params.output_cube_prefiltered;
67 uint16_t cube_size = ti.width;
68
69 // Process all mip levels using compute shader
70 for(uint8_t mip = 0; mip < max_mips; ++mip)
71 {
72 gfx::render_pass::push_scope(fmt::format("mip {}", mip).c_str());
73
74 uint16_t dim = cube_size >> mip;
75
76 gfx::render_pass pass("prefilter_compute_pass");
77
78 // Begin compute program
79 cs_.program->begin();
80
81 // Bind input cubemap
82 gfx::set_texture(cs_.s_env, 0, input_cube);
83
84 // Bind output cubemap as 2D array image (all faces at once)
85 gfx::set_image(1, output_cube->native_handle(), mip, bgfx::Access::Write);
86
87 // Set uniforms for this mip level (no face index needed)
88 float data[4] = {float(mip), 0.0f, float(cube_size), float(max_mips)};
89 gfx::set_uniform(cs_.u_data, data);
90
91 // Calculate dispatch size for this mip level
92 // Process all faces in parallel with Z dimension
93 uint32_t num_groups_x = (dim + 7) / 8;
94 uint32_t num_groups_y = (dim + 7) / 8;
95 uint32_t num_groups_z = 1; // All 6 faces handled by workgroup size
96
97 // Dispatch compute shader for all faces at once
98 bgfx::dispatch(pass.id, cs_.program->native_handle(), num_groups_x, num_groups_y, num_groups_z);
99
100 cs_.program->end();
101
103 }
104
105 // Add memory barrier to ensure all compute operations are complete
106 // This ensures proper synchronization between mip levels and before texture usage
107 gfx::render_pass barrier_pass("prefilter_barrier_pass");
108 // bgfx handles memory barriers automatically between frame boundaries,
109 // but we add explicit synchronization for safety
110 }
111
112 return params.output_cube_prefiltered;
113}
114
115} // 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.
auto run(const run_params &params) -> gfx::texture::ptr
Execute prefilter. Returns the filtered cubemap (output_cube or created internally).
auto run_compute(const run_params &params) -> gfx::texture::ptr
Execute prefilter using compute shader. Returns the filtered cubemap.
auto init(rtti::context &ctx) -> bool
Initialize shaders. Call once after bgfx::init() and asset registration.
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
#define APP_SCOPE_PERF(name)
Create a scoped performance timer that only accepts string literals.
Definition profiler.h:160
static void pop_scope()
gfx::view_id id
Definition render_pass.h:98
static void push_scope(const char *name)