Skip to content

Commit 8aadd36

Browse files
committed
[add] ColorAttachmentTexture and DepthTextures to the high level API.
1 parent 53c8a9f commit 8aadd36

4 files changed

Lines changed: 243 additions & 51 deletions

File tree

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

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,7 @@ impl RenderContextBuilder {
161161
let texture_usage = config.usage;
162162

163163
// Initialize a depth texture matching the surface size.
164-
let depth_format = platform::texture::DepthFormat::Depth32Float;
165-
let depth_texture = Some(
166-
platform::texture::DepthTextureBuilder::new()
167-
.with_size(size.0.max(1), size.1.max(1))
168-
.with_format(depth_format)
169-
.with_label("lambda-depth")
170-
.build(&gpu),
171-
);
164+
let depth_format = texture::DepthFormat::Depth32Float;
172165

173166
return Ok(RenderContext {
174167
label: name,
@@ -179,7 +172,7 @@ impl RenderContextBuilder {
179172
present_mode,
180173
texture_usage,
181174
size,
182-
depth_texture,
175+
depth_texture: None,
183176
depth_format,
184177
depth_sample_count: 1,
185178
msaa_color: None,
@@ -219,10 +212,10 @@ pub struct RenderContext {
219212
present_mode: surface::PresentMode,
220213
texture_usage: texture::TextureUsages,
221214
size: (u32, u32),
222-
depth_texture: Option<platform::texture::DepthTexture>,
223-
depth_format: platform::texture::DepthFormat,
215+
depth_texture: Option<texture::DepthTexture>,
216+
depth_format: texture::DepthFormat,
224217
depth_sample_count: u32,
225-
msaa_color: Option<platform::texture::ColorAttachmentTexture>,
218+
msaa_color: Option<texture::ColorAttachmentTexture>,
226219
msaa_sample_count: u32,
227220
render_passes: Vec<RenderPassDesc>,
228221
render_pipelines: Vec<RenderPipeline>,
@@ -319,12 +312,12 @@ impl RenderContext {
319312

320313
// Recreate depth texture to match new size.
321314
self.depth_texture = Some(
322-
platform::texture::DepthTextureBuilder::new()
315+
texture::DepthTextureBuilder::new()
323316
.with_size(self.size.0.max(1), self.size.1.max(1))
324317
.with_format(self.depth_format)
325318
.with_sample_count(self.depth_sample_count)
326319
.with_label("lambda-depth")
327-
.build(self.gpu()),
320+
.build(self),
328321
);
329322
// Drop MSAA color target so it is rebuilt on demand with the new size.
330323
self.msaa_color = None;
@@ -352,7 +345,7 @@ impl RenderContext {
352345
return self.config.format;
353346
}
354347

355-
pub(crate) fn depth_format(&self) -> platform::texture::DepthFormat {
348+
pub(crate) fn depth_format(&self) -> texture::DepthFormat {
356349
return self.depth_format;
357350
}
358351

@@ -368,12 +361,12 @@ impl RenderContext {
368361

369362
pub(crate) fn supports_depth_sample_count(
370363
&self,
371-
format: platform::texture::DepthFormat,
364+
format: texture::DepthFormat,
372365
sample_count: u32,
373366
) -> bool {
374367
return self
375368
.gpu
376-
.supports_sample_count_for_depth(format, sample_count);
369+
.supports_sample_count_for_depth(format.to_platform(), sample_count);
377370
}
378371

379372
/// Device limit: maximum bytes that can be bound for a single uniform buffer binding.
@@ -409,21 +402,19 @@ impl RenderContext {
409402
fn ensure_msaa_color_texture(
410403
&mut self,
411404
sample_count: u32,
412-
) -> platform::surface::TextureViewRef<'_> {
405+
) -> surface::TextureView<'_> {
413406
let need_recreate = match &self.msaa_color {
414407
Some(_) => self.msaa_sample_count != sample_count,
415408
None => true,
416409
};
417410

418411
if need_recreate {
419412
self.msaa_color = Some(
420-
platform::texture::ColorAttachmentTextureBuilder::new(
421-
self.config.format.to_platform(),
422-
)
423-
.with_size(self.size.0.max(1), self.size.1.max(1))
424-
.with_sample_count(sample_count)
425-
.with_label("lambda-msaa-color")
426-
.build(self.gpu()),
413+
texture::ColorAttachmentTextureBuilder::new(self.config.format)
414+
.with_size(self.size.0.max(1), self.size.1.max(1))
415+
.with_sample_count(sample_count)
416+
.with_label("lambda-msaa-color")
417+
.build(self),
427418
);
428419
self.msaa_sample_count = sample_count;
429420
}
@@ -520,10 +511,7 @@ impl RenderContext {
520511
// Create color attachments for the surface pass. The MSAA view is
521512
// retrieved here after the mutable borrow for texture creation ends.
522513
let msaa_view = if sample_count > 1 {
523-
self
524-
.msaa_color
525-
.as_ref()
526-
.map(|t| surface::TextureView::from_platform(t.view_ref()))
514+
self.msaa_color.as_ref().map(|t| t.view_ref())
527515
} else {
528516
None
529517
};
@@ -547,8 +535,7 @@ impl RenderContext {
547535

548536
// If stencil is requested on the pass, ensure we use a stencil-capable format.
549537
if pass.stencil_operations().is_some()
550-
&& self.depth_format
551-
!= platform::texture::DepthFormat::Depth24PlusStencil8
538+
&& self.depth_format != texture::DepthFormat::Depth24PlusStencil8
552539
{
553540
#[cfg(any(
554541
debug_assertions,
@@ -558,8 +545,7 @@ impl RenderContext {
558545
"Render pass has stencil ops but depth format {:?} lacks stencil; upgrading to Depth24PlusStencil8",
559546
self.depth_format
560547
);
561-
self.depth_format =
562-
platform::texture::DepthFormat::Depth24PlusStencil8;
548+
self.depth_format = texture::DepthFormat::Depth24PlusStencil8;
563549
}
564550

565551
let format_mismatch = self
@@ -573,12 +559,12 @@ impl RenderContext {
573559
|| format_mismatch
574560
{
575561
self.depth_texture = Some(
576-
platform::texture::DepthTextureBuilder::new()
562+
texture::DepthTextureBuilder::new()
577563
.with_size(self.size.0.max(1), self.size.1.max(1))
578564
.with_format(self.depth_format)
579565
.with_sample_count(desired_samples)
580566
.with_label("lambda-depth")
581-
.build(self.gpu()),
567+
.build(self),
582568
);
583569
self.depth_sample_count = desired_samples;
584570
}
@@ -587,7 +573,7 @@ impl RenderContext {
587573
.depth_texture
588574
.as_ref()
589575
.expect("depth texture should be present")
590-
.view_ref();
576+
.platform_view_ref();
591577

592578
// Map depth operations when explicitly provided; leave depth
593579
// untouched for stencil-only passes.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,17 @@ impl RenderPipelineBuilder {
580580
// Derive the pass attachment depth format from pass configuration.
581581
let pass_has_stencil = _render_pass.stencil_operations().is_some();
582582
let pass_depth_format = if pass_has_stencil {
583-
platform_texture::DepthFormat::Depth24PlusStencil8
583+
texture::DepthFormat::Depth24PlusStencil8
584584
} else {
585585
render_context.depth_format()
586586
};
587587

588588
// Align the pipeline depth format with the pass attachment format to
589589
// avoid hidden global state on the render context. When formats differ,
590590
// prefer the pass attachment format and log for easier debugging.
591-
let final_depth_format = if requested_depth_format != pass_depth_format {
591+
let final_depth_format = if requested_depth_format
592+
!= pass_depth_format.to_platform()
593+
{
592594
#[cfg(any(
593595
debug_assertions,
594596
feature = "render-validation-depth",
@@ -599,9 +601,9 @@ impl RenderPipelineBuilder {
599601
requested_depth_format,
600602
pass_depth_format
601603
);
602-
pass_depth_format
604+
pass_depth_format.to_platform()
603605
} else {
604-
pass_depth_format
606+
pass_depth_format.to_platform()
605607
};
606608

607609
rp_builder = rp_builder.with_depth_stencil(final_depth_format);

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use lambda_platform::wgpu as platform;
99
use logging;
1010

11-
use super::RenderContext;
11+
use super::{
12+
texture,
13+
RenderContext,
14+
};
1215
use crate::render::validation;
1316

1417
/// Color load operation for the first color attachment.
@@ -303,13 +306,13 @@ impl RenderPassBuilder {
303306
&self,
304307
sample_count: u32,
305308
surface_format: platform::texture::TextureFormat,
306-
depth_format: platform::texture::DepthFormat,
309+
depth_format: texture::DepthFormat,
307310
supports_surface: FSurface,
308311
supports_depth: FDepth,
309312
) -> u32
310313
where
311314
FSurface: Fn(u32) -> bool,
312-
FDepth: Fn(platform::texture::DepthFormat, u32) -> bool,
315+
FDepth: Fn(texture::DepthFormat, u32) -> bool,
313316
{
314317
let mut resolved_sample_count = sample_count.max(1);
315318

@@ -330,7 +333,7 @@ impl RenderPassBuilder {
330333
self.depth_operations.is_some() || self.stencil_operations.is_some();
331334
if wants_depth_or_stencil && resolved_sample_count > 1 {
332335
let validated_depth_format = if self.stencil_operations.is_some() {
333-
platform::texture::DepthFormat::Depth24PlusStencil8
336+
texture::DepthFormat::Depth24PlusStencil8
334337
} else {
335338
depth_format
336339
};
@@ -392,7 +395,7 @@ mod tests {
392395
let resolved = builder.resolve_sample_count(
393396
4,
394397
surface_format(),
395-
platform::texture::DepthFormat::Depth32Float,
398+
texture::DepthFormat::Depth32Float,
396399
|_samples| {
397400
return false;
398401
},
@@ -412,7 +415,7 @@ mod tests {
412415
let resolved = builder.resolve_sample_count(
413416
8,
414417
surface_format(),
415-
platform::texture::DepthFormat::Depth32Float,
418+
texture::DepthFormat::Depth32Float,
416419
|_samples| {
417420
return true;
418421
},
@@ -428,13 +431,13 @@ mod tests {
428431
#[test]
429432
fn stencil_support_uses_stencil_capable_depth_format() {
430433
let builder = RenderPassBuilder::new().with_stencil().with_multi_sample(2);
431-
let requested_formats: RefCell<Vec<platform::texture::DepthFormat>> =
434+
let requested_formats: RefCell<Vec<texture::DepthFormat>> =
432435
RefCell::new(Vec::new());
433436

434437
let resolved = builder.resolve_sample_count(
435438
2,
436439
surface_format(),
437-
platform::texture::DepthFormat::Depth32Float,
440+
texture::DepthFormat::Depth32Float,
438441
|_samples| {
439442
return true;
440443
},
@@ -447,7 +450,7 @@ mod tests {
447450
assert_eq!(resolved, 2);
448451
assert_eq!(
449452
requested_formats.borrow().first().copied(),
450-
Some(platform::texture::DepthFormat::Depth24PlusStencil8)
453+
Some(texture::DepthFormat::Depth24PlusStencil8)
451454
);
452455
}
453456

@@ -459,7 +462,7 @@ mod tests {
459462
let resolved = builder.resolve_sample_count(
460463
4,
461464
surface_format(),
462-
platform::texture::DepthFormat::Depth32Float,
465+
texture::DepthFormat::Depth32Float,
463466
|_samples| {
464467
return true;
465468
},
@@ -479,7 +482,7 @@ mod tests {
479482
let resolved = builder.resolve_sample_count(
480483
0,
481484
surface_format(),
482-
platform::texture::DepthFormat::Depth32Float,
485+
texture::DepthFormat::Depth32Float,
483486
|_samples| {
484487
return true;
485488
},

0 commit comments

Comments
 (0)