From 5661f558a0abfa769b68d12bfc3b50d045953baa Mon Sep 17 00:00:00 2001 From: Luke Street Date: Wed, 22 Mar 2023 15:32:49 -0400 Subject: [PATCH] Minor GUI fixes & cleanup --- retrotool-gui/src/loaders/model.rs | 10 ++----- retrotool-gui/src/loaders/texture.rs | 2 +- retrotool-gui/src/render/grid.rs | 44 ++++++++++++++++++---------- retrotool-gui/src/render/mod.rs | 21 ++++++++++++- retrotool-gui/src/render/model.rs | 23 +++------------ retrotool-gui/src/tabs/modcon.rs | 16 +++++----- retrotool-gui/src/tabs/model.rs | 3 +- 7 files changed, 67 insertions(+), 52 deletions(-) diff --git a/retrotool-gui/src/loaders/model.rs b/retrotool-gui/src/loaders/model.rs index 957ec04..52f4a0b 100644 --- a/retrotool-gui/src/loaders/model.rs +++ b/retrotool-gui/src/loaders/model.rs @@ -18,13 +18,14 @@ use retrolib::format::{ ETextureAnisotropicRatio, ETextureFilter, ETextureMipFilter, ETextureWrap, STextureSamplerData, }, - CColor4f, }; use uuid::Uuid; use wgpu_types::{AddressMode, Face, FilterMode}; use crate::{ - loaders::texture::TextureAsset, material::CustomMaterial, render::model::MESH_FLAG_OPAQUE, + loaders::texture::TextureAsset, + material::CustomMaterial, + render::{convert_color, model::MESH_FLAG_OPAQUE}, AssetRef, }; @@ -192,11 +193,6 @@ impl ModelAsset { } } -#[inline] -fn convert_color(value: &CColor4f) -> Color { - Color::rgba_linear(value.r, value.g, value.b, value.a) -} - fn build_material( key: &MaterialKey, materials: &[CMaterialCache], diff --git a/retrotool-gui/src/loaders/texture.rs b/retrotool-gui/src/loaders/texture.rs index a237f99..17450f6 100644 --- a/retrotool-gui/src/loaders/texture.rs +++ b/retrotool-gui/src/loaders/texture.rs @@ -189,7 +189,7 @@ fn texture_to_image( fn wgpu_format(format: ETextureFormat) -> Option { use wgpu_types::{AstcBlock::*, AstcChannel::*, TextureFormat::*}; Some(match format { - ETextureFormat::R8Unorm => Rgba8Unorm, + ETextureFormat::R8Unorm => R8Unorm, ETextureFormat::R8Snorm => R8Snorm, ETextureFormat::R8Uint => R8Uint, ETextureFormat::R8Sint => R8Sint, diff --git a/retrotool-gui/src/render/grid.rs b/retrotool-gui/src/render/grid.rs index e491060..84e088b 100644 --- a/retrotool-gui/src/render/grid.rs +++ b/retrotool-gui/src/render/grid.rs @@ -15,7 +15,7 @@ use bevy::{ }, renderer::{RenderContext, RenderDevice}, texture::BevyDefault, - view::{ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms}, + view::{ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms}, RenderApp, RenderSet, }, }; @@ -99,10 +99,9 @@ impl Node for GridCameraDriver { render_context: &mut RenderContext, world: &World, ) -> Result<(), NodeRunError> { - let clear_res = world.resource::(); let pipeline_res = world.resource::(); let pipeline_cache = world.resource::(); - let uniforms = world.resource::(); + let view_uniforms = world.resource::(); let view_entity = graph.get_input_entity(Self::IN_VIEW)?; let Ok(( @@ -119,7 +118,7 @@ impl Node for GridCameraDriver { Some(resource), ) = ( pipeline_cache.get_render_pipeline(pipeline_ids.id), - uniforms.uniforms.binding(), + view_uniforms.uniforms.binding(), ) else { return Ok(()); }; render_context.command_encoder().push_debug_group("grid"); @@ -131,17 +130,20 @@ impl Node for GridCameraDriver { entries: &[wgpu::BindGroupEntry { binding: 0, resource }], }); + let color_attachment = target.get_color_attachment(wgpu::Operations { + load: match settings.clear_color { + ClearColorConfig::Default => { + wgpu::LoadOp::Clear(world.resource::().0.into()) + } + ClearColorConfig::Custom(color) => wgpu::LoadOp::Clear(color.into()), + ClearColorConfig::None => wgpu::LoadOp::Load, + }, + store: true, + }); let mut render_pass = render_context.begin_tracked_render_pass(wgpu::RenderPassDescriptor { label: Some("grid_render_pass"), - color_attachments: &[Some(target.get_color_attachment(wgpu::Operations { - load: match settings.clear_color { - ClearColorConfig::Default => wgpu::LoadOp::Clear(clear_res.0.into()), - ClearColorConfig::Custom(color) => wgpu::LoadOp::Clear(color.into()), - ClearColorConfig::None => wgpu::LoadOp::Load, - }, - store: true, - }))], + color_attachments: &[Some(color_attachment)], depth_stencil_attachment: None, }); if let Some(viewport) = &camera.viewport { @@ -169,6 +171,7 @@ pub struct GridPipeline { #[derive(PartialEq, Eq, Hash, Clone)] pub struct GridPipelineKey { pub msaa_samples: u32, + pub texture_format: wgpu::TextureFormat, } impl FromWorld for GridPipeline { @@ -212,9 +215,9 @@ impl SpecializedRenderPipeline for GridPipeline { shader_defs: vec![], entry_point: "fragment".into(), targets: vec![Some(wgpu::ColorTargetState { - format: wgpu::TextureFormat::bevy_default(), + format: key.texture_format, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), - write_mask: wgpu::ColorWrites::ALL, + write_mask: wgpu::ColorWrites::COLOR, })], }), primitive: wgpu::PrimitiveState { @@ -237,12 +240,17 @@ pub fn prepare_grid_pipeline( pipeline_cache: Res, mut pipelines: ResMut>, pipeline: Res, - views: Query<(Entity, &GridSettings)>, + views: Query<(Entity, &ExtractedView), With>, msaa: Res, ) { - for (entity, _settings) in &views { + for (entity, view) in &views { let pipeline_id = pipelines.specialize(&pipeline_cache, &pipeline, GridPipelineKey { msaa_samples: msaa.samples(), + texture_format: if view.hdr { + ViewTarget::TEXTURE_FORMAT_HDR + } else { + wgpu::TextureFormat::bevy_default() + }, }); commands.entity(entity).insert(GridPipelineIds { id: pipeline_id }); } @@ -253,6 +261,10 @@ pub struct GridSettings { pub clear_color: ClearColorConfig, } +impl Default for GridSettings { + fn default() -> Self { Self { clear_color: ClearColorConfig::None } } +} + // noinspection RsSortImplTraitMembers impl ExtractComponent for GridSettings { type Filter = (); diff --git a/retrotool-gui/src/render/mod.rs b/retrotool-gui/src/render/mod.rs index 3bd9588..b0dd920 100644 --- a/retrotool-gui/src/render/mod.rs +++ b/retrotool-gui/src/render/mod.rs @@ -2,7 +2,26 @@ pub mod camera; pub mod grid; pub mod model; -use bevy::prelude::*; +use bevy::{prelude::*, render::primitives::Aabb}; +use retrolib::format::{CAABox, CColor4f, CTransform4f}; #[derive(Component)] pub struct TemporaryLabel; + +#[inline] +pub fn convert_aabb(aabb: &CAABox) -> Aabb { + let min = mint::Vector3::from(aabb.min); + let max = mint::Vector3::from(aabb.max); + Aabb::from_min_max(min.into(), max.into()) +} + +#[inline] +pub fn convert_transform(xf: &CTransform4f) -> Transform { + let mtx = mint::ColumnMatrix4::from(*xf); + Transform::from_matrix(mtx.into()) +} + +#[inline] +pub fn convert_color(value: &CColor4f) -> Color { + Color::rgba_linear(value.r, value.g, value.b, value.a) +} diff --git a/retrotool-gui/src/render/model.rs b/retrotool-gui/src/render/model.rs index f17810d..4457a29 100644 --- a/retrotool-gui/src/render/model.rs +++ b/retrotool-gui/src/render/model.rs @@ -12,12 +12,9 @@ use bit_set::BitSet; use half::prelude::*; use retrolib::{ array_ref, - format::{ - cmdl::{ - CMaterialCache, EBufferType, EVertexComponent, EVertexDataFormat, ModelData, - SVertexDataComponent, - }, - CAABox, CTransform4f, + format::cmdl::{ + CMaterialCache, EBufferType, EVertexComponent, EVertexDataFormat, ModelData, + SVertexDataComponent, }, }; use wgpu_types::PrimitiveTopology; @@ -27,6 +24,7 @@ use crate::{ material::{ ATTRIBUTE_TANGENT_1, ATTRIBUTE_TANGENT_2, ATTRIBUTE_UV_1, ATTRIBUTE_UV_2, ATTRIBUTE_UV_3, }, + render::convert_aabb, }; pub const MESH_FLAG_OPAQUE: u16 = 1; @@ -136,19 +134,6 @@ pub fn load_model(asset: &ModelAsset, meshes: &mut Assets) -> Result Aabb { - let min = mint::Vector3::from(aabb.min); - let max = mint::Vector3::from(aabb.max); - Aabb::from_min_max(min.into(), max.into()) -} - -#[inline] -pub fn convert_transform(xf: &CTransform4f) -> Transform { - let mtx = mint::ColumnMatrix4::from(*xf); - Transform::from_matrix(mtx.into()) -} - #[derive(Debug, Clone, Default)] struct VertexBufferInfo { pub attributes: Vec<(MeshVertexAttribute, VertexAttributeValues)>, diff --git a/retrotool-gui/src/tabs/modcon.rs b/retrotool-gui/src/tabs/modcon.rs index f035bc1..4fe50f0 100644 --- a/retrotool-gui/src/tabs/modcon.rs +++ b/retrotool-gui/src/tabs/modcon.rs @@ -19,9 +19,7 @@ use crate::{ }, material::CustomMaterial, render::{ - camera::ModelCamera, - grid::GridSettings, - model::{convert_transform, load_model}, + camera::ModelCamera, convert_transform, grid::GridSettings, model::load_model, TemporaryLabel, }, tabs::{model::ModelTab, SystemTab, TabState, TabType}, @@ -120,8 +118,8 @@ impl SystemTab for ModConTab { SRes, SRes>, SRes>, - SQuery<&'static Parent, With>>, - SQuery<&'static ModelLabel>, + SQuery, With>>, + SQuery>, ); fn load(&mut self, _ctx: &mut EguiContext, query: SystemParamItem<'_, '_, Self::LoadParam>) { @@ -236,8 +234,12 @@ impl SystemTab for ModConTab { let mut min = Vec3A::splat(f32::MAX); let mut max = Vec3A::splat(f32::MIN); for info in &self.models { - min = info.aabb.min().min(min); - max = info.aabb.max().max(max); + let m_min = Vec3::from(info.aabb.min()); + let m_max = Vec3::from(info.aabb.max()); + for &xf in &info.transforms { + min = min.min(Vec3A::from(xf * m_min)); + max = max.max(Vec3A::from(xf * m_max)); + } } let aabb = Aabb::from_min_max(min.into(), max.into()); self.camera.init(&aabb, true); diff --git a/retrotool-gui/src/tabs/model.rs b/retrotool-gui/src/tabs/model.rs index c891920..7c45510 100644 --- a/retrotool-gui/src/tabs/model.rs +++ b/retrotool-gui/src/tabs/model.rs @@ -24,8 +24,9 @@ use crate::{ material::CustomMaterial, render::{ camera::ModelCamera, + convert_aabb, grid::GridSettings, - model::{convert_aabb, load_model, ModelLod}, + model::{load_model, ModelLod}, TemporaryLabel, }, tabs::{