移除HDR配置相关代码,优化交换链重建逻辑以支持动态格式选择
This commit is contained in:
@@ -8,7 +8,6 @@ int main(int argc, char* argv[]) {
|
|||||||
mirai_app_config config;
|
mirai_app_config config;
|
||||||
config.window_mgr_config.main_window.size = vec2i{800, 600};
|
config.window_mgr_config.main_window.size = vec2i{800, 600};
|
||||||
config.window_mgr_config.main_window.flags = SDL_WINDOW_RESIZABLE;
|
config.window_mgr_config.main_window.flags = SDL_WINDOW_RESIZABLE;
|
||||||
config.window_mgr_config.main_window.hdr_enabled = true;
|
|
||||||
mirai::mirai_app app;
|
mirai::mirai_app app;
|
||||||
|
|
||||||
app.setup(config);
|
app.setup(config);
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ namespace mirai {
|
|||||||
|
|
||||||
void wgpu_context::shutdown() {
|
void wgpu_context::shutdown() {
|
||||||
wgpu_device_.destroy();
|
wgpu_device_.destroy();
|
||||||
|
wgpu_adapter_.release();
|
||||||
wgpu_instance_.release();
|
wgpu_instance_.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ namespace mirai {
|
|||||||
options.setDefault();
|
options.setDefault();
|
||||||
options.powerPreference = wgpu::PowerPreference::HighPerformance;
|
options.powerPreference = wgpu::PowerPreference::HighPerformance;
|
||||||
options.compatibleSurface = compatible_surface;
|
options.compatibleSurface = compatible_surface;
|
||||||
wgpu::raii::Adapter adapter = wgpu_instance_.requestAdapter(options);
|
wgpu_adapter_ = wgpu_instance_.requestAdapter(options);
|
||||||
|
|
||||||
wgpu::DeviceDescriptor device_desc{};
|
wgpu::DeviceDescriptor device_desc{};
|
||||||
device_desc.setDefault();
|
device_desc.setDefault();
|
||||||
@@ -69,7 +70,7 @@ namespace mirai {
|
|||||||
device_desc.deviceLostCallback = [](WGPUDeviceLostReason reason, char const* message, void* userdata) {
|
device_desc.deviceLostCallback = [](WGPUDeviceLostReason reason, char const* message, void* userdata) {
|
||||||
MIRAI_LOG_ERROR("设备丢失 {}: {}", static_cast<int>(reason), message);
|
MIRAI_LOG_ERROR("设备丢失 {}: {}", static_cast<int>(reason), message);
|
||||||
};
|
};
|
||||||
wgpu_device_ = adapter->requestDevice(device_desc);
|
wgpu_device_ = wgpu_adapter_.requestDevice(device_desc);
|
||||||
if (!wgpu_device_) {
|
if (!wgpu_device_) {
|
||||||
MIRAI_LOG_ERROR("无法创建 WebGPU 设备");
|
MIRAI_LOG_ERROR("无法创建 WebGPU 设备");
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ namespace mirai {
|
|||||||
auto get_device() {
|
auto get_device() {
|
||||||
return wgpu_device_;
|
return wgpu_device_;
|
||||||
}
|
}
|
||||||
|
auto get_adapter() {
|
||||||
|
return wgpu_adapter_;
|
||||||
|
}
|
||||||
auto get_test_pipeline() {
|
auto get_test_pipeline() {
|
||||||
return render_pipeline_;
|
return render_pipeline_;
|
||||||
}
|
}
|
||||||
@@ -32,6 +35,7 @@ namespace mirai {
|
|||||||
wgpu_context() = default;
|
wgpu_context() = default;
|
||||||
wgpu::Instance wgpu_instance_;
|
wgpu::Instance wgpu_instance_;
|
||||||
wgpu::Device wgpu_device_;
|
wgpu::Device wgpu_device_;
|
||||||
|
wgpu::Adapter wgpu_adapter_;
|
||||||
std::shared_ptr<render_pipeline> render_pipeline_;
|
std::shared_ptr<render_pipeline> render_pipeline_;
|
||||||
std::unique_ptr<wgpu::ErrorCallback> device_error_callback_;
|
std::unique_ptr<wgpu::ErrorCallback> device_error_callback_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace mirai {
|
|||||||
return MAKE_ERROR_INFO(error_code::window_creation_failed,
|
return MAKE_ERROR_INFO(error_code::window_creation_failed,
|
||||||
"SDL_CreateWindow 失败: {}", SDL_GetError());
|
"SDL_CreateWindow 失败: {}", SDL_GetError());
|
||||||
}
|
}
|
||||||
hdr_enabled_ = config.hdr_enabled;
|
|
||||||
|
|
||||||
if (config.wgpu_window) {
|
if (config.wgpu_window) {
|
||||||
wgpu_surface_ = SDL_GetWGPUSurface(wgpu_context::instance().get_instance(), win_ptr);
|
wgpu_surface_ = SDL_GetWGPUSurface(wgpu_context::instance().get_instance(), win_ptr);
|
||||||
@@ -182,7 +181,7 @@ namespace mirai {
|
|||||||
return need_swapchain_rebuild_;
|
return need_swapchain_rebuild_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void_result_t window::rebuild_swapchain(bool hdr_enabled, vec2i new_extent) {
|
void_result_t window::rebuild_swapchain(vec2i new_extent) {
|
||||||
if (!wgpu_surface_) {
|
if (!wgpu_surface_) {
|
||||||
return MAKE_ERROR_INFO(error_code::invalid_operation,
|
return MAKE_ERROR_INFO(error_code::invalid_operation,
|
||||||
"无法重建交换链: WGPU Surface 未创建");
|
"无法重建交换链: WGPU Surface 未创建");
|
||||||
@@ -194,13 +193,34 @@ namespace mirai {
|
|||||||
if (!need_swapchain_rebuild_) {
|
if (!need_swapchain_rebuild_) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
hdr_enabled_ = hdr_enabled;
|
// 查询表面是否支持RGBA16Float格式
|
||||||
|
wgpu::TextureFormat using_format = wgpu::TextureFormat::BGRA8Unorm;
|
||||||
|
{
|
||||||
|
auto adapter = wgpu_context::instance().get_adapter();
|
||||||
|
wgpu::SurfaceCapabilities capabilities{};
|
||||||
|
wgpu_surface_.getCapabilities(adapter, &capabilities);
|
||||||
|
|
||||||
|
// 按优先级选择格式
|
||||||
|
// 1 RGBA16Float
|
||||||
|
// 2 RGB10A2Unorm
|
||||||
|
// 3 BGRA8Unorm
|
||||||
|
for (int i = 0; i < capabilities.formatCount; ++i) {
|
||||||
|
const auto& format = capabilities.formats[i];
|
||||||
|
if (format == wgpu::TextureFormat::RGBA16Float) {
|
||||||
|
using_format = wgpu::TextureFormat::RGBA16Float;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (format == wgpu::TextureFormat::RGB10A2Unorm) {
|
||||||
|
using_format = wgpu::TextureFormat::RGB10A2Unorm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wgpu::SurfaceConfiguration wgpu_surface_config{};
|
wgpu::SurfaceConfiguration wgpu_surface_config{};
|
||||||
wgpu_surface_config.setDefault();
|
wgpu_surface_config.setDefault();
|
||||||
wgpu_surface_config.usage = wgpu::TextureUsage::RenderAttachment;
|
wgpu_surface_config.usage = wgpu::TextureUsage::RenderAttachment;
|
||||||
wgpu_surface_config.device = wgpu_context::instance().get_device();
|
wgpu_surface_config.device = wgpu_context::instance().get_device();
|
||||||
wgpu_surface_config.format = hdr_enabled ? wgpu::TextureFormat::RGBA16Float : wgpu::TextureFormat::BGRA8Unorm;
|
wgpu_surface_config.format = using_format;
|
||||||
wgpu_surface_config.width = static_cast<uint32_t>(new_extent.x());
|
wgpu_surface_config.width = static_cast<uint32_t>(new_extent.x());
|
||||||
wgpu_surface_config.height = static_cast<uint32_t>(new_extent.y());
|
wgpu_surface_config.height = static_cast<uint32_t>(new_extent.y());
|
||||||
wgpu_surface_config.presentMode = wgpu::PresentMode::Mailbox;
|
wgpu_surface_config.presentMode = wgpu::PresentMode::Mailbox;
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ namespace mirai {
|
|||||||
SDL_WindowFlags flags{};
|
SDL_WindowFlags flags{};
|
||||||
bool wgpu_window = true;
|
bool wgpu_window = true;
|
||||||
bool visible_on_create = true;
|
bool visible_on_create = true;
|
||||||
bool hdr_enabled = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class window : public object {
|
class window : public object {
|
||||||
@@ -47,9 +46,9 @@ namespace mirai {
|
|||||||
bool is_minimized() const;
|
bool is_minimized() const;
|
||||||
|
|
||||||
bool need_rebuild_swapchain() const;
|
bool need_rebuild_swapchain() const;
|
||||||
void_result_t rebuild_swapchain(bool hdr_enabled, vec2i new_extent);
|
void_result_t rebuild_swapchain(vec2i new_extent);
|
||||||
void_result_t rebuild_swapchain() {
|
void_result_t rebuild_swapchain() {
|
||||||
return rebuild_swapchain(hdr_enabled_, get_framebuffer_size());
|
return rebuild_swapchain(get_framebuffer_size());
|
||||||
}
|
}
|
||||||
wgpu::Surface get_wgpu_surface() const {
|
wgpu::Surface get_wgpu_surface() const {
|
||||||
return wgpu_surface_;
|
return wgpu_surface_;
|
||||||
@@ -64,6 +63,5 @@ namespace mirai {
|
|||||||
wgpu::Surface wgpu_surface_{};
|
wgpu::Surface wgpu_surface_{};
|
||||||
bool need_swapchain_rebuild_{true};
|
bool need_swapchain_rebuild_{true};
|
||||||
bool closing_{false};
|
bool closing_{false};
|
||||||
bool hdr_enabled_{false};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user