Split the ImTexture allocator into a generic struct

This commit is contained in:
Tad Hardesty 2018-09-10 00:54:00 -07:00
parent 456258524e
commit fb7ea044e2
3 changed files with 45 additions and 17 deletions

View File

@ -7,9 +7,7 @@ use gfx::memory::Bind;
use gfx::texture::{FilterMethod, SamplerInfo, WrapMode}; use gfx::texture::{FilterMethod, SamplerInfo, WrapMode};
use gfx::traits::FactoryExt; use gfx::traits::FactoryExt;
use gfx::{Bundle, CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice}; use gfx::{Bundle, CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice};
use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, ImTexture}; use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures};
use std::collections::HashMap;
pub type RendererResult<T> = Result<T, RendererError>; pub type RendererResult<T> = Result<T, RendererError>;
@ -101,8 +99,7 @@ pub type Texture<R> = (gfx::handle::ShaderResourceView<R, [f32; 4]>, gfx::handle
pub struct Renderer<R: Resources> { pub struct Renderer<R: Resources> {
bundle: Bundle<R, pipe::Data<R>>, bundle: Bundle<R, pipe::Data<R>>,
index_buffer: Buffer<R, u16>, index_buffer: Buffer<R, u16>,
textures: HashMap<usize, Texture<R>>, textures: Textures<Texture<R>>,
next_texture: usize,
} }
impl<R: Resources> Renderer<R> { impl<R: Resources> Renderer<R> {
@ -140,9 +137,8 @@ impl<R: Resources> Renderer<R> {
let sampler = let sampler =
factory.create_sampler(SamplerInfo::new(FilterMethod::Trilinear, WrapMode::Clamp)); factory.create_sampler(SamplerInfo::new(FilterMethod::Trilinear, WrapMode::Clamp));
let pair = (texture, sampler); let pair = (texture, sampler);
let mut textures = HashMap::new(); let mut textures = Textures::new();
textures.insert(0, pair.clone()); imgui.set_texture_id(textures.insert(pair.clone()));
imgui.set_texture_id(0);
let data = pipe::Data { let data = pipe::Data {
vertex_buffer: vertex_buffer, vertex_buffer: vertex_buffer,
@ -171,8 +167,7 @@ impl<R: Resources> Renderer<R> {
Ok(Renderer { Ok(Renderer {
bundle: Bundle::new(slice, pso, data), bundle: Bundle::new(slice, pso, data),
index_buffer: index_buffer, index_buffer: index_buffer,
textures, textures: textures,
next_texture: 1,
}) })
} }
@ -180,11 +175,8 @@ impl<R: Resources> Renderer<R> {
self.bundle.data.out = out; self.bundle.data.out = out;
} }
pub fn add_texture(&mut self, texture: Texture<R>) -> ImTexture { pub fn textures(&mut self) -> &mut Textures<Texture<R>> {
let id = self.next_texture; &mut self.textures
self.textures.insert(id, texture);
self.next_texture += 1;
id
} }
pub fn render<'a, F: Factory<R>, C: CommandBuffer<R>>( pub fn render<'a, F: Factory<R>, C: CommandBuffer<R>>(
@ -235,7 +227,7 @@ impl<R: Resources> Renderer<R> {
self.bundle.slice.start = 0; self.bundle.slice.start = 0;
for cmd in draw_list.cmd_buffer { for cmd in draw_list.cmd_buffer {
if let Some(tex) = self.textures.get(&(cmd.texture_id as usize)) { if let Some(tex) = self.textures.get(cmd.texture_id as usize) {
// cloning handles is okay, since they're Arcs internally // cloning handles is okay, since they're Arcs internally
self.bundle.data.tex = tex.clone(); self.bundle.data.tex = tex.clone();
} }

View File

@ -1,5 +1,6 @@
use super::{ImVec2, ImVec4, Ui}; use super::{ImVec2, ImVec4, Ui};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::collections::HashMap;
use std::os::raw::c_void; use std::os::raw::c_void;
use sys; use sys;
@ -88,3 +89,38 @@ impl<'ui> Image<'ui> {
} }
} }
} }
/// Generic texture mapping for use by renderers.
pub struct Textures<T> {
textures: HashMap<usize, T>,
next: usize,
}
impl<T> Textures<T> {
pub fn new() -> Self {
Textures {
textures: HashMap::new(),
next: 0,
}
}
pub fn insert(&mut self, texture: T) -> ImTexture {
let id = self.next;
self.textures.insert(id, texture);
self.next += 1;
id
}
pub fn replace(&mut self, id: ImTexture, texture: T) {
assert!(self.textures.contains_key(&id));
self.textures.insert(id, texture);
}
pub fn remove(&mut self, id: ImTexture) {
self.textures.remove(&id);
}
pub fn get(&self, id: ImTexture) -> Option<&T> {
self.textures.get(&id)
}
}

View File

@ -18,7 +18,7 @@ pub use drag::{
DragInt4, DragIntRange2, DragInt4, DragIntRange2,
}; };
pub use fonts::{FontGlyphRange, ImFont, ImFontAtlas, ImFontConfig}; pub use fonts::{FontGlyphRange, ImFont, ImFontAtlas, ImFontConfig};
pub use image::{ImTexture, Image}; pub use image::{ImTexture, Image, Textures};
pub use input::{ pub use input::{
InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3, InputInt4, InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3, InputInt4,
InputText, InputTextMultiline, InputText, InputTextMultiline,