-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce wgpu based re_renderer (experimental!) (#175)
Only renders a placeholder triangle for now
- Loading branch information
Showing
19 changed files
with
346 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "re_renderer" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
license.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
parking_lot = "0.12" | ||
|
||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] | ||
wgpu = { version = "0.14", default-features = false } | ||
|
||
[target.'cfg(target_arch = "wasm32")'.dependencies] | ||
wgpu = { version = "0.14", default-features = false, features = ["webgl"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The Rerun renderer | ||
|
||
A [wgpu](https://github.com/gfx-rs/wgpu/) based renderer for all your visualization needs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
struct VertexOut { | ||
@location(0) color: vec4<f32>, | ||
@builtin(position) position: vec4<f32>, | ||
}; | ||
|
||
var<private> v_positions: array<vec2<f32>, 3> = array<vec2<f32>, 3>( | ||
vec2<f32>(0.0, 1.0), | ||
vec2<f32>(1.0, -1.0), | ||
vec2<f32>(-1.0, -1.0), | ||
); | ||
|
||
var<private> v_colors: array<vec4<f32>, 3> = array<vec4<f32>, 3>( | ||
vec4<f32>(1.0, 0.0, 0.0, 1.0), | ||
vec4<f32>(0.0, 1.0, 0.0, 1.0), | ||
vec4<f32>(0.0, 0.0, 1.0, 1.0), | ||
); | ||
|
||
@vertex | ||
fn vs_main(@builtin(vertex_index) v_idx: u32) -> VertexOut { | ||
var out: VertexOut; | ||
|
||
out.position = vec4<f32>(v_positions[v_idx], 0.0, 1.0); | ||
out.color = v_colors[v_idx]; | ||
|
||
return out; | ||
} | ||
|
||
@fragment | ||
fn fs_main(in: VertexOut) -> @location(0) vec4<f32> { | ||
return in.color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/// Any resource involving wgpu rendering which can be re-used accross different scenes. | ||
/// I.e. render pipelines, resource pools, etc. | ||
pub struct RenderContext { | ||
/// The color format used by the eframe output buffer. | ||
output_format_color: wgpu::TextureFormat, | ||
|
||
/// The depth format used by the eframe output buffer. | ||
/// TODO(andreas): Should we maintain depth buffers per view and ask for no depth from eframe? | ||
output_format_depth: Option<wgpu::TextureFormat>, | ||
|
||
// TODO(andreas): Introduce a pipeline manager | ||
test_triangle: Option<wgpu::RenderPipeline>, | ||
// TODO(andreas): Strongly consider https://docs.rs/slotmap/latest/slotmap/ for resource pools | ||
} | ||
|
||
/// Render pipeline handle that needs to be requested from the `RenderContext` and can be resolved to a `wgpu::RenderPipeline` before drawing. | ||
#[derive(Clone, Copy)] | ||
pub(crate) struct RenderPipelineHandle; | ||
|
||
impl RenderContext { | ||
pub fn new( | ||
_device: &wgpu::Device, | ||
_queue: &wgpu::Queue, | ||
output_format_color: wgpu::TextureFormat, | ||
output_format_depth: Option<wgpu::TextureFormat>, | ||
) -> Self { | ||
RenderContext { | ||
output_format_color, | ||
output_format_depth, | ||
test_triangle: None, | ||
} | ||
} | ||
|
||
/// Requests a render pipeline and returns a handle to it. | ||
/// | ||
/// Internally, this ensures the requested pipeline is created and tracked. | ||
/// Returns a handle even if creating the pipeline fails! | ||
/// (this might be due to shader compilation error that might be fixed later) | ||
pub(crate) fn request_render_pipeline( | ||
&mut self, | ||
device: &wgpu::Device, | ||
) -> RenderPipelineHandle { | ||
self.test_triangle.get_or_insert_with(|| { | ||
// TODO(andreas): Standardize bind group and render pipeline layouts so we only ever have a handful. | ||
// (is this feasable?) | ||
// let bind_group_layout = | ||
// device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { | ||
// label: Some("custom3d"), | ||
// entries: &[], | ||
// }); | ||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: Some("custom3d"), | ||
bind_group_layouts: &[], | ||
push_constant_ranges: &[], | ||
}); | ||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { | ||
label: Some("custom3d"), | ||
source: wgpu::ShaderSource::Wgsl( | ||
include_str!("../shader/test_triangle.wgsl").into(), | ||
), | ||
}); | ||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { | ||
label: Some("test triangle"), | ||
layout: Some(&pipeline_layout), | ||
vertex: wgpu::VertexState { | ||
module: &shader, | ||
entry_point: "vs_main", | ||
buffers: &[], | ||
}, | ||
fragment: Some(wgpu::FragmentState { | ||
module: &shader, | ||
entry_point: "fs_main", | ||
targets: &[Some(self.output_format_color.into())], | ||
}), | ||
primitive: wgpu::PrimitiveState::default(), | ||
depth_stencil: self | ||
.output_format_depth | ||
.map(|format| wgpu::DepthStencilState { | ||
format, | ||
depth_compare: wgpu::CompareFunction::Always, | ||
depth_write_enabled: false, | ||
stencil: Default::default(), | ||
bias: Default::default(), | ||
}), | ||
multisample: wgpu::MultisampleState::default(), | ||
multiview: None, | ||
}) | ||
}); | ||
|
||
RenderPipelineHandle | ||
} | ||
|
||
/// Retrieves a [`wgpu::RenderPipeline`] given a handle. | ||
/// Returns None if the pipeline does not exist or failed to create. | ||
pub(crate) fn render_pipeline( | ||
&self, | ||
_handle: RenderPipelineHandle, | ||
) -> Option<&wgpu::RenderPipeline> { | ||
// TODO(andreas)render_context | ||
self.test_triangle.as_ref() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use parking_lot::RwLock; | ||
use std::sync::Arc; | ||
|
||
use crate::context::{RenderContext, RenderPipelineHandle}; | ||
|
||
/// Mirrors the GPU contents of a frame-global uniform buffer. | ||
/// Contains information that is constant for a single frame like camera. | ||
/// (does not contain information that is special to a particular renderer or global to the Context) | ||
//struct FrameUniformBuffer { | ||
// TODO(andreas): camera matrix and the like. | ||
//} | ||
|
||
/// The highest level rendering block in `re_renderer`. | ||
/// | ||
/// They are used to build up/collect various resources and then send them off for rendering. | ||
/// Collecting objects in this fashion allows for re-use of common resources (e.g. camera) | ||
#[derive(Default)] | ||
pub struct FrameBuilder { | ||
render_pipeline: Option<RenderPipelineHandle>, | ||
} | ||
|
||
pub type SharedFrameBuilder = Arc<RwLock<FrameBuilder>>; | ||
|
||
impl FrameBuilder { | ||
pub fn new() -> Self { | ||
FrameBuilder { | ||
render_pipeline: None, | ||
} | ||
} | ||
|
||
pub fn new_shared() -> SharedFrameBuilder { | ||
Arc::new(RwLock::new(FrameBuilder::new())) | ||
} | ||
|
||
pub fn test_triangle(&mut self, ctx: &mut RenderContext, device: &wgpu::Device) -> &mut Self { | ||
self.render_pipeline = Some(ctx.request_render_pipeline(device)); | ||
self | ||
} | ||
|
||
/// Draws the final result of a `FrameBuilder` to a given output `RenderPass` | ||
/// | ||
/// The bound surface(s) on the `RenderPass` are expected to be the same format as specified on `Context` creation. | ||
pub fn draw<'a>(&self, ctx: &'a RenderContext, pass: &mut wgpu::RenderPass<'a>) { | ||
if let Some(handle) = self.render_pipeline { | ||
let render_pipeline = ctx.render_pipeline(handle); | ||
|
||
if let Some(render_pipeline) = render_pipeline { | ||
pass.set_pipeline(render_pipeline); | ||
pass.draw(0..3, 0..1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Rerun's renderer. | ||
//! | ||
//! A wgpu based renderer [wgpu](https://github.com/gfx-rs/wgpu/) for all your visualization needs. | ||
//! Used in `re_runner` to display the contents of any view contents other than pure UI. | ||
pub mod context; | ||
pub mod frame_builder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.