Skip to content

Commit

Permalink
Merge branch 'main' into emilk/cleaner-space-view-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 8, 2022
2 parents 30ff89b + c04d80d commit 49f1321
Show file tree
Hide file tree
Showing 50 changed files with 3,292 additions and 532 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"objectron",
"Skybox",
"smallvec",
"texcoords",
"Tonemapper",
"tonemapping",
"Wgsl"
Expand Down
32 changes: 22 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ egui-wgpu = { git = "https://github.com/emilk/egui", rev = "51ff32797da027125e9c
# Because gltf hasn't published a new version: https://github.com/gltf-rs/gltf/issues/357
gltf = { git = "https://github.com/rerun-io/gltf", rev = "3c14ded73755d1ce9e47010edb06db63cb7e2cca" }

# 2022-10-12 - Eq for DepthBiasState and DepthStencilState
wgpu = { git = "https://github.com/gfx-rs/wgpu.git", ref = "aed3b1ae59ee097901b852d934493c7ec167d344" }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu.git", ref = "aed3b1ae59ee097901b852d934493c7ec167d344" }
# 2022-10-12 - Alpha to coverage support for GLES
wgpu = { git = "https://github.com/gfx-rs/wgpu.git", ref = "a377ae2b7fe6c1c9412751166f0917e617164e49" }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu.git", ref = "a377ae2b7fe6c1c9412751166f0917e617164e49" }
#wgpu = { path = "../wgpu/wgpu" }
7 changes: 4 additions & 3 deletions crates/re_data_store/src/stores/field_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ impl<Time: 'static + Copy + Ord> FieldStore<Time> {
}
BatchOrSplat::Batch(batch) => {
if let Some(index) = instance_index {
let value = batch.get_index(index).expect("Batches should be self-consistent");
time_msgid_index.push((*time, *msg_id));
values.push(value.clone());
if let Some(value) = batch.get_index(index) {
time_msgid_index.push((*time, *msg_id));
values.push(value.clone());
}
} else {
for (_index_hash, _, value) in batch.iter() {
time_msgid_index.push((*time, *msg_id));
Expand Down
61 changes: 34 additions & 27 deletions crates/re_renderer/examples/renderer_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use macaw::IsoTransform;
use rand::Rng;
use re_renderer::{
config::{supported_backends, HardwareTier, RenderContextConfig},
mesh_manager::{GpuMeshHandle, MeshManager},
renderer::*,
resource_managers::ResourceLifeTime,
view_builder::{TargetConfiguration, ViewBuilder},
DebugLabel, *,
};
Expand Down Expand Up @@ -88,7 +88,13 @@ fn draw_views(
let skybox = GenericSkyboxDrawable::new(re_ctx, device);
let lines = build_lines(re_ctx, device, queue, seconds_since_startup);
let point_cloud = PointCloudDrawable::new(re_ctx, device, queue, &state.random_points).unwrap();
let meshes = build_meshes(re_ctx, device, queue, &state.meshes, seconds_since_startup);
let meshes = build_mesh_instances(
re_ctx,
device,
queue,
&state.model_mesh_instances,
seconds_since_startup,
);

let splits = split_resolution(resolution, 2, 2).collect::<Vec<_>>();

Expand Down Expand Up @@ -174,25 +180,27 @@ fn draw_view<'a, D: 'static + Drawable + Sync + Send + Clone>(
(view_builder, encoder.finish())
}

fn build_meshes(
fn build_mesh_instances(
re_ctx: &mut RenderContext,
device: &wgpu::Device,
queue: &wgpu::Queue,
mesh_handles: &[GpuMeshHandle],
model_mesh_instances: &[MeshInstance],
seconds_since_startup: f32,
) -> MeshDrawable {
let mesh_instances = lorenz_points(10.0)
.iter()
.enumerate()
.flat_map(|(i, p)| {
mesh_handles.iter().map(move |mesh| MeshInstance {
mesh: *mesh,
world_from_mesh: macaw::Conformal3::from_scale_rotation_translation(
0.025 + (i % 10) as f32 * 0.01,
glam::Quat::from_rotation_y(i as f32 + seconds_since_startup * 5.0),
*p,
),
})
model_mesh_instances
.iter()
.map(move |model_mesh_instances| MeshInstance {
mesh: model_mesh_instances.mesh,
world_from_mesh: macaw::Conformal3::from_scale_rotation_translation(
0.025 + (i % 10) as f32 * 0.01,
glam::Quat::from_rotation_y(i as f32 + seconds_since_startup * 5.0),
*p,
) * model_mesh_instances.world_from_mesh,
})
})
.collect_vec();
MeshDrawable::new(re_ctx, device, queue, &mesh_instances).unwrap()
Expand Down Expand Up @@ -352,7 +360,7 @@ impl Application {
},
);

let state = AppState::new(&mut re_ctx, &device, &queue);
let state = AppState::new(&mut re_ctx);

Ok(Self {
event_loop,
Expand Down Expand Up @@ -490,50 +498,49 @@ struct AppState {
time: Time,

/// Lazily loaded mesh.
meshes: Vec<GpuMeshHandle>,
model_mesh_instances: Vec<MeshInstance>,

// Want to have a large cloud of random points, but doing rng for all of them every frame is too slow
random_points: Vec<PointCloudPoint>,
}

impl AppState {
fn new(re_ctx: &mut RenderContext, device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
fn new(re_ctx: &mut RenderContext) -> Self {
let mut rnd = <rand::rngs::StdRng as rand::SeedableRng>::seed_from_u64(42);
let random_point_range = -2.0_f32..2.0_f32;
let random_point_range = -5.0_f32..5.0_f32;
let random_points = (0..500000)
.map(|_| PointCloudPoint {
position: glam::vec3(
rnd.gen_range(random_point_range.clone()),
rnd.gen_range(random_point_range.clone()),
rnd.gen_range(random_point_range.clone()),
),
radius: rnd.gen_range(0.005..0.025),
radius: rnd.gen_range(0.005..0.05),
srgb_color: [rnd.gen(), rnd.gen(), rnd.gen(), 255],
})
.collect_vec();

let meshes = {
let model_mesh_instances = {
let reader = std::io::Cursor::new(include_bytes!("rerun.obj.zip"));
let mut zip = zip::ZipArchive::new(reader).unwrap();
let mut zipped_obj = zip.by_name("rerun.obj").unwrap();
let mut obj_data = Vec::new();
zipped_obj.read_to_end(&mut obj_data).unwrap();
importer::obj::load_obj_from_buffer(&obj_data)
.unwrap()
.meshes
.iter()
.map(|mesh_data| {
MeshManager::new_long_lived_mesh(re_ctx, device, queue, mesh_data).unwrap()
})
.collect()
importer::obj::load_obj_from_buffer(
&obj_data,
ResourceLifeTime::LongLived,
&mut re_ctx.mesh_manager,
&mut re_ctx.texture_manager_2d,
)
.unwrap()
};

Self {
time: Time {
start_time: Instant::now(),
last_draw_time: Instant::now(),
},
meshes,
model_mesh_instances,
random_points,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#import <./types.wgsl>
#import <./utils/srgb.wgsl>
#import <./global_bindings.wgsl>

struct VertexOutput {
@builtin(position) position: Vec4,
@location(0) texcoord: Vec2,
};

// TODO(andreas): Move global bindings to shared include
@group(0) @binding(1)
var nearest_sampler: sampler;

@group(1) @binding(0)
var hdr_texture: texture_2d<f32>;
var input_texture: texture_2d<f32>;

@fragment
fn main(in: VertexOutput) -> @location(0) Vec4 {
// Note that we can't use a simple textureLoad using @builtin(position) here despite the lack of filtering.
// The issue is that positions provided by @builtin(position) are not dependent on the set viewport,
// but are about the location of the texel in the target texture.
let hdr = textureSample(hdr_texture, nearest_sampler, in.texcoord).rgb;
let input = textureSample(input_texture, nearest_sampler, in.texcoord).rgb;
// TODO(andreas): Do something meaningful with values above 1
let hdr = clamp(hdr, vec3<f32>(0.0), vec3<f32>(1.0));
return Vec4(srgb_from_linear(hdr), 1.0);
let input = clamp(input, vec3<f32>(0.0), vec3<f32>(1.0));

// Convert to srgb - this is necessary since the final eframe output does *not* have an srgb format.
// Note that the input here is assumed to be linear - if the input texture was an srgb texture it would have been converted on load.
return Vec4(srgb_from_linear(input), 1.0);
}
5 changes: 5 additions & 0 deletions crates/re_renderer/shader/global_bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ struct FrameUniformBuffer {

camera_position: vec3<f32>,
top_right_screen_corner_in_view: vec2<f32>,
/// Multiply this with a camera distance to get a measure of how wide a pixel is in world units.
pixel_world_size_from_camera_distance: f32,
};
@group(0) @binding(0)
var<uniform> frame: FrameUniformBuffer;

@group(0) @binding(1)
var nearest_sampler: sampler;

@group(0) @binding(2)
var trilinear_sampler: sampler;
9 changes: 8 additions & 1 deletion crates/re_renderer/shader/instanced_mesh.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#import <./mesh_vertex.wgsl>
#import <./utils/quaternion.wgsl>

@group(1) @binding(0)
var albedo_texture: texture_2d<f32>;

struct VertexOut {
@builtin(position) position: Vec4,
@location(0) texcoord: Vec2,
Expand All @@ -24,10 +27,14 @@ fn vs_main(in_vertex: VertexIn, in_instance: InstanceIn) -> VertexOut {

@fragment
fn fs_main(in: VertexOut) -> @location(0) Vec4 {
let albedo = textureSample(albedo_texture, trilinear_sampler, in.texcoord).rgb;

// Hardcoded lambert lighting. TODO(andreas): Some microfacet model.
let light_dir = normalize(vec3(1.0, 2.0, 0.0)); // TODO(andreas): proper lighting
let normal = normalize(in.normal_world_space);
let shading = clamp(dot(normal, light_dir), 0.0, 1.0) + 0.2;

return Vec4(shading, shading, shading, 0.0);
let radiance = albedo * shading;

return Vec4(radiance, 1.0);
}
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
@fragment
fn fs_main(in: VertexOut) -> @location(0) Vec4 {
// TODO(andreas): Rounded caps, proper shading/lighting, etc.
let shading = 1.2 - length(in.position_world - in.position_world_line) / in.line_radius;
return in.color * shading ;
let shading = max(0.2, 1.2 - length(in.position_world - in.position_world_line) / in.line_radius);
return in.color * shading;
}
Loading

0 comments on commit 49f1321

Please sign in to comment.