Skip to content

Commit 4932f60

Browse files
committed
[add] high level texture usages abstraction.
1 parent afb144c commit 4932f60

2 files changed

Lines changed: 99 additions & 7 deletions

File tree

crates/lambda-rs/src/render/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl RenderContextBuilder {
139139
&gpu,
140140
size,
141141
platform::surface::PresentMode::Fifo,
142-
platform::texture::TextureUsages::RENDER_ATTACHMENT,
142+
texture::TextureUsages::RENDER_ATTACHMENT.to_platform(),
143143
)
144144
.map_err(|e| {
145145
RenderContextError::SurfaceConfig(format!(
@@ -154,7 +154,7 @@ impl RenderContextBuilder {
154154
)
155155
})?;
156156
let present_mode = config.present_mode;
157-
let texture_usage = config.usage;
157+
let texture_usage = texture::TextureUsages::from_platform(config.usage);
158158

159159
// Initialize a depth texture matching the surface size.
160160
let depth_format = platform::texture::DepthFormat::Depth32Float;
@@ -213,7 +213,7 @@ pub struct RenderContext {
213213
gpu: platform::gpu::Gpu,
214214
config: platform::surface::SurfaceConfig,
215215
present_mode: platform::surface::PresentMode,
216-
texture_usage: platform::texture::TextureUsages,
216+
texture_usage: texture::TextureUsages,
217217
size: (u32, u32),
218218
depth_texture: Option<platform::texture::DepthTexture>,
219219
depth_format: platform::texture::DepthFormat,
@@ -1044,7 +1044,7 @@ impl RenderContext {
10441044
})?;
10451045

10461046
self.present_mode = config.present_mode;
1047-
self.texture_usage = config.usage;
1047+
self.texture_usage = texture::TextureUsages::from_platform(config.usage);
10481048
self.config = config;
10491049
return Ok(());
10501050
}

crates/lambda-rs/src/render/texture.rs

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,61 @@ impl DepthFormat {
3131
}
3232
}
3333

34-
#[derive(Debug, Clone, Copy)]
35-
/// Supported color texture formats for sampling.
34+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35+
/// Supported color texture formats for sampling and render targets.
3636
pub enum TextureFormat {
37+
/// 8-bit RGBA, linear (non-sRGB).
3738
Rgba8Unorm,
39+
/// 8-bit RGBA, sRGB encoded.
3840
Rgba8UnormSrgb,
41+
/// 8-bit BGRA, linear (non-sRGB). Common swapchain format.
42+
Bgra8Unorm,
43+
/// 8-bit BGRA, sRGB encoded. Common swapchain format.
44+
Bgra8UnormSrgb,
3945
}
4046

4147
impl TextureFormat {
42-
fn to_platform(self) -> platform::TextureFormat {
48+
pub(crate) fn to_platform(self) -> platform::TextureFormat {
4349
return match self {
4450
TextureFormat::Rgba8Unorm => platform::TextureFormat::RGBA8_UNORM,
4551
TextureFormat::Rgba8UnormSrgb => {
4652
platform::TextureFormat::RGBA8_UNORM_SRGB
4753
}
54+
TextureFormat::Bgra8Unorm => platform::TextureFormat::BGRA8_UNORM,
55+
TextureFormat::Bgra8UnormSrgb => {
56+
platform::TextureFormat::BGRA8_UNORM_SRGB
57+
}
4858
};
4959
}
60+
61+
pub(crate) fn from_platform(fmt: platform::TextureFormat) -> Option<Self> {
62+
if fmt == platform::TextureFormat::RGBA8_UNORM {
63+
return Some(TextureFormat::Rgba8Unorm);
64+
}
65+
if fmt == platform::TextureFormat::RGBA8_UNORM_SRGB {
66+
return Some(TextureFormat::Rgba8UnormSrgb);
67+
}
68+
if fmt == platform::TextureFormat::BGRA8_UNORM {
69+
return Some(TextureFormat::Bgra8Unorm);
70+
}
71+
if fmt == platform::TextureFormat::BGRA8_UNORM_SRGB {
72+
return Some(TextureFormat::Bgra8UnormSrgb);
73+
}
74+
return None;
75+
}
76+
77+
/// Whether this format is sRGB encoded.
78+
pub fn is_srgb(self) -> bool {
79+
return matches!(
80+
self,
81+
TextureFormat::Rgba8UnormSrgb | TextureFormat::Bgra8UnormSrgb
82+
);
83+
}
84+
85+
/// Number of bytes per pixel for this format.
86+
pub fn bytes_per_pixel(self) -> u32 {
87+
return 4;
88+
}
5089
}
5190

5291
#[derive(Debug, Clone, Copy)]
@@ -98,6 +137,59 @@ impl AddressMode {
98137
};
99138
}
100139
}
140+
141+
/// Texture usage flags.
142+
///
143+
/// Use bitwise-OR to combine flags when creating textures with multiple usages.
144+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
145+
pub struct TextureUsages(platform::TextureUsages);
146+
147+
impl TextureUsages {
148+
/// Texture can be used as a render attachment (color or depth/stencil).
149+
pub const RENDER_ATTACHMENT: TextureUsages =
150+
TextureUsages(platform::TextureUsages::RENDER_ATTACHMENT);
151+
/// Texture can be bound for sampling in shaders.
152+
pub const TEXTURE_BINDING: TextureUsages =
153+
TextureUsages(platform::TextureUsages::TEXTURE_BINDING);
154+
/// Texture can be used as the destination of a copy operation.
155+
pub const COPY_DST: TextureUsages =
156+
TextureUsages(platform::TextureUsages::COPY_DST);
157+
/// Texture can be used as the source of a copy operation.
158+
pub const COPY_SRC: TextureUsages =
159+
TextureUsages(platform::TextureUsages::COPY_SRC);
160+
161+
/// Create an empty flags set.
162+
pub const fn empty() -> Self {
163+
return TextureUsages(platform::TextureUsages::empty());
164+
}
165+
166+
pub(crate) fn to_platform(self) -> platform::TextureUsages {
167+
return self.0;
168+
}
169+
170+
pub(crate) fn from_platform(usage: platform::TextureUsages) -> Self {
171+
return TextureUsages(usage);
172+
}
173+
174+
/// Check whether this flags set contains another set.
175+
pub fn contains(self, other: TextureUsages) -> bool {
176+
return self.0.contains(other.0);
177+
}
178+
}
179+
180+
impl std::ops::BitOr for TextureUsages {
181+
type Output = TextureUsages;
182+
183+
fn bitor(self, rhs: TextureUsages) -> TextureUsages {
184+
return TextureUsages(self.0 | rhs.0);
185+
}
186+
}
187+
188+
impl std::ops::BitOrAssign for TextureUsages {
189+
fn bitor_assign(&mut self, rhs: TextureUsages) {
190+
self.0 |= rhs.0;
191+
}
192+
}
101193
#[derive(Debug, Clone)]
102194
/// High‑level texture wrapper that owns a platform texture.
103195
pub struct Texture {

0 commit comments

Comments
 (0)