Fix gfx DX11 support

This commit is contained in:
Joonas Javanainen 2019-07-01 11:41:41 +03:00
parent 0f15b80fdb
commit eda44c46ca
2 changed files with 72 additions and 95 deletions

View File

@ -6,10 +6,11 @@ use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::time::Instant; use std::time::Instant;
type ColorFormat = gfx::format::Rgba8; type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) { pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
type DepthFormat = gfx::format::DepthStencil;
let mut events_loop = glutin::EventsLoop::new(); let mut events_loop = glutin::EventsLoop::new();
let context = glutin::ContextBuilder::new().with_vsync(true); let context = glutin::ContextBuilder::new().with_vsync(true);
let builder = glutin::WindowBuilder::new() let builder = glutin::WindowBuilder::new()
@ -126,6 +127,7 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
} }
encoder.clear(&main_color, clear_color); encoder.clear(&main_color, clear_color);
platform.prepare_render(&ui, &windowed_context.window());
let draw_data = ui.render(); let draw_data = ui.render();
renderer renderer
.render(&mut factory, &mut encoder, &mut main_color, draw_data) .render(&mut factory, &mut encoder, &mut main_color, draw_data)
@ -138,97 +140,62 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
#[cfg(feature = "directx")] #[cfg(feature = "directx")]
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) { pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
use gfx::{self, Device};
use gfx_window_dxgi;
use glutin;
let mut events_loop = glutin::EventsLoop::new(); let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new() let window = glutin::WindowBuilder::new()
.with_title(title) .with_title(title)
.with_dimensions(glutin::dpi::LogicalSize::new(1024f64, 768f64)); .with_dimensions(glutin::dpi::LogicalSize::new(1024f64, 768f64));
let (window, mut device, mut factory, mut main_color) = let (mut window, mut device, mut factory, mut main_color) =
gfx_window_dxgi::init::<ColorFormat>(window, &events_loop) gfx_window_dxgi::init::<ColorFormat>(window, &events_loop)
.expect("Failed to initalize graphics"); .expect("Failed to initialize graphics");
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into(); let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let mut imgui = ImGui::init(); let mut imgui = Context::create();
{
// Fix incorrect colors with sRGB framebuffer
fn imgui_gamma_to_linear(col: ImVec4) -> ImVec4 {
let x = col.x.powf(2.2);
let y = col.y.powf(2.2);
let z = col.z.powf(2.2);
let w = 1.0 - (1.0 - col.w).powf(2.2);
ImVec4::new(x, y, z, w)
}
let style = imgui.style_mut();
for col in 0..style.colors.len() {
style.colors[col] = imgui_gamma_to_linear(style.colors[col]);
}
}
imgui.set_ini_filename(None); imgui.set_ini_filename(None);
// In the examples we only use integer DPI factors, because the UI can get very blurry let mut platform = WinitPlatform::init(&mut imgui);
// otherwise. This might or might not be what you want in a real application. platform.attach_window(imgui.io_mut(), &window.inner, HiDpiMode::Rounded);
let hidpi_factor = window.inner.get_hidpi_factor().round();
let hidpi_factor = platform.hidpi_factor();
let font_size = (13.0 * hidpi_factor) as f32; let font_size = (13.0 * hidpi_factor) as f32;
imgui.fonts().add_font(&[
FontSource::DefaultFontData {
config: Some(FontConfig {
size_pixels: font_size,
..FontConfig::default()
}),
},
FontSource::TtfData {
data: include_bytes!("../../../resources/mplus-1p-regular.ttf"),
size_pixels: font_size,
config: Some(FontConfig {
rasterizer_multiply: 1.75,
glyph_ranges: FontGlyphRanges::japanese(),
..FontConfig::default()
}),
},
]);
imgui.fonts().add_default_font_with_config( imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32;
ImFontConfig::new()
.oversample_h(1)
.pixel_snap_h(true)
.size_pixels(font_size),
);
imgui.fonts().add_font_with_config( let mut renderer = GfxRenderer::init(&mut imgui, &mut factory, Shaders::HlslSm40)
include_bytes!("../../../resources/mplus-1p-regular.ttf"), .expect("Failed to initialize renderer");
ImFontConfig::new()
.merge_mode(true)
.oversample_h(1)
.pixel_snap_h(true)
.size_pixels(font_size)
.rasterizer_multiply(1.75),
&FontGlyphRange::japanese(),
);
imgui.set_font_global_scale((1.0 / hidpi_factor) as f32);
let mut renderer = GfxRenderer::init(
&mut imgui,
&mut factory,
Shaders::HlslSm40,
main_color.clone(),
)
.expect("Failed to initialize renderer");
imgui_winit_support::configure_keys(&mut imgui);
let mut last_frame = Instant::now(); let mut last_frame = Instant::now();
let mut quit = false; let mut quit = false;
loop { loop {
let mut new_size = None;
events_loop.poll_events(|event| { events_loop.poll_events(|event| {
use glutin::{ platform.handle_event(imgui.io_mut(), &window.inner, &event);
Event,
WindowEvent::{CloseRequested, Resized},
};
imgui_winit_support::handle_event(
&mut imgui,
&event,
window.inner.get_hidpi_factor(),
hidpi_factor,
);
if let Event::WindowEvent { event, .. } = event { if let Event::WindowEvent { event, .. } = event {
match event { match event {
Resized(size) => { WindowEvent::Resized(size) => {
// gfx_window_dxgi::update_views(&window, &mut factory, &mut device, ); let physical = size.to_physical(window.inner.get_hidpi_factor());
renderer.update_render_target(main_color.clone()); let (width, height): (u32, u32) = physical.into();
new_size = Some((width as u16, height as u16));
} }
CloseRequested => quit = true, WindowEvent::CloseRequested => quit = true,
_ => (), _ => (),
} }
} }
@ -236,25 +203,34 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
if quit { if quit {
break; break;
} }
if let Some((width, height)) = new_size {
drop(main_color);
main_color = gfx_window_dxgi::update_views(
&mut window,
&mut factory,
&mut device,
width,
height,
)
.expect("Failed to update resize");
}
let now = Instant::now(); let io = imgui.io_mut();
let delta = now - last_frame; platform
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0; .prepare_frame(io, &window.inner)
last_frame = now; .expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);
imgui_winit_support::update_mouse_cursor(&imgui, &window.inner); let ui = imgui.frame();
let frame_size = imgui_winit_support::get_frame_size(&window.inner, hidpi_factor).unwrap();
let ui = imgui.frame(frame_size, delta_s);
if !run_ui(&ui) { if !run_ui(&ui) {
break; break;
} }
encoder.clear(&main_color, clear_color); encoder.clear(&main_color, clear_color);
platform.prepare_render(&ui, &window); platform.prepare_render(&ui, &window.inner);
let draw_data = ui.render();
renderer renderer
.render(ui, &mut factory, &mut encoder) .render(&mut factory, &mut encoder, &mut main_color, draw_data)
.expect("Rendering failed"); .expect("Rendering failed");
encoder.flush(&mut device); encoder.flush(&mut device);
window.swap_buffers(1); window.swap_buffers(1);

View File

@ -430,22 +430,23 @@ mod pipeline {
None => return Err(InitError::VertexImport(&at.name, None)), None => return Err(InitError::VertexImport(&at.name, None)),
} }
} }
for gc in &info.globals { #[cfg(feature = "directx")]
#[cfg(not(feature = "directx"))] for cb in &info.constant_buffers {
{ match meta.constants.link_constant_buffer(cb, &self.constants) {
match meta.matrix.link_global_constant(gc, &self.matrix) { Some(Ok(d)) => {
Some(Ok(())) => assert!(meta.matrix.is_active()), assert!(meta.constants.is_active());
Some(Err(e)) => return Err(InitError::GlobalConstant(&gc.name, Some(e))), desc.constant_buffers[cb.slot as usize] = Some(d);
None => return Err(InitError::GlobalConstant(&gc.name, None)),
} }
Some(Err(e)) => return Err(InitError::ConstantBuffer(&cb.name, Some(e))),
None => return Err(InitError::ConstantBuffer(&cb.name, None)),
} }
#[cfg(feature = "directx")] }
{ #[cfg(not(feature = "directx"))]
match meta.constants.link_global_constant(gc, &self.constants) { for gc in &info.globals {
Some(Ok(())) => assert!(meta.constants.is_active()), match meta.matrix.link_global_constant(gc, &self.matrix) {
Some(Err(e)) => return Err(InitError::GlobalConstant(&gc.name, Some(e))), Some(Ok(())) => assert!(meta.matrix.is_active()),
None => return Err(InitError::GlobalConstant(&gc.name, None)), Some(Err(e)) => return Err(InitError::GlobalConstant(&gc.name, Some(e))),
} None => return Err(InitError::GlobalConstant(&gc.name, None)),
} }
} }
for srv in &info.textures { for srv in &info.textures {
@ -534,7 +535,7 @@ mod pipeline {
#[cfg(not(feature = "directx"))] #[cfg(not(feature = "directx"))]
matrix: "matrix", matrix: "matrix",
#[cfg(feature = "directx")] #[cfg(feature = "directx")]
constants: "constants", constants: "Constants",
tex: "tex", tex: "tex",
target: ("Target0", ColorMask::all(), blend::ALPHA), target: ("Target0", ColorMask::all(), blend::ALPHA),
scissor: (), scissor: (),