Skip to content

Commit 7c3e88a

Browse files
author
GBDixonAlex
committed
- move imgui to use loaded shader from disk instead of embedded shader source in code
1 parent 3a7e360 commit 7c3e88a

3 files changed

Lines changed: 17 additions & 62 deletions

File tree

examples/imgui_demo/main.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use hotline_rs::{*, prelude::*};
33
use os::{App, Window};
44
use gfx::{CmdBuf, Device, SwapChain};
55

6+
use std::fs;
7+
68
#[repr(C)]
79
struct Vertex {
810
position: [f32; 2],
@@ -53,21 +55,13 @@ fn main() -> Result<(), hotline_rs::Error> {
5355
let mut swap_chain = dev.create_swap_chain::<os_platform::App>(&swap_chain_info, &win)?;
5456
let mut cmdbuffer = dev.create_cmd_buf(2);
5557

56-
let exe_path = std::env::current_exe().ok().unwrap();
57-
let asset_path = exe_path.parent().unwrap().join("../..");
58-
59-
let font_path = asset_path
60-
.join("data/fonts/roboto_medium.ttf")
61-
.to_str()
62-
.unwrap()
63-
.to_string();
64-
58+
let roboto = get_data_path("fonts/roboto_medium.ttf");
6559
let mut imgui_info = imgui::ImGuiInfo {
6660
device: &mut dev,
6761
swap_chain: &mut swap_chain,
6862
main_window: &win,
6963
fonts: vec![imgui::FontInfo {
70-
filepath: font_path,
64+
filepath: roboto,
7165
glyph_ranges: None
7266
}],
7367
monitors: app.enumerate_display_monitors()

shaders/imgui.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct PS_INPUT
2121
PS_INPUT vs_main(VS_INPUT input)
2222
{
2323
PS_INPUT output;
24-
output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.0, 1.0));
24+
output.pos = mul(float4(input.pos.xy, 0.0, 1.0), ProjectionMatrix);
2525
output.col = input.col;
2626
output.uv = input.uv;
2727
return output;

src/imgui.rs

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ use crate::gfx::Device;
1414
use crate::gfx::SwapChain;
1515
use crate::gfx::Texture;
1616

17+
use maths_rs::Vec4f;
18+
1719
use std::ffi::CStr;
1820
use std::ffi::CString;
19-
20-
use maths_rs::Vec4f;
21+
use std::fs;
2122

2223
use crate::static_ref;
2324
use crate::static_ref_mut;
@@ -240,64 +241,24 @@ fn create_render_pipeline<D: Device, A: App>(info: &ImGuiInfo<D, A>) -> Result<D
240241
let device = &info.device;
241242
let swap_chain = &info.swap_chain;
242243

243-
// TODO: temp: compile shaders
244-
let src = "
245-
cbuffer vertexBuffer : register(b0)
246-
{
247-
float4x4 ProjectionMatrix;
248-
};
249-
struct VS_INPUT
250-
{
251-
float2 pos : POSITION;
252-
float4 col : COLOR0;
253-
float2 uv : TEXCOORD0;
254-
};
244+
let vsc_filepath = crate::get_data_path("shaders/imgui/vs_main.vsc");
245+
let psc_filepath = crate::get_data_path("shaders/imgui/ps_main.psc");
255246

256-
struct PS_INPUT
257-
{
258-
float4 pos : SV_POSITION;
259-
float4 col : COLOR0;
260-
float2 uv : TEXCOORD0;
261-
};
262-
263-
PS_INPUT VSMain(VS_INPUT input)
264-
{
265-
PS_INPUT output;
266-
output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.0, 1.0));
267-
output.col = input.col;
268-
output.uv = input.uv;
269-
return output;
270-
}
271-
272-
SamplerState sampler0 : register(s0);
273-
Texture2D texture0 : register(t0);
274-
275-
float4 PSMain(PS_INPUT input) : SV_Target
276-
{
277-
float4 out_col = input.col * texture0.Sample(sampler0, input.uv);
278-
return out_col;
279-
}";
247+
let vsc_data = fs::read(vsc_filepath)?;
248+
let psc_data = fs::read(psc_filepath)?;
280249

281250
let vs_info = gfx::ShaderInfo {
282251
shader_type: gfx::ShaderType::Vertex,
283-
compile_info: Some(gfx::ShaderCompileInfo {
284-
entry_point: String::from("VSMain"),
285-
target: String::from("vs_5_0"),
286-
flags: gfx::ShaderCompileFlags::NONE,
287-
}),
252+
compile_info: None
288253
};
289254

290-
let fs_info = gfx::ShaderInfo {
255+
let ps_info = gfx::ShaderInfo {
291256
shader_type: gfx::ShaderType::Fragment,
292-
compile_info: Some(gfx::ShaderCompileInfo {
293-
entry_point: String::from("PSMain"),
294-
target: String::from("ps_5_0"),
295-
flags: gfx::ShaderCompileFlags::NONE,
296-
}),
257+
compile_info: None
297258
};
298259

299-
let vs = device.create_shader(&vs_info, src.as_bytes())?;
300-
let fs = device.create_shader(&fs_info, src.as_bytes())?;
260+
let vs = device.create_shader(&vs_info, &vsc_data)?;
261+
let fs = device.create_shader(&ps_info, &psc_data)?;
301262

302263
device.create_render_pipeline(&gfx::RenderPipelineInfo {
303264
vs: Some(&vs),

0 commit comments

Comments
 (0)