Report rather than swallowing bad texture ids

This commit is contained in:
Tad Hardesty 2018-09-19 22:34:03 -07:00
parent a2b69395d1
commit d90d377e70
2 changed files with 12 additions and 15 deletions

View File

@ -8,7 +8,7 @@ use gfx::texture::{FilterMethod, SamplerInfo, WrapMode};
use gfx::traits::FactoryExt; use gfx::traits::FactoryExt;
use gfx::{CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice}; use gfx::{CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice};
use gfx::pso::{PipelineData, PipelineState}; use gfx::pso::{PipelineData, PipelineState};
use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures}; use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures, ImTexture};
pub type RendererResult<T> = Result<T, RendererError>; pub type RendererResult<T> = Result<T, RendererError>;
@ -18,6 +18,7 @@ pub enum RendererError {
Buffer(gfx::buffer::CreationError), Buffer(gfx::buffer::CreationError),
Pipeline(gfx::PipelineStateError<String>), Pipeline(gfx::PipelineStateError<String>),
Combined(gfx::CombinedError), Combined(gfx::CombinedError),
BadTexture(ImTexture),
} }
impl From<gfx::UpdateError<usize>> for RendererError { impl From<gfx::UpdateError<usize>> for RendererError {
@ -136,7 +137,6 @@ 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: Textures<Texture<R>>, textures: Textures<Texture<R>>,
backup_texture: Texture<R>,
} }
impl<R: Resources> Renderer<R> { impl<R: Resources> Renderer<R> {
@ -175,7 +175,7 @@ impl<R: Resources> Renderer<R> {
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 = Textures::new(); let mut textures = Textures::new();
imgui.set_font_texture_id(textures.insert(pair.clone())); imgui.set_font_texture_id(textures.insert(pair));
let slice = Slice { let slice = Slice {
start: 0, start: 0,
@ -193,7 +193,6 @@ impl<R: Resources> Renderer<R> {
}, },
index_buffer: index_buffer, index_buffer: index_buffer,
textures: textures, textures: textures,
backup_texture: pair,
}) })
} }
@ -254,7 +253,9 @@ 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 {
let tex = self.textures.get(cmd.texture_id.into()).unwrap_or(&self.backup_texture); let texture_id = cmd.texture_id.into();
let tex = self.textures.get(texture_id)
.ok_or_else(|| RendererError::BadTexture(texture_id))?;
self.bundle.slice.end = self.bundle.slice.start + cmd.elem_count; self.bundle.slice.end = self.bundle.slice.start + cmd.elem_count;
let scissor = Rect { let scissor = Rect {

View File

@ -8,7 +8,7 @@ use glium::program;
use glium::texture; use glium::texture;
use glium::vertex; use glium::vertex;
use glium::{DrawError, IndexBuffer, Program, Surface, Texture2d, VertexBuffer}; use glium::{DrawError, IndexBuffer, Program, Surface, Texture2d, VertexBuffer};
use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures}; use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures, ImTexture};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
@ -22,6 +22,7 @@ pub enum RendererError {
Program(program::ProgramChooserCreationError), Program(program::ProgramChooserCreationError),
Texture(texture::TextureCreationError), Texture(texture::TextureCreationError),
Draw(DrawError), Draw(DrawError),
BadTexture(ImTexture),
} }
impl fmt::Display for RendererError { impl fmt::Display for RendererError {
@ -33,6 +34,7 @@ impl fmt::Display for RendererError {
Program(ref e) => write!(f, "Program creation failed: {}", e), Program(ref e) => write!(f, "Program creation failed: {}", e),
Texture(_) => write!(f, "Texture creation failed"), Texture(_) => write!(f, "Texture creation failed"),
Draw(ref e) => write!(f, "Drawing failed: {}", e), Draw(ref e) => write!(f, "Drawing failed: {}", e),
BadTexture(ref t) => write!(f, "Bad texture ID: {}", t.id()),
} }
} }
} }
@ -134,15 +136,9 @@ impl Renderer {
let mut idx_start = 0; let mut idx_start = 0;
for cmd in draw_list.cmd_buffer { for cmd in draw_list.cmd_buffer {
let texture = match self.device_objects.textures.get(cmd.texture_id.into()) { let texture_id = cmd.texture_id.into();
Some(tex) => tex, let texture = self.device_objects.textures.get(texture_id)
// if an invalid ID is supplied, fall back to the font .ok_or_else(|| RendererError::BadTexture(texture_id))?;
None => match self.device_objects.textures.get(0.into()) {
Some(tex) => tex,
// if the font is missing, which should never happen, skip
None => continue,
}
};
let idx_end = idx_start + cmd.elem_count as usize; let idx_end = idx_start + cmd.elem_count as usize;