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

add components_per_format, bytes_per_type #318

Merged
merged 2 commits into from
Nov 1, 2024
Merged
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
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,36 @@ pub trait HasContext: __private::Sealed {
unsafe fn max_shader_compiler_threads(&self, count: u32);
}

/// Returns number of components used by format
pub fn components_per_format(format: u32) -> usize {
match format {
RED | GREEN | BLUE => 1,
RED_INTEGER | GREEN_INTEGER | BLUE_INTEGER => 1,
ALPHA | LUMINANCE | DEPTH_COMPONENT => 1,
RG | LUMINANCE_ALPHA => 2,
RGB | BGR => 3,
RGBA | BGRA => 4,
_ => panic!("unsupported format: {:?}", format),
}
}

/// Returns number of bytes used by pixel type (in one component)
pub fn bytes_per_type(pixel_type: u32) -> usize {
// per https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_type
match pixel_type {
BYTE | UNSIGNED_BYTE => 1,
SHORT | UNSIGNED_SHORT => 2,
INT | UNSIGNED_INT => 4,
HALF_FLOAT | HALF_FLOAT_OES => 2,
FLOAT => 4,
_ => panic!("unsupported pixel type: {:?}", pixel_type),
}
}

pub fn compute_size(width: i32, height: i32, format: u32, pixel_type: u32) -> usize {
width as usize * height as usize * components_per_format(format) * bytes_per_type(pixel_type)
}

pub const ACTIVE_ATOMIC_COUNTER_BUFFERS: u32 = 0x92D9;

pub const ACTIVE_ATTRIBUTES: u32 = 0x8B89;
Expand Down
Loading