Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions examples/cube/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use psp::sys::{
FrontFaceDirection, ShadingModel, GuState, TexturePixelFormat, DepthFunc,
VertexType, ClearBuffer, MipmapLevel,
};
use psp::gu_utils::get_static_vram_buffer;
use psp::vram_alloc::SimpleVramAllocator;
use psp::{BUF_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT};

psp::module!("sample_cube", 1, 1);
Expand Down Expand Up @@ -88,18 +88,19 @@ fn psp_main() {
unsafe fn psp_main_inner() {
psp::enable_home_button();

let fbp0 = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let fbp1 = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let zbp = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444);
let mut allocator = SimpleVramAllocator::new();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).start();
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).start();
let zbp = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444).start();

sys::sceGumLoadIdentity();

sys::sceGuInit();

sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0, BUF_WIDTH as i32);
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1, BUF_WIDTH as i32);
sys::sceGuDepthBuffer(zbp, BUF_WIDTH as i32);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1 as _, BUF_WIDTH as i32);
sys::sceGuDepthBuffer(zbp as _, BUF_WIDTH as i32);
sys::sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
sys::sceGuViewport(2048, 2048, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
sys::sceGuDepthRange(65535, 0);
Expand Down
16 changes: 9 additions & 7 deletions examples/gu-background/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use core::ffi::c_void;
use psp::sys::{self, GuState, TexturePixelFormat, DisplayPixelFormat};
use psp::gu_utils::get_static_vram_buffer;
use psp::vram_alloc::SimpleVramAllocator;
use psp::{BUF_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT};

psp::module!("sample_gu_background", 1, 1);
Expand All @@ -15,19 +15,21 @@ static mut LIST: psp::Align16<[u32; 0x40000]> = psp::Align16([0; 0x40000]);
fn psp_main() {
psp::enable_home_button();

let mut allocator = SimpleVramAllocator::new();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).start();
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).start();
let zbp = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444).start();

unsafe {
let fbp0 = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let fbp1 = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let zbp = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444);

sys::sceGuInit();
sys::sceGuStart(
sys::GuContextType::Direct,
&mut LIST as *mut _ as *mut c_void,
);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0, BUF_WIDTH as i32);
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1, BUF_WIDTH as i32);
sys::sceGuDepthBuffer(zbp, BUF_WIDTH as i32);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1 as _, BUF_WIDTH as i32);
sys::sceGuDepthBuffer(zbp as _, BUF_WIDTH as i32);
sys::sceGuOffset(2048 - (SCREEN_WIDTH/2), 2048 - (SCREEN_HEIGHT/2));
sys::sceGuViewport(2048, 2048, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
sys::sceGuDepthRange(65535, 0);
Expand Down
8 changes: 5 additions & 3 deletions examples/gu-debug-print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use core::ffi::c_void;
use psp::sys::{self, TexturePixelFormat, DisplayPixelFormat};
use psp::gu_utils::get_static_vram_buffer;
use psp::vram_alloc::SimpleVramAllocator;
use psp::{BUF_WIDTH, SCREEN_HEIGHT};

psp::module!("sample_gu_debug", 1, 1);
Expand All @@ -16,14 +16,16 @@ static mut LIST: psp::Align16<[u32; 0x40000]> = psp::Align16([0; 0x40000]);
fn psp_main() {
psp::enable_home_button();

let mut allocator = SimpleVramAllocator::new();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).start();

unsafe {
let fbp0 = get_static_vram_buffer(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
sys::sceGuInit();
sys::sceGuStart(
sys::GuContextType::Direct,
&mut LIST as *mut _ as *mut c_void,
);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0, BUF_WIDTH as i32);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
sys::sceGuDebugPrint(100, 100, 0xff0000ff, b"Hello World\0" as *const u8);
sys::sceGuDebugFlush();

Expand Down
31 changes: 0 additions & 31 deletions psp/src/gu_utils.rs

This file was deleted.

2 changes: 1 addition & 1 deletion psp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod debug;
#[macro_use] mod vfpu;
mod eabi;
pub mod sys;
pub mod gu_utils;
pub mod vram_alloc;

#[cfg(not(feature = "stub-only"))] mod alloc_impl;
#[cfg(not(feature = "stub-only"))] pub mod panic;
Expand Down
81 changes: 81 additions & 0 deletions psp/src/vram_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::sys::TexturePixelFormat;
use crate::sys::sceGeEdramGetSize;
use core::mem::size_of;

pub struct VramMemChunk {
start: u32,
len: u32,
}

impl VramMemChunk {
fn new(start: u32, len: u32) -> Self {
Self { start, len }
}

pub fn start(&self) -> u32 {
self.start
}

pub fn len(&self) -> u32 {
self.len
}
}

// A dead-simple VRAM bump allocator.
pub struct SimpleVramAllocator {
offset: u32,
}

impl SimpleVramAllocator {
pub fn new() -> Self {
Self { offset: 0 }
}

pub fn alloc(&mut self, size: u32) -> VramMemChunk {
let old_offset = self.offset;
self.offset += size;

if self.offset > self.total_mem() {
panic!("Total VRAM size exceeded!");
}

VramMemChunk::new(old_offset, size)
}

pub fn alloc_sized<T: Sized>(&mut self, count: u32) -> VramMemChunk {
let size = size_of::<T>() as u32;
self.alloc(count * size)
}

pub fn alloc_texture_pixels(
&mut self,
width: u32,
height: u32,
psm: TexturePixelFormat,
) -> VramMemChunk {
let size = get_memory_size(width, height, psm);
self.alloc(size)
}

fn total_mem(&self) -> u32 {
unsafe {
sceGeEdramGetSize()
}
}
}

fn get_memory_size(width: u32, height: u32, psm: TexturePixelFormat) -> u32 {
match psm {
TexturePixelFormat::PsmT4 => (width * height) >> 1,
TexturePixelFormat::PsmT8 => width * height,

TexturePixelFormat::Psm5650
| TexturePixelFormat::Psm5551
| TexturePixelFormat::Psm4444
| TexturePixelFormat::PsmT16 => 2 * width * height,

TexturePixelFormat::Psm8888 | TexturePixelFormat::PsmT32 => 4 * width * height,

_ => unimplemented!(),
}
}