PSP: it actually renders now I guess

This commit is contained in:
UnknownShadow200 2023-04-01 15:37:50 +11:00
parent bf52adf76c
commit e0f7e85502
2 changed files with 73 additions and 28 deletions

View File

@ -8,7 +8,7 @@ struct Stream;
/* Represents a packed 32 bit RGBA colour, suitable for native graphics API texture pixels. */ /* Represents a packed 32 bit RGBA colour, suitable for native graphics API texture pixels. */
typedef cc_uint32 BitmapCol; typedef cc_uint32 BitmapCol;
#if defined CC_BUILD_WEB || defined CC_BUILD_ANDROID #if defined CC_BUILD_WEB || defined CC_BUILD_ANDROID || defined CC_BUILD_PSP
#define BITMAPCOLOR_R_SHIFT 0 #define BITMAPCOLOR_R_SHIFT 0
#define BITMAPCOLOR_G_SHIFT 8 #define BITMAPCOLOR_G_SHIFT 8
#define BITMAPCOLOR_B_SHIFT 16 #define BITMAPCOLOR_B_SHIFT 16

View File

@ -23,17 +23,20 @@ static unsigned int __attribute__((aligned(16))) list[262144];
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------General---------------------------------------------------------* *---------------------------------------------------------General---------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static cc_uint16 gfx_indices[GFX_MAX_INDICES]; static cc_uint16 __attribute__((aligned(16))) gfx_indices[GFX_MAX_INDICES];
static int formatFields[] = { static int formatFields[] = {
GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D, GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D
}; };
static ScePspFMatrix4 identity;
static void guInit(void) { static void guInit(void) {
void* framebuffer0 = (void*)0; void* framebuffer0 = (void*)0;
void* framebuffer1 = (void*)FB_SIZE; void* framebuffer1 = (void*)FB_SIZE;
void* depthbuffer = (void*)(FB_SIZE + FB_SIZE); void* depthbuffer = (void*)(FB_SIZE + FB_SIZE);
gumLoadIdentity(&identity);
sceGuInit();
sceGuStart(GU_DIRECT, list); sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888, framebuffer0, BUFFER_WIDTH); sceGuDrawBuffer(GU_PSM_8888, framebuffer0, BUFFER_WIDTH);
sceGuDispBuffer(SCREEN_WIDTH, SCREEN_HEIGHT, framebuffer1, BUFFER_WIDTH); sceGuDispBuffer(SCREEN_WIDTH, SCREEN_HEIGHT, framebuffer1, BUFFER_WIDTH);
@ -46,9 +49,18 @@ static void guInit(void) {
sceGuShadeModel(GU_SMOOTH); sceGuShadeModel(GU_SMOOTH);
sceGuDisable(GU_TEXTURE_2D); sceGuDisable(GU_TEXTURE_2D);
// TODO needed? sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuClearColor(0); sceGuDepthFunc(GU_GEQUAL);
sceGuClearDepth(0); sceGuClearDepth(0);
sceGuDepthRange(65535,0);
sceGuSetMatrix(GU_MODEL, &identity);
sceGuColor(0xffffffff);
sceGuScissor(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
sceGuDisable(GU_SCISSOR_TEST);
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuFinish(); sceGuFinish();
sceGuSync(0,0); sceGuSync(0,0);
@ -62,11 +74,13 @@ void Gfx_Create(void) {
Gfx.MaxTexHeight = 512; Gfx.MaxTexHeight = 512;
Gfx.Created = true; Gfx.Created = true;
MakeIndices(gfx_indices, GFX_MAX_INDICES); MakeIndices(gfx_indices, GFX_MAX_INDICES);
guInit();
InitDefaultResources();
} }
cc_bool Gfx_TryRestoreContext(void) { return true; } cc_bool Gfx_TryRestoreContext(void) { return true; }
void Gfx_Free(void) { } void Gfx_Free(void) { FreeDefaultResources(); }
void Gfx_RestoreState(void) { } void Gfx_RestoreState(void) { }
void Gfx_FreeState(void) { } void Gfx_FreeState(void) { }
#define GU_Toggle(cap) if (enabled) { sceGuEnable(cap); } else { sceGuDisable(cap); } #define GU_Toggle(cap) if (enabled) { sceGuEnable(cap); } else { sceGuDisable(cap); }
@ -75,7 +89,13 @@ void Gfx_FreeState(void) { }
*---------------------------------------------------------Textures--------------------------------------------------------* *---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) { GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
return 0; int size = bmp->width * bmp->height * 4;
cc_uint8* data = memalign(16, 16 + size);
((cc_uint32*)data)[0] = bmp->width;
((cc_uint32*)data)[1] = bmp->height;
Mem_Copy(data + 16, bmp->scan0, size);
return data;
} }
void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) {
@ -85,12 +105,20 @@ void Gfx_UpdateTexturePart(GfxResourceID texId, int x, int y, struct Bitmap* par
} }
void Gfx_DeleteTexture(GfxResourceID* texId) { void Gfx_DeleteTexture(GfxResourceID* texId) {
GfxResourceID data = *texId;
if (data) Mem_Free(data);
*texId = NULL;
} }
void Gfx_EnableMipmaps(void) { } void Gfx_EnableMipmaps(void) { }
void Gfx_DisableMipmaps(void) { } void Gfx_DisableMipmaps(void) { }
void Gfx_BindTexture(GfxResourceID texId) { void Gfx_BindTexture(GfxResourceID texId) {
cc_uint8* data = (cc_uint8*)texId;
int width = ((cc_uint32*)data)[0];
int height = ((cc_uint32*)data)[1];
sceGuTexMode(GU_PSM_8888,0,0,0);
sceGuTexImage(0, width, height, width, data + 16);
} }
@ -121,11 +149,15 @@ void Gfx_SetDepthTest(cc_bool enabled) { GU_Toggle(GU_DEPTH_TEST); }
*---------------------------------------------------------Matrices--------------------------------------------------------* *---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix) { void Gfx_CalcOrthoMatrix(float width, float height, struct Matrix* matrix) {
//*matrix = Matrix_Identity;
//gumOrtho(matrix, 0.0f, width, height, 0.0f, ORTHO_NEAR, ORTHO_FAR);
Matrix_Orthographic(matrix, 0.0f, width, 0.0f, height, ORTHO_NEAR, ORTHO_FAR); Matrix_Orthographic(matrix, 0.0f, width, 0.0f, height, ORTHO_NEAR, ORTHO_FAR);
} }
void Gfx_CalcPerspectiveMatrix(float fov, float aspect, float zFar, struct Matrix* matrix) { void Gfx_CalcPerspectiveMatrix(float fov, float aspect, float zFar, struct Matrix* matrix) {
float zNear = 0.1f; float zNear = 0.1f;
Matrix_PerspectiveFieldOfView(matrix, fov, aspect, zNear, zFar); *matrix = Matrix_Identity;
gumPerspective(matrix, 70.0f, aspect, 0.1f, zFar);
//Matrix_PerspectiveFieldOfView(matrix, fov, aspect, zNear, zFar);
} }
@ -176,39 +208,55 @@ static int gfx_stride, gfx_format = -1, gfx_fields;
GfxResourceID Gfx_CreateIb(void* indices, int indicesCount) { return 0; } GfxResourceID Gfx_CreateIb(void* indices, int indicesCount) { return 0; }
void Gfx_BindIb(GfxResourceID ib) { } void Gfx_BindIb(GfxResourceID ib) { }
void Gfx_DeleteIb(GfxResourceID* ib) { } void Gfx_DeleteIb(GfxResourceID* ib) { }
static int vb_size;
GfxResourceID Gfx_CreateVb(VertexFormat fmt, int count) { GfxResourceID Gfx_CreateVb(VertexFormat fmt, int count) {
return Mem_Alloc(count, strideSizes[fmt], "gfx VB"); void* data = memalign(16, count * strideSizes[fmt]);
if (!data) Logger_Abort("Failed to allocate memory for GFX VB");
return data;
//return Mem_Alloc(count, strideSizes[fmt], "gfx VB");
} }
void Gfx_BindVb(GfxResourceID vb) { gfx_vertices = vb; } void Gfx_BindVb(GfxResourceID vb) { gfx_vertices = vb; }
void Gfx_DeleteVb(GfxResourceID* vb) { void Gfx_DeleteVb(GfxResourceID* vb) {
GfxResourceID data = *vb; GfxResourceID data = *vb;
if (!data) return; if (data) Mem_Free(data);
Mem_Free(data);
*vb = 0; *vb = 0;
} }
void* Gfx_LockVb(GfxResourceID vb, VertexFormat fmt, int count) { void* Gfx_LockVb(GfxResourceID vb, VertexFormat fmt, int count) {
vb_size = count * strideSizes[fmt];
return vb; return vb;
} }
void Gfx_UnlockVb(GfxResourceID vb) { gfx_vertices = vb; } void Gfx_UnlockVb(GfxResourceID vb) {
gfx_vertices = vb;
sceKernelDcacheWritebackInvalidateRange(vb, vb_size);
}
GfxResourceID Gfx_CreateDynamicVb(VertexFormat fmt, int maxVertices) { GfxResourceID Gfx_CreateDynamicVb(VertexFormat fmt, int maxVertices) {
return Mem_Alloc(maxVertices, strideSizes[fmt], "gfx VB"); void* data = memalign(16, maxVertices * strideSizes[fmt]);
if (!data) Logger_Abort("Failed to allocate memory for GFX VB");
return data;
//return Mem_Alloc(maxVertices, strideSizes[fmt], "gfx VB");
} }
void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count) { void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count) {
vb_size = count * strideSizes[fmt];
return vb; return vb;
} }
void Gfx_UnlockDynamicVb(GfxResourceID vb) { gfx_vertices = vb; }
void Gfx_UnlockDynamicVb(GfxResourceID vb) {
gfx_vertices = vb;
sceKernelDcacheWritebackInvalidateRange(vb, vb_size);
}
void Gfx_SetDynamicVbData(GfxResourceID vb, void* vertices, int vCount) { void Gfx_SetDynamicVbData(GfxResourceID vb, void* vertices, int vCount) {
Mem_Copy(vb, vertices, vCount * gfx_stride); Mem_Copy(vb, vertices, vCount * gfx_stride);
sceKernelDcacheWritebackInvalidateRange(vertices, vCount * gfx_stride);
} }
@ -221,13 +269,13 @@ static int gfx_fogMode = -1;
void Gfx_SetFog(cc_bool enabled) { void Gfx_SetFog(cc_bool enabled) {
gfx_fogEnabled = enabled; gfx_fogEnabled = enabled;
GU_Toggle(GU_FOG); //GU_Toggle(GU_FOG);
} }
void Gfx_SetFogCol(PackedCol color) { void Gfx_SetFogCol(PackedCol color) {
if (color == gfx_fogColor) return; if (color == gfx_fogColor) return;
gfx_fogColor = color; gfx_fogColor = color;
sceGuFog(0.0f, gfx_fogEnd, gfx_fogColor); //sceGuFog(0.0f, gfx_fogEnd, gfx_fogColor);
} }
void Gfx_SetFogDensity(float value) { void Gfx_SetFogDensity(float value) {
@ -236,7 +284,7 @@ void Gfx_SetFogDensity(float value) {
void Gfx_SetFogEnd(float value) { void Gfx_SetFogEnd(float value) {
if (value == gfx_fogEnd) return; if (value == gfx_fogEnd) return;
gfx_fogEnd = value; gfx_fogEnd = value;
sceGuFog(0.0f, gfx_fogEnd, gfx_fogColor); //sceGuFog(0.0f, gfx_fogEnd, gfx_fogColor);
} }
void Gfx_SetFogMode(FogFunc func) { void Gfx_SetFogMode(FogFunc func) {
@ -257,18 +305,15 @@ void Gfx_DepthOnlyRendering(cc_bool depthOnly) {
*---------------------------------------------------------Matrices--------------------------------------------------------* *---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static int matrix_modes[] = { GU_PROJECTION, GU_VIEW }; static int matrix_modes[] = { GU_PROJECTION, GU_VIEW };
static int lastMatrix;
void Gfx_LoadMatrix(MatrixType type, const struct Matrix* matrix) { void Gfx_LoadMatrix(MatrixType type, const struct Matrix* matrix) {
if (type != lastMatrix) { lastMatrix = type; sceGumMatrixMode(matrix_modes[type]); } ScePspFMatrix4 m;
sceGumLoadMatrix((const float*)matrix); gumLoadMatrix(&m, matrix);
sceGumUpdateMatrix(); sceGuSetMatrix(matrix_modes[type], &m);
} }
void Gfx_LoadIdentityMatrix(MatrixType type) { void Gfx_LoadIdentityMatrix(MatrixType type) {
if (type != lastMatrix) { lastMatrix = type; sceGumMatrixMode(matrix_modes[type]); } sceGuSetMatrix(matrix_modes[type], &identity);
sceGumLoadIdentity();
sceGumUpdateMatrix();
} }
void Gfx_EnableTextureOffset(float x, float y) { void Gfx_EnableTextureOffset(float x, float y) {
@ -304,14 +349,14 @@ void Gfx_DrawVb_Lines(int verticesCount) {
} }
void Gfx_DrawVb_IndexedTris_Range(int verticesCount, int startVertex) { void Gfx_DrawVb_IndexedTris_Range(int verticesCount, int startVertex) {
sceGuDrawArray(GU_TRIANGLES, gfx_fields, verticesCount, gfx_indices, gfx_vertices + startVertex * gfx_stride); sceGuDrawArray(GU_TRIANGLES, gfx_fields | GU_INDEX_16BIT, verticesCount, gfx_indices, gfx_vertices + startVertex * gfx_stride);
} }
void Gfx_DrawVb_IndexedTris(int verticesCount) { void Gfx_DrawVb_IndexedTris(int verticesCount) {
sceGuDrawArray(GU_TRIANGLES, gfx_fields, verticesCount, gfx_indices, gfx_vertices); sceGuDrawArray(GU_TRIANGLES, gfx_fields | GU_INDEX_16BIT, verticesCount, gfx_indices, gfx_vertices);
} }
void Gfx_DrawIndexedTris_T2fC4b(int verticesCount, int startVertex) { void Gfx_DrawIndexedTris_T2fC4b(int verticesCount, int startVertex) {
sceGuDrawArray(GU_TRIANGLES, gfx_fields, verticesCount, gfx_indices, gfx_vertices + startVertex * SIZEOF_VERTEX_TEXTURED); sceGuDrawArray(GU_TRIANGLES, gfx_fields | GU_INDEX_16BIT, verticesCount, gfx_indices, gfx_vertices + startVertex * SIZEOF_VERTEX_TEXTURED);
} }
#endif #endif