kitgit

tirbofish/dropbear · diff

e4ca62f · Thribhu K

feature: hdr

Unverified

diff --git a/crates/dropbear-engine/src/graphics.rs b/crates/dropbear-engine/src/graphics.rs
index f5072e1..6f1c631 100644
--- a/crates/dropbear-engine/src/graphics.rs
+++ b/crates/dropbear-engine/src/graphics.rs
@@ -13,6 +13,7 @@ use wgpu::*;
 use winit::window::Window;
 
 use crate::mipmap::MipMapper;
+use crate::pipelines::hdr::HdrPipeline;
 
 pub const NO_TEXTURE: &[u8] = include_bytes!("../../../resources/textures/no-texture.png");
 
@@ -32,6 +33,7 @@ pub struct SharedGraphicsContext {
     pub future_queue: Arc<FutureQueue>,
     pub supports_storage: bool,
     pub mipmapper: Arc<MipMapper>,
+    pub hdr: Arc<RwLock<HdrPipeline>>,
     // pub yakui_renderer: Arc<Mutex<yakui_wgpu::YakuiWgpu>>,
     // pub yakui_texture: yakui::TextureId,
 }
@@ -76,6 +78,7 @@ impl SharedGraphicsContext {
             surface_format: state.surface_format,
             supports_storage: state.supports_storage,
             mipmapper: state.mipmapper.clone(),
+            hdr: state.hdr.clone(),
             // yakui_renderer: state.yakui_renderer.clone(),
             // yakui_texture: state.yakui_texture.clone(),
             surface_config: state.config.clone(),
diff --git a/crates/dropbear-engine/src/lib.rs b/crates/dropbear-engine/src/lib.rs
index 3351b71..618ac77 100644
--- a/crates/dropbear-engine/src/lib.rs
+++ b/crates/dropbear-engine/src/lib.rs
@@ -62,7 +62,7 @@ pub use gilrs;
 pub use wgpu;
 pub use winit;
 use winit::window::{WindowAttributes, WindowId};
-// use crate::pipelines::hdr::HdrPipeline;
+use crate::pipelines::hdr::HdrPipeline;
 use crate::scene::Scene;
 
 pub struct BindGroupLayouts {
@@ -95,7 +95,7 @@ pub struct State {
     pub texture_id: Arc<TextureId>,
     pub future_queue: Arc<FutureQueue>,
     pub mipmapper: Arc<MipMapper>,
-    // pub hdr: Arc<HdrPipeline>,
+    pub hdr: Arc<RwLock<HdrPipeline>>,
 
     physics_accumulator: Duration,
 
@@ -214,16 +214,16 @@ Hardware:
 
         let surface_caps = surface.get_capabilities(&adapter);
 
-        // let surface_format = surface_caps
-        //     .formats
-        //     .iter()
-        //     .find(|f| f.is_srgb())
-        //     .copied()
-        //     .unwrap_or(TextureFormat::Rgba16Float);
+        let surface_format = surface_caps
+            .formats
+            .iter()
+            .find(|f| f.is_srgb())
+            .copied()
+            .unwrap_or(TextureFormat::Rgba16Float);
         
         let config = SurfaceConfiguration {
             usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
-            format: Texture::TEXTURE_FORMAT,
+            format: surface_format,
             width: initial_width,
             height: initial_height,
             present_mode: surface_caps.present_modes[0],
@@ -245,7 +245,7 @@ Hardware:
 
         let mut egui_renderer = Arc::new(Mutex::new(EguiRenderer::new(
             &device,
-            Texture::TEXTURE_FORMAT,
+            surface_format,
             None,
             1,
             &window,
@@ -335,7 +335,11 @@ Hardware:
         let device = Arc::new(device);
         let queue = Arc::new(queue);
 
-        // let hdr = Arc::new(HdrPipeline::new(&device, &config));
+        let hdr = Arc::new(RwLock::new(HdrPipeline::new(
+            &device,
+            &config,
+            Texture::TEXTURE_FORMAT,
+        )));
 
         // let yakui_renderer = Arc::new(Mutex::new(yakui_wgpu::YakuiWgpu::new(
         //     &device,
@@ -352,7 +356,7 @@ Hardware:
 
         let result = Self {
             surface: Arc::new(surface),
-            surface_format: Texture::TEXTURE_FORMAT,
+            surface_format,
             device,
             queue,
             config: Arc::new(RwLock::new(config)),
@@ -379,7 +383,7 @@ Hardware:
             supports_storage: supports_storage_resources,
             // yakui_renderer,
             // yakui_texture,
-            // hdr,
+            hdr,
         };
 
         Ok(result)
@@ -395,8 +399,7 @@ Hardware:
             }
             self.surface.configure(&self.device, &self.config.read());
             self.is_surface_configured = true;
-            // Arc::get_mut(&mut self.hdr)
-            //     .resize(&self.device, width, height);
+            self.hdr.write().resize(&self.device, width, height);
         }
 
         self.depth_texture =
@@ -470,11 +473,12 @@ Hardware:
             let mut encoder = CommandEncoder::new(graphics.clone(), Some("surface clear render encoder"));
 
             {
+                let hdr = self.hdr.read();
                 let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                     label: Some("surface clear pass"),
                     color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                        // view: &self.hdr.view(),
-                        view: &view,
+                        view: hdr.view(),
+                        // view: &view,
                         depth_slice: None,
                         resolve_target: None,
                         ops: wgpu::Operations {
@@ -528,6 +532,11 @@ Hardware:
                 label: Some("egui render encoder"),
             });
 
+        {
+            let hdr = self.hdr.read();
+            hdr.process(&mut encoder, &self.viewport_texture.view);
+        }
+        
         self.egui_renderer.lock().end_frame_and_draw(
             &self.device,
             &self.queue,
diff --git a/crates/dropbear-engine/src/lighting.rs b/crates/dropbear-engine/src/lighting.rs
index 74e7d4d..33c7115 100644
--- a/crates/dropbear-engine/src/lighting.rs
+++ b/crates/dropbear-engine/src/lighting.rs
@@ -271,10 +271,7 @@ impl Light {
         label: Option<&str>,
     ) -> Self {
         let forward = DVec3::new(0.0, 0.0, -1.0);
-        let mut direction = transform.rotation * forward;
-        if matches!(light.light_type, LightType::Directional) {
-            direction = -direction;
-        }
+        let direction = transform.rotation * forward;
 
         let uniform = LightUniform {
             position: dvec3_to_uniform_array(transform.position),
@@ -343,10 +340,7 @@ impl Light {
         self.uniform.position = dvec3_to_uniform_array(transform.position);
 
         let forward = DVec3::new(0.0, 0.0, -1.0);
-        let mut direction = transform.rotation * forward;
-        if matches!(light.light_type, LightType::Directional) {
-            direction = -direction;
-        }
+        let direction = transform.rotation * forward;
         self.uniform.direction =
             dvec3_direction_to_uniform_array(direction, light.outer_cutoff_angle);
 
diff --git a/crates/dropbear-engine/src/pipelines/hdr.rs b/crates/dropbear-engine/src/pipelines/hdr.rs
index b4d6501..9ba354e 100644
--- a/crates/dropbear-engine/src/pipelines/hdr.rs
+++ b/crates/dropbear-engine/src/pipelines/hdr.rs
@@ -13,7 +13,11 @@ pub struct HdrPipeline {
 }
 
 impl HdrPipeline {
-    pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
+    pub fn new(
+        device: &wgpu::Device,
+        config: &wgpu::SurfaceConfiguration,
+        output_format: wgpu::TextureFormat,
+    ) -> Self {
         let width = config.width;
         let height = config.height;
 
@@ -82,7 +86,7 @@ impl HdrPipeline {
             Some("hdr render pipeline"),
             device,
             &pipeline_layout,
-            config.format.add_srgb_suffix(),
+            output_format,
             None,
             // We'll use some math to generate the vertex data in
             // the shader, so we don't need any vertex buffers
diff --git a/crates/dropbear-engine/src/pipelines/light_cube.rs b/crates/dropbear-engine/src/pipelines/light_cube.rs
index ab5a07c..737bdfc 100644
--- a/crates/dropbear-engine/src/pipelines/light_cube.rs
+++ b/crates/dropbear-engine/src/pipelines/light_cube.rs
@@ -38,6 +38,7 @@ impl DropbearShaderPipeline for LightCubePipeline {
             push_constant_ranges: &[],
         });
 
+        let hdr_format = graphics.hdr.read().format();
         let pipeline = graphics.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
             label: Some("light cube pipeline"),
             layout: Some(&pipeline_layout),
@@ -56,7 +57,7 @@ impl DropbearShaderPipeline for LightCubePipeline {
                 module: &shader.module,
                 entry_point: Some("fs_main"),
                 targets: &[Some(wgpu::ColorTargetState {
-                    format: Texture::TEXTURE_FORMAT,
+                    format: hdr_format,
                     blend: Some(wgpu::BlendState {
                         alpha: wgpu::BlendComponent::REPLACE,
                         color: wgpu::BlendComponent::REPLACE,
diff --git a/crates/dropbear-engine/src/pipelines/mod.rs b/crates/dropbear-engine/src/pipelines/mod.rs
index afb626a..13c0f4f 100644
--- a/crates/dropbear-engine/src/pipelines/mod.rs
+++ b/crates/dropbear-engine/src/pipelines/mod.rs
@@ -5,7 +5,7 @@ use crate::shader::Shader;
 pub mod shader;
 pub mod light_cube;
 pub mod globals;
-// pub mod hdr;
+pub mod hdr;
 
 pub use globals::{Globals, GlobalsUniform};
 
diff --git a/crates/dropbear-engine/src/pipelines/shader.rs b/crates/dropbear-engine/src/pipelines/shader.rs
index e165009..e0b7be9 100644
--- a/crates/dropbear-engine/src/pipelines/shader.rs
+++ b/crates/dropbear-engine/src/pipelines/shader.rs
@@ -38,6 +38,7 @@ impl DropbearShaderPipeline for MainRenderPipeline {
                     push_constant_ranges: &[],
                 });
 
+        let hdr_format = graphics.hdr.read().format();
         let pipeline =
             graphics.device
                 .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
@@ -57,7 +58,7 @@ impl DropbearShaderPipeline for MainRenderPipeline {
                             Some("u_fs_main")
                         },
                         targets: &[Some(wgpu::ColorTargetState {
-                            format: Texture::TEXTURE_FORMAT,
+                            format: hdr_format,
                             blend: Some(wgpu::BlendState::REPLACE),
                             write_mask: wgpu::ColorWrites::ALL,
                         })],
diff --git a/crates/eucalyptus-core/src/physics/collider/shader.rs b/crates/eucalyptus-core/src/physics/collider/shader.rs
index e1067df..d20983d 100644
--- a/crates/eucalyptus-core/src/physics/collider/shader.rs
+++ b/crates/eucalyptus-core/src/physics/collider/shader.rs
@@ -34,6 +34,7 @@ impl DropbearShaderPipeline for ColliderWireframePipeline {
             push_constant_ranges: &[],
         });
 
+        let hdr_format = graphics.hdr.read().format();
         let pipeline = graphics.device.create_render_pipeline(&RenderPipelineDescriptor {
             label: Some("Collider Wireframe Pipeline"),
             layout: Some(&pipeline_layout),
@@ -60,7 +61,7 @@ impl DropbearShaderPipeline for ColliderWireframePipeline {
                 module: &shader.module,
                 entry_point: Some("fs_main"),
                 targets: &[Some(ColorTargetState {
-                    format: Texture::TEXTURE_FORMAT,
+                    format: hdr_format,
                     blend: Some(BlendState::ALPHA_BLENDING),
                     write_mask: ColorWrites::ALL,
                 })],
diff --git a/crates/eucalyptus-editor/src/editor/component.rs b/crates/eucalyptus-editor/src/editor/component.rs
index 444b0fd..0616ce2 100644
--- a/crates/eucalyptus-editor/src/editor/component.rs
+++ b/crates/eucalyptus-editor/src/editor/component.rs
@@ -481,7 +481,7 @@ fn inspect_light_transform(
 
         if ui.button("Reset Rotation").clicked() {
             transform.rotation = glam::DQuat::IDENTITY;
-            cfg.transform_rotation_cache.insert(*entity, DVec3::ZERO);
+            cfg.transform_rotation_cache.insert(*entity, DVec3::NEG_Y);
         }
         ui.add_space(5.0);
     }
diff --git a/crates/eucalyptus-editor/src/editor/scene.rs b/crates/eucalyptus-editor/src/editor/scene.rs
index f525312..f6dbb89 100644
--- a/crates/eucalyptus-editor/src/editor/scene.rs
+++ b/crates/eucalyptus-editor/src/editor/scene.rs
@@ -351,6 +351,8 @@ impl Scene for Editor {
         self.texture_id = Some(*graphics.texture_id.clone());
         self.window = Some(graphics.window.clone());
 
+        let hdr = graphics.hdr.read();
+
         self.show_ui(&graphics.get_egui_context());
         let clear_color = Color {
             r: 100.0 / 255.0,
@@ -394,7 +396,7 @@ impl Scene for Editor {
                 let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                     label: Some("viewport clear pass"),
                     color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                        view: &graphics.viewport_texture.view,
+                        view: hdr.view(),
                         depth_slice: None,
                         resolve_target: None,
                         ops: wgpu::Operations {
@@ -503,7 +505,7 @@ impl Scene for Editor {
                 .begin_render_pass(&wgpu::RenderPassDescriptor {
                     label: Some("light cube render pass"),
                     color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                        view: &graphics.viewport_texture.view,
+                            view: hdr.view(),
                         depth_slice: None,
                         resolve_target: None,
                         ops: wgpu::Operations {
@@ -551,7 +553,7 @@ impl Scene for Editor {
                     .begin_render_pass(&wgpu::RenderPassDescriptor {
                         label: Some("model render pass"),
                         color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                            view: &graphics.viewport_texture.view,
+                            view: hdr.view(),
                             depth_slice: None,
                             resolve_target: None,
                             ops: wgpu::Operations {
@@ -601,7 +603,7 @@ impl Scene for Editor {
                         .begin_render_pass(&wgpu::RenderPassDescriptor {
                             label: Some("model render pass"),
                             color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                                view: &graphics.viewport_texture.view,
+                                view: hdr.view(),
                                 depth_slice: None,
                                 resolve_target: None,
                                 ops: wgpu::Operations {
diff --git a/crates/redback-runtime/src/lib.rs b/crates/redback-runtime/src/lib.rs
index 864cc46..c22eb9a 100644
--- a/crates/redback-runtime/src/lib.rs
+++ b/crates/redback-runtime/src/lib.rs
@@ -220,11 +220,12 @@ impl PlayMode {
         self.shader_globals = Some(GlobalsUniform::new(graphics.clone(), Some("runtime shader globals")));
         self.collider_wireframe_pipeline = Some(ColliderWireframePipeline::new(graphics.clone()));
         
+        let hdr_format = graphics.hdr.read().format();
         self.kino = Some(KinoState::new(
             KinoWGPURenderer::new(
                 &graphics.device,
                 &graphics.queue,
-                Texture::TEXTURE_FORMAT,
+                hdr_format,
                 [
                     graphics.viewport_texture.size.width as f32,
                     graphics.viewport_texture.size.height as f32,
diff --git a/crates/redback-runtime/src/scene.rs b/crates/redback-runtime/src/scene.rs
index 1a4bdd9..fbda95f 100644
--- a/crates/redback-runtime/src/scene.rs
+++ b/crates/redback-runtime/src/scene.rs
@@ -618,6 +618,8 @@ impl Scene for PlayMode {
             a: 1.0,
         };
 
+        let hdr = graphics.hdr.read();
+
         let mut encoder = CommandEncoder::new(graphics.clone(), Some("runtime viewport encoder"));
 
         let Some(active_camera) = self.active_camera else {
@@ -646,7 +648,7 @@ impl Scene for PlayMode {
                 let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                     label: Some("viewport clear pass"),
                     color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                        view: &graphics.viewport_texture.view,
+                        view: hdr.view(),
                         depth_slice: None,
                         resolve_target: None,
                         ops: wgpu::Operations {
@@ -755,7 +757,7 @@ impl Scene for PlayMode {
                 .begin_render_pass(&wgpu::RenderPassDescriptor {
                     label: Some("light cube render pass"),
                     color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                        view: &graphics.viewport_texture.view,
+                        view: hdr.view(),
                         depth_slice: None,
                         resolve_target: None,
                         ops: wgpu::Operations {
@@ -802,7 +804,7 @@ impl Scene for PlayMode {
                     .begin_render_pass(&wgpu::RenderPassDescriptor {
                         label: Some("model render pass"),
                         color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                            view: &graphics.viewport_texture.view,
+                            view: hdr.view(),
                             depth_slice: None,
                             resolve_target: None,
                             ops: wgpu::Operations {
@@ -853,7 +855,7 @@ impl Scene for PlayMode {
                         .begin_render_pass(&wgpu::RenderPassDescriptor {
                             label: Some("model render pass"),
                             color_attachments: &[Some(wgpu::RenderPassColorAttachment {
-                                view: &graphics.viewport_texture.view,
+                                view: hdr.view(),
                                 depth_slice: None,
                                 resolve_target: None,
                                 ops: wgpu::Operations {
@@ -994,7 +996,7 @@ impl Scene for PlayMode {
 
         if let Some(kino) = &mut self.kino {
             let mut encoder = CommandEncoder::new(graphics.clone(), Some("kino encoder"));
-            kino.render(&graphics.device, &graphics.queue, &mut encoder, &graphics.viewport_texture.view);
+            kino.render(&graphics.device, &graphics.queue, &mut encoder, hdr.view());
             if let Err(e) = encoder.submit() {
                 log_once::error_once!("Unable to submit kino: {}", e);
             }