speed up gfx, colored lighting is out for now
This commit is contained in:
parent
3c3e961995
commit
01e6059dd1
17 changed files with 1055 additions and 250 deletions
|
|
@ -53,8 +53,11 @@ fn main() {
|
|||
.blocklist_type("pxl8_vec3")
|
||||
.blocklist_type("pxl8_vec4")
|
||||
.blocklist_type("pxl8_mat4")
|
||||
.blocklist_item(".*_simd.*")
|
||||
.blocklist_item("PXL8_SIMD.*")
|
||||
.blocklist_type("__m128.*")
|
||||
.blocklist_type(".*32x4_t|.*16x8_t")
|
||||
.raw_line("pub use crate::math::{pxl8_vec2, pxl8_vec3, pxl8_vec4, pxl8_mat4};")
|
||||
.clang_arg("-DPXL8_NO_SIMD")
|
||||
.use_core()
|
||||
.rustified_enum(".*")
|
||||
.generate()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ extern crate alloc;
|
|||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use crate::math::{Vec3, VEC3_ZERO};
|
||||
use crate::math::Vec3;
|
||||
use crate::pxl8::*;
|
||||
|
||||
pub type Vertex = pxl8_bsp_vertex;
|
||||
|
|
@ -31,8 +31,8 @@ impl Default for Face {
|
|||
side: 0,
|
||||
styles: [0; 4],
|
||||
material_id: 0,
|
||||
aabb_min: VEC3_ZERO,
|
||||
aabb_max: VEC3_ZERO,
|
||||
aabb_min: Vec3::ZERO,
|
||||
aabb_max: Vec3::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -68,7 +68,7 @@ impl Default for Plane {
|
|||
fn default() -> Self {
|
||||
Self {
|
||||
dist: 0.0,
|
||||
normal: VEC3_ZERO,
|
||||
normal: Vec3::ZERO,
|
||||
type_: 0,
|
||||
}
|
||||
}
|
||||
|
|
@ -76,7 +76,7 @@ impl Default for Plane {
|
|||
|
||||
impl Default for Vertex {
|
||||
fn default() -> Self {
|
||||
Self { position: VEC3_ZERO }
|
||||
Self { position: Vec3::ZERO }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
use core::ops::{Add, Mul, Sub};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct Vec2 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct Vec3 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
|
|
@ -16,7 +16,7 @@ pub struct Vec3 {
|
|||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct Vec4 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
|
|
@ -25,7 +25,7 @@ pub struct Vec4 {
|
|||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct Mat4 {
|
||||
pub m: [f32; 16],
|
||||
}
|
||||
|
|
@ -39,31 +39,40 @@ pub type pxl8_vec4 = Vec4;
|
|||
#[allow(non_camel_case_types)]
|
||||
pub type pxl8_mat4 = Mat4;
|
||||
|
||||
pub const VEC3_ZERO: Vec3 = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
|
||||
pub const VEC3_Y: Vec3 = Vec3 { x: 0.0, y: 1.0, z: 0.0 };
|
||||
impl Vec3 {
|
||||
pub const ZERO: Vec3 = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
|
||||
pub const Y: Vec3 = Vec3 { x: 0.0, y: 1.0, z: 0.0 };
|
||||
|
||||
pub trait Vec3Ext {
|
||||
fn new(x: f32, y: f32, z: f32) -> Self;
|
||||
fn dot(self, rhs: Self) -> f32;
|
||||
}
|
||||
|
||||
impl Vec3Ext for pxl8_vec3 {
|
||||
fn new(x: f32, y: f32, z: f32) -> Self {
|
||||
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
||||
Self { x, y, z }
|
||||
}
|
||||
|
||||
fn dot(self, rhs: Self) -> f32 {
|
||||
pub fn dot(self, rhs: Self) -> f32 {
|
||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for pxl8_vec3 {
|
||||
fn default() -> Self {
|
||||
VEC3_ZERO
|
||||
pub fn cross(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
x: self.y * rhs.z - self.z * rhs.y,
|
||||
y: self.z * rhs.x - self.x * rhs.z,
|
||||
z: self.x * rhs.y - self.y * rhs.x,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn length(self) -> f32 {
|
||||
libm::sqrtf(self.dot(self))
|
||||
}
|
||||
|
||||
pub fn normalize(self) -> Self {
|
||||
let len_sq = self.dot(self);
|
||||
if len_sq < 1e-12 {
|
||||
return Self::ZERO;
|
||||
}
|
||||
self * (1.0 / libm::sqrtf(len_sq))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for pxl8_vec3 {
|
||||
impl Add for Vec3 {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
|
|
@ -74,7 +83,7 @@ impl Add for pxl8_vec3 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Sub for pxl8_vec3 {
|
||||
impl Sub for Vec3 {
|
||||
type Output = Self;
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
|
|
@ -85,7 +94,7 @@ impl Sub for pxl8_vec3 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for pxl8_vec3 {
|
||||
impl Mul<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
fn mul(self, rhs: f32) -> Self {
|
||||
Self {
|
||||
|
|
|
|||
|
|
@ -5,8 +5,7 @@ use alloc::vec::Vec;
|
|||
use libm::sqrtf;
|
||||
|
||||
use crate::bsp::{Bsp, BspBuilder, CellPortals, Edge, Face, Leaf, Node, Plane, Portal, Vertex};
|
||||
use crate::math::{Vec3, Vec3Ext};
|
||||
use crate::pxl8::{pxl8_vec3_cross, pxl8_vec3_dot, pxl8_vec3_normalize, pxl8_vec3_scale, pxl8_vec3_add, pxl8_vec3_sub};
|
||||
use crate::math::Vec3;
|
||||
|
||||
pub const CELL_SIZE: f32 = 64.0;
|
||||
pub const WALL_HEIGHT: f32 = 128.0;
|
||||
|
|
@ -408,11 +407,11 @@ const AO_RAY_LENGTH: f32 = 48.0;
|
|||
|
||||
fn generate_hemisphere_samples(normal: Vec3) -> [Vec3; AO_NUM_SAMPLES] {
|
||||
let tangent = if normal.y.abs() < 0.9 {
|
||||
unsafe { pxl8_vec3_normalize(pxl8_vec3_cross(normal, Vec3::new(0.0, 1.0, 0.0))) }
|
||||
normal.cross(Vec3::new(0.0, 1.0, 0.0)).normalize()
|
||||
} else {
|
||||
unsafe { pxl8_vec3_normalize(pxl8_vec3_cross(normal, Vec3::new(1.0, 0.0, 0.0))) }
|
||||
normal.cross(Vec3::new(1.0, 0.0, 0.0)).normalize()
|
||||
};
|
||||
let bitangent = unsafe { pxl8_vec3_cross(normal, tangent) };
|
||||
let bitangent = normal.cross(tangent);
|
||||
|
||||
let mut samples = [Vec3::new(0.0, 0.0, 0.0); AO_NUM_SAMPLES];
|
||||
for i in 0..AO_NUM_SAMPLES {
|
||||
|
|
@ -425,46 +424,44 @@ fn generate_hemisphere_samples(normal: Vec3) -> [Vec3; AO_NUM_SAMPLES] {
|
|||
let local_y = cos_theta;
|
||||
let local_z = sin_theta * sin_phi;
|
||||
|
||||
unsafe {
|
||||
let t_contrib = pxl8_vec3_scale(tangent, local_x);
|
||||
let n_contrib = pxl8_vec3_scale(normal, local_y);
|
||||
let b_contrib = pxl8_vec3_scale(bitangent, local_z);
|
||||
samples[i] = pxl8_vec3_normalize(pxl8_vec3_add(pxl8_vec3_add(t_contrib, n_contrib), b_contrib));
|
||||
}
|
||||
let t_contrib = tangent * local_x;
|
||||
let n_contrib = normal * local_y;
|
||||
let b_contrib = bitangent * local_z;
|
||||
samples[i] = (t_contrib + n_contrib + b_contrib).normalize();
|
||||
}
|
||||
samples
|
||||
}
|
||||
|
||||
fn ray_triangle_intersect(origin: Vec3, dir: Vec3, v0: Vec3, v1: Vec3, v2: Vec3, max_dist: f32) -> bool {
|
||||
let edge1 = unsafe { pxl8_vec3_sub(v1, v0) };
|
||||
let edge2 = unsafe { pxl8_vec3_sub(v2, v0) };
|
||||
let h = unsafe { pxl8_vec3_cross(dir, edge2) };
|
||||
let a = unsafe { pxl8_vec3_dot(edge1, h) };
|
||||
let edge1 = v1 - v0;
|
||||
let edge2 = v2 - v0;
|
||||
let h = dir.cross(edge2);
|
||||
let a = edge1.dot(h);
|
||||
|
||||
if a > -0.0001 && a < 0.0001 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let f = 1.0 / a;
|
||||
let s = unsafe { pxl8_vec3_sub(origin, v0) };
|
||||
let u = f * unsafe { pxl8_vec3_dot(s, h) };
|
||||
let s = origin - v0;
|
||||
let u = f * s.dot(h);
|
||||
if u < 0.0 || u > 1.0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let q = unsafe { pxl8_vec3_cross(s, edge1) };
|
||||
let v = f * unsafe { pxl8_vec3_dot(dir, q) };
|
||||
let q = s.cross(edge1);
|
||||
let v = f * dir.dot(q);
|
||||
if v < 0.0 || u + v > 1.0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let t = f * unsafe { pxl8_vec3_dot(edge2, q) };
|
||||
let t = f * edge2.dot(q);
|
||||
t > 0.001 && t < max_dist
|
||||
}
|
||||
|
||||
fn compute_vertex_ao(bsp: &BspBuilder, pos: Vec3, normal: Vec3) -> f32 {
|
||||
let samples = generate_hemisphere_samples(normal);
|
||||
let offset_pos = unsafe { pxl8_vec3_add(pos, pxl8_vec3_scale(normal, 0.5)) };
|
||||
let offset_pos = pos + normal * 0.5;
|
||||
|
||||
let mut occluded = 0;
|
||||
|
||||
|
|
@ -526,16 +523,15 @@ fn compute_vertex_light(
|
|||
let mut total = 0.0;
|
||||
|
||||
for light in lights {
|
||||
let to_light = unsafe { pxl8_vec3_sub(light.position, pos) };
|
||||
let dist = unsafe { pxl8_vec3_dot(to_light, to_light) };
|
||||
let dist = sqrtf(dist).max(1.0);
|
||||
let to_light = light.position - pos;
|
||||
let dist = sqrtf(to_light.dot(to_light)).max(1.0);
|
||||
|
||||
if dist > light.radius {
|
||||
continue;
|
||||
}
|
||||
|
||||
let light_dir = unsafe { pxl8_vec3_normalize(to_light) };
|
||||
let ndotl = unsafe { pxl8_vec3_dot(normal, light_dir) }.max(0.0);
|
||||
let light_dir = to_light.normalize();
|
||||
let ndotl = normal.dot(light_dir).max(0.0);
|
||||
|
||||
let attenuation = (1.0 - dist / light.radius).max(0.0);
|
||||
let attenuation = attenuation * attenuation;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ extern crate alloc;
|
|||
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use crate::math::{Vec3, Vec3Ext, VEC3_ZERO};
|
||||
use crate::math::Vec3;
|
||||
use crate::pxl8::*;
|
||||
use crate::voxel::VoxelWorld;
|
||||
use crate::world::World;
|
||||
|
|
@ -17,8 +17,8 @@ const MAX_ENTITIES: usize = 1024;
|
|||
impl Default for Entity {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pos: VEC3_ZERO,
|
||||
vel: VEC3_ZERO,
|
||||
pos: Vec3::ZERO,
|
||||
vel: Vec3::ZERO,
|
||||
yaw: 0.0,
|
||||
pitch: 0.0,
|
||||
flags: 0,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue