Skip to content

Fix SimpleVramAllocator #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
65 changes: 44 additions & 21 deletions examples/cube/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![no_std]
#![no_main]

use core::{ptr, f32::consts::PI};
use psp::Align16;
use core::{f32::consts::PI, ptr};
use psp::sys::{
self, ScePspFVector3, DisplayPixelFormat, GuContextType, GuSyncMode, GuSyncBehavior,
GuPrimitive, TextureFilter, TextureEffect, TextureColorComponent,
FrontFaceDirection, ShadingModel, GuState, TexturePixelFormat, DepthFunc,
VertexType, ClearBuffer, MipmapLevel,
self, ClearBuffer, DepthFunc, DisplayPixelFormat, FrontFaceDirection, GuContextType,
GuPrimitive, GuState, GuSyncBehavior, GuSyncMode, MipmapLevel, ScePspFVector3, ShadingModel,
TextureColorComponent, TextureEffect, TextureFilter, TexturePixelFormat, VertexType,
};
use psp::vram_alloc::get_vram_allocator;
use psp::{BUF_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT};
use psp::vram_alloc::VramAllocator;
use psp::Align16;
use psp::{BUF_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH};

psp::module!("sample_cube", 1, 1);

// Both width and height, this is a square image.
const IMAGE_SIZE: usize = 128;

// The image data *must* be aligned to a 16 byte boundary.
static FERRIS: Align16<[u8; IMAGE_SIZE * IMAGE_SIZE * 4]> = Align16(*include_bytes!("../ferris.bin"));
static FERRIS: Align16<[u8; IMAGE_SIZE * IMAGE_SIZE * 4]> =
Align16(*include_bytes!("../ferris.bin"));

static mut LIST: Align16<[u32; 0x40000]> = Align16([0; 0x40000]);

Expand All @@ -31,6 +31,7 @@ struct Vertex {
z: f32,
}

#[rustfmt::skip]
static VERTICES: Align16<[Vertex; 12 * 3]> = Align16([
Vertex { u: 0.0, v: 0.0, x: -1.0, y: -1.0, z: 1.0}, // 0
Vertex { u: 1.0, v: 0.0, x: -1.0, y: 1.0, z: 1.0}, // 4
Expand Down Expand Up @@ -88,10 +89,16 @@ fn psp_main() {
unsafe fn psp_main_inner() {
psp::enable_home_button();

let mut allocator = get_vram_allocator().unwrap();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888);
let zbp = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444);
let allocator = VramAllocator::take().unwrap();
let fbp0 = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
.unwrap();
let fbp1 = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
.unwrap();
let zbp = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444)
.unwrap();
// Attempting to free the three VRAM chunks at this point would give a
// compile-time error since fbp0, fbp1 and zbp are used later on
//allocator.free_all();
Expand All @@ -100,10 +107,19 @@ unsafe fn psp_main_inner() {

sys::sceGuInit();

sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0.as_mut_ptr_from_zero() as _, BUF_WIDTH as i32);
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1.as_mut_ptr_from_zero() as _, BUF_WIDTH as i32);
sys::sceGuDepthBuffer(zbp.as_mut_ptr_from_zero() as _, BUF_WIDTH as i32);
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut _);
sys::sceGuDrawBuffer(
DisplayPixelFormat::Psm8888,
fbp0.as_mut_ptr_from_zero().cast(),
BUF_WIDTH as i32,
);
sys::sceGuDispBuffer(
SCREEN_WIDTH as i32,
SCREEN_HEIGHT as i32,
fbp1.as_mut_ptr_from_zero().cast(),
BUF_WIDTH as i32,
);
sys::sceGuDepthBuffer(zbp.as_mut_ptr_from_zero().cast(), 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 All @@ -128,7 +144,10 @@ unsafe fn psp_main_inner() {
let mut val = 0.0;

loop {
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
sys::sceGuStart(
GuContextType::Direct,
&mut LIST.0 as *mut [u32; 0x40000] as *mut _,
);

// clear screen
sys::sceGuClearColor(0xff554433);
Expand All @@ -148,7 +167,11 @@ unsafe fn psp_main_inner() {
sys::sceGumLoadIdentity();

{
let pos = ScePspFVector3 { x: 0.0, y: 0.0, z: -2.5 };
let pos = ScePspFVector3 {
x: 0.0,
y: 0.0,
z: -2.5,
};
let rot = ScePspFVector3 {
x: val * 0.79 * (PI / 180.0),
y: val * 0.98 * (PI / 180.0),
Expand All @@ -162,7 +185,7 @@ unsafe fn psp_main_inner() {
// setup texture

sys::sceGuTexMode(TexturePixelFormat::Psm8888, 0, 0, 0);
sys::sceGuTexImage(MipmapLevel::None, 128, 128, 128, &FERRIS as *const _ as *const _);
sys::sceGuTexImage(MipmapLevel::None, 128, 128, 128, &FERRIS as *const _);
sys::sceGuTexFunc(TextureEffect::Replace, TextureColorComponent::Rgb);
sys::sceGuTexFilter(TextureFilter::Linear, TextureFilter::Linear);
sys::sceGuTexScale(1.0, 1.0);
Expand All @@ -175,7 +198,7 @@ unsafe fn psp_main_inner() {
VertexType::TEXTURE_32BITF | VertexType::VERTEX_32BITF | VertexType::TRANSFORM_3D,
12 * 3,
ptr::null_mut(),
&VERTICES as *const Align16<_> as *const _,
&VERTICES as *const _,
);

sys::sceGuFinish();
Expand Down
38 changes: 25 additions & 13 deletions examples/gu-background/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#![no_main]

use core::ffi::c_void;
use psp::sys::{self, GuState, TexturePixelFormat, DisplayPixelFormat};
use psp::vram_alloc::get_vram_allocator;
use psp::{BUF_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT};
use psp::sys::{self, DisplayPixelFormat, GuState, TexturePixelFormat};
use psp::vram_alloc::VramAllocator;
use psp::{BUF_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH};

psp::module!("sample_gu_background", 1, 1);

Expand All @@ -15,22 +15,35 @@ static mut LIST: psp::Align16<[u32; 0x40000]> = psp::Align16([0; 0x40000]);
fn psp_main() {
psp::enable_home_button();

let mut allocator = get_vram_allocator().unwrap();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
let zbp = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444).as_mut_ptr_from_zero();
let allocator = VramAllocator::take().unwrap();
let fbp0 = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
.unwrap()
.as_mut_ptr_from_zero();
let fbp1 = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
.unwrap()
.as_mut_ptr_from_zero();
let zbp = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm4444)
.unwrap()
.as_mut_ptr_from_zero();

unsafe {

sys::sceGuInit();
sys::sceGuStart(
sys::GuContextType::Direct,
&mut LIST as *mut _ as *mut c_void,
);
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::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::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);
sys::sceGuScissor(0, 0, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
Expand All @@ -43,13 +56,12 @@ fn psp_main() {
loop {
sys::sceGuStart(
sys::GuContextType::Direct,
&mut LIST as *mut _ as *mut c_void
&mut LIST as *mut _ as *mut c_void,
);
sys::sceGuClearColor(0xff554433);
sys::sceGuClearDepth(0);
sys::sceGuClear(
sys::ClearBuffer::COLOR_BUFFER_BIT |
sys::ClearBuffer::DEPTH_BUFFER_BIT
sys::ClearBuffer::COLOR_BUFFER_BIT | sys::ClearBuffer::DEPTH_BUFFER_BIT,
);
sys::sceGuFinish();
sys::sceGuSync(sys::GuSyncMode::Finish, sys::GuSyncBehavior::Wait);
Expand Down
11 changes: 7 additions & 4 deletions examples/gu-debug-print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![no_main]

use core::ffi::c_void;
use psp::sys::{self, TexturePixelFormat, DisplayPixelFormat};
use psp::vram_alloc::get_vram_allocator;
use psp::sys::{self, DisplayPixelFormat, TexturePixelFormat};
use psp::vram_alloc::VramAllocator;
use psp::{BUF_WIDTH, SCREEN_HEIGHT};

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

let mut allocator = get_vram_allocator().unwrap();
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
let allocator = VramAllocator::take().unwrap();
let fbp0 = allocator
.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888)
.unwrap()
.as_mut_ptr_from_zero();

unsafe {
sys::sceGuInit();
Expand Down
Loading