Skip to content

Commit

Permalink
Initialize fonts texture after render loop setup (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
veeenu authored Mar 2, 2024
1 parent c67ca09 commit 619cfea
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hudhook"
version = "0.6.0"
version = "0.6.1"
edition = "2021"
description = "A graphics API hook with dear imgui render loop. Supports DirectX 9, 11, 12, and OpenGL 3."
homepage = "https://github.com/veeenu/hudhook"
Expand Down
25 changes: 15 additions & 10 deletions src/renderer/backend/dx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,11 @@ impl D3D11RenderEngine {
let projection_buffer = Buffer::new(&device, 1, D3D11_BIND_CONSTANT_BUFFER)?;

let shader_program = ShaderProgram::new(&device)?;
let mut texture_heap = TextureHeap::new(&device)?;
let texture_heap = TextureHeap::new(&device)?;

ctx.set_ini_filename(None);
ctx.io_mut().backend_flags |= BackendFlags::RENDERER_HAS_VTX_OFFSET;
ctx.set_renderer_name(String::from(concat!("hudhook-dx11@", env!("CARGO_PKG_VERSION"))));
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(Self {
device,
Expand Down Expand Up @@ -89,6 +80,20 @@ impl RenderEngine for D3D11RenderEngine {

Ok(())
}

fn setup_fonts(&mut self, ctx: &mut Context) -> Result<()> {
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
self.texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(())
}
}

impl D3D11RenderEngine {
Expand Down
25 changes: 15 additions & 10 deletions src/renderer/backend/dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl D3D12RenderEngine {
let (device, command_queue, command_allocator, command_list) =
unsafe { create_command_objects(command_queue) }?;

let (rtv_heap, mut texture_heap) = unsafe { create_heaps(&device) }?;
let (rtv_heap, texture_heap) = unsafe { create_heaps(&device) }?;
let rtv_heap_start = unsafe { rtv_heap.GetCPUDescriptorHandleForHeapStart() };

let (root_signature, pipeline_state) = unsafe { create_shader_program(&device) }?;
Expand All @@ -57,15 +57,6 @@ impl D3D12RenderEngine {
ctx.set_ini_filename(None);
ctx.io_mut().backend_flags |= BackendFlags::RENDERER_HAS_VTX_OFFSET;
ctx.set_renderer_name(String::from(concat!("hudhook-dx12@", env!("CARGO_PKG_VERSION"))));
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(Self {
device,
Expand Down Expand Up @@ -130,6 +121,20 @@ impl RenderEngine for D3D12RenderEngine {

Ok(())
}

fn setup_fonts(&mut self, ctx: &mut Context) -> Result<()> {
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
self.texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(())
}
}

impl D3D12RenderEngine {
Expand Down
25 changes: 15 additions & 10 deletions src/renderer/backend/dx9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl D3D9RenderEngine {
pub fn new(device: &IDirect3DDevice9, ctx: &mut Context) -> Result<Self> {
let device = device.clone();

let mut texture_heap = TextureHeap::new(&device)?;
let texture_heap = TextureHeap::new(&device)?;

let vertex_buffer = Buffer::new(&device, 5000)?;
let index_buffer = Buffer::new(&device, 10000)?;
Expand All @@ -62,15 +62,6 @@ impl D3D9RenderEngine {
ctx.set_ini_filename(None);
ctx.io_mut().backend_flags |= BackendFlags::RENDERER_HAS_VTX_OFFSET;
ctx.set_renderer_name(String::from(concat!("hudhook-dx9@", env!("CARGO_PKG_VERSION"))));
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(Self { device, texture_heap, vertex_buffer, index_buffer, projection_buffer })
}
Expand All @@ -96,6 +87,20 @@ impl RenderEngine for D3D9RenderEngine {
}
Ok(())
}

fn setup_fonts(&mut self, ctx: &mut Context) -> Result<()> {
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
self.texture_heap.create_texture(
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(())
}
}

impl D3D9RenderEngine {
Expand Down
27 changes: 16 additions & 11 deletions src/renderer/backend/opengl3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,10 @@ impl OpenGl3RenderEngine {

let vao = util::out_param(|x| unsafe { gl.GenVertexArrays(1, x) });

let mut texture_heap = TextureHeap::new();
let texture_heap = TextureHeap::new();

ctx.set_ini_filename(None);
ctx.set_renderer_name(String::from(concat!("hudhook-opengl3@", env!("CARGO_PKG_VERSION"))));
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
texture_heap.create_texture(
&gl,
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(Self {
gl,
Expand Down Expand Up @@ -120,6 +110,21 @@ impl RenderEngine for OpenGl3RenderEngine {
}
Ok(())
}

fn setup_fonts(&mut self, ctx: &mut Context) -> Result<()> {
let fonts = ctx.fonts();
let fonts_texture = fonts.build_rgba32_texture();
fonts.tex_id = unsafe {
self.texture_heap.create_texture(
&self.gl,
fonts_texture.data,
fonts_texture.width,
fonts_texture.height,
)
}?;

Ok(())
}
}

impl OpenGl3RenderEngine {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ mod input;
mod keys;
mod pipeline;

use imgui::{DrawData, TextureId};
use imgui::{Context, DrawData, TextureId};
use windows::core::Result;

pub(crate) trait RenderEngine {
type RenderTarget;

fn load_image(&mut self, data: &[u8], width: u32, height: u32) -> Result<TextureId>;
fn render(&mut self, draw_data: &DrawData, render_target: Self::RenderTarget) -> Result<()>;
fn setup_fonts(&mut self, ctx: &mut Context) -> Result<()>;
}
#[cfg(feature = "dx11")]
pub(crate) use backend::dx11::D3D11RenderEngine;
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl<T: RenderEngine> Pipeline<T> {
engine.load_image(data, width, height)
});

if let Err(e) = engine.setup_fonts(&mut ctx) {
return Err((e, render_loop));
}

let wnd_proc = unsafe {
mem::transmute(SetWindowLongPtrA(hwnd, GWLP_WNDPROC, pipeline_wnd_proc as usize as _))
};
Expand Down

0 comments on commit 619cfea

Please sign in to comment.