(PS2 gfx) Cleanups

This commit is contained in:
twinaphex 2020-02-14 13:06:56 +01:00
parent cb0c145e3d
commit 2d946d0d95

View File

@ -23,10 +23,12 @@
#include "../../libretro-common/include/libretro_gskit_ps2.h"
#define GS_TEXT GS_SETREG_RGBAQ(0x80,0x80,0x80,0x80,0x00) // turn white GS Screen
#define GS_BLACK GS_SETREG_RGBAQ(0x00,0x00,0x00,0x00,0x00) // turn white GS Screen
/* turn white GS Screen */
#define GS_TEXT GS_SETREG_RGBAQ(0x80,0x80,0x80,0x80,0x00)
/* turn white GS Screen */
#define GS_BLACK GS_SETREG_RGBAQ(0x00,0x00,0x00,0x00,0x00)
#define NTSC_WIDTH 640
#define NTSC_WIDTH 640
#define NTSC_HEIGHT 448
typedef struct ps2_video
@ -35,8 +37,11 @@ typedef struct ps2_video
GSTEXTURE *menuTexture;
GSTEXTURE *coreTexture;
bool clearVRAM;
bool clearVRAM_font; /* I need to create this additional field to be used in the font driver*/
struct retro_hw_render_interface_gskit_ps2 iface; /* Palette in the cores */
/* I need to create this additional field
* to be used in the font driver*/
bool clearVRAM_font;
/* Palette in the cores */
struct retro_hw_render_interface_gskit_ps2 iface;
bool menuVisible;
bool fullscreen;
@ -49,21 +54,21 @@ typedef struct ps2_video
int core_filter;
} ps2_video_t;
// PRIVATE METHODS
/* PRIVATE METHODS */
static GSGLOBAL *init_GSGlobal(void)
{
GSGLOBAL *gsGlobal = gsKit_init_global();
GSGLOBAL *gsGlobal = gsKit_init_global();
gsGlobal->Mode = GS_MODE_NTSC;
gsGlobal->Interlace = GS_INTERLACED;
gsGlobal->Field = GS_FIELD;
gsGlobal->Width = NTSC_WIDTH;
gsGlobal->Height = NTSC_HEIGHT;
gsGlobal->Mode = GS_MODE_NTSC;
gsGlobal->Interlace = GS_INTERLACED;
gsGlobal->Field = GS_FIELD;
gsGlobal->Width = NTSC_WIDTH;
gsGlobal->Height = NTSC_HEIGHT;
gsGlobal->PSM = GS_PSM_CT16;
gsGlobal->PSMZ = GS_PSMZ_16;
gsGlobal->PSM = GS_PSM_CT16;
gsGlobal->PSMZ = GS_PSMZ_16;
gsGlobal->DoubleBuffering = GS_SETTING_OFF;
gsGlobal->ZBuffering = GS_SETTING_OFF;
gsGlobal->ZBuffering = GS_SETTING_OFF;
gsGlobal->PrimAlphaEnable = GS_SETTING_OFF;
dmaKit_init(D_CTRL_RELE_OFF,D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
@ -87,70 +92,80 @@ static GSTEXTURE * prepare_new_texture(void)
static void init_ps2_video(ps2_video_t *ps2)
{
ps2->gsGlobal = init_GSGlobal();
ps2->gsGlobal = init_GSGlobal();
ps2->menuTexture = prepare_new_texture();
ps2->coreTexture = prepare_new_texture();
/* Used for cores that supports palette */
ps2->iface.interface_type = RETRO_HW_RENDER_INTERFACE_GSKIT_PS2;
ps2->iface.interface_type = RETRO_HW_RENDER_INTERFACE_GSKIT_PS2;
ps2->iface.interface_version = RETRO_HW_RENDER_INTERFACE_GSKIT_PS2_VERSION;
ps2->iface.coreTexture = ps2->coreTexture;
ps2->iface.coreTexture = ps2->coreTexture;
}
static void deinitTexture(GSTEXTURE *texture)
{
texture->Mem = NULL;
texture->Mem = NULL;
texture->Clut = NULL;
}
static bool texture_need_prepare(GSTEXTURE *texture, int width, int height, int PSM)
static bool texture_need_prepare(GSTEXTURE *texture,
int width, int height, int PSM)
{
return texture->Width != width || texture->Height != height || texture->PSM != PSM;
return texture->Width != width ||
texture->Height != height ||
texture->PSM != PSM;
}
static void transfer_texture(GSTEXTURE *texture, const void *frame,
int width, int height, int PSM, int filter, bool color_correction)
{
texture->Width = width;
texture->Width = width;
texture->Height = height;
texture->PSM = PSM;
texture->PSM = PSM;
texture->Filter = filter;
texture->Mem = (void *)frame;
texture->Mem = (void *)frame;
}
static void vram_alloc(GSGLOBAL *gsGlobal, GSTEXTURE *texture)
{
uint32_t size = gsKit_texture_size(texture->Width, texture->Height, texture->PSM);
uint32_t size = gsKit_texture_size(texture->Width,
texture->Height, texture->PSM);
texture->Vram = gsKit_vram_alloc(gsGlobal, size, GSKIT_ALLOC_USERBUFFER);
if(texture->Vram == GSKIT_ALLOC_ERROR) {
if(texture->Vram == GSKIT_ALLOC_ERROR)
{
printf("VRAM Allocation Failed. Will not upload texture.\n");
}
if (texture->Clut) {
if (texture->Clut)
{
/* Right now just supporting 16 x 16 = 256 colours */
size = gsKit_texture_size(16, 16, texture->ClutPSM);
size = gsKit_texture_size(16, 16, texture->ClutPSM);
texture->VramClut = gsKit_vram_alloc(gsGlobal, size , GSKIT_ALLOC_USERBUFFER);
}
}
static void prim_texture(GSGLOBAL *gsGlobal, GSTEXTURE *texture, int zPosition, bool force_aspect, struct retro_hw_ps2_insets padding)
{
float x1, y1, x2, y2;
float visible_width = texture->Width - padding.left - padding.right;
float visible_height = texture->Height - padding.top - padding.bottom;
if (force_aspect) {
float width_proportion = (float)gsGlobal->Width / (float)visible_width;
float x1, y1, x2, y2;
float visible_width = texture->Width - padding.left - padding.right;
float visible_height = texture->Height - padding.top - padding.bottom;
if (force_aspect)
{
float width_proportion = (float)gsGlobal->Width / (float)visible_width;
float height_proportion = (float)gsGlobal->Height / (float)visible_height;
float delta = MIN(width_proportion, height_proportion);
float newWidth = visible_width * delta;
float newHeight = visible_height * delta;
float delta = MIN(width_proportion, height_proportion);
float newWidth = visible_width * delta;
float newHeight = visible_height * delta;
x1 = (gsGlobal->Width - newWidth) / 2.0f;
y1 = (gsGlobal->Height - newHeight) / 2.0f;
x2 = newWidth + x1;
y2 = newHeight + y1;
} else {
}
else
{
x1 = 0.0f;
y1 = 0.0f;
x2 = gsGlobal->Width;
@ -158,38 +173,44 @@ static void prim_texture(GSGLOBAL *gsGlobal, GSTEXTURE *texture, int zPosition,
}
gsKit_prim_sprite_texture( gsGlobal, texture,
x1, //X1
y1, // Y1
padding.left, // U1
padding.top, // V1
x2, // X2
y2, // Y2
texture->Width - padding.right, // U2
texture->Height - padding.bottom, // V2
zPosition,
GS_TEXT);
x1, /* X1 */
y1, /* Y1 */
padding.left, /* U1 */
padding.top, /* V1 */
x2, /* X2 */
y2, /* Y2 */
texture->Width - padding.right, /* U2 */
texture->Height - padding.bottom, /* V2 */
zPosition,
GS_TEXT);
}
static void clearVRAMIfNeeded(ps2_video_t *ps2, const void *frame, int width, int height)
static void clearVRAMIfNeeded(ps2_video_t *ps2,
const void *frame, int width, int height)
{
if (!ps2->clearVRAM) {
if(frame && frame != RETRO_HW_FRAME_BUFFER_VALID) {
bool coreVRAMClear = false;
coreVRAMClear = texture_need_prepare(ps2->coreTexture, width, height, ps2->PSM);
if (!ps2->clearVRAM)
{
if(frame && frame != RETRO_HW_FRAME_BUFFER_VALID)
{
bool coreVRAMClear = texture_need_prepare(
ps2->coreTexture, width, height, ps2->PSM);
ps2->clearVRAM = ps2->clearVRAM || coreVRAMClear;
}
}
if (ps2->clearVRAM) {
if (ps2->clearVRAM)
{
gsKit_vram_clear(ps2->gsGlobal);
ps2->iface.updatedPalette = true;
ps2->clearVRAM_font = true; /* we need to upload also palette in the font driver */
/* we need to upload also palette in the font driver */
ps2->clearVRAM_font = true;
}
}
static void refreshScreen(ps2_video_t *ps2)
{
if (ps2->vsync) {
if (ps2->vsync)
{
gsKit_sync_flip(ps2->gsGlobal);
}
gsKit_queue_exec(ps2->gsGlobal);
@ -199,22 +220,30 @@ static void refreshScreen(ps2_video_t *ps2)
ps2->clearVRAM = false;
}
static void ps2_texture_upload(GSGLOBAL *gsGlobal, GSTEXTURE *Texture, bool sendPalette)
static void ps2_texture_upload(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
bool sendPalette)
{
gsKit_setup_tbw(Texture);
if (Texture->PSM == GS_PSM_T8) {
if (Texture->PSM == GS_PSM_T8)
{
gsKit_texture_send(Texture->Mem, Texture->Width, Texture->Height, Texture->Vram, Texture->PSM, Texture->TBW, GS_CLUT_TEXTURE);
if (sendPalette) {
if (sendPalette)
{
gsKit_texture_send(Texture->Clut, 16, 16, Texture->VramClut, Texture->ClutPSM, 1, GS_CLUT_PALLETE);
}
} else if (Texture->PSM == GS_PSM_T4) {
}
else if (Texture->PSM == GS_PSM_T4)
{
gsKit_texture_send(Texture->Mem, Texture->Width, Texture->Height, Texture->Vram, Texture->PSM, Texture->TBW, GS_CLUT_TEXTURE);
if (sendPalette) {
if (sendPalette)
{
gsKit_texture_send(Texture->Clut, 8, 2, Texture->VramClut, Texture->ClutPSM, 1, GS_CLUT_PALLETE);
}
} else {
}
else
{
gsKit_texture_send(Texture->Mem, Texture->Width, Texture->Height, Texture->Vram, Texture->PSM, Texture->TBW, GS_CLUT_NONE);
}
}
@ -222,31 +251,32 @@ static void ps2_texture_upload(GSGLOBAL *gsGlobal, GSTEXTURE *Texture, bool send
static void *ps2_gfx_init(const video_info_t *video,
input_driver_t **input, void **input_data)
{
void *ps2input = NULL;
*input_data = NULL;
(void)video;
void *ps2input = NULL;
ps2_video_t *ps2 = (ps2_video_t*)calloc(1, sizeof(ps2_video_t));
*input_data = NULL;
if (!ps2)
return NULL;
init_ps2_video(ps2);
if (video->font_enable) {
if (video->font_enable)
font_driver_init_osd(ps2, false, video->is_threaded, FONT_DRIVER_RENDER_PS2);
}
ps2->PSM = (video->rgb32 ? GS_PSM_CT32 : GS_PSM_CT16);
ps2->fullscreen = video->fullscreen;
ps2->core_filter = video->smooth ? GS_FILTER_LINEAR : GS_FILTER_NEAREST;
ps2->force_aspect = video->force_aspect;
ps2->vsync = video->vsync;
ps2->clearVRAM = true;
if (input && input_data) {
ps2->PSM = (video->rgb32 ? GS_PSM_CT32 : GS_PSM_CT16);
ps2->fullscreen = video->fullscreen;
ps2->core_filter = video->smooth ? GS_FILTER_LINEAR : GS_FILTER_NEAREST;
ps2->force_aspect = video->force_aspect;
ps2->vsync = video->vsync;
ps2->clearVRAM = true;
if (input && input_data)
{
settings_t *settings = config_get_ptr();
ps2input = input_ps2.init(settings->arrays.input_joypad_driver);
*input = ps2input ? &input_ps2 : NULL;
*input_data = ps2input;
ps2input = input_ps2.init(
settings->arrays.input_joypad_driver);
*input = ps2input ? &input_ps2 : NULL;
*input_data = ps2input;
}
return ps2;
@ -262,7 +292,8 @@ static bool ps2_gfx_frame(void *data, const void *frame,
return false;
#if defined(DEBUG)
if (frame_count%60==0) {
if (frame_count%60==0)
{
printf("ps2_gfx_frame %lu\n", frame_count);
}
#endif
@ -270,36 +301,44 @@ static bool ps2_gfx_frame(void *data, const void *frame,
clearVRAMIfNeeded(ps2, frame, width, height);
gsKit_clear(ps2->gsGlobal, GS_BLACK);
if (frame) {
if (frame)
{
bool sendPalette = false;
struct retro_hw_ps2_insets padding = empty_ps2_insets;
if (frame != RETRO_HW_FRAME_BUFFER_VALID){ /* Checking if the transfer is done in the core */
if (frame != RETRO_HW_FRAME_BUFFER_VALID)
{
/* Checking if the transfer is done in the core */
/* calculate proper width based in the pitch */
int bytes_per_pixel = (ps2->PSM == GS_PSM_CT32) ? 4 : 2;
int real_width = pitch / bytes_per_pixel;
int real_width = pitch / bytes_per_pixel;
transfer_texture(ps2->coreTexture, frame, real_width, height, ps2->PSM, ps2->core_filter, 1);
padding.right = real_width - width;
} else {
sendPalette = ps2->iface.updatedPalette;
padding.right = real_width - width;
}
else
{
sendPalette = ps2->iface.updatedPalette;
ps2->iface.updatedPalette = false;
padding = ps2->iface.padding;
if (ps2->iface.clearTexture) {
if (ps2->iface.clearTexture)
ps2->iface.clearTexture = false;
}
}
if(ps2->clearVRAM) {
if(ps2->clearVRAM)
vram_alloc(ps2->gsGlobal, ps2->coreTexture);
}
ps2_texture_upload(ps2->gsGlobal, ps2->coreTexture, sendPalette);
prim_texture(ps2->gsGlobal, ps2->coreTexture, 1, ps2->force_aspect, padding);
}
if (ps2->menuVisible) {
if (ps2->menuVisible)
{
bool texture_empty = !ps2->menuTexture->Width || !ps2->menuTexture->Height;
if (!texture_empty) {
if(ps2->clearVRAM) {
if (!texture_empty)
{
if(ps2->clearVRAM)
vram_alloc(ps2->gsGlobal, ps2->menuTexture);
}
gsKit_texture_upload(ps2->gsGlobal, ps2->menuTexture);
prim_texture(ps2->gsGlobal, ps2->menuTexture, 2, ps2->fullscreen, empty_ps2_insets);
}
@ -330,30 +369,10 @@ static void ps2_gfx_set_nonblock_state(void *data, bool toggle)
ps2->vsync = !toggle;
}
static bool ps2_gfx_alive(void *data)
{
(void)data;
return true;
}
static bool ps2_gfx_focus(void *data)
{
(void)data;
return true;
}
static bool ps2_gfx_suppress_screensaver(void *data, bool enable)
{
(void)data;
(void)enable;
return false;
}
static bool ps2_gfx_has_windowed(void *data)
{
(void)data;
return true;
}
static bool ps2_gfx_alive(void *data) { return true; }
static bool ps2_gfx_focus(void *data) { return true; }
static bool ps2_gfx_suppress_screensaver(void *data, bool enable) { return false; }
static bool ps2_gfx_has_windowed(void *data) { return false; }
static void ps2_gfx_free(void *data)
{
@ -376,14 +395,7 @@ static void ps2_gfx_free(void *data)
}
static bool ps2_gfx_set_shader(void *data,
enum rarch_shader_type type, const char *path)
{
(void)data;
(void)type;
(void)path;
return false;
}
enum rarch_shader_type type, const char *path) { return false; }
static void ps2_set_filtering(void *data, unsigned index, bool smooth)
{
@ -393,13 +405,13 @@ static void ps2_set_filtering(void *data, unsigned index, bool smooth)
}
static void ps2_set_texture_frame(void *data, const void *frame, bool rgb32,
unsigned width, unsigned height, float alpha)
unsigned width, unsigned height, float alpha)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
ps2_video_t *ps2 = (ps2_video_t*)data;
bool color_correction = false;
int PSM = (rgb32 ? GS_PSM_CT32 : GS_PSM_CT16);
bool texture_changed = texture_need_prepare(ps2->menuTexture, width, height, PSM);
int PSM = (rgb32 ? GS_PSM_CT32 : GS_PSM_CT16);
bool texture_changed = texture_need_prepare(ps2->menuTexture, width, height, PSM);
transfer_texture(ps2->menuTexture, frame, width, height, PSM, ps2->menu_filter, color_correction);
ps2->clearVRAM = ps2->clearVRAM || texture_changed;
@ -408,24 +420,27 @@ static void ps2_set_texture_frame(void *data, const void *frame, bool rgb32,
static void ps2_set_texture_enable(void *data, bool enable, bool fullscreen)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
if (ps2->menuVisible != enable) {
if (ps2->menuVisible != enable)
{
/* If Menu change status, CLEAR VRAM */
ps2->clearVRAM = true;
ps2->iface.clearTexture = true;
ps2->clearVRAM = true;
ps2->iface.clearTexture = true;
ps2->iface.updatedPalette = true;
}
ps2->menuVisible = enable;
ps2->fullscreen = fullscreen;
ps2->fullscreen = fullscreen;
}
static bool ps2_get_hw_render_interface(void* data,
const struct retro_hw_render_interface** iface)
{
ps2_video_t* ps2 = (ps2_video_t*)data;
ps2->iface.clearTexture = ps2->clearVRAM;
ps2_video_t *ps2 = (ps2_video_t*)data;
ps2->iface.clearTexture = ps2->clearVRAM;
ps2->iface.updatedPalette = true;
ps2->iface.padding = empty_ps2_insets;
*iface = (const struct retro_hw_render_interface*)&ps2->iface;
ps2->iface.padding = empty_ps2_insets;
*iface =
(const struct retro_hw_render_interface*)&ps2->iface;
return true;
}