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 auto cs = am.get_asset<gfx::shader>("engine:/data/shaders/prefilter/cs_prefilter.sc");
14 cs_.program = std::make_unique<gpu_program>(cs);
15 cs_.cache_uniforms();
16 return cs_.program->is_valid();
17}
18
20{
21 return run_compute(rview, params);
22}
23
25{
26 // Prepare output cubemap
27 const auto& ti = params.output_cube->info;
28 uint8_t max_mips = ti.numMips;
29
30 // Copy captured face textures into the cubemap (all mips; faces auto-generate box mips on resolve).
31 {
32 APP_SCOPE_PERF("Rendering/Env Blit Pass");
33
34 auto output_cube = params.output_cube;
35
36 if(!params.apply_prefilter)
37 {
38 output_cube = params.output_cube_prefiltered;
39 }
40 gfx::render_pass pass("Prefilter/Blit Faces to Cubemap Pass");
41 pass.touch();
42 for(uint8_t face = 0; face < 6; ++face)
43 {
44 auto src = params.input_faces[face]->native_handle();
45 for(uint8_t mip = 0; mip < max_mips; ++mip)
46 {
47 uint16_t dim = ti.width >> mip;
48 bgfx::blit(pass.id, output_cube->native_handle(), mip, 0, 0, face, src, mip, 0, 0, 0, dim, dim, 1);
49 }
50 }
51
52 if(!params.apply_prefilter)
53 {
54 return output_cube;
55 }
56 }
57
58 // Compute shader prefiltering
59 {
60 APP_SCOPE_PERF("Rendering/Env Compute Prefilter Pass");
61
62 const auto& input_cube = params.output_cube;
63 const auto& output_cube = params.output_cube_prefiltered;
64 uint16_t cube_size = ti.width;
65
66 // Process all mip levels using compute shader
67 for(uint8_t mip = 0; mip < max_mips; ++mip)
68 {
70 gfx::render_pass::push_scope(fmt::format("MIP {}", mip).c_str());
71
72 uint16_t dim = cube_size >> mip;
73
74 gfx::render_pass pass("Prefilter Compute Pass");
75 pass.touch();
76
77 // Begin compute program
78 cs_.program->begin();
79
80 // Bind input cubemap
81 gfx::set_texture(cs_.s_env, 0, input_cube);
82
83 // Bind output cubemap as 2D array image (all faces at once)
84 gfx::set_image(1, output_cube->native_handle(), mip, bgfx::Access::Write);
85
86 // Set uniforms for this mip level (no face index needed)
87 float data[4] = {float(mip), 0.0f, float(cube_size), float(max_mips)};
88 gfx::set_uniform(cs_.u_data, data);
89
90 // Calculate dispatch size for this mip level
91 // Process all faces in parallel with Z dimension
92 uint32_t num_groups_x = (dim + 7) / 8;
93 uint32_t num_groups_y = (dim + 7) / 8;
94 uint32_t num_groups_z = 1; // All 6 faces handled by workgroup size
95
96 // Dispatch compute shader for all faces at once
97 bgfx::dispatch(pass.id, cs_.program->native_handle(), num_groups_x, num_groups_y, num_groups_z);
98
99 cs_.program->end();
100
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
116} // namespace unravel
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard, load_mode mode=load_mode::immediate) -> asset_handle< T >
Gets an asset by its key.
auto run(gfx::render_view &rview, const run_params &params) -> gfx::texture::ptr
Execute prefilter. Returns the filtered cubemap (output_cube or created internally).
auto run_compute(gfx::render_view &rview, 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)
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:977
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
static void pop_scope()
gfx::view_id id
void touch() const
static void push_scope(const char *name)