2019-11-17 22:41:00 +00:00
|
|
|
use super::{gfx_backend, RenderError};
|
2019-04-29 20:37:19 +00:00
|
|
|
use gfx::{self, traits::Factory};
|
|
|
|
use image::{DynamicImage, GenericImageView};
|
2019-02-28 00:37:47 +00:00
|
|
|
use vek::Vec2;
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
type DefaultShaderFormat = (gfx::format::R8_G8_B8_A8, gfx::format::Srgb);
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
/// Represents an image that has been uploaded to the GPU.
|
2019-11-17 22:41:00 +00:00
|
|
|
pub struct Texture<F: gfx::format::Formatted = DefaultShaderFormat>
|
|
|
|
where
|
|
|
|
F::Surface: gfx::format::TextureSurface,
|
|
|
|
F::Channel: gfx::format::TextureChannel,
|
|
|
|
<F::Surface as gfx::format::SurfaceTyped>::DataType: Copy,
|
|
|
|
{
|
|
|
|
pub tex: gfx::handle::Texture<gfx_backend::Resources, <F as gfx::format::Formatted>::Surface>,
|
2019-04-29 20:37:19 +00:00
|
|
|
pub srv: gfx::handle::ShaderResourceView<
|
|
|
|
gfx_backend::Resources,
|
2019-11-17 22:41:00 +00:00
|
|
|
<F as gfx::format::Formatted>::View,
|
2019-04-29 20:37:19 +00:00
|
|
|
>,
|
2019-01-30 12:11:34 +00:00
|
|
|
pub sampler: gfx::handle::Sampler<gfx_backend::Resources>,
|
|
|
|
}
|
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
impl<F: gfx::format::Formatted> Texture<F>
|
|
|
|
where
|
|
|
|
F::Surface: gfx::format::TextureSurface,
|
|
|
|
F::Channel: gfx::format::TextureChannel,
|
|
|
|
<F::Surface as gfx::format::SurfaceTyped>::DataType: Copy,
|
|
|
|
{
|
2019-01-30 12:11:34 +00:00
|
|
|
pub fn new(
|
|
|
|
factory: &mut gfx_backend::Factory,
|
|
|
|
image: &DynamicImage,
|
2019-10-17 16:11:55 +00:00
|
|
|
filter_method: Option<gfx::texture::FilterMethod>,
|
|
|
|
wrap_mode: Option<gfx::texture::WrapMode>,
|
2019-01-30 12:11:34 +00:00
|
|
|
) -> Result<Self, RenderError> {
|
2019-04-29 20:37:19 +00:00
|
|
|
let (tex, srv) = factory
|
2019-11-17 22:41:00 +00:00
|
|
|
.create_texture_immutable_u8::<F>(
|
2019-04-29 20:37:19 +00:00
|
|
|
gfx::texture::Kind::D2(
|
|
|
|
image.width() as u16,
|
|
|
|
image.height() as u16,
|
|
|
|
gfx::texture::AaMode::Single,
|
|
|
|
),
|
|
|
|
gfx::texture::Mipmap::Provided,
|
2019-11-17 22:41:00 +00:00
|
|
|
&[&image.raw_pixels()],
|
2019-04-29 20:37:19 +00:00
|
|
|
)
|
2020-06-11 18:44:03 +00:00
|
|
|
.map_err(RenderError::CombinedError)?;
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
tex,
|
|
|
|
srv,
|
|
|
|
sampler: factory.create_sampler(gfx::texture::SamplerInfo::new(
|
2019-10-17 16:11:55 +00:00
|
|
|
filter_method.unwrap_or(gfx::texture::FilterMethod::Scale),
|
|
|
|
wrap_mode.unwrap_or(gfx::texture::WrapMode::Clamp),
|
2019-01-30 12:11:34 +00:00
|
|
|
)),
|
|
|
|
})
|
|
|
|
}
|
2019-10-17 16:11:55 +00:00
|
|
|
|
2019-02-23 02:41:52 +00:00
|
|
|
pub fn new_dynamic(
|
|
|
|
factory: &mut gfx_backend::Factory,
|
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
) -> Result<Self, RenderError> {
|
|
|
|
let tex = factory.create_texture(
|
|
|
|
gfx::texture::Kind::D2(
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
gfx::texture::AaMode::Single,
|
|
|
|
),
|
|
|
|
1 as gfx::texture::Level,
|
|
|
|
gfx::memory::Bind::SHADER_RESOURCE,
|
|
|
|
gfx::memory::Usage::Dynamic,
|
2019-11-17 22:41:00 +00:00
|
|
|
Some(<<F as gfx::format::Formatted>::Channel as gfx::format::ChannelTyped>::get_channel_type()),
|
2019-02-23 02:41:52 +00:00
|
|
|
)
|
|
|
|
.map_err(|err| RenderError::CombinedError(gfx::CombinedError::Texture(err)))?;
|
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
let srv = factory
|
2019-11-17 22:41:00 +00:00
|
|
|
.view_texture_as_shader_resource::<F>(&tex, (0, 0), gfx::format::Swizzle::new())
|
2019-02-23 02:41:52 +00:00
|
|
|
.map_err(|err| RenderError::CombinedError(gfx::CombinedError::Resource(err)))?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
tex,
|
|
|
|
srv,
|
|
|
|
sampler: factory.create_sampler(gfx::texture::SamplerInfo::new(
|
2019-04-20 12:59:26 +00:00
|
|
|
gfx::texture::FilterMethod::Scale,
|
2019-02-23 02:41:52 +00:00
|
|
|
gfx::texture::WrapMode::Clamp,
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Update a texture with the given data (used for updating the glyph cache
|
|
|
|
/// texture).
|
2020-06-11 18:44:03 +00:00
|
|
|
|
2019-02-23 02:41:52 +00:00
|
|
|
pub fn update(
|
|
|
|
&self,
|
|
|
|
encoder: &mut gfx::Encoder<gfx_backend::Resources, gfx_backend::CommandBuffer>,
|
|
|
|
offset: [u16; 2],
|
|
|
|
size: [u16; 2],
|
2019-11-17 22:41:00 +00:00
|
|
|
data: &[<F::Surface as gfx::format::SurfaceTyped>::DataType],
|
2019-02-23 02:41:52 +00:00
|
|
|
) -> Result<(), RenderError> {
|
|
|
|
let info = gfx::texture::ImageInfoCommon {
|
|
|
|
xoffset: offset[0],
|
|
|
|
yoffset: offset[1],
|
|
|
|
zoffset: 0,
|
|
|
|
width: size[0],
|
|
|
|
height: size[1],
|
|
|
|
depth: 0,
|
|
|
|
format: (),
|
|
|
|
mipmap: 0,
|
|
|
|
};
|
|
|
|
encoder
|
2019-11-17 22:41:00 +00:00
|
|
|
.update_texture::<<F as gfx::format::Formatted>::Surface, F>(
|
2019-04-29 20:37:19 +00:00
|
|
|
&self.tex, None, info, data,
|
|
|
|
)
|
2020-06-11 18:44:03 +00:00
|
|
|
.map_err(RenderError::TexUpdateError)
|
2019-02-23 02:41:52 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Get dimensions of the represented image.
|
2019-02-28 00:37:47 +00:00
|
|
|
pub fn get_dimensions(&self) -> Vec2<u16> {
|
|
|
|
let (w, h, ..) = self.tex.get_info().kind.get_dimensions();
|
|
|
|
Vec2::new(w, h)
|
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|