4#include "spdlog/common.h"
23thread_local std::vector<uint32_t> s_bgfx_profiler_scope_stack;
29 [](
const std::string& msg,
const char* file_path, uint16_t line)
34 [](
const std::string& msg,
const char* file_path, uint16_t line)
39 [](
const std::string& msg,
const char* file_path, uint16_t line)
44 [](
const std::string& msg,
const char* file_path, uint16_t line)
49 [](
const std::string& msg,
const char* file_path, uint16_t line)
54 auto main_thread_id = std::this_thread::get_id();
56 [main_thread_id](
const char*
name, uint32_t abgr,
const char* file_path, uint16_t line)
65 auto current_id = std::this_thread::get_id();
66 if(current_id != main_thread_id)
68 s_bgfx_profiler_scope_stack.push_back(
73 s_bgfx_profiler_scope_stack.push_back(
77 [main_thread_id](
const char*
name, uint32_t abgr,
const char* file_path, uint16_t line)
86 auto current_id = std::this_thread::get_id();
87 if(current_id != main_thread_id)
89 s_bgfx_profiler_scope_stack.push_back(
99 if(s_bgfx_profiler_scope_stack.empty())
103 const uint32_t
token = s_bgfx_profiler_scope_stack.back();
104 s_bgfx_profiler_scope_stack.pop_back();
105 if(
token != UINT32_MAX)
117 parser.
set_optional<std::string>(
"r",
"renderer",
"auto",
"Select preferred renderer.");
118 parser.
set_optional<
bool>(
"n",
"novsync",
false,
"Disable vsync.");
123 APPLOG_TRACE(
"{}::{}", hpp::type_name_str(*
this), __func__);
130 if(!init_backend(parser))
139 ->
const std::unique_ptr<render_window>&
141 auto mode = os::display::get_desktop_mode(
index);
142 auto bounds = os::display::get_usable_bounds(
index);
144 if(flags & os::window::resizable)
146 auto frame_size = init_window_->get_frame_size();
147 bounds.y +=
static_cast<int>(
static_cast<float>(frame_size.top) / mode.display_scale);
148 bounds.h -=
static_cast<int>(
static_cast<float>(frame_size.top + frame_size.bottom) / mode.display_scale);
149 bounds.x +=
static_cast<int>(
static_cast<float>(frame_size.left) / mode.display_scale);
150 bounds.w -=
static_cast<int>(
static_cast<float>(frame_size.left + frame_size.right) / mode.display_scale);
153 os::window window(
title, bounds.x, bounds.y, bounds.w * mode.display_scale, bounds.h * mode.display_scale, flags);
155 set_main_window(std::move(window));
156 return render_window_;
161 render_window_ = std::make_unique<render_window>(std::move(window));
166 APPLOG_TRACE(
"{}::{}", hpp::type_name_str(*
this), __func__);
172 init_window_.reset();
174 std::make_unique<os::window>(
"INIT", os::window::centered, os::window::centered, 64, 64, os::window::hidden);
175 const auto sz = init_window_->get_size();
178 init_data.type = get_renderer_type(parser);
179 init_data.resolution.width = sz.w;
180 init_data.resolution.height = sz.h;
181 init_data.resolution.reset = get_reset_flags(parser);
182 init_data.platformData.ndt = init_window_->get_native_display();
183 init_data.platformData.nwh = init_window_->get_native_handle();
184 std::string video_driver = os::window::get_current_video_driver();
185 if(video_driver ==
"wayland")
187 init_data.platformData.type = bgfx::NativeWindowHandleType::Wayland;
189 reset_flags_ = init_data.resolution.reset;
191 init_data.limits.numDrawCalls = 65536;
192 init_data.limits.numDrawCallPeakFrames = 0;
196 APPLOG_ERROR(
"Could not initialize rendering backend!");
204 const bgfx::Caps* caps = bgfx::getCaps();
205 if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
219 if(e.type == os::events::window)
221 if(e.window.type == os::window_event_id::close)
223 auto window_id = e.window.window_id;
233 if(e.window.type == os::window_event_id::resized)
235 auto window_id = e.window.window_id;
251 auto preferred_renderer_type = gfx::renderer_type::Count;
253 std::string preferred_renderer;
254 if(parser.try_get(
"renderer", preferred_renderer))
256 if(preferred_renderer ==
"opengl")
258 preferred_renderer_type = gfx::renderer_type::OpenGL;
260 else if(preferred_renderer ==
"vulkan")
262 preferred_renderer_type = gfx::renderer_type::Vulkan;
264 else if(preferred_renderer ==
"directx11" || preferred_renderer ==
"direct3d11" || preferred_renderer ==
"dx11")
266 preferred_renderer_type = gfx::renderer_type::Direct3D11;
268 else if(preferred_renderer ==
"directx12" || preferred_renderer ==
"direct3d12" || preferred_renderer ==
"dx12")
270 preferred_renderer_type = gfx::renderer_type::Direct3D12;
274 return preferred_renderer_type;
279 bool novsync =
false;
280 parser.try_get(
"novsync", novsync);
281 return get_reset_flags(!novsync);
286 uint32_t flags = BGFX_RESET_MAXANISOTROPY | BGFX_RESET_HIDPI;
290 flags |= BGFX_RESET_VSYNC;
294 flags |= BGFX_RESET_NONE;
365 auto& pass = window->begin_present_pass();
void set_optional(const std::string &name, const std::string &alternative, T defaultValue, const std::string &description="", bool dominant=false)
std::chrono::duration< float > delta_t
#define APPLOG_ERROR(...)
#define APPLOG_INFO_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_DEBUG_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_WARNING_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_ERROR_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_TRACE_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
#define APPLOG_TRACE(...)
renderer_type get_renderer_type()
bool init(init_type init_data)
void set_info_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
uint32_t frame(uint8_t _flags)
void set_debug_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
const char * get_renderer_name(renderer_type _type)
void reset(uint32_t _width, uint32_t _height, uint32_t _flags)
void request_screen_shot(frame_buffer_handle _handle, const char *_filePath)
void set_warning_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
void set_trace_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
void set_profiler_hooks(gfx_profiler_begin_hook on_begin, gfx_profiler_begin_literal_hook on_begin_literal, gfx_profiler_end_hook on_end)
bgfx::RendererType::Enum renderer_type
void set_error_logger(const std::function< void(const std::string &, const char *, uint16_t)> &logger)
auto profile_begin(const char *name) -> uint32_t
Begin a profiling scope. Returns an event index for profile_end().
void profile_end(uint32_t idx)
End a profiling scope started by profile_begin().
auto get_thread_profile_data() -> thread_profile_data *
auto profile_begin_owned(hpp::string_view name) -> uint32_t
Like profile_begin() but copies the label into name_owned (script / dynamic names).
void update_eviction(const eviction_settings &settings)
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
static auto get_max_pass_id() -> gfx::view_id
void bind(const frame_buffer *fb=nullptr) const
hpp::event< void(rtti::context &, os::event &e)> on_os_event
os events
Struct representing a render window.
auto get_reset_flags(const cmd_line::parser &parser) const -> uint32_t
std::string request_screenshot_
void frame_begin(rtti::context &ctx, delta_t dt)
auto get_renderer_type(const cmd_line::parser &parser) const -> gfx::renderer_type
auto init_backend(const cmd_line::parser &parser) -> bool
std::unique_ptr< render_window > render_window_
std::unique_ptr< os::window > init_window_
engine windows
void on_os_event(rtti::context &ctx, os::event &e)
auto deinit(rtti::context &ctx) -> bool
void frame_end(rtti::context &ctx, delta_t dt)
auto get_vsync() const -> bool
std::shared_ptr< int > sentinel_
void request_screenshot(const std::string &file)
renderer(rtti::context &ctx, cmd_line::parser &parser)
auto init(rtti::context &ctx, const cmd_line::parser &parser) -> bool
void set_vsync(bool vsync)
void set_main_window(os::window &&window)
auto create_window_for_display(int index, const std::string &title, uint32_t flags) -> const std::unique_ptr< render_window > &
auto get_main_window() const -> render_window *
eviction_settings eviction
struct unravel::settings::graphics_settings graphics
void ddInit(bx::AllocatorI *_allocator)