From 0275d01b96824d949b46fa1b60f040d04cc2e4dd Mon Sep 17 00:00:00 2001 From: Jared Roberts Date: Thu, 6 Oct 2016 18:20:08 -0700 Subject: [PATCH] use texture array instead of atlas for tiles --- src/scenes/shader/tile_frag.glsl | 10 ++++++---- src/scenes/world.rs | 4 ++-- src/tile.rs | 13 +++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/scenes/shader/tile_frag.glsl b/src/scenes/shader/tile_frag.glsl index 51b0f88..650fa52 100644 --- a/src/scenes/shader/tile_frag.glsl +++ b/src/scenes/shader/tile_frag.glsl @@ -1,9 +1,11 @@ #version 150 +#define MILLIS_PER_TILE 4000u + in vec2 v_uv; flat in uint v_tileidx; out vec4 pixcolor; -uniform sampler2D t_atlas; +uniform sampler2DArray t_tiles; uniform b_constants { uvec4 anim; float R1; @@ -19,8 +21,8 @@ uniform b_locals { void main() { vec2 anim_uv = v_uv; if (v_tileidx < 128u && bool(anim[v_tileidx / 32u] & 1u << v_tileidx % 32u)) { - anim_uv = vec2(v_uv.x, float((uint(v_uv.y * 1000.0) + millis / 4u) % 1000u) / 1000.0); + anim_uv = vec2(v_uv.x, v_uv.y + float(millis % MILLIS_PER_TILE) / MILLIS_PER_TILE); } - vec2 uv = vec2(anim_uv.x, float(v_tileidx) / 256.0 + (1.0 - anim_uv.y) / 256.0); - pixcolor = texture(t_atlas, uv); + + pixcolor = texture(t_tiles, vec3(anim_uv.x, 1.0 - anim_uv.y, v_tileidx)); } diff --git a/src/scenes/world.rs b/src/scenes/world.rs index c76aabb..d059785 100644 --- a/src/scenes/world.rs +++ b/src/scenes/world.rs @@ -52,7 +52,7 @@ gfx_defines! { trans: gfx::ConstantBuffer<::view::Trans> = "b_trans", constants: gfx::ConstantBuffer = "b_constants", locals: gfx::ConstantBuffer = "b_locals", - atlas: gfx::TextureSampler<[f32; 4]> = "t_atlas", + atlas: gfx::TextureSampler<[f32; 4]> = "t_tiles", pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor", depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE, } @@ -157,7 +157,7 @@ impl> WorldScene { locals: factory.create_constant_buffer(1), atlas: tile::get_tiles::<_, _, view::ColorFormat>(factory), sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Scale, - tex::WrapMode::Clamp)), + tex::WrapMode::Tile)), f: PhantomData, vbuf: vertex_buffer, diff --git a/src/tile.rs b/src/tile.rs index 3c4aa1c..882003c 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -22,12 +22,13 @@ pub fn get_tiles(factory: &mut F) -> (//gfx::handle::Texture(tex::Kind::D2(16, 16 * 256, - tex::AaMode::Single), - &[&ega_page.data]) + let mut ega_bytes = Vec::new(); + file.read_to_end(&mut ega_bytes).expect("Read tiles file"); + let ega_page = ega::decode(&ega_bytes, ega::Compression::UNCOMPRESSED, ega::Tiling::TILED(16)); + let tiles: Vec<&[u8]> = ega_page.iter().collect(); + let tex = factory.create_texture_const_u8::(tex::Kind::D2Array(16, 16, 256, + tex::AaMode::Single), + &tiles) .expect("create tile texture"); tex.1 }