Skip to content

Commit

Permalink
fixed some todos
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Dec 1, 2023
1 parent 5e61512 commit c8121d4
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 124 deletions.
2 changes: 1 addition & 1 deletion crates/renderling-gpui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub struct Gpui(pub Renderling);
impl Gpui {
/// Create a new UI renderer.
pub fn new(width: u32, height: u32) -> Self {
let r = Renderling::headless(width, height).unwrap();
let r = Renderling::headless(width, height);
Self::new_from(&r)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/renderling-shader/src/gltf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Gltf types that are used in shaders.
use crate::{
self as renderling_shader, array::Array, id::Id, pbr::PbrMaterial, slab::Slabbed,
stage::GpuTexture,
texture::GpuTexture,
};
#[repr(transparent)]
#[derive(Default, Clone, Copy, Slabbed)]
Expand Down
1 change: 1 addition & 0 deletions crates/renderling-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod pbr;
pub mod skybox;
pub mod slab;
pub mod stage;
pub mod texture;
pub mod tonemapping;
pub mod tutorial;
pub mod ui;
Expand Down
3 changes: 2 additions & 1 deletion crates/renderling-shader/src/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use crate::{
id::Id,
math,
slab::Slab,
stage::{GpuLight, GpuTexture, LightType, LightingModel},
stage::{GpuLight, LightType, LightingModel},
texture::GpuTexture,
IsVector,
};

Expand Down
3 changes: 0 additions & 3 deletions crates/renderling-shader/src/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ pub trait Slabbed: core::any::Any + Sized {
///
/// If the type cannot be read, the returned index will be equal
/// to `index`.
// TODO: recondsider the mutability of `self` here.
// We could make this a `&self` and that might help with using it
// from multiple threads.
fn read_slab(&mut self, index: usize, slab: &[u32]) -> usize;

/// Write the type into the slab at starting `index` and return
Expand Down
4 changes: 1 addition & 3 deletions crates/renderling-shader/src/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ use crate::{
id::{Id, ID_NONE},
pbr::{self, PbrMaterial},
slab::{Slab, Slabbed},
texture::GpuTexture,
IsMatrix, IsVector,
};

mod texture;
pub use texture::*;

/// A vertex in a mesh.
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
#[repr(C)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! GPU textures.
//!
// TODO: move this (stage/texture.rs) to lib/texture.rs
use glam::{UVec2, Vec2};
use renderling_derive::Slabbed;

Expand Down
2 changes: 1 addition & 1 deletion crates/renderling/src/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mod test {

#[test]
fn can_merge_atlas() {
let r = Renderling::headless(100, 100).unwrap();
let r = Renderling::headless(100, 100);
let (device, queue) = r.get_device_and_queue_owned();
println!("{}", std::env::current_dir().unwrap().display());
let cheetah = SceneImage::from_path("../../img/cheetah.jpg").unwrap();
Expand Down
5 changes: 2 additions & 3 deletions crates/renderling/src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,8 @@ mod test {

#[test]
fn bloom_on_off() {
let mut renderling = Renderling::headless(100, 100)
.unwrap()
.with_background_color(glam::Vec4::splat(1.0));
let mut renderling =
Renderling::headless(100, 100).with_background_color(glam::Vec4::splat(1.0));
let mut builder = renderling.new_scene();
let loader = builder
.gltf_load("../../gltf/EmissiveStrengthTest.glb")
Expand Down
58 changes: 20 additions & 38 deletions crates/renderling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ mod test {
}

fn cmy_triangle_setup() -> CmyTri {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec4::splat(1.0));
let mut r = Renderling::headless(100, 100).with_background_color(Vec4::splat(1.0));
let (projection, view) = default_ortho2d(100.0, 100.0);
let mut builder = r.new_scene().with_camera(projection, view);
let tri = builder
Expand Down Expand Up @@ -349,9 +347,7 @@ mod test {

#[test]
fn cmy_cube_sanity() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec4::splat(1.0));
let mut r = Renderling::headless(100, 100).with_background_color(Vec4::splat(1.0));
let mut builder = r.new_scene().with_camera(
Mat4::perspective_rh(std::f32::consts::PI / 4.0, 1.0, 0.1, 100.0),
Mat4::look_at_rh(Vec3::new(0.0, 12.0, 20.0), Vec3::ZERO, Vec3::Y),
Expand All @@ -377,9 +373,7 @@ mod test {

#[test]
fn cmy_cube_visible() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec4::splat(1.0));
let mut r = Renderling::headless(100, 100).with_background_color(Vec4::splat(1.0));

let (projection, view) = camera::default_perspective(100.0, 100.0);
let mut builder = r.new_scene().with_camera(projection, view);
Expand Down Expand Up @@ -440,9 +434,7 @@ mod test {

#[test]
fn cmy_cube_remesh() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec4::splat(1.0));
let mut r = Renderling::headless(100, 100).with_background_color(Vec4::splat(1.0));
let (projection, view) = camera::default_perspective(100.0, 100.0);
let mut builder = r
.new_scene()
Expand Down Expand Up @@ -533,9 +525,7 @@ mod test {
// tests that updating the material actually updates the rendering of an unlit
// mesh
fn unlit_textured_cube_material() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec4::splat(0.0));
let mut r = Renderling::headless(100, 100).with_background_color(Vec4::splat(0.0));
let (proj, view) = camera::default_perspective(100.0, 100.0);
let mut builder = r.new_scene().with_camera(proj, view);
let sandstone = SceneImage::from(image::open("../../img/sandstone.png").unwrap());
Expand Down Expand Up @@ -589,7 +579,8 @@ mod test {
100,
100,
None as Option<CreateSurfaceFn>,
));
))
.unwrap();

let points = vec![
Vec4::new(0.0, 0.0, 0.0, 0.0),
Expand Down Expand Up @@ -632,9 +623,8 @@ mod test {

#[test]
fn gpu_scene_sanity1() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec3::splat(0.0).extend(1.0));
let mut r =
Renderling::headless(100, 100).with_background_color(Vec3::splat(0.0).extend(1.0));
let mut builder = r.new_scene();

let verts = vec![
Expand Down Expand Up @@ -730,9 +720,8 @@ mod test {

#[test]
fn gpu_scene_sanity2() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec3::splat(0.0).extend(1.0));
let mut r =
Renderling::headless(100, 100).with_background_color(Vec3::splat(0.0).extend(1.0));
let (projection, view) = camera::default_ortho2d(100.0, 100.0);
let mut builder = r.new_scene().with_camera(projection, view);
// now test the textures functionality
Expand Down Expand Up @@ -865,9 +854,8 @@ mod test {

#[test]
fn atlas_uv_mapping() {
let mut r = Renderling::headless(32, 32)
.unwrap()
.with_background_color(Vec3::splat(0.0).extend(1.0));
let mut r =
Renderling::headless(32, 32).with_background_color(Vec3::splat(0.0).extend(1.0));
let (projection, view) = camera::default_ortho2d(32.0, 32.0);
let mut builder = r.new_scene().with_camera(projection, view);
let dirt = image::open("../../img/dirt.jpg").unwrap();
Expand Down Expand Up @@ -938,9 +926,7 @@ mod test {
let sheet_h = icon_h * 3;
let w = sheet_w * 3 + 2;
let h = sheet_h;
let mut r = Renderling::headless(w, h)
.unwrap()
.with_background_color(Vec4::new(1.0, 1.0, 0.0, 1.0));
let mut r = Renderling::headless(w, h).with_background_color(Vec4::new(1.0, 1.0, 0.0, 1.0));
let (projection, view) = camera::default_ortho2d(w as f32, h as f32);
let mut builder = r.new_scene().with_camera(projection, view);
let dirt = image::open("../../img/dirt.jpg").unwrap();
Expand Down Expand Up @@ -1037,9 +1023,7 @@ mod test {
let sheet_h = icon_h * 3;
let w = sheet_w * 3 + 2;
let h = sheet_h;
let mut r = Renderling::headless(w, h)
.unwrap()
.with_background_color(Vec4::new(1.0, 1.0, 0.0, 1.0));
let mut r = Renderling::headless(w, h).with_background_color(Vec4::new(1.0, 1.0, 0.0, 1.0));
let (projection, view) = camera::default_ortho2d(w as f32, h as f32);
let mut builder = r.new_scene().with_camera(projection, view);
let dirt = image::open("../../img/dirt.jpg").unwrap();
Expand Down Expand Up @@ -1151,9 +1135,8 @@ mod test {
#[test]
/// Ensures that the directional light coloring works.
fn scene_cube_directional() {
let mut r = Renderling::headless(100, 100)
.unwrap()
.with_background_color(Vec3::splat(0.0).extend(1.0));
let mut r =
Renderling::headless(100, 100).with_background_color(Vec3::splat(0.0).extend(1.0));

let mut builder = r.new_scene();
let red = Vec3::X.extend(1.0);
Expand Down Expand Up @@ -1254,7 +1237,7 @@ mod test {
#[test]
// tests that nested children are transformed by their parent's transform
fn scene_parent_sanity() {
let mut r = Renderling::headless(100, 100).unwrap();
let mut r = Renderling::headless(100, 100);
r.set_background_color(Vec4::splat(0.0));
let (projection, view) = camera::default_ortho2d(100.0, 100.0);
let mut builder = r.new_scene().with_camera(projection, view);
Expand Down Expand Up @@ -1421,9 +1404,8 @@ mod test {
// see https://learnopengl.com/PBR/Lighting
fn pbr_metallic_roughness_spheres() {
let ss = 600;
let mut r = Renderling::headless(ss, ss)
.unwrap()
.with_background_color(Vec3::splat(0.0).extend(1.0));
let mut r =
Renderling::headless(ss, ss).with_background_color(Vec3::splat(0.0).extend(1.0));

let radius = 0.5;
let mut icosphere = icosahedron::Polyhedron::new_isocahedron(radius, 5);
Expand Down
20 changes: 15 additions & 5 deletions crates/renderling/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub enum RenderlingError {
State { source: WgpuStateError },
}

impl From<WgpuStateError> for RenderlingError {
fn from(source: WgpuStateError) -> Self {
Self::State { source }
}
}

/// A thread-safe, clonable wrapper around `wgpu::Device`.
#[derive(Clone)]
pub struct Device(pub Arc<wgpu::Device>);
Expand Down Expand Up @@ -180,7 +186,7 @@ impl Renderling {
height,
None as Option<CreateSurfaceFn>,
)
.await;
.await?;
let depth_texture = crate::Texture::create_depth_texture(&device, width, height);
Ok(Self::new(
target,
Expand Down Expand Up @@ -210,7 +216,7 @@ impl Renderling {
.map_err(|e| WgpuStateError::CreateSurface { source: e })
}) as crate::CreateSurfaceFn),
)
.await;
.await?;
let depth_texture = crate::Texture::create_depth_texture(&device, width, height);

Ok(Self::new(
Expand Down Expand Up @@ -251,9 +257,13 @@ impl Renderling {
Self::try_from_raw_window_handle(window, inner_size.width, inner_size.height)
}

// TODO: No reason for `headless` to return Result
pub fn headless(width: u32, height: u32) -> Result<Self, RenderlingError> {
futures_lite::future::block_on(Self::try_new_headless(width, height))
/// Create a new headless renderer.
///
/// ## Panics
/// This function will panic if an adapter cannot be found. For example this would
/// happen on machines without a GPU.
pub fn headless(width: u32, height: u32) -> Self {
futures_lite::future::block_on(Self::try_new_headless(width, height)).unwrap()
}

pub fn set_background_color(&mut self, color: impl Into<Vec4>) {
Expand Down
3 changes: 1 addition & 2 deletions crates/renderling/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use moongraph::{Move, View, ViewMut};
use renderling_shader::debug::DebugChannel;
use snafu::prelude::*;

pub use renderling_shader::{pbr::PbrMaterial, stage::*};
pub use renderling_shader::{pbr::PbrMaterial, stage::*, texture::*};

use crate::{
bloom::BloomResult, frame::FrameTextureView, hdr::HdrSurface, Atlas, BufferArray, DepthTexture,
Expand Down Expand Up @@ -495,7 +495,6 @@ impl Scene {
}],
});

// TODO: rename this or regroup the bindgroups
let render_buffers_bindgroup = create_scene_buffers_bindgroup(
&device,
&constants,
Expand Down
Loading

0 comments on commit c8121d4

Please sign in to comment.