From 6b4c80ce0fa89b3b33afa69c0a72db343e4e9ad6 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 3 Feb 2024 12:46:24 -0800 Subject: [PATCH] Removed old anchor code --- crates/render/shaders/include/anchor.wgsl | 51 ----------------- crates/render/shaders/radialbar.wgsl | 31 +++++------ crates/render/shaders/ui.wgsl | 28 +++------- crates/render/src/lib.rs | 2 - crates/render/src/positionanchor.rs | 57 ------------------- crates/render/src/shaderprocessor.rs | 10 ---- crates/render/src/ui/util/radialbar.rs | 6 +- crates/render/src/ui/util/sprite.rs | 1 - crates/render/src/vertexbuffer/types.rs | 68 ++++++++--------------- 9 files changed, 48 insertions(+), 206 deletions(-) delete mode 100644 crates/render/shaders/include/anchor.wgsl delete mode 100644 crates/render/src/positionanchor.rs diff --git a/crates/render/shaders/include/anchor.wgsl b/crates/render/shaders/include/anchor.wgsl deleted file mode 100644 index ad9568d..0000000 --- a/crates/render/shaders/include/anchor.wgsl +++ /dev/null @@ -1,51 +0,0 @@ -// Given an anchored position and sprite dimensions, -// return the translation that should be applied on -// vertex coordinates -fn anchor( - anchor: u32, // Anchor index - position: vec2, // Anchored position - dim: vec2, // Sprite dimensions (width, height) -) -> vec2 { - - // TODO: remove, do in ui rust lib - var trans: vec2 = vec2(0.0, 0.0); - let window_dim = ( - vec2(global_data.window_size_w, global_data.window_size_h) / - global_data.window_scale - ); - - if anchor == 0u { // NW C (screen anchor, sprite anchor) - trans += vec2(-window_dim.x, window_dim.y) / 2.0; // origin - trans += vec2(0.0, 0.0) / 2.0; // offset - } else if anchor == 1u { // NW NW - trans += vec2(-window_dim.x, window_dim.y) / 2.0; - trans += vec2(dim.x, -dim.y) / 2.0; - } else if anchor == 2u { // NW NE - trans += vec2(-window_dim.x, window_dim.y) / 2.0; - trans += vec2(-dim.x, -dim.y) / 2.0; - } else if anchor == 3u { // NW SW - trans += vec2(-window_dim.x, window_dim.y) / 2.0; - trans += vec2(dim.x, dim.y) / 2.0; - } else if anchor == 4u { // NW SE - trans += vec2(-window_dim.x, window_dim.y) / 2.0; - trans += vec2(-dim.x, dim.y) / 2.0; - } else if anchor == 5u { // NE NE - trans += vec2(window_dim.x, window_dim.y) / 2.0; - trans += vec2(-dim.x, -dim.y) / 2.0; - } else if anchor == 6u { // C C - trans += vec2(0.0, 0.0) / 2.0; - trans += vec2(0.0, 0.0) / 2.0; - } else if anchor == 7u { // C NW - trans += vec2(0.0, 0.0) / 2.0; - trans += vec2(dim.x, -dim.y) / 2.0; - } else { // center / center as default, since it's the most visible variant. - trans += vec2(0.0, 0.0) / 2.0; - trans += vec2(0.0, 0.0) / 2.0; - } - - trans += position; - - // This renders correctly, but the offsets here are off by a factor of two. - // I'm not sure why... might be because WGPU screen coordinates are -1 to 1. - return (trans / window_dim) * 2.0; -} diff --git a/crates/render/shaders/radialbar.wgsl b/crates/render/shaders/radialbar.wgsl index bdf278a..fd8e665 100644 --- a/crates/render/shaders/radialbar.wgsl +++ b/crates/render/shaders/radialbar.wgsl @@ -1,12 +1,11 @@ // INCLUDE: global uniform header struct InstanceInput { - @location(2) anchor: u32, - @location(3) position: vec2, - @location(4) diameter: f32, - @location(5) stroke: f32, - @location(6) angle: f32, - @location(7) color: vec4, + @location(2) position: vec2, + @location(3) diameter: f32, + @location(4) stroke: f32, + @location(5) angle: f32, + @location(6) color: vec4, }; struct VertexInput { @@ -29,9 +28,6 @@ var texture_array: binding_array>; @group(0) @binding(1) var sampler_array: binding_array; - -// INCLUDE: anchor.wgsl - @vertex fn vertex_main( vertex: VertexInput, @@ -45,17 +41,18 @@ fn vertex_main( out.color = instance.color; out.angle = instance.angle; - // Center of this radial bar, in logical pixels, - // with (0, 0) at the center of the screen. - out.center = anchor( - instance.anchor, - instance.position, - vec2(instance.diameter, instance.diameter) - ) / 2.0 * ( + + let window_dim = ( + vec2(global_data.window_size_w, global_data.window_size_h) / + global_data.window_scale + ); + + // Center of this radial bar, in logical pixels, + // with (0, 0) at the center of the screen. + out.center = (instance.position / window_dim) * ( vec2(global_data.window_size_w, global_data.window_size_h) / global_data.window_scale ); - // ^ slight correction, since anchor gives us a different result than we need here return out; } diff --git a/crates/render/shaders/ui.wgsl b/crates/render/shaders/ui.wgsl index 40847a6..487d35b 100644 --- a/crates/render/shaders/ui.wgsl +++ b/crates/render/shaders/ui.wgsl @@ -1,14 +1,13 @@ // INCLUDE: global uniform header struct InstanceInput { - @location(2) anchor: u32, - @location(3) position: vec2, - @location(4) angle: f32, - @location(5) dim: vec2, - @location(6) color_transform: vec4, - @location(7) texture_index: vec2, - @location(8) texture_fade: f32, - @location(9) mask_index: vec2, + @location(2) position: vec2, + @location(3) angle: f32, + @location(4) dim: vec2, + @location(5) color_transform: vec4, + @location(6) texture_index: vec2, + @location(7) texture_fade: f32, + @location(8) mask_index: vec2, }; struct VertexInput { @@ -34,10 +33,6 @@ var texture_array: binding_array>; @group(0) @binding(1) var sampler_array: binding_array; - -// INCLUDE: anchor.wgsl - - fn transform_vertex( instance: InstanceInput, vertex_position: vec3, @@ -57,24 +52,19 @@ fn transform_vertex( vertex_position.y * scale ); + // Apply rotation (and adjust sprite angle, since sprites point north) pos = mat2x2( vec2(cos(instance.angle - 1.5708), sin(instance.angle - 1.5708)), vec2(-sin(instance.angle - 1.5708), cos(instance.angle - 1.5708)) ) * pos; - // Apply rotation (and adjust sprite angle, since sprites point north) - // Correct for screen aspect, preserving height pos = vec2( pos.x / global_data.window_aspect, pos.y ); - pos = pos + anchor( - instance.anchor, - instance.position, - instance.dim - ); + pos = pos + (instance.position / window_dim) * 2.0; return vec4(pos, 0.0, 1.0); } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 7c65bcc..426ea89 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -10,7 +10,6 @@ mod globaluniform; mod gpustate; mod pipeline; -mod positionanchor; mod renderinput; mod renderstate; mod shaderprocessor; @@ -20,7 +19,6 @@ mod ui; mod vertexbuffer; pub use gpustate::GPUState; -pub use positionanchor::PositionAnchor; pub use renderinput::RenderInput; use renderstate::*; diff --git a/crates/render/src/positionanchor.rs b/crates/render/src/positionanchor.rs deleted file mode 100644 index 262172a..0000000 --- a/crates/render/src/positionanchor.rs +++ /dev/null @@ -1,57 +0,0 @@ -/// The location of a UI element, in one of a few -/// possible coordinate systems. -/// -/// Positive Y always points up, -/// positive X always points right. -#[derive(Debug, Clone)] -pub enum PositionAnchor { - /// Position of this sprite's center, - /// relative to the nw corner of the window. - NwC, - - /// Position of this sprite's nw corner, - /// relative to the nw corner of the window. - NwNw, - - /// Position of this sprite's ne corner, - /// relative to the nw corner of the window. - NwNe, - - /// Position of this sprite's sw corner, - /// relative to the nw corner of the window. - NwSw, - - /// Position of this sprite's se corner, - /// relative to the nw corner of the window. - NwSe, - - /// Position of this sprite's ne corner, - /// relative to the ne corner of the window. - NeNe, - - /// Position of this sprite's center, - /// relative to the center of the window. - CC, - - /// Position of this sprite's NW corner, - /// relative to the center of the window. - CNw, -} - -// These offsets are implemented in wgsl shaders. - -impl PositionAnchor { - /// Get the uint that represents this anchor mode in shaders - pub fn to_int(&self) -> u32 { - match self { - Self::NwC => 0, - Self::NwNw => 1, - Self::NwNe => 2, - Self::NwSw => 3, - Self::NwSe => 4, - Self::NeNe => 5, - Self::CC => 6, - Self::CNw => 7, - } - } -} diff --git a/crates/render/src/shaderprocessor.rs b/crates/render/src/shaderprocessor.rs index bba37cc..6794c34 100644 --- a/crates/render/src/shaderprocessor.rs +++ b/crates/render/src/shaderprocessor.rs @@ -14,15 +14,5 @@ pub(crate) fn preprocess_shader( &global_uniform.shader_header(global_uniform_group), ); - // Insert common functions - let shader = shader.replace( - "// INCLUDE: anchor.wgsl", - &include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/shaders/include/", - "anchor.wgsl" - )), - ); - return shader; } diff --git a/crates/render/src/ui/util/radialbar.rs b/crates/render/src/ui/util/radialbar.rs index b5dc582..dda09d6 100644 --- a/crates/render/src/ui/util/radialbar.rs +++ b/crates/render/src/ui/util/radialbar.rs @@ -2,10 +2,7 @@ use galactica_content::Content; use std::f32::consts::TAU; use super::super::api::Rect; -use crate::{ - ui::api::Color, vertexbuffer::types::RadialBarInstance, PositionAnchor, RenderInput, - RenderState, -}; +use crate::{ui::api::Color, vertexbuffer::types::RadialBarInstance, RenderInput, RenderState}; #[derive(Debug, Clone)] pub struct RadialBar { @@ -39,7 +36,6 @@ impl RadialBar { state.push_radialbar_buffer(RadialBarInstance { position: [rect.pos.x, rect.pos.y], - anchor: PositionAnchor::CC.to_int(), diameter: rect.dim.x.min(rect.dim.y), stroke: self.stroke * input.ct.get_config().ui_scale, color: self.color.as_array(), diff --git a/crates/render/src/ui/util/sprite.rs b/crates/render/src/ui/util/sprite.rs index eee0eb2..7d425bf 100644 --- a/crates/render/src/ui/util/sprite.rs +++ b/crates/render/src/ui/util/sprite.rs @@ -53,7 +53,6 @@ impl Sprite { let anim_state = self.anim.get_texture_idx(); state.push_ui_buffer(UiInstance { - anchor: crate::PositionAnchor::CC.to_int(), position: rect.pos.into(), angle: to_radians(90.0), dim: rect.dim.into(), diff --git a/crates/render/src/vertexbuffer/types.rs b/crates/render/src/vertexbuffer/types.rs index afc20d0..33f047d 100644 --- a/crates/render/src/vertexbuffer/types.rs +++ b/crates/render/src/vertexbuffer/types.rs @@ -143,10 +143,6 @@ impl BufferObject for ObjectInstance { #[repr(C)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] pub struct UiInstance { - /// How to interpret this object's coordinates. - /// this should be generated from an AnchoredUiPosition - pub anchor: u32, - /// Position of this object in logical pixels pub position: [f32; 2], @@ -183,52 +179,46 @@ impl BufferObject for UiInstance { // instance when the shader starts processing a new instance step_mode: wgpu::VertexStepMode::Instance, attributes: &[ - // Anchor - wgpu::VertexAttribute { - offset: 0, - shader_location: 2, - format: wgpu::VertexFormat::Uint32, - }, // Position wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 1]>() as wgpu::BufferAddress, - shader_location: 3, + offset: mem::size_of::<[f32; 0]>() as wgpu::BufferAddress, + shader_location: 2, format: wgpu::VertexFormat::Float32x2, }, // Angle wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, - shader_location: 4, + offset: mem::size_of::<[f32; 2]>() as wgpu::BufferAddress, + shader_location: 3, format: wgpu::VertexFormat::Float32, }, // Dim wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress, - shader_location: 5, + offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, + shader_location: 4, format: wgpu::VertexFormat::Float32x2, }, // Color wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 6]>() as wgpu::BufferAddress, - shader_location: 6, + offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress, + shader_location: 5, format: wgpu::VertexFormat::Float32x4, }, // Texture wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 10]>() as wgpu::BufferAddress, - shader_location: 7, + offset: mem::size_of::<[f32; 9]>() as wgpu::BufferAddress, + shader_location: 6, format: wgpu::VertexFormat::Uint32x2, }, // Texture fade wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress, - shader_location: 8, + offset: mem::size_of::<[f32; 11]>() as wgpu::BufferAddress, + shader_location: 7, format: wgpu::VertexFormat::Float32, }, // Mask wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 13]>() as wgpu::BufferAddress, - shader_location: 9, + offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress, + shader_location: 8, format: wgpu::VertexFormat::Uint32x2, }, ], @@ -239,10 +229,6 @@ impl BufferObject for UiInstance { #[repr(C)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] pub struct RadialBarInstance { - /// How to interpret this object's coordinates. - /// this should be generated from an AnchoredUiPosition - pub anchor: u32, - /// Position of this object in logical pixels pub position: [f32; 2], @@ -267,40 +253,34 @@ impl BufferObject for RadialBarInstance { array_stride: Self::SIZE, step_mode: wgpu::VertexStepMode::Instance, attributes: &[ - // Anchor - wgpu::VertexAttribute { - offset: 0, - shader_location: 2, - format: wgpu::VertexFormat::Uint32, - }, // Position wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 1]>() as wgpu::BufferAddress, - shader_location: 3, + offset: mem::size_of::<[f32; 0]>() as wgpu::BufferAddress, + shader_location: 2, format: wgpu::VertexFormat::Float32x2, }, // Diameter + wgpu::VertexAttribute { + offset: mem::size_of::<[f32; 2]>() as wgpu::BufferAddress, + shader_location: 3, + format: wgpu::VertexFormat::Float32, + }, + // Width wgpu::VertexAttribute { offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, shader_location: 4, format: wgpu::VertexFormat::Float32, }, - // Width + // Angle wgpu::VertexAttribute { offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress, shader_location: 5, format: wgpu::VertexFormat::Float32, }, - // Angle + // Color wgpu::VertexAttribute { offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress, shader_location: 6, - format: wgpu::VertexFormat::Float32, - }, - // Color - wgpu::VertexAttribute { - offset: mem::size_of::<[f32; 6]>() as wgpu::BufferAddress, - shader_location: 7, format: wgpu::VertexFormat::Float32x4, }, ],