Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ImageFormat::Invalid #2317

Merged
merged 3 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions webrender/src/debug_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@ impl DebugRenderer {
let line_vao = device.create_vao(&DESC_COLOR);
let tri_vao = device.create_vao(&DESC_COLOR);

let mut font_texture = device.create_texture(TextureTarget::Array);
let mut font_texture = device.create_texture(TextureTarget::Array, ImageFormat::R8);
device.init_texture(
&mut font_texture,
debug_font_data::BMP_WIDTH,
debug_font_data::BMP_HEIGHT,
ImageFormat::R8,
TextureFilter::Linear,
None,
1,
Expand Down
78 changes: 24 additions & 54 deletions webrender/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ use std::ptr;
use std::rc::Rc;
use std::thread;

// Apparently, in some cases calling `glTexImage3D` with
// similar parameters that the texture already has confuses
// Angle when running with optimizations.
const WORK_AROUND_TEX_IMAGE: bool = cfg!(windows);

#[derive(Debug, Copy, Clone, PartialEq, Ord, Eq, PartialOrd)]
#[cfg_attr(feature = "capture", derive(Deserialize, Serialize))]
Expand Down Expand Up @@ -130,10 +126,8 @@ pub enum UploadMethod {

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ReadPixelsFormat {
R8,
Standard(ImageFormat),
Rgba8,
Bgra8,
Rgba32F,
}

pub fn get_gl_format_bgra(gl: &gl::Gl) -> gl::GLuint {
Expand Down Expand Up @@ -472,16 +466,6 @@ impl Texture {
self.render_target.clone()
}

pub fn get_bpp(&self) -> u32 {
match self.format {
ImageFormat::R8 => 1,
ImageFormat::BGRA8 => 4,
ImageFormat::RG8 => 2,
ImageFormat::RGBAF32 => 16,
ImageFormat::Invalid => unreachable!(),
}
}

pub fn has_depth(&self) -> bool {
self.depth_rb.is_some()
}
Expand Down Expand Up @@ -941,14 +925,16 @@ impl Device {
}
}

pub fn create_texture(&mut self, target: TextureTarget) -> Texture {
pub fn create_texture(
&mut self, target: TextureTarget, format: ImageFormat,
) -> Texture {
Texture {
id: self.gl.gen_textures(1)[0],
target: target.to_gl_target(),
width: 0,
height: 0,
layer_count: 0,
format: ImageFormat::Invalid,
format,
filter: TextureFilter::Nearest,
render_target: None,
fbo_ids: vec![],
Expand Down Expand Up @@ -994,7 +980,7 @@ impl Device {

self.bind_texture(DEFAULT_TEXTURE, texture);
self.set_texture_parameters(texture.target, texture.filter);
self.update_target_storage(texture, &rt_info, true, false, None);
self.update_target_storage(texture, &rt_info, true, None);

let rect = DeviceIntRect::new(DeviceIntPoint::zero(), old_size.to_i32());
for (read_fbo, &draw_fbo) in old_fbos.into_iter().zip(&texture.fbo_ids) {
Expand All @@ -1012,19 +998,15 @@ impl Device {
texture: &mut Texture,
width: u32,
height: u32,
format: ImageFormat,
filter: TextureFilter,
render_target: Option<RenderTargetInfo>,
layer_count: i32,

pixels: Option<&[u8]>,
) {
debug_assert!(self.inside_frame);

let is_resized = texture.width != width || texture.height != height;
let is_format_changed = texture.format != format;

texture.format = format;
texture.width = width;
texture.height = height;
texture.filter = filter;
Expand All @@ -1036,7 +1018,7 @@ impl Device {

match render_target {
Some(info) => {
self.update_target_storage(texture, &info, is_resized, is_format_changed, pixels);
self.update_target_storage(texture, &info, is_resized, pixels);
}
None => {
self.update_texture_storage(texture, pixels);
Expand All @@ -1050,32 +1032,17 @@ impl Device {
texture: &mut Texture,
rt_info: &RenderTargetInfo,
is_resized: bool,
is_format_changed: bool,
pixels: Option<&[u8]>,
) {
assert!(texture.layer_count > 0);
assert!(texture.layer_count > 0 || texture.width + texture.height == 0);

let needed_layer_count = texture.layer_count - texture.fbo_ids.len() as i32;
let allocate_color = needed_layer_count != 0 ||
is_resized || is_format_changed || pixels.is_some();
let allocate_color = needed_layer_count != 0 || is_resized || pixels.is_some();

if allocate_color {
let desc = gl_describe_format(self.gl(), texture.format);
match texture.target {
gl::TEXTURE_2D_ARRAY => {
if WORK_AROUND_TEX_IMAGE {
// reset the contents before resizing
self.gl.tex_image_3d(
texture.target,
0,
gl::RGBA32F as _,
2, 2, 1,
0,
gl::RGBA,
gl::FLOAT,
None,
)
}
self.gl.tex_image_3d(
texture.target,
0,
Expand Down Expand Up @@ -1269,7 +1236,7 @@ impl Device {
pub fn free_texture_storage(&mut self, texture: &mut Texture) {
debug_assert!(self.inside_frame);

if texture.format == ImageFormat::Invalid {
if texture.width + texture.height == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me do a double take, but I guess it works :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I see, the only case it tries to handle is when the texture wasn't ever given contents (leaving the size to 0 and the format to Invalid).

return;
}

Expand All @@ -1291,7 +1258,6 @@ impl Device {
self.gl.delete_framebuffers(&fbo_ids[..]);
}

texture.format = ImageFormat::Invalid;
texture.width = 0;
texture.height = 0;
texture.layer_count = 0;
Expand Down Expand Up @@ -1552,13 +1518,19 @@ impl Device {
format: ReadPixelsFormat,
output: &mut [u8],
) {
let (gl_format, gl_type, pixel_size) = match format {
ReadPixelsFormat::R8 => (gl::RED, gl::UNSIGNED_BYTE, 1),
ReadPixelsFormat::Rgba8 => (gl::RGBA, gl::UNSIGNED_BYTE, 4),
ReadPixelsFormat::Bgra8 => (get_gl_format_bgra(self.gl()), gl::UNSIGNED_BYTE, 4),
ReadPixelsFormat::Rgba32F => (gl::RGBA, gl::FLOAT, 16),
let (bytes_per_pixel, desc) = match format {
ReadPixelsFormat::Standard(imf) => {
(imf.bytes_per_pixel(), gl_describe_format(self.gl(), imf))
}
ReadPixelsFormat::Rgba8 => {
(4, FormatDesc {
external: gl::RGBA,
internal: gl::RGBA8 as _,
pixel_type: gl::UNSIGNED_BYTE,
})
}
};
let size_in_bytes = (pixel_size * rect.size.width * rect.size.height) as usize;
let size_in_bytes = (bytes_per_pixel * rect.size.width * rect.size.height) as usize;
assert_eq!(output.len(), size_in_bytes);

self.gl.flush();
Expand All @@ -1567,8 +1539,8 @@ impl Device {
rect.origin.y as _,
rect.size.width as _,
rect.size.height as _,
gl_format,
gl_type,
desc.external,
desc.pixel_type,
output,
);
}
Expand Down Expand Up @@ -2066,7 +2038,6 @@ fn gl_describe_format(gl: &gl::Gl, format: ImageFormat) -> FormatDesc {
external: gl::RG,
pixel_type: gl::UNSIGNED_BYTE,
},
ImageFormat::Invalid => unreachable!(),
}
}

Expand Down Expand Up @@ -2180,7 +2151,6 @@ impl<'a> UploadTarget<'a> {
ImageFormat::BGRA8 => (get_gl_format_bgra(self.gl), 4, gl::UNSIGNED_BYTE),
ImageFormat::RG8 => (gl::RG, 2, gl::UNSIGNED_BYTE),
ImageFormat::RGBAF32 => (gl::RGBA, 16, gl::FLOAT),
ImageFormat::Invalid => unreachable!(),
};

let row_length = match chunk.stride {
Expand Down
Loading