RetroArch/gfx/drivers/ps2_gfx.c

400 lines
10 KiB
C
Raw Normal View History

2018-09-18 08:08:06 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2018-09-18 08:08:06 +02:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../video_driver.h"
2018-09-18 08:08:06 +02:00
#include "../../driver.h"
#include "../../verbosity.h"
2018-09-18 08:08:06 +02:00
#include <loadcore.h>
#include <kernel.h>
#include <gsKit.h>
#include <gsInline.h>
2018-09-18 08:08:06 +02:00
2018-10-10 08:02:13 +02:00
#define GS_SCREEN_WIDTH 640;
#define GS_SCREEN_HEIGHT 448;
#define GS_BLACK GS_SETREG_RGBAQ(0x00,0x00,0x00,0x00,0x00) // turn black GS Screen
2018-10-15 00:48:22 +02:00
#define GS_WHITE GS_SETREG_RGBAQ(0xFF,0xFF,0xFF,0x00,0x00) // turn white GS Screen
#define GS_TEXCOL GS_SETREG_RGBAQ(0x80,0x80,0x80,0x80,0x00)
2018-09-18 08:08:06 +02:00
typedef struct ps2_video
2018-09-18 08:08:06 +02:00
{
GSGLOBAL *gsGlobal;
2018-10-31 19:45:43 +01:00
GSTEXTURE *menuTexture;
GSTEXTURE *coreTexture;
bool menuVisible;
} ps2_video_t;
2018-09-18 08:08:06 +02:00
// PRIVATE METHODS
static void initGSGlobal(ps2_video_t *ps2) {
2018-10-31 19:45:43 +01:00
ps2->gsGlobal = gsKit_init_global()
2018-09-18 08:08:06 +02:00
2018-10-15 00:48:22 +02:00
ps2->gsGlobal->PSM = GS_PSM_CT16;
// ps2->gsGlobal->PSMZ = GS_PSMZ_16S;
ps2->gsGlobal->DoubleBuffering = GS_SETTING_OFF;
ps2->gsGlobal->ZBuffering = GS_SETTING_OFF;
ps2->gsGlobal->PrimAlphaEnable = GS_SETTING_ON; /* Enable alpha blending for primitives. */
2018-09-18 08:08:06 +02:00
2018-10-15 00:48:22 +02:00
dmaKit_init(D_CTRL_RELE_OFF,D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
2018-09-18 08:08:06 +02:00
2018-10-15 00:48:22 +02:00
// Initialize the DMAC
dmaKit_chan_init(DMA_CHANNEL_GIF);
2018-09-18 08:08:06 +02:00
2018-10-15 00:48:22 +02:00
gsKit_init_screen(ps2->gsGlobal);
2018-09-18 08:08:06 +02:00
2018-10-15 00:48:22 +02:00
// gsKit_mode_switch(ps2->gsGlobal, GS_PERSISTENT);
gsKit_mode_switch(ps2->gsGlobal, GS_ONESHOT);
gsKit_clear(ps2->gsGlobal, GS_WHITE);
}
2018-09-18 08:08:06 +02:00
2018-10-10 08:02:13 +02:00
static u32 textureSize(GSTEXTURE *texture) {
return gsKit_texture_size(texture->Width, texture->Height, texture->PSM);
}
static size_t gskitTextureSize(GSTEXTURE *texture) {
return gsKit_texture_size_ee(texture->Width, texture->Height, texture->PSM);
2018-09-18 08:08:06 +02:00
}
2018-10-31 19:45:43 +01:00
static void initTexture(GSTEXTURE *texture) {
texture = malloc(sizeof *texture);
2018-09-18 08:08:06 +02:00
}
static void deinitTexturePTR(void *texture_ptr) {
if(texture_ptr!=NULL){
free(texture_ptr);
texture_ptr=NULL;
2018-09-18 08:08:06 +02:00
}
}
2018-09-18 08:08:06 +02:00
static void deinitTexture(GSTEXTURE *texture) {
deinitTexturePTR(texture->Mem);
deinitTexturePTR(texture->Clut);
}
2018-09-18 08:08:06 +02:00
2018-10-10 08:02:13 +02:00
static void *ps2_gfx_init(const video_info_t *video,
const input_driver_t **input, void **input_data)
{
2018-10-17 19:02:50 +02:00
void *ps2input = NULL;
*input_data = NULL;
(void)video;
2018-09-18 08:08:06 +02:00
ps2_video_t *ps2 = (ps2_video_t*)calloc(1, sizeof(ps2_video_t));
2018-09-18 08:08:06 +02:00
2018-10-31 19:45:43 +01:00
if (!ps2)
return NULL;
initGSGlobal(ps2);
2018-10-31 19:45:43 +01:00
ps2->menuTexture = malloc(sizeof *ps2->menuTexture);
ps2->coreTexture = malloc(sizeof *ps2->coreTexture);
2018-09-18 08:08:06 +02:00
2018-10-17 19:02:50 +02:00
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;
}
2018-09-18 08:08:06 +02:00
return ps2;
}
static bool ps2_gfx_frame(void *data, const void *frame,
2018-09-18 08:08:06 +02:00
unsigned width, unsigned height, uint64_t frame_count,
unsigned pitch, const char *msg, video_frame_info_t *video_info)
{
2018-10-10 08:02:13 +02:00
#ifdef DISPLAY_FPS
uint32_t diff;
static uint64_t currentTick,lastTick;
static int frames;
static float fps = 0.0;
#endif
ps2_video_t *ps2 = (ps2_video_t*)data;
if (!width || !height)
return false;
2018-10-31 19:45:43 +01:00
gsKit_vram_clear(ps2->gsGlobal);
if (frame)
{
int PSM = GS_PSM_CT16;
if ( !ps2->coreTexture->Mem ||
ps2->coreTexture->Width != width ||
ps2->coreTexture->Height != height ||
ps2->coreTexture->PSM != PSM ) {
ps2->coreTexture->Width = width;
ps2->coreTexture->Height = height;
ps2->coreTexture->PSM = PSM;
ps2->coreTexture->Filter = GS_FILTER_NEAREST;
free(ps2->coreTexture->Mem);
ps2->coreTexture->Mem = memalign(128, gskitTextureSize(ps2->coreTexture));
}
memcpy(ps2->coreTexture->Mem, frame, width * height * 2);
}
if (frame)
{
ps2->coreTexture->Vram=gsKit_vram_alloc(ps2->gsGlobal, textureSize(ps2->coreTexture) , GSKIT_ALLOC_USERBUFFER);
if(ps2->coreTexture->Vram == GSKIT_ALLOC_ERROR)
{
printf("VRAM Allocation Failed. Will not upload texture.\n");
}
}
if (ps2->menuVisible)
{
ps2->menuTexture->Vram=gsKit_vram_alloc(ps2->gsGlobal, textureSize(ps2->menuTexture) , GSKIT_ALLOC_USERBUFFER);
if(ps2->menuTexture->Vram == GSKIT_ALLOC_ERROR)
{
printf("VRAM Allocation Failed. Will not upload texture.\n");
}
}
if (frame)
{
gsKit_texture_upload(ps2->gsGlobal, ps2->coreTexture);
}
if (ps2->menuVisible)
{
gsKit_texture_upload(ps2->gsGlobal, ps2->menuTexture);
}
if (frame)
{
gsKit_prim_sprite_texture( ps2->gsGlobal, ps2->coreTexture,
0.0f,
0.0f, // Y1
0.0f, // U1
0.0f, // V1
ps2->gsGlobal->Width, // X2
ps2->gsGlobal->Height, // Y2
ps2->coreTexture->Width, // U2
ps2->coreTexture->Height, // V2
2,
GS_WHITE);
}
if (ps2->menuVisible)
{
gsKit_prim_sprite_texture( ps2->gsGlobal, ps2->menuTexture,
0.0f,
0.0f, // Y1
0.0f, // U1
0.0f, // V1
ps2->gsGlobal->Width, // X2
ps2->gsGlobal->Height, // Y2
ps2->menuTexture->Width, // U2
ps2->menuTexture->Height, // V2
2,
GS_WHITE);
}
2018-10-15 00:48:22 +02:00
gsKit_sync_flip(ps2->gsGlobal);
gsKit_queue_exec(ps2->gsGlobal);
2018-10-10 08:02:13 +02:00
2018-09-18 08:08:06 +02:00
return true;
}
static void ps2_gfx_set_nonblock_state(void *data, bool toggle)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)toggle;
2018-09-18 08:08:06 +02:00
}
static bool ps2_gfx_alive(void *data)
2018-09-18 08:08:06 +02:00
{
(void)data;
return true;
}
static bool ps2_gfx_focus(void *data)
2018-09-18 08:08:06 +02:00
{
(void)data;
return true;
}
static bool ps2_gfx_suppress_screensaver(void *data, bool enable)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)enable;
return false;
}
static bool ps2_gfx_has_windowed(void *data)
2018-09-18 08:08:06 +02:00
{
(void)data;
return true;
2018-09-18 08:08:06 +02:00
}
static void ps2_gfx_free(void *data)
2018-09-18 08:08:06 +02:00
{
ps2_video_t *ps2 = (ps2_video_t*)data;
2018-10-31 19:45:43 +01:00
gsKit_clear(ps2->gsGlobal, GS_WHITE);
deinitTexture(ps2->menuTexture);
deinitTexture(ps2->coreTexture);
gsKit_vram_clear(ps2->gsGlobal);
gsKit_deinit_global(ps2->gsGlobal);
2018-09-18 08:08:06 +02:00
free(data);
2018-09-18 08:08:06 +02:00
}
static bool ps2_gfx_set_shader(void *data,
enum rarch_shader_type type, const char *path)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)type;
(void)path;
2018-09-18 08:08:06 +02:00
return false;
2018-09-18 08:08:06 +02:00
}
static void ps2_gfx_set_rotation(void *data,
unsigned rotation)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)rotation;
2018-09-18 08:08:06 +02:00
}
static void ps2_gfx_viewport_info(void *data,
struct video_viewport *vp)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)vp;
2018-09-18 08:08:06 +02:00
}
static bool ps2_gfx_read_viewport(void *data, uint8_t *buffer, bool is_idle)
2018-09-18 08:08:06 +02:00
{
(void)data;
(void)buffer;
return true;
2018-09-18 08:08:06 +02:00
}
2018-10-10 08:02:13 +02:00
static void ps2_set_filtering(void *data, unsigned index, bool smooth)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
}
static void ps2_set_aspect_ratio(void *data, unsigned aspect_ratio_idx)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
}
static void ps2_apply_state_changes(void *data)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
}
static void ps2_set_texture_frame(void *data, const void *frame, bool rgb32,
unsigned width, unsigned height, float alpha)
{
ps2_video_t *ps2 = (ps2_video_t*)data;
#ifdef DEBUG
2018-10-31 19:45:43 +01:00
/* ps2->menuTexture.Mem buffer size is (640 * 448)*2 Bytes */
// retro_assert((width*height) < (480 * 448));
2018-10-10 08:02:13 +02:00
#endif
2018-10-15 00:48:22 +02:00
int PSM = rgb32 ? GS_PSM_CT32 : GS_PSM_CT16;
2018-10-10 08:02:13 +02:00
printf("ps2_set_texture_frame, width:%i, height:%i, rgb32:%i, alpha:%f\n", width, height, rgb32, alpha);
2018-10-31 19:45:43 +01:00
if ( !ps2->menuTexture->Mem ||
ps2->menuTexture->Width != width ||
ps2->menuTexture->Height != height ||
ps2->menuTexture->PSM != PSM ) {
ps2->menuTexture->Width = width;
ps2->menuTexture->Height = height;
ps2->menuTexture->PSM = PSM;
ps2->menuTexture->Filter = GS_FILTER_NEAREST;
free(ps2->menuTexture->Mem);
ps2->menuTexture->Mem = memalign(128, gskitTextureSize(ps2->menuTexture));
2018-10-15 00:48:22 +02:00
}
2018-10-31 19:45:43 +01:00
memcpy(ps2->menuTexture->Mem, frame, width * height * (rgb32 ? 4 : 2));
2018-10-10 08:02:13 +02:00
}
2018-10-31 19:45:43 +01:00
static void ps2_set_texture_enable(void *data, bool enable, bool full_screen)
2018-10-10 08:02:13 +02:00
{
(void) full_screen;
ps2_video_t *ps2 = (ps2_video_t*)data;
2018-10-31 19:45:43 +01:00
ps2->menuVisible = enable;
2018-10-10 08:02:13 +02:00
}
static const video_poke_interface_t ps2_poke_interface = {
NULL, /* get_flags */
NULL, /* set_coords */
NULL, /* set_mvp */
NULL,
NULL,
NULL,
NULL, /* get_refresh_rate */
ps2_set_filtering,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_current_framebuffer */
NULL, /* get_proc_address */
ps2_set_aspect_ratio,
ps2_apply_state_changes,
ps2_set_texture_frame,
ps2_set_texture_enable,
NULL, /* set_osd_msg */
NULL, /* show_mouse */
NULL, /* grab_mouse_toggle */
NULL, /* get_current_shader */
NULL, /* get_current_software_framebuffer */
NULL /* get_hw_render_interface */
};
static void ps2_gfx_get_poke_interface(void *data,
const video_poke_interface_t **iface)
2018-09-18 08:08:06 +02:00
{
(void)data;
2018-10-10 08:02:13 +02:00
*iface = &ps2_poke_interface;
2018-09-18 08:08:06 +02:00
}
video_driver_t video_ps2 = {
ps2_gfx_init,
ps2_gfx_frame,
ps2_gfx_set_nonblock_state,
ps2_gfx_alive,
ps2_gfx_focus,
ps2_gfx_suppress_screensaver,
ps2_gfx_has_windowed,
ps2_gfx_set_shader,
ps2_gfx_free,
2018-09-18 08:08:06 +02:00
"ps2",
NULL, /* set_viewport */
ps2_gfx_set_rotation,
ps2_gfx_viewport_info,
ps2_gfx_read_viewport,
2018-09-18 08:08:06 +02:00
NULL, /* read_frame_raw */
2018-09-18 08:08:06 +02:00
#ifdef HAVE_OVERLAY
NULL, /* overlay_interface */
2018-09-18 08:08:06 +02:00
#endif
ps2_gfx_get_poke_interface,
2018-09-18 08:08:06 +02:00
};