去掉异步渲染,因为OpenGL和Metal支持问题

This commit is contained in:
Nanako 2025-02-23 23:59:18 +08:00
parent 192bb51f1d
commit 7a01b9d897
6 changed files with 23 additions and 33 deletions

View File

@ -6,6 +6,6 @@ int main(int argc, char* argv[]) {
init.size = {800, 600};
init.resizable = true;
init.centered = true;
// init.api = mirage::renderer_api::opengl;
init.api = mirage::renderer_api::opengl;
return run(init);
}

View File

@ -49,14 +49,15 @@ namespace mirage {
LLGL::BindFlags::ConstantBuffer,
LLGL::StageFlags::VertexStage,
0
);
pipeline = generated_pipelines::create_aorii_rect_pipeline(get_renderer(), swap_chain->GetRenderPass(), pipeline_layout_desc);
);
pipeline = generated_pipelines::create_aorii_rect_pipeline(get_renderer(), swap_chain->GetRenderPass(),
pipeline_layout_desc);
push_rectangle({0, 0}, {100, 100}, linear_color::white);
return true;
}
render_context::update_status render_context::update(const duration_type& in_delta_time) {
render_context::update_status render_context::render_update(const duration_type& in_delta_time) {
if (!command_buffer || !swap_chain) {
return update_status::fail;
}
@ -64,11 +65,12 @@ namespace mirage {
return update_status::wait_for_present;
}
if (auto size = new_size.exchange({})) {
swap_chain->ResizeBuffers(size.value());
if (new_size) {
swap_chain->ResizeBuffers(new_size.value());
pipeline_param param;
param.mvp = get_projection_matrix();
param_buffer->set(param);
new_size.reset();
}
command_buffer->Begin(); {
@ -88,6 +90,7 @@ namespace mirage {
command_buffer->EndRenderPass();
}
command_buffer->End();
get_renderer()->GetCommandQueue()->Submit(*command_buffer);
swap_chain->Present();
return update_status::success;
}
@ -106,7 +109,8 @@ namespace mirage {
spdlog::info("垂直同步: {}", vsync);
}
void render_context::push_rectangle(const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size, const linear_color_type& in_color) {
void render_context::push_rectangle(const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size,
const linear_color_type& in_color) {
vertex va{};
vertex vb{};
vertex vc{};
@ -139,7 +143,8 @@ namespace mirage {
}
Eigen::Matrix4f render_context::get_projection_matrix() const {
const bool is_clip_range_unit_cube = get_renderer()->GetRenderingCaps().clippingRange == LLGL::ClippingRange::MinusOneToOne;
const bool is_clip_range_unit_cube = get_renderer()->GetRenderingCaps().clippingRange ==
LLGL::ClippingRange::MinusOneToOne;
const auto& size = swap_chain->GetResolution();
Eigen::Matrix4f matrix = Eigen::Matrix4f::Identity();

View File

@ -12,7 +12,7 @@ namespace mirage {
~render_context();
bool init(const swap_chain_descriptor& in_desc, const std::shared_ptr<LLGL::Surface>& in_surface);
update_status update(const duration_type& in_delta_time);
update_status render_update(const duration_type& in_delta_time);
void resize_swap_chain(const LLGL::Extent2D& in_size, long in_flag);
void set_vsync(bool in_vsync);
@ -43,6 +43,6 @@ namespace mirage {
LLGL::SwapChain* swap_chain = nullptr;
bool vsync = true;
pipeline_info pipeline;
std::atomic<std::optional<LLGL::Extent2D>> new_size;
std::optional<LLGL::Extent2D> new_size;
};
} // namespace mirage

View File

@ -45,7 +45,7 @@ namespace mirage {
void window::render_update(const duration_type& in_delta_time) {
if (context) {
context->update(in_delta_time);
context->render_update(in_delta_time);
}
}
}

View File

@ -6,9 +6,8 @@
namespace mirage {
void window_manager::update(const duration_type& in_delta_time) {
LLGL::Surface::ProcessEvents();
render_thread_func();
to_destroy.clear();
std::shared_lock lock(mutex);
should_exit_flag = windows.empty() && to_destroy.empty();
}
@ -23,13 +22,11 @@ namespace mirage {
return false;
}
last_time = std::chrono::high_resolution_clock::now();
render_thread = std::thread(&window_manager::render_thread_func, this);
return true;
}
bool window_manager::destroy() {
should_exit_flag = true;
render_thread.join();
windows.clear();
to_destroy.clear();
return true;
@ -38,10 +35,7 @@ namespace mirage {
std::weak_ptr<window> window_manager::create_window(const LLGL::WindowDescriptor& desc) {
auto window_ptr = std::make_shared<window>(desc);
{
std::unique_lock lock(mutex);
windows.push_back(window_ptr);
}
windows.push_back(window_ptr);
if (!window_ptr) {
spdlog::error("无法创建窗口");
return {};
@ -50,7 +44,6 @@ namespace mirage {
}
void window_manager::destroy_window(const window& in_window) {
std::unique_lock lock(mutex);
for (auto it = windows.begin(); it != windows.end(); ++it) {
if (it->get() == &in_window) {
to_destroy.push_back(*it);
@ -61,7 +54,6 @@ namespace mirage {
}
std::weak_ptr<window> window_manager::get_main_window() {
std::shared_lock lock(mutex);
if (windows.empty()) {
return {};
}
@ -69,17 +61,12 @@ namespace mirage {
}
void window_manager::render_thread_func() {
while (!should_exit_flag) {
const auto& current_time = std::chrono::high_resolution_clock::now();
const auto in_delta_time = current_time - last_time;
last_time = current_time;
const auto& current_time = std::chrono::high_resolution_clock::now();
const auto in_delta_time = current_time - last_time;
last_time = current_time;
std::shared_lock lock(mutex);
for (const auto& w : windows) {
w->render_update(in_delta_time);
}
std::this_thread::sleep_for(std::chrono::milliseconds(16));
for (const auto& w : windows) {
w->render_update(in_delta_time);
}
}
} // namespace mirage

View File

@ -31,9 +31,7 @@ namespace mirage {
std::vector<std::shared_ptr<window>> to_destroy;
void render_thread_func();
std::thread render_thread;
time_type last_time = {};
std::atomic_bool should_exit_flag = false;
std::shared_mutex mutex;
};
} // namespace mirage