ambient_renderer/
target.rs1use std::sync::Arc;
2
3use ambient_gpu::{
4 gpu::{Gpu, DEFAULT_SAMPLE_COUNT},
5 shader_module::DEPTH_FORMAT,
6 texture::{Texture, TextureView},
7};
8use glam::UVec2;
9use wgpu::{TextureFormat, TextureViewDescriptor};
10
11pub(crate) fn to_linear_format(format: TextureFormat) -> TextureFormat {
13 match format {
14 TextureFormat::Rgba8UnormSrgb => TextureFormat::Rgba8Unorm,
15 TextureFormat::Bgra8UnormSrgb => TextureFormat::Bgra8Unorm,
16 TextureFormat::Bc1RgbaUnormSrgb => TextureFormat::Bc1RgbaUnorm,
17 TextureFormat::Bc2RgbaUnormSrgb => TextureFormat::Bc2RgbaUnorm,
18 TextureFormat::Bc3RgbaUnormSrgb => TextureFormat::Bc3RgbaUnorm,
19 TextureFormat::Bc7RgbaUnormSrgb => TextureFormat::Bc7RgbaUnorm,
20 TextureFormat::Etc2Rgb8UnormSrgb => TextureFormat::Etc2Rgb8Unorm,
21 TextureFormat::Etc2Rgb8A1UnormSrgb => TextureFormat::Etc2Rgb8A1Unorm,
22 TextureFormat::Etc2Rgba8UnormSrgb => TextureFormat::Etc2Rgba8Unorm,
23 _ => format,
24 }
25}
26
27#[derive(Debug)]
28pub struct RenderTarget {
29 pub depth_buffer: Arc<Texture>,
30 pub depth_buffer_view: TextureView,
31 pub depth_stencil_view: TextureView,
32 pub color_buffer: Arc<Texture>,
33 pub color_buffer_view: TextureView,
34 pub normals_quat_buffer: Arc<Texture>,
35 pub normals_quat_buffer_view: TextureView,
36}
37impl RenderTarget {
38 pub fn new(gpu: Arc<Gpu>, size: UVec2, usage: Option<wgpu::TextureUsages>) -> Self {
39 let usage =
40 usage.unwrap_or(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC);
41 let sc_desc = gpu.sc_desc(size);
42 let depth_buffer = Arc::new(Texture::new(
43 gpu.clone(),
44 &wgpu::TextureDescriptor {
45 label: Some("RenderTarget.depth_buffer"),
46 size: wgpu::Extent3d { width: sc_desc.width, height: sc_desc.height, depth_or_array_layers: 1 },
47 mip_level_count: 1,
48 sample_count: DEFAULT_SAMPLE_COUNT,
49 dimension: wgpu::TextureDimension::D2,
50 format: DEPTH_FORMAT,
51 usage,
52 view_formats: &[]
53 },
54 ));
55 let color_buffer = Arc::new(Texture::new(
56 gpu.clone(),
57 &wgpu::TextureDescriptor {
58 label: Some("RenderTarget.color_buffer"),
59 size: wgpu::Extent3d { width: sc_desc.width, height: sc_desc.height, depth_or_array_layers: 1 },
60 mip_level_count: 1,
61 sample_count: 1,
62 dimension: wgpu::TextureDimension::D2,
63 format: sc_desc.format,
64 usage,
65 view_formats: &[]
66 },
67 ));
68 let normals_buffer = Arc::new(Texture::new(
69 gpu,
70 &wgpu::TextureDescriptor {
71 label: Some("RenderTarget.normals_quat_buffer"),
72 size: wgpu::Extent3d { width: sc_desc.width, height: sc_desc.height, depth_or_array_layers: 1 },
73 mip_level_count: 1,
74 sample_count: 1,
75 dimension: wgpu::TextureDimension::D2,
76 format: to_linear_format(sc_desc.format),
77 usage,
78 view_formats: &[]
79 },
80 ));
81 Self {
82 depth_buffer_view: depth_buffer
83 .create_view(&TextureViewDescriptor { aspect: wgpu::TextureAspect::DepthOnly, ..Default::default() }),
84 depth_stencil_view: depth_buffer.create_view(&TextureViewDescriptor { ..Default::default() }),
85 depth_buffer,
86 color_buffer_view: color_buffer.create_view(&Default::default()),
87 color_buffer,
88 normals_quat_buffer_view: normals_buffer.create_view(&Default::default()),
89 normals_quat_buffer: normals_buffer,
90 }
91 }
92}