Skip to content

Commit e8624ba

Browse files
author
Matt Kleinschafer
committed
More rendering
1 parent 765f3c4 commit e8624ba

File tree

3 files changed

+83
-41
lines changed

3 files changed

+83
-41
lines changed

modules/graphics/src/rendering/commands.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ impl RenderQueue {
9090
}
9191

9292
/// Sets the render target to the given target.
93-
pub fn set_render_target(&mut self, target_id: TargetId) {
94-
self.enqueue(RenderCommand::SetRenderTarget { target_id });
93+
pub fn set_render_target(&mut self, target: &RenderTarget) {
94+
self.enqueue(RenderCommand::SetRenderTarget { target_id: target.id() });
9595
}
9696

9797
/// Sets the render target to the display.
@@ -130,6 +130,29 @@ impl RenderQueue {
130130
})
131131
}
132132

133+
/// Blits the given [`RenderTarget`] to the display.
134+
pub fn blit_render_target_to_display(&mut self, target: &RenderTarget, clear_color: Option<Color>) {
135+
self.enqueue(RenderCommand::SetRenderTargetToDisplay);
136+
137+
if let Some(color) = clear_color {
138+
self.enqueue(RenderCommand::ClearColorBuffer { color });
139+
}
140+
141+
// self.enqueue(RenderCommand::SetShader {
142+
// shader_id: target.blit_shader().id(),
143+
// uniforms: Box::new(target.blit_uniforms().clone()),
144+
// blend_state: BlendState::Disabled,
145+
// culling_mode: CullingMode::Disabled,
146+
// scissor_mode: ScissorMode::Disabled,
147+
// });
148+
// self.enqueue(RenderCommand::DrawMesh {
149+
// mesh_id: target.blit_mesh().id(),
150+
// topology: PrimitiveTopology::TriangleList,
151+
// vertex_count: target.blit_mesh().vertices(),
152+
// index_count: target.blit_mesh().indices(),
153+
// });
154+
}
155+
133156
/// Clears all [`RenderCommand`] from the queue.
134157
pub fn clear(&mut self) {
135158
let mut commands = self.commands.lock().unwrap();

modules/graphics/src/rendering/culling.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ pub struct MaterialSortingKey {
2929
flags: MaterialFlags,
3030
}
3131

32+
/// Represents an object that is visible to a camera.
33+
pub struct VisibleObject<'a> {
34+
/// The object itself.
35+
pub object: &'a dyn RenderObject,
36+
/// The material of the object.
37+
pub material: &'a Material,
38+
}
39+
40+
/// A set of visible objects that can be rendered in a scene.
41+
///
42+
/// This is a subset of the objects in a scene that are visible to a specific
43+
/// camera, and can be used to optimize rendering by only rendering the objects
44+
/// that are visible to the camera.
45+
pub struct VisibleObjectSet<'a> {
46+
/// The frustum of the camera that was used to cull the objects.
47+
pub frustum: Frustum,
48+
/// The objects that are visible to the camera.
49+
pub objects: Vec<VisibleObject<'a>>,
50+
}
51+
52+
impl<'a> VisibleObjectSet<'a> {
53+
/// Gets an iterator over the objects in the set.
54+
pub fn group_by_material(&self) -> impl Iterator<Item = (&'a Material, &[VisibleObject<'a>])> {
55+
self
56+
.objects
57+
.chunk_by(|a, b| MaterialSortingKey::from(a.material) == MaterialSortingKey::from(b.material))
58+
.map(|chunk| (chunk[0].material, chunk))
59+
}
60+
}
61+
3262
impl From<&Material> for MaterialSortingKey {
3363
/// Gets the sorting key for the given material.
3464
fn from(material: &Material) -> Self {
@@ -64,34 +94,3 @@ impl From<&Material> for MaterialSortingKey {
6494
Self { flags }
6595
}
6696
}
67-
68-
/// Represents an object that is visible to a camera, along with it's material
69-
/// properties that are used to render it.
70-
pub struct VisibleObject<'a> {
71-
/// The object itself.
72-
pub object: &'a dyn RenderObject,
73-
/// The sorting key for the material of the object.
74-
pub material: &'a Material,
75-
}
76-
77-
/// A set of visible objects that can be rendered in a scene.
78-
///
79-
/// This is a subset of the objects in a scene that are visible to a specific
80-
/// camera, and can be used to optimize rendering by only rendering the objects
81-
/// that are visible to the camera.
82-
pub struct VisibleObjectSet<'a> {
83-
/// The frustum of the camera that was used to cull the objects.
84-
pub frustum: Frustum,
85-
/// The objects that are visible to the camera.
86-
pub objects: Vec<VisibleObject<'a>>,
87-
}
88-
89-
impl<'a> VisibleObjectSet<'a> {
90-
/// Gets an iterator over the objects in the set.
91-
pub fn group_by_material(&self) -> impl Iterator<Item = (&'a Material, &[VisibleObject<'a>])> {
92-
self
93-
.objects
94-
.chunk_by(|a, b| MaterialSortingKey::from(a.material) == MaterialSortingKey::from(b.material))
95-
.map(|chunk| (chunk[0].material, chunk))
96-
}
97-
}

modules/graphics/src/rendering/pipelines.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub struct RenderFrame<'a> {
1212
}
1313

1414
impl<'a> RenderFrame<'a> {
15-
/// Draws the given objects to the frame.
16-
pub fn draw_scene(&mut self, scene: &dyn RenderScene, camera: &dyn Camera) {
15+
/// Draws the object visible to the given camera.
16+
pub fn draw_camera(&mut self, scene: &dyn RenderScene, camera: &dyn Camera) {
1717
let visible_object_set = scene.cull_visible_objects(camera);
1818

1919
for (material, group) in visible_object_set.group_by_material() {
@@ -111,21 +111,27 @@ pub mod forward {
111111
impl RenderPass for DepthPass {
112112
fn render_camera(&self, scene: &dyn RenderScene, camera: &dyn Camera, frame: &mut RenderFrame<'_>) {
113113
frame.queue.clear_color_buffer(Color::BLACK);
114-
frame.draw_scene(scene, camera);
114+
frame.draw_camera(scene, camera);
115115
}
116116
}
117117

118118
/// A [`RenderPass`] that renders all objects in the scene to a color target.
119-
struct ColorPass {}
119+
struct ColorPass {
120+
color_target: RenderTarget,
121+
}
120122

121123
impl RenderPass for ColorPass {
124+
fn begin_frame(&self, _scene: &dyn RenderScene, frame: &mut RenderFrame<'_>) {
125+
frame.queue.set_render_target(&self.color_target);
126+
}
127+
122128
fn render_camera(&self, scene: &dyn RenderScene, camera: &dyn Camera, frame: &mut RenderFrame<'_>) {
123129
frame.queue.clear_color_buffer(Color::BLACK);
124-
frame.draw_scene(scene, camera);
130+
frame.draw_camera(scene, camera);
125131
}
126132

127-
fn end_frame(&self, _scene: &dyn RenderScene, _frame: &mut RenderFrame<'_>) {
128-
// TODO: blit the color target to the screen.
133+
fn end_frame(&self, _scene: &dyn RenderScene, frame: &mut RenderFrame<'_>) {
134+
frame.queue.blit_render_target_to_display(&self.color_target, None);
129135
}
130136
}
131137

@@ -135,7 +141,21 @@ pub mod forward {
135141
MultiPassPipeline {
136142
renderer: Renderer::new(graphics),
137143
queue: RenderQueue::default(),
138-
passes: vec![Box::new(DepthPass {}), Box::new(ColorPass {})],
144+
passes: vec![
145+
Box::new(DepthPass {}),
146+
Box::new(ColorPass {
147+
color_target: RenderTarget::new(graphics, &RenderTargetDescriptor {
148+
color_attachment: RenderTextureDescriptor {
149+
width: 1920,
150+
height: 1080,
151+
options: TextureOptions::default(),
152+
},
153+
depth_attachment: None,
154+
stencil_attachment: None,
155+
})
156+
.unwrap(),
157+
}),
158+
],
139159
}
140160
}
141161
}

0 commit comments

Comments
 (0)