Skip to content

Commit

Permalink
Support generic depth format
Browse files Browse the repository at this point in the history
Up version to for release
Resolves #8
  • Loading branch information
alexheretic committed Sep 26, 2017
1 parent ebd2d1b commit ec77f1d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 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 = "gfx_glyph"
version = "0.4.1"
version = "0.4.2"
authors = ["Alex Butler <alexheretic@gmail.com>"]
description = "Fast GPU cached text rendering using gfx-rs & rusttype"
repository = "https://github.com/alexheretic/gfx-glyph"
Expand Down
2 changes: 1 addition & 1 deletion examples/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
.with_dimensions(1024, 576);
let context = glutin::ContextBuilder::new();
let (window, mut device, mut factory, mut main_color, mut main_depth) =
gfx_window_glutin::init::<format::Srgba8, format::Depth>(window_builder, context, &events_loop);
gfx_window_glutin::init::<format::Srgba8, format::DepthStencil>(window_builder, context, &events_loop);

let mut glyph_brush = gfx_glyph::GlyphBrushBuilder::using_font(include_bytes!("Arial Unicode.ttf") as &[u8])
.initial_cache_size((1024, 1024))
Expand Down
76 changes: 57 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ const IDENTITY_MATRIX4: [[f32; 4]; 4] = [
// Inner module used to avoid public access
mod gfx_structs {
use super::*;
use gfx::*;
use gfx::pso::*;
use gfx_core::pso;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RawDepthTarget;

impl<'a> DataLink<'a> for RawDepthTarget {
type Init = (format::Format, state::Depth);
fn new() -> Self { RawDepthTarget }
fn is_active(&self) -> bool { true }
fn link_depth_stencil(&mut self, init: &Self::Init) -> Option<pso::DepthStencilDesc> {
Some((init.0, init.1.into()))
}
}

impl<R: Resources> DataBind<R> for RawDepthTarget {
type Data = handle::RawDepthStencilView<R>;
fn bind_to(&self,
out: &mut RawDataSet<R>,
data: &Self::Data,
man: &mut handle::Manager<R>,
_: &mut AccessInfo<R>) {
let dsv = data;
out.pixel_targets.add_depth_stencil(man.ref_dsv(dsv), true, false, dsv.get_dimensions());
}
}

gfx_defines!{
vertex GlyphVertex {
Expand All @@ -132,21 +159,26 @@ mod gfx_structs {
}

gfx_pipeline_base!( glyph_pipe {
vbuf: gfx::VertexBuffer<GlyphVertex>,
font_tex: gfx::TextureSampler<TexFormView>,
transform: gfx::Global<[[f32; 4]; 4]>,
out: gfx::RawRenderTarget,
out_depth: gfx::DepthTarget<gfx::format::Depth>,
vbuf: VertexBuffer<GlyphVertex>,
font_tex: TextureSampler<TexFormView>,
transform: Global<[[f32; 4]; 4]>,
out: RawRenderTarget,
out_depth: RawDepthTarget,
});

impl<'a> glyph_pipe::Init<'a> {
pub fn new(format: gfx::format::Format, depth_test: gfx::state::Depth) -> Self {
pub fn new(
color_format: format::Format,
depth_format: format::Format,
depth_test: state::Depth)
-> Self
{
glyph_pipe::Init {
vbuf: (),
font_tex: "font_tex",
transform: "transform",
out: ("Target0", format, state::ColorMask::all(), Some(preset::blend::ALPHA)),
out_depth: depth_test,
out: ("Target0", color_format, state::ColorMask::all(), Some(preset::blend::ALPHA)),
out_depth: (depth_format, depth_test),
}
}
}
Expand Down Expand Up @@ -339,30 +371,32 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
/// Draws all queued sections onto a render target, applying a position transform (e.g.
/// a projection).
/// See [`queue`](struct.GlyphBrush.html#method.queue).
pub fn draw_queued<C, T>(
pub fn draw_queued<C, T, D>(
&mut self,
encoder: &mut gfx::Encoder<R, C>,
target: &gfx::handle::RenderTargetView<R, T>,
depth_target: &gfx::handle::DepthStencilView<R, gfx::format::Depth>)
depth_target: &gfx::handle::DepthStencilView<R, D>)
-> Result<(), String>
where C: gfx::CommandBuffer<R>,
T: format::RenderFormat,
D: format::DepthFormat,
{
self.draw_queued_with_transform(IDENTITY_MATRIX4, encoder, target, depth_target)
}

/// Draws all queued sections onto a render target, applying a position transform (e.g.
/// a projection).
/// See [`queue`](struct.GlyphBrush.html#method.queue).
pub fn draw_queued_with_transform<C, T>(
pub fn draw_queued_with_transform<C, T, D>(
&mut self,
transform: [[f32; 4]; 4],
mut encoder: &mut gfx::Encoder<R, C>,
target: &gfx::handle::RenderTargetView<R, T>,
depth_target: &gfx::handle::DepthStencilView<R, gfx::format::Depth>)
depth_target: &gfx::handle::DepthStencilView<R, D>)
-> Result<(), String>
where C: gfx::CommandBuffer<R>,
T: format::RenderFormat,
D: format::DepthFormat,
{
let start = Instant::now();

Expand Down Expand Up @@ -458,9 +492,9 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
let mut cache = self.draw_cache.take().unwrap();
cache.pipe_data.vbuf = vbuf;
cache.pipe_data.out = target.raw().clone();
cache.pipe_data.out_depth = depth_target.clone();
cache.pipe_data.out_depth = depth_target.raw().clone();
if cache.pso.0 != T::get_format() {
cache.pso = (T::get_format(), self.pso_using(T::get_format()));
cache.pso = (T::get_format(), self.pso_using(T::get_format(), D::get_format()));
}
cache.slice = slice;
cache.last_text_state = current_text_state;
Expand All @@ -481,10 +515,10 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
font_tex: (self.font_cache_tex.1.clone(), sampler),
transform: transform,
out: target.raw().clone(),
out_depth: depth_target.clone(),
out_depth: depth_target.raw().clone(),
}
},
pso: (T::get_format(), self.pso_using(T::get_format())),
pso: (T::get_format(), self.pso_using(T::get_format(), D::get_format())),
slice,
last_text_state: 0,
texture_updated: false,
Expand Down Expand Up @@ -528,7 +562,7 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
let mut active = HashSet::with_capacity(
self.section_buffer.len() + self.keep_in_cache.len()
);

for h in self.section_buffer.drain(..) {
active.insert(h);
}
Expand All @@ -544,11 +578,15 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
}
}

fn pso_using(&mut self, format: gfx::format::Format) -> gfx::PipelineState<R, glyph_pipe::Meta> {
fn pso_using(
&mut self,
color_format: gfx::format::Format,
depth_format: gfx::format::Format) -> gfx::PipelineState<R, glyph_pipe::Meta>
{
self.factory.create_pipeline_simple(
include_bytes!("shader/vert.glsl"),
include_bytes!("shader/frag.glsl"),
glyph_pipe::Init::new(format, self.depth_test)).unwrap()
glyph_pipe::Init::new(color_format, depth_format, self.depth_test)).unwrap()
}
}

Expand Down

0 comments on commit ec77f1d

Please sign in to comment.