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;
type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
#[cfg(feature = "opengl")]
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 context = glutin::ContextBuilder::new().with_vsync(true);
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);
platform.prepare_render(&ui, &windowed_context.window());
let draw_data = ui.render();
renderer
.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")]
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 window = glutin::WindowBuilder::new()
.with_title(title)
.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)
.expect("Failed to initalize graphics");
.expect("Failed to initialize graphics");
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let mut imgui = ImGui::init();
{
// 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]);
}
}
let mut imgui = Context::create();
imgui.set_ini_filename(None);
// In the examples we only use integer DPI factors, because the UI can get very blurry
// otherwise. This might or might not be what you want in a real application.
let hidpi_factor = window.inner.get_hidpi_factor().round();
let mut platform = WinitPlatform::init(&mut imgui);
platform.attach_window(imgui.io_mut(), &window.inner, HiDpiMode::Rounded);
let hidpi_factor = platform.hidpi_factor();
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(
ImFontConfig::new()
.oversample_h(1)
.pixel_snap_h(true)
.size_pixels(font_size),
);
imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32;
imgui.fonts().add_font_with_config(
include_bytes!("../../../resources/mplus-1p-regular.ttf"),
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 renderer = GfxRenderer::init(&mut imgui, &mut factory, Shaders::HlslSm40)
.expect("Failed to initialize renderer");
let mut last_frame = Instant::now();
let mut quit = false;
loop {
let mut new_size = None;
events_loop.poll_events(|event| {
use glutin::{
Event,
WindowEvent::{CloseRequested, Resized},
};
imgui_winit_support::handle_event(
&mut imgui,
&event,
window.inner.get_hidpi_factor(),
hidpi_factor,
);
platform.handle_event(imgui.io_mut(), &window.inner, &event);
if let Event::WindowEvent { event, .. } = event {
match event {
Resized(size) => {
// gfx_window_dxgi::update_views(&window, &mut factory, &mut device, );
renderer.update_render_target(main_color.clone());
WindowEvent::Resized(size) => {
let physical = size.to_physical(window.inner.get_hidpi_factor());
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 {
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 delta = now - last_frame;
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
last_frame = now;
let io = imgui.io_mut();
platform
.prepare_frame(io, &window.inner)
.expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);
imgui_winit_support::update_mouse_cursor(&imgui, &window.inner);
let frame_size = imgui_winit_support::get_frame_size(&window.inner, hidpi_factor).unwrap();
let ui = imgui.frame(frame_size, delta_s);
let ui = imgui.frame();
if !run_ui(&ui) {
break;
}
encoder.clear(&main_color, clear_color);
platform.prepare_render(&ui, &window);
platform.prepare_render(&ui, &window.inner);
let draw_data = ui.render();
renderer
.render(ui, &mut factory, &mut encoder)
.render(&mut factory, &mut encoder, &mut main_color, draw_data)
.expect("Rendering failed");
encoder.flush(&mut device);
window.swap_buffers(1);

View File

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