Fixing remaining magic numbers

This commit is contained in:
Francisco Javier Trujillo Mata
2025-05-08 17:52:07 +02:00
parent 7f1ca0ab34
commit 2ecbb9d8b8
19 changed files with 143 additions and 98 deletions

View File

@@ -31,12 +31,15 @@ enum PspDisplayPixelFormats {
}; };
enum PspDisplaySetBufSync { enum PspDisplaySetBufSync {
/** Buffer change effective immediately */ /** Buffer change effective next hsync */
PSP_DISPLAY_SETBUF_IMMEDIATE = 0, PSP_DISPLAY_SETBUF_NEXTHSYNC = 0,
/** Buffer change effective next frame */ /** Buffer change effective next vsync */
PSP_DISPLAY_SETBUF_NEXTFRAME = 1 PSP_DISPLAY_SETBUF_NEXTVSYNC = 1
}; };
/** Values for retro compatibility */
#define PSP_DISPLAY_SETBUF_IMMEDIATE PSP_DISPLAY_SETBUF_NEXTHSYNC
#define PSP_DISPLAY_SETBUF_NEXTFRAME PSP_DISPLAY_SETBUF_NEXTVSYNC
enum PspDisplayErrorCodes enum PspDisplayErrorCodes
{ {

View File

@@ -57,6 +57,7 @@ libpspgu_a_SOURCES = \
sceGuEnable.c \ sceGuEnable.c \
sceGuEndObject.c \ sceGuEndObject.c \
sceGuFinish.c \ sceGuFinish.c \
sceGuFinishId.c \
sceGuFog.c \ sceGuFog.c \
sceGuFrontFace.c \ sceGuFrontFace.c \
sceGuGetAllStatus.c \ sceGuGetAllStatus.c \

View File

@@ -116,7 +116,7 @@ extern GuLightSettings light_settings[4];
void callbackSig(int id, void *arg); void callbackSig(int id, void *arg);
void callbackFin(int id, void *arg); void callbackFin(int id, void *arg);
void resetValues(); void _sceGuResetGlobalVariables();
typedef enum GECommand typedef enum GECommand
{ {

View File

@@ -433,8 +433,10 @@ void sceGuFog(float near, float far, unsigned int color);
* Initalize the GU system * Initalize the GU system
* *
* This function MUST be called as the first function, otherwise state is undetermined. * This function MUST be called as the first function, otherwise state is undetermined.
*
* @return 0 for success, < 0 for failure
**/ **/
void sceGuInit(void); int sceGuInit(void);
/** /**
* Shutdown the GU system * Shutdown the GU system
@@ -1525,8 +1527,8 @@ void sceGuDrawArrayN(int primitive_type, int vertex_type, int vcount, int primco
* Set how the display should be set * Set how the display should be set
* *
* Available behaviours are: * Available behaviours are:
* - PSP_DISPLAY_SETBUF_IMMEDIATE - Display is swapped immediately * - PSP_DISPLAY_SETBUF_NEXTHSYNC - Display is swapped on the next hsync
* - PSP_DISPLAY_SETBUF_NEXTFRAME - Display is swapped on the next frame * - PSP_DISPLAY_SETBUF_NEXTVSYNC - Display is swapped on the next vsync
* *
* Do remember that this swaps the pointers internally, regardless of setting, so be careful to wait until the next * Do remember that this swaps the pointers internally, regardless of setting, so be careful to wait until the next
* vertical blank or use another buffering algorithm (see guSwapBuffersCallback()). * vertical blank or use another buffering algorithm (see guSwapBuffersCallback()).

View File

@@ -8,7 +8,7 @@
#include "guInternal.h" #include "guInternal.h"
void resetValues() void _sceGuResetGlobalVariables()
{ {
unsigned int i; unsigned int i;
@@ -18,7 +18,7 @@ void resetValues()
gu_current_frame = 0; gu_current_frame = 0;
gu_object_stack_depth = 0; gu_object_stack_depth = 0;
gu_display_on = 0; gu_display_on = GU_FALSE;
gu_call_mode = GU_CALL_NORMAL; gu_call_mode = GU_CALL_NORMAL;
gu_draw_buffer.pixel_size = 1; gu_draw_buffer.pixel_size = 1;

View File

@@ -10,5 +10,5 @@
void sceGuColor(unsigned int color) void sceGuColor(unsigned int color)
{ {
sceGuMaterial(7,color); sceGuMaterial(GU_AMBIENT | GU_DIFFUSE | GU_SPECULAR, color);
} }

View File

@@ -89,6 +89,6 @@ void sceGuDisable(int state)
break; break;
} }
if (state < 22) if (state < GU_MAX_STATUS)
gu_states &= ~(1 << state); gu_states &= ~(1 << state);
} }

View File

@@ -30,5 +30,5 @@ void sceGuDispBuffer(int width, int height, void *dispbp, int dispbw)
sceDisplaySetMode(0, gu_draw_buffer.width, gu_draw_buffer.height); sceDisplaySetMode(0, gu_draw_buffer.width, gu_draw_buffer.height);
if (gu_display_on) if (gu_display_on)
sceDisplaySetFrameBuf((void *)(((unsigned int)ge_edram_address) + ((unsigned int)gu_draw_buffer.disp_buffer)), dispbw, gu_draw_buffer.pixel_size, PSP_DISPLAY_SETBUF_NEXTFRAME); sceDisplaySetFrameBuf((void *)(((unsigned int)ge_edram_address) + ((unsigned int)gu_draw_buffer.disp_buffer)), dispbw, gu_draw_buffer.pixel_size, PSP_DISPLAY_SETBUF_NEXTVSYNC);
} }

View File

@@ -13,10 +13,10 @@
int sceGuDisplay(int state) int sceGuDisplay(int state)
{ {
if (state) if (state == GU_TRUE)
sceDisplaySetFrameBuf((void*)((unsigned int)ge_edram_address+(unsigned int)gu_draw_buffer.disp_buffer),gu_draw_buffer.frame_width,gu_draw_buffer.pixel_size,PSP_DISPLAY_SETBUF_NEXTFRAME); sceDisplaySetFrameBuf((void *)((unsigned int)ge_edram_address + (unsigned int)gu_draw_buffer.disp_buffer), gu_draw_buffer.frame_width, gu_draw_buffer.pixel_size, PSP_DISPLAY_SETBUF_NEXTVSYNC);
else else
sceDisplaySetFrameBuf(0,0,0,PSP_DISPLAY_SETBUF_NEXTFRAME); sceDisplaySetFrameBuf(NULL, 0, 0, PSP_DISPLAY_SETBUF_NEXTVSYNC);
gu_display_on = state; gu_display_on = state;
return state; return state;

View File

@@ -89,6 +89,6 @@ void sceGuEnable(int state)
break; break;
} }
if (state < 22) if (state < GU_MAX_STATUS)
gu_states |= (1 << state); gu_states |= (1 << state);
} }

View File

@@ -8,42 +8,6 @@
#include "guInternal.h" #include "guInternal.h"
int sceGuFinishId(unsigned int id)
{
switch (gu_curr_context)
{
case GU_DIRECT:
case GU_SEND:
{
sendCommandi(FINISH, id & 0xffff);
sendCommandiStall(END, 0);
}
break;
case GU_CALL:
{
if (gu_call_mode == GU_CALL_SIGNAL)
{
sendCommandi(SIGNAL, 0x120000);
sendCommandi(END, 0);
sendCommandiStall(NOP, 0);
}
else
{
sendCommandi(RET, 0);
}
}
break;
}
unsigned int size = ((unsigned int)gu_list->current) - ((unsigned int)gu_list->start);
// go to parent list
gu_curr_context = gu_list->parent_context;
gu_list = &gu_contexts[gu_curr_context].list;
return size;
}
int sceGuFinish(void) int sceGuFinish(void)
{ {
return sceGuFinishId(0); return sceGuFinishId(0);

45
src/gu/sceGuFinishId.c Normal file
View File

@@ -0,0 +1,45 @@
/*
* PSP Software Development Kit - https://github.com/pspdev
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2005 Jesper Svennevid
*/
#include "guInternal.h"
int sceGuFinishId(unsigned int id)
{
switch (gu_curr_context)
{
case GU_DIRECT:
case GU_SEND:
{
sendCommandi(FINISH, id & 0xffff);
sendCommandiStall(END, 0);
}
break;
case GU_CALL:
{
if (gu_call_mode == GU_CALL_SIGNAL)
{
sendCommandi(SIGNAL, 0x120000);
sendCommandi(END, 0);
sendCommandiStall(NOP, 0);
}
else
{
sendCommandi(RET, 0);
}
}
break;
}
unsigned int size = ((unsigned int)gu_list->current) - ((unsigned int)gu_list->start);
// go to parent list
gu_curr_context = gu_list->parent_context;
gu_list = &gu_contexts[gu_curr_context].list;
return size;
}

View File

@@ -235,46 +235,70 @@ static unsigned int __attribute__((aligned(16))) ge_init_list[] =
ZV(END), ZV(END),
}; };
void callbackFin(int id, void* arg) void callbackFin(int id, void *arg)
{ {
GuSettings* settings = (GuSettings*)arg; GuSettings *settings = (GuSettings *)arg;
if (settings->fin) if (settings->fin)
settings->fin(id & 0xffff); settings->fin(id & 0xffff);
} }
void callbackSig(int id, void* arg) void callbackSig(int id, void *arg)
{ {
GuSettings* settings = (GuSettings*)arg; GuSettings *settings = (GuSettings *)arg;
settings->signal_history[(settings->signal_offset++) & 15] = id & 0xffff; settings->signal_history[(settings->signal_offset++) & 15] = id & 0xffff;
if (settings->sig) if (settings->sig)
settings->sig(id & 0xffff); settings->sig(id & 0xffff);
sceKernelSetEventFlag(settings->kernel_event_flag,1); sceKernelSetEventFlag(settings->kernel_event_flag, 1);
} }
void sceGuInit(void) int sceGuInit(void)
{ {
int res;
PspGeCallbackData callback; PspGeCallbackData callback;
ge_edram_address = sceGeEdramGetAddr();
_sceGuResetGlobalVariables();
res = sceKernelCreateEventFlag("SceGuSignal", PSP_EVENT_WAITMULTIPLE, 3, 0);
if (res < 0)
{
return res;
}
gu_settings.kernel_event_flag = res;
callback.signal_func = callbackSig; callback.signal_func = callbackSig;
callback.signal_arg = &gu_settings; callback.signal_arg = &gu_settings;
callback.finish_func = callbackFin; callback.finish_func = callbackFin;
callback.finish_arg = &gu_settings; callback.finish_arg = &gu_settings;
gu_settings.ge_callback_id = sceGeSetCallback(&callback); res = sceGeSetCallback(&callback);
if (res < 0)
gu_settings.swapBuffersCallback = 0; {
gu_settings.swapBuffersBehaviour = PSP_DISPLAY_SETBUF_IMMEDIATE; sceKernelDeleteEventFlag(gu_settings.kernel_event_flag);
gu_settings.kernel_event_flag = -1;
ge_edram_address = sceGeEdramGetAddr(); return res;
}
gu_settings.ge_callback_id = res;
// initialize graphics hardware // initialize graphics hardware
ge_list_executed[0] = sceGeListEnQueue((void *)((unsigned int)ge_init_list & 0x1fffffff), 0, gu_settings.ge_callback_id, 0); res = sceGeListEnQueue((void *)((unsigned int)ge_init_list & 0x1fffffff), 0, gu_settings.ge_callback_id, 0);
if (res < 0)
resetValues(); {
sceKernelDeleteEventFlag(gu_settings.kernel_event_flag);
gu_settings.kernel_event_flag = sceKernelCreateEventFlag("SceGuSignal", 512, 3, 0); sceGeUnsetCallback(gu_settings.ge_callback_id);
gu_settings.ge_callback_id = -1;
gu_settings.kernel_event_flag = -1;
return res;
}
ge_list_executed[0] = res;
// wait for init to complete // wait for init to complete
sceGeListSync(ge_list_executed[0], 0); sceGeListSync(ge_list_executed[0], 0);
sceGeDrawSync(0);
gu_settings.swapBuffersCallback = NULL;
gu_settings.swapBuffersBehaviour = PSP_DISPLAY_SETBUF_NEXTHSYNC;
return 0;
} }

View File

@@ -13,15 +13,18 @@
int sceGuSendList(int mode, const void *list, PspGeContext *context) int sceGuSendList(int mode, const void *list, PspGeContext *context)
{ {
gu_settings.signal_offset = 0;
// TODO: figure out this structure
PspGeListArgs args; PspGeListArgs args;
args.size = 8; // Size of structure? int list_id;
args.context = context; int callback;
int list_id = 0; args.size = sizeof(PspGeListArgs);
int callback = gu_settings.ge_callback_id; args.context = context;
args.numStacks = 0;
args.stacks = NULL;
callback = gu_settings.ge_callback_id;
gu_settings.signal_offset = 0;
list_id = -1;
switch (mode) switch (mode)
{ {

View File

@@ -8,24 +8,19 @@
#include "guInternal.h" #include "guInternal.h"
void* sceGuSetCallback(int signal, GuCallback callback) void *sceGuSetCallback(int signal, GuCallback callback)
{ {
GuCallback old_callback = 0; GuCallback old_callback = NULL;
switch (signal) switch (signal)
{ {
case GU_CALLBACK_SIGNAL: case GU_CALLBACK_SIGNAL:
{
old_callback = gu_settings.sig; old_callback = gu_settings.sig;
gu_settings.sig = callback; gu_settings.sig = callback;
}
break; break;
case GU_CALLBACK_FINISH: case GU_CALLBACK_FINISH:
{
old_callback = gu_settings.fin; old_callback = gu_settings.fin;
gu_settings.fin = callback; gu_settings.fin = callback;
}
break; break;
} }

View File

@@ -10,5 +10,5 @@
void sceGuShadeModel(int mode) void sceGuShadeModel(int mode)
{ {
sendCommandi(SHADE_MODE, mode ? 1 : 0); sendCommandi(SHADE_MODE, mode);
} }

View File

@@ -15,9 +15,15 @@ int sceGuSync(int mode, int what)
{ {
switch (mode) switch (mode)
{ {
case GU_SYNC_FINISH: return sceGeDrawSync(what); case GU_SYNC_FINISH:
case GU_SYNC_LIST: return sceGeListSync(ge_list_executed[0],what); return sceGeDrawSync(what);
case GU_SYNC_SEND: return sceGeListSync(ge_list_executed[1],what); case GU_SYNC_LIST:
default: case GU_SYNC_SIGNAL: case GU_SYNC_DONE: return 0; return sceGeListSync(ge_list_executed[0], what);
case GU_SYNC_SEND:
return sceGeListSync(ge_list_executed[1], what);
default:
case GU_SYNC_SIGNAL:
case GU_SYNC_DONE:
return 0;
} }
} }

View File

@@ -129,8 +129,8 @@ int main_thread(SceSize args, void *argp){
// get vram // get vram
int ret = sceDisplayGetFrameBuf((void*)&vram32, &bufferwidth, &pixelformat, PSP_DISPLAY_SETBUF_IMMEDIATE); int ret = sceDisplayGetFrameBuf((void*)&vram32, &bufferwidth, &pixelformat, PSP_DISPLAY_SETBUF_NEXTHSYNC);
// works with both PSP_DISPLAY_SETBUF_NEXTFRAME and PSP_DISPLAY_SETBUF_IMMEDIATE // works with both PSP_DISPLAY_SETBUF_NEXTVSYNC and PSP_DISPLAY_SETBUF_NEXTHSYNC
// check if return value from sceDisplayGetFrameBuf is valid // check if return value from sceDisplayGetFrameBuf is valid
// check if vram32 is not NULL // check if vram32 is not NULL

View File

@@ -690,6 +690,8 @@ typedef struct SceKernelEventFlagOptParam SceKernelEventFlagOptParam;
/** Event flag creation attributes */ /** Event flag creation attributes */
enum PspEventFlagAttributes enum PspEventFlagAttributes
{ {
/** Allow the event flag to be waited upon by a single thread */
PSP_EVENT_WAITSINGLE = 0x00,
/** Allow the event flag to be waited upon by multiple threads */ /** Allow the event flag to be waited upon by multiple threads */
PSP_EVENT_WAITMULTIPLE = 0x200 PSP_EVENT_WAITMULTIPLE = 0x200
}; };