PSP cached blending and texturing state, ABGR1555 stencil hack

This commit is contained in:
stdgregwar 2021-01-09 10:33:38 +01:00 committed by Sam Lantinga
parent fe405eb27b
commit 0f5368fe11
1 changed files with 130 additions and 97 deletions

View File

@ -56,7 +56,7 @@ static unsigned int __attribute__((aligned(16))) DisplayList[262144];
#define COL4444(r,g,b,a) ((r>>4) | ((g>>4)<<4) | ((b>>4)<<8) | ((a>>4)<<12)) #define COL4444(r,g,b,a) ((r>>4) | ((g>>4)<<4) | ((b>>4)<<8) | ((a>>4)<<12))
#define COL8888(r,g,b,a) ((r) | ((g)<<8) | ((b)<<16) | ((a)<<24)) #define COL8888(r,g,b,a) ((r) | ((g)<<8) | ((b)<<16) | ((a)<<24))
//#define LOGGING #define LOGGING
#ifdef LOGGING #ifdef LOGGING
#define LOG(...) printf(__VA_ARGS__) #define LOG(...) printf(__VA_ARGS__)
#else #else
@ -80,10 +80,17 @@ typedef struct PSP_TextureData
unsigned int format; /**< Image format - one of ::pgePixelFormat. */ unsigned int format; /**< Image format - one of ::pgePixelFormat. */
unsigned int pitch; unsigned int pitch;
SDL_bool swizzled; /**< Is image swizzled. */ SDL_bool swizzled; /**< Is image swizzled. */
struct PSP_TextureData* prevhotw; /**< More recently used render target */ struct PSP_TextureData* prevhotw; /**< More recently used render target */
struct PSP_TextureData* nexthotw; /**< Less recently used render target */ struct PSP_TextureData* nexthotw; /**< Less recently used render target */
} PSP_TextureData; } PSP_TextureData;
typedef struct
{
SDL_BlendMode mode;
unsigned int color;
int shadeModel;
SDL_Texture* texture;
} PSP_BlendState;
typedef struct typedef struct
{ {
@ -96,8 +103,7 @@ typedef struct
unsigned int bpp; /**< bits per pixel of the main display */ unsigned int bpp; /**< bits per pixel of the main display */
SDL_bool vsync; /**< wether we do vsync */ SDL_bool vsync; /**< wether we do vsync */
unsigned int currentColor; /**< current drawing color */ PSP_BlendState blendState; /**< current blend mode */
int currentBlendMode; /**< current blend mode */
PSP_TextureData* most_recent_target; /**< start of render target LRU double linked list */ PSP_TextureData* most_recent_target; /**< start of render target LRU double linked list */
PSP_TextureData* least_recent_target; /**< end of the LRU list */ PSP_TextureData* least_recent_target; /**< end of the LRU list */
@ -174,7 +180,7 @@ Swap(float *a, float *b)
static inline int static inline int
InVram(void* data) InVram(void* data)
{ {
return data < 0x04200000; return data < (void*)0x04200000;
} }
/* Return next power of 2 */ /* Return next power of 2 */
@ -264,7 +270,7 @@ LRUTargetRemove(PSP_RenderData* data, PSP_TextureData* psp_texture) {
static void static void
LRUTargetBringFront(PSP_RenderData* data, PSP_TextureData* psp_texture) { LRUTargetBringFront(PSP_RenderData* data, PSP_TextureData* psp_texture) {
LOG("Bringing %p (%dKB) front.\n", (void*)psp_texture, psp_texture->size/1024); //LOG("Bringing %p (%dKB) front.\n", (void*)psp_texture, psp_texture->size/1024);
if(data->most_recent_target == psp_texture) { if(data->most_recent_target == psp_texture) {
return; //nothing to do return; //nothing to do
} }
@ -473,7 +479,7 @@ TextureSpillLRU(PSP_RenderData* data, size_t wanted) {
if(TextureSpillToSram(data, lru) < 0) { if(TextureSpillToSram(data, lru) < 0) {
return -1; return -1;
} }
LOG("Spilled %p (%dKB) to ram", (void*)lru, lru->size/1024); LOG("Spilled %p (%dKB) to ram\n", (void*)lru, lru->size/1024);
LRUTargetRemove(data, lru); LRUTargetRemove(data, lru);
#ifdef LOGGING #ifdef LOGGING
LRUWalk(data); LRUWalk(data);
@ -508,10 +514,20 @@ TextureBindAsTarget(PSP_RenderData* data, PSP_TextureData* psp_texture) {
} }
} }
LRUTargetBringFront(data, psp_texture); LRUTargetBringFront(data, psp_texture);
#ifdef LOGGING //LRUWalk(data);
LRUWalk(data);
#endif
sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth); sceGuDrawBufferList(psp_texture->format, vrelptr(psp_texture->data), psp_texture->textureWidth);
// Stencil alpha dst hack
unsigned int dstFormat = psp_texture->format;
if(dstFormat == GU_PSM_5551) {
sceGuEnable(GU_STENCIL_TEST);
sceGuStencilOp(GU_REPLACE, GU_REPLACE, GU_REPLACE);
sceGuStencilFunc(GU_GEQUAL, 0xff, 0xff);
sceGuEnable(GU_ALPHA_TEST);
sceGuAlphaFunc(GU_GREATER, 0x00, 0xff);
} else {
sceGuDisable(GU_STENCIL_TEST);
sceGuDisable(GU_ALPHA_TEST);
}
return 0; return 0;
} }
@ -562,6 +578,9 @@ PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
psp_texture->data = valloc(psp_texture->size); psp_texture->data = valloc(psp_texture->size);
if(psp_texture->data) { if(psp_texture->data) {
LRUTargetPushFront(data, psp_texture); LRUTargetPushFront(data, psp_texture);
#ifdef LOGGING
LRUWalk(data);
#endif
} }
} else { } else {
psp_texture->data = SDL_calloc(1, psp_texture->size); psp_texture->data = SDL_calloc(1, psp_texture->size);
@ -577,12 +596,6 @@ PSP_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
return 0; return 0;
} }
static inline int
InVram(void* data)
{
return data < 0x04200000;
}
static int static int
TextureShouldSwizzle(PSP_TextureData* psp_texture, SDL_Texture *texture) TextureShouldSwizzle(PSP_TextureData* psp_texture, SDL_Texture *texture)
{ {
@ -602,13 +615,11 @@ TextureActivate(SDL_Texture * texture)
TextureSwizzle(psp_texture, NULL); TextureSwizzle(psp_texture, NULL);
} }
sceGuEnable(GU_TEXTURE_2D);
sceGuTexWrap(GU_REPEAT, GU_REPEAT); sceGuTexWrap(GU_REPEAT, GU_REPEAT);
sceGuTexMode(psp_texture->format, 0, 0, psp_texture->swizzled); sceGuTexMode(psp_texture->format, 0, 0, psp_texture->swizzled);
sceGuTexFilter(scaleMode, scaleMode); /* GU_NEAREST good for tile-map */ sceGuTexFilter(scaleMode, scaleMode); /* GU_NEAREST good for tile-map */
/* GU_LINEAR good for scaling */ /* GU_LINEAR good for scaling */
sceGuTexImage(0, psp_texture->textureWidth, psp_texture->textureHeight, psp_texture->textureWidth, psp_texture->data); sceGuTexImage(0, psp_texture->textureWidth, psp_texture->textureHeight, psp_texture->textureWidth, psp_texture->data);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
} }
static int static int
@ -985,6 +996,16 @@ PSP_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
return 0; return 0;
} }
static void
ResetBlendState(PSP_BlendState* state) {
sceGuColor(0xffffffff);
state->color = 0xffffffff;
state->mode = SDL_BLENDMODE_INVALID;
state->texture = NULL;
sceGuDisable(GU_TEXTURE_2D);
sceGuShadeModel(GU_SMOOTH);
state->shadeModel = GU_SMOOTH;
}
static void static void
StartDrawing(SDL_Renderer * renderer) StartDrawing(SDL_Renderer * renderer)
@ -995,6 +1016,7 @@ StartDrawing(SDL_Renderer * renderer)
if(!data->displayListAvail) { if(!data->displayListAvail) {
sceGuStart(GU_DIRECT, DisplayList); sceGuStart(GU_DIRECT, DisplayList);
data->displayListAvail = SDL_TRUE; data->displayListAvail = SDL_TRUE;
ResetBlendState(&data->blendState);
} }
// Check if we need a draw buffer change // Check if we need a draw buffer change
@ -1014,38 +1036,59 @@ StartDrawing(SDL_Renderer * renderer)
static void static void
PSP_SetBlendMode(SDL_Renderer * renderer, int blendMode) PSP_SetBlendState(PSP_RenderData* data, PSP_BlendState* state)
{ {
PSP_RenderData *data = (PSP_RenderData *) renderer->driverdata; PSP_BlendState* current = &data->blendState;
if (blendMode != data-> currentBlendMode) {
switch (blendMode) { if (state->mode != current->mode) {
switch (state->mode) {
case SDL_BLENDMODE_NONE: case SDL_BLENDMODE_NONE:
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA); sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
sceGuDisable(GU_BLEND); sceGuDisable(GU_BLEND);
break; break;
case SDL_BLENDMODE_BLEND: case SDL_BLENDMODE_BLEND:
sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA); sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuEnable(GU_BLEND); sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 );
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 ); sceGuEnable(GU_BLEND);
break; break;
case SDL_BLENDMODE_ADD: case SDL_BLENDMODE_ADD:
sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA); sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
sceGuEnable(GU_BLEND); sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0x00FFFFFF );
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, 0x00FFFFFF ); sceGuEnable(GU_BLEND);
break; break;
case SDL_BLENDMODE_MOD: case SDL_BLENDMODE_MOD:
sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA); sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
sceGuEnable(GU_BLEND); sceGuBlendFunc(GU_ADD, GU_FIX, GU_SRC_COLOR, 0, 0);
sceGuBlendFunc(GU_ADD, GU_FIX, GU_SRC_COLOR, 0, 0); sceGuEnable(GU_BLEND);
break; break;
case SDL_BLENDMODE_MUL: case SDL_BLENDMODE_MUL:
sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA); sceGuTexFunc(GU_TFX_MODULATE , GU_TCC_RGBA);
sceGuEnable(GU_BLEND); sceGuBlendFunc(GU_ADD, GU_DST_COLOR, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuBlendFunc(GU_ADD, GU_DST_COLOR, GU_ONE_MINUS_SRC_ALPHA, 0, 0); sceGuEnable(GU_BLEND);
break;
case SDL_BLENDMODE_INVALID:
break; break;
} }
data->currentBlendMode = blendMode;
} }
if(state->color != current->color) {
sceGuColor(state->color);
}
if(state->shadeModel != current->shadeModel) {
sceGuShadeModel(state->shadeModel);
}
if(state->texture != current->texture) {
if(state->texture != NULL) {
TextureActivate(state->texture);
sceGuEnable(GU_TEXTURE_2D);
} else {
sceGuDisable(GU_TEXTURE_2D);
}
}
*current = *state;
} }
static int static int
@ -1099,10 +1142,9 @@ PSP_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
const Uint8 g = cmd->data.color.g; const Uint8 g = cmd->data.color.g;
const Uint8 b = cmd->data.color.b; const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a; const Uint8 a = cmd->data.color.a;
const Uint32 color = (((Uint32)a << 24) | (b << 16) | (g << 8) | r); sceGuClearColor(GU_RGBA(r,g,b,a));
/* !!! FIXME: we could cache drawstate like clear color */ sceGuClearStencil(a);
sceGuClearColor(color); sceGuClear(GU_COLOR_BUFFER_BIT | GU_STENCIL_BUFFER_BIT);
sceGuClear(GU_COLOR_BUFFER_BIT);//|GU_FAST_CLEAR_BIT);
break; break;
} }
@ -1113,14 +1155,14 @@ PSP_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
const Uint8 g = cmd->data.draw.g; const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b; const Uint8 b = cmd->data.draw.b;
const Uint8 a = cmd->data.draw.a; const Uint8 a = cmd->data.draw.a;
const Uint32 color = (((Uint32)a << 24) | (b << 16) | (g << 8) | r); PSP_BlendState state = {
/* !!! FIXME: we could cache draw state like color, texturing, etc */ .color = GU_RGBA(r,g,b,a),
sceGuColor(color); .texture = NULL,
sceGuDisable(GU_TEXTURE_2D); .mode = cmd->data.draw.blend,
sceGuShadeModel(GU_FLAT); .shadeModel = GU_FLAT
};
PSP_SetBlendState(data, &state);
sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts); sceGuDrawArray(GU_POINTS, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_TEXTURE_2D);
break; break;
} }
@ -1131,14 +1173,14 @@ PSP_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
const Uint8 g = cmd->data.draw.g; const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b; const Uint8 b = cmd->data.draw.b;
const Uint8 a = cmd->data.draw.a; const Uint8 a = cmd->data.draw.a;
const Uint32 color = (((Uint32)a << 24) | (b << 16) | (g << 8) | r); PSP_BlendState state = {
/* !!! FIXME: we could cache draw state like color, texturing, etc */ .color = GU_RGBA(r,g,b,a),
sceGuColor(color); .texture = NULL,
sceGuDisable(GU_TEXTURE_2D); .mode = cmd->data.draw.blend,
sceGuShadeModel(GU_FLAT); .shadeModel = GU_FLAT
};
PSP_SetBlendState(data, &state);
sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts); sceGuDrawArray(GU_LINE_STRIP, GU_VERTEX_32BITF|GU_TRANSFORM_2D, count, 0, verts);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_TEXTURE_2D);
break; break;
} }
@ -1149,59 +1191,49 @@ PSP_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *verti
const Uint8 g = cmd->data.draw.g; const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b; const Uint8 b = cmd->data.draw.b;
const Uint8 a = cmd->data.draw.a; const Uint8 a = cmd->data.draw.a;
const Uint32 color = (((Uint32)a << 24) | (b << 16) | (g << 8) | r); PSP_BlendState state = {
/* !!! FIXME: we could cache draw state like color, texturing, etc */ .color = GU_RGBA(r,g,b,a),
sceGuColor(color); .texture = NULL,
sceGuDisable(GU_TEXTURE_2D); .mode = cmd->data.draw.blend,
sceGuShadeModel(GU_FLAT); .shadeModel = GU_FLAT
};
PSP_SetBlendState(data, &state);
sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts); sceGuDrawArray(GU_SPRITES, GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_TEXTURE_2D);
break; break;
} }
case SDL_RENDERCMD_COPY: { case SDL_RENDERCMD_COPY: {
const size_t count = cmd->data.draw.count; const size_t count = cmd->data.draw.count;
const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first); const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first);
const Uint8 alpha = cmd->data.draw.a; const Uint8 a = cmd->data.draw.a;
TextureActivate(cmd->data.draw.texture); const Uint8 r = cmd->data.draw.r;
PSP_SetBlendMode(renderer, cmd->data.draw.blend); const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b;
if(alpha != 255) { /* !!! FIXME: is this right? */ PSP_BlendState state = {
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA); .color = GU_RGBA(r,g,b,a),
sceGuColor(GU_RGBA(255, 255, 255, alpha)); .texture = cmd->data.draw.texture,
} else { .mode = cmd->data.draw.blend,
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA); .shadeModel = GU_SMOOTH
sceGuColor(0xFFFFFFFF); };
} PSP_SetBlendState(data, &state);
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts); sceGuDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2 * count, 0, verts);
if(alpha != 255) {
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
}
break; break;
} }
case SDL_RENDERCMD_COPY_EX: { case SDL_RENDERCMD_COPY_EX: {
const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first); const VertTV *verts = (VertTV *) (gpumem + cmd->data.draw.first);
const Uint8 alpha = cmd->data.draw.a; const Uint8 a = cmd->data.draw.a;
TextureActivate(cmd->data.draw.texture); const Uint8 r = cmd->data.draw.r;
PSP_SetBlendMode(renderer, cmd->data.draw.blend); const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b;
if(alpha != 255) { /* !!! FIXME: is this right? */ PSP_BlendState state = {
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA); .color = GU_RGBA(r,g,b,a),
sceGuColor(GU_RGBA(255, 255, 255, alpha)); .texture = cmd->data.draw.texture,
} else { .mode = cmd->data.draw.blend,
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA); .shadeModel = GU_SMOOTH
sceGuColor(0xFFFFFFFF); };
} PSP_SetBlendState(data, &state);
sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 4, 0, verts); sceGuDrawArray(GU_TRIANGLE_FAN, GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 4, 0, verts);
if(alpha != 255) {
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
}
break; break;
} }
@ -1395,6 +1427,7 @@ PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
doublebuffer = valloc(PSP_FRAME_BUFFER_SIZE*data->bpp*2); doublebuffer = valloc(PSP_FRAME_BUFFER_SIZE*data->bpp*2);
data->backbuffer = doublebuffer; data->backbuffer = doublebuffer;
data->frontbuffer = ((uint8_t*)doublebuffer)+PSP_FRAME_BUFFER_SIZE*data->bpp; data->frontbuffer = ((uint8_t*)doublebuffer)+PSP_FRAME_BUFFER_SIZE*data->bpp;
memset(&data->blendState, 0, sizeof(PSP_BlendState));
sceGuInit(); sceGuInit();
/* setup GU */ /* setup GU */
@ -1426,8 +1459,8 @@ PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
sceGuTexWrap(GU_REPEAT, GU_REPEAT); sceGuTexWrap(GU_REPEAT, GU_REPEAT);
/* Blending */ /* Blending */
sceGuEnable(GU_BLEND); //sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0); //sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuTexFilter(GU_LINEAR,GU_LINEAR); sceGuTexFilter(GU_LINEAR,GU_LINEAR);