mirror of
https://github.com/dborth/vbagx.git
synced 2024-11-01 08:25:12 +01:00
c21ff25fac
others
960 lines
26 KiB
C++
960 lines
26 KiB
C++
/****************************************************************************
|
|
* Visual Boy Advance GX
|
|
*
|
|
* Tantric September 2008
|
|
* softdev 2007
|
|
*
|
|
* video.cpp
|
|
*
|
|
* Video routines
|
|
***************************************************************************/
|
|
|
|
#include <gccore.h>
|
|
#include <ogcsys.h>
|
|
#include <ogc/machine/processor.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "vbagx.h"
|
|
#include "menu.h"
|
|
#include "input.h"
|
|
#include "vbasupport.h"
|
|
|
|
s32 CursorX, CursorY;
|
|
bool CursorVisible;
|
|
bool CursorValid;
|
|
bool TiltScreen = false;
|
|
float TiltAngle = 0;
|
|
u32 FrameTimer = 0;
|
|
|
|
/*** External 2D Video ***/
|
|
/*** 2D Video Globals ***/
|
|
GXRModeObj *vmode = NULL; // Graphics Mode Object
|
|
unsigned int *xfb[2] = { NULL, NULL }; // Framebuffers
|
|
int whichfb = 0; // Frame buffer toggle
|
|
|
|
static Mtx GXmodelView2D;
|
|
|
|
u8 * gameScreenPng = NULL;
|
|
int gameScreenPngSize = 0;
|
|
|
|
int screenheight = 480;
|
|
int screenwidth = 640;
|
|
|
|
u16 *InitialBorder = NULL;
|
|
int InitialBorderWidth = 0;
|
|
int InitialBorderHeight = 0;
|
|
bool SGBBorderLoadedFromGame = false;
|
|
|
|
/*** 3D GX ***/
|
|
#define DEFAULT_FIFO_SIZE ( 256 * 1024 )
|
|
static u8 gp_fifo[DEFAULT_FIFO_SIZE] ATTRIBUTE_ALIGN(32);
|
|
static unsigned int copynow = GX_FALSE;
|
|
|
|
/*** Texture memory ***/
|
|
static u8 *texturemem = NULL;
|
|
static int texturesize;
|
|
|
|
static GXTexObj texobj;
|
|
static Mtx view;
|
|
static int vwidth, vheight;
|
|
static int updateScaling;
|
|
bool progressive = false;
|
|
|
|
/* New texture based scaler */
|
|
typedef struct tagcamera
|
|
{
|
|
guVector pos;
|
|
guVector up;
|
|
guVector view;
|
|
}
|
|
camera;
|
|
|
|
/*** Square Matrix
|
|
This structure controls the size of the image on the screen.
|
|
***/
|
|
static s16 square[] ATTRIBUTE_ALIGN(32) = {
|
|
/*
|
|
* X, Y, Z
|
|
* Values set are for roughly 4:3 aspect
|
|
*/
|
|
-200, 200, 0, // 0
|
|
200, 200, 0, // 1
|
|
200, -200, 0, // 2
|
|
-200, -200, 0 // 3
|
|
};
|
|
|
|
static camera cam = { {0.0F, 0.0F, 0.0F},
|
|
{0.0F, 0.5F, 0.0F},
|
|
{0.0F, 0.0F, -0.5F}
|
|
};
|
|
|
|
/****************************************************************************
|
|
* VideoThreading
|
|
***************************************************************************/
|
|
#define TSTACK 16384
|
|
static lwp_t vbthread;
|
|
static unsigned char vbstack[TSTACK];
|
|
|
|
/****************************************************************************
|
|
* vbgetback
|
|
*
|
|
* This callback enables the emulator to keep running while waiting for a
|
|
* vertical blank.
|
|
*
|
|
* Putting LWP to good use :)
|
|
***************************************************************************/
|
|
static void *
|
|
vbgetback (void *arg)
|
|
{
|
|
while (1)
|
|
{
|
|
VIDEO_WaitVSync (); /**< Wait for video vertical blank */
|
|
LWP_SuspendThread (vbthread);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* copy_to_xfb
|
|
*
|
|
* Stock code to copy the GX buffer to the current display mode.
|
|
* Also increments the frameticker, as it's called for each vb.
|
|
***************************************************************************/
|
|
static inline void
|
|
copy_to_xfb (u32 arg)
|
|
{
|
|
if (copynow == GX_TRUE)
|
|
{
|
|
GX_CopyDisp (xfb[whichfb], GX_TRUE);
|
|
GX_Flush ();
|
|
copynow = GX_FALSE;
|
|
}
|
|
++FrameTimer;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Scaler Support Functions
|
|
****************************************************************************/
|
|
static inline void draw_init(void)
|
|
{
|
|
GX_ClearVtxDesc ();
|
|
GX_SetVtxDesc (GX_VA_POS, GX_INDEX8);
|
|
GX_SetVtxDesc (GX_VA_CLR0, GX_INDEX8);
|
|
GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);
|
|
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
|
|
|
|
GX_SetArray (GX_VA_POS, square, 3 * sizeof (s16));
|
|
|
|
GX_SetNumTexGens (1);
|
|
GX_SetNumChans (0);
|
|
|
|
GX_SetTexCoordGen (GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
|
|
|
|
GX_SetTevOp (GX_TEVSTAGE0, GX_REPLACE);
|
|
GX_SetTevOrder (GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLORNULL);
|
|
|
|
memset (&view, 0, sizeof (Mtx));
|
|
guLookAt(view, &cam.pos, &cam.up, &cam.view);
|
|
GX_LoadPosMtxImm (view, GX_PNMTX0);
|
|
|
|
GX_InvVtxCache (); // update vertex cache
|
|
|
|
GX_InitTexObj(&texobj, texturemem, vwidth, vheight, GX_TF_RGB565,
|
|
GX_CLAMP, GX_CLAMP, GX_FALSE);
|
|
if (GCSettings.render == 2)
|
|
GX_InitTexObjLOD(&texobj,GX_NEAR,GX_NEAR_MIP_NEAR,2.5,9.0,0.0,GX_FALSE,GX_FALSE,GX_ANISO_1); // original/unfiltered video mode: force texture filtering OFF
|
|
}
|
|
|
|
static inline void draw_vert(u8 pos, u8 c, f32 s, f32 t)
|
|
{
|
|
GX_Position1x8(pos);
|
|
GX_Color1x8(c);
|
|
GX_TexCoord2f32(s, t);
|
|
}
|
|
|
|
static inline void draw_square(Mtx v)
|
|
{
|
|
Mtx m; // model matrix.
|
|
Mtx mv; // modelview matrix.
|
|
|
|
if (TiltScreen)
|
|
{
|
|
guMtxRotDeg(m, 'z', -TiltAngle);
|
|
guMtxScaleApply(m, m, 0.8, 0.8, 1);
|
|
}
|
|
else
|
|
{
|
|
guMtxIdentity(m);
|
|
}
|
|
|
|
guMtxTransApply(m, m, 0, 0, -100);
|
|
guMtxConcat(v, m, mv);
|
|
|
|
GX_LoadPosMtxImm(mv, GX_PNMTX0);
|
|
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
|
|
draw_vert(0, 0, 0.0, 0.0);
|
|
draw_vert(1, 0, 1.0, 0.0);
|
|
draw_vert(2, 0, 1.0, 1.0);
|
|
draw_vert(3, 0, 0.0, 1.0);
|
|
GX_End();
|
|
}
|
|
|
|
#ifdef HW_RVL
|
|
static inline void draw_cursor(Mtx v)
|
|
{
|
|
if (!CursorVisible || !CursorValid)
|
|
return;
|
|
|
|
GX_InitTexObj(&texobj, pointer[0]->GetImage(), 96, 96, GX_TF_RGBA8,GX_CLAMP, GX_CLAMP,GX_FALSE);
|
|
GX_LoadTexObj(&texobj, GX_TEXMAP0);
|
|
GX_SetBlendMode(GX_BM_BLEND,GX_BL_DSTALPHA,GX_BL_INVSRCALPHA,GX_LO_CLEAR);
|
|
GX_SetTevOp (GX_TEVSTAGE0, GX_REPLACE);
|
|
GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);
|
|
|
|
Mtx m; // model matrix.
|
|
|
|
guMtxIdentity(m);
|
|
guMtxScaleApply(m, m, 0.070f, 0.10f, 0.06f);
|
|
// I needed to hack this position
|
|
guMtxTransApply(m, m, CursorX-315, 220-CursorY, -100);
|
|
|
|
GX_LoadPosMtxImm(m, GX_PNMTX0);
|
|
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
|
|
|
|
// I needed to hack the texture coords to cut out the opaque bit around the outside
|
|
draw_vert(0, 0, 0.4, 0.45);
|
|
draw_vert(1, 0, 0.76, 0.45);
|
|
draw_vert(2, 0, 0.76, 0.97);
|
|
draw_vert(3, 0, 0.4, 0.97);
|
|
|
|
GX_End();
|
|
|
|
GX_ClearVtxDesc ();
|
|
GX_SetVtxDesc (GX_VA_POS, GX_INDEX8);
|
|
GX_SetVtxDesc (GX_VA_CLR0, GX_INDEX8);
|
|
GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);
|
|
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
|
|
|
|
GX_SetArray (GX_VA_POS, square, 3 * sizeof (s16));
|
|
|
|
GX_SetNumTexGens (1);
|
|
GX_SetTexCoordGen (GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
|
|
|
|
GX_InvVtxCache (); // update vertex cache
|
|
|
|
GX_InitTexObj(&texobj, texturemem, vwidth, vheight, GX_TF_RGB565,
|
|
GX_CLAMP, GX_CLAMP, GX_FALSE);
|
|
if (GCSettings.render == 2)
|
|
GX_InitTexObjLOD(&texobj,GX_NEAR,GX_NEAR_MIP_NEAR,2.5,9.0,0.0,GX_FALSE,GX_FALSE,GX_ANISO_1); // original/unfiltered video mode: force texture filtering OFF
|
|
}
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
* StopGX
|
|
*
|
|
* Stops GX (when exiting)
|
|
***************************************************************************/
|
|
void StopGX()
|
|
{
|
|
GX_AbortFrame();
|
|
GX_Flush();
|
|
|
|
VIDEO_SetBlack(TRUE);
|
|
VIDEO_Flush();
|
|
}
|
|
|
|
/****************************************************************************
|
|
* FindVideoMode
|
|
*
|
|
* Finds the optimal video mode, or uses the user-specified one
|
|
* Also configures original video modes
|
|
***************************************************************************/
|
|
static GXRModeObj * FindVideoMode()
|
|
{
|
|
GXRModeObj * mode;
|
|
|
|
// choose the desired video mode
|
|
switch(GCSettings.videomode)
|
|
{
|
|
case 1: // NTSC (480i)
|
|
mode = &TVNtsc480IntDf;
|
|
break;
|
|
case 2: // Progressive (480p)
|
|
mode = &TVNtsc480Prog;
|
|
break;
|
|
case 3: // PAL (50Hz)
|
|
mode = &TVPal576IntDfScale;
|
|
break;
|
|
case 4: // PAL (60Hz)
|
|
mode = &TVEurgb60Hz480IntDf;
|
|
break;
|
|
case 5: // NTSC (240p)
|
|
mode = &TVNtsc240Ds;
|
|
break;
|
|
case 6: // PAL (60Hz 240p)
|
|
mode = &TVEurgb60Hz240Ds;
|
|
break;
|
|
default:
|
|
mode = VIDEO_GetPreferredMode(NULL);
|
|
|
|
#ifdef HW_DOL
|
|
/* we have component cables, but the preferred mode is interlaced
|
|
* why don't we switch into progressive?
|
|
* on the Wii, the user can do this themselves on their Wii Settings */
|
|
if(VIDEO_HaveComponentCable())
|
|
mode = &TVNtsc480Prog;
|
|
#endif
|
|
|
|
break;
|
|
}
|
|
|
|
// check for progressive scan
|
|
if (mode->viTVMode == VI_TVMODE_NTSC_PROG)
|
|
progressive = true;
|
|
else
|
|
progressive = false;
|
|
|
|
#ifdef HW_RVL
|
|
bool pal = false;
|
|
|
|
if (mode == &TVPal576IntDfScale)
|
|
pal = true;
|
|
|
|
/*if (CONF_GetAspectRatio() == CONF_ASPECT_16_9 && mode->xfbHeight != 240)
|
|
{
|
|
if (pal)
|
|
{
|
|
mode = &TVPal528IntDf;
|
|
mode->xfbHeight = 542;
|
|
mode->viHeight = 542;
|
|
}
|
|
else
|
|
{
|
|
mode->xfbHeight = 456;
|
|
mode->viHeight = 456;
|
|
}
|
|
|
|
mode->fbWidth = 640;
|
|
mode->efbHeight = 456;
|
|
mode->viWidth = 686;
|
|
}
|
|
else*/
|
|
{
|
|
mode->viWidth = 704;
|
|
}
|
|
|
|
if (pal)
|
|
{
|
|
mode->viXOrigin = (VI_MAX_WIDTH_PAL - mode->viWidth) / 2;
|
|
mode->viYOrigin = (VI_MAX_HEIGHT_PAL - mode->viHeight) / 2;
|
|
}
|
|
else
|
|
{
|
|
mode->viXOrigin = (VI_MAX_WIDTH_NTSC - mode->viWidth) / 2;
|
|
mode->viYOrigin = (VI_MAX_HEIGHT_NTSC - mode->viHeight) / 2;
|
|
}
|
|
#endif
|
|
|
|
return mode;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* SetupVideoMode
|
|
*
|
|
* Sets up the given video mode
|
|
***************************************************************************/
|
|
static void SetupVideoMode(GXRModeObj * mode)
|
|
{
|
|
if(vmode == mode)
|
|
return;
|
|
|
|
VIDEO_SetPostRetraceCallback (NULL);
|
|
copynow = GX_FALSE;
|
|
VIDEO_Configure (mode);
|
|
VIDEO_Flush();
|
|
|
|
// Clear framebuffers etc.
|
|
VIDEO_ClearFrameBuffer (mode, xfb[0], COLOR_BLACK);
|
|
VIDEO_ClearFrameBuffer (mode, xfb[1], COLOR_BLACK);
|
|
VIDEO_SetNextFramebuffer (xfb[0]);
|
|
|
|
VIDEO_SetBlack (FALSE);
|
|
VIDEO_Flush ();
|
|
VIDEO_WaitVSync ();
|
|
|
|
if (mode->viTVMode & VI_NON_INTERLACE)
|
|
VIDEO_WaitVSync();
|
|
else
|
|
while (VIDEO_GetNextField())
|
|
VIDEO_WaitVSync();
|
|
|
|
VIDEO_SetPostRetraceCallback ((VIRetraceCallback)copy_to_xfb);
|
|
vmode = mode;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* InitializeVideo
|
|
*
|
|
* This function MUST be called at startup.
|
|
* - also sets up menu video mode
|
|
***************************************************************************/
|
|
|
|
void
|
|
InitializeVideo ()
|
|
{
|
|
VIDEO_Init();
|
|
|
|
// Allocate the video buffers
|
|
xfb[0] = (u32 *) memalign(32, 640*576*2);
|
|
xfb[1] = (u32 *) memalign(32, 640*576*2);
|
|
DCInvalidateRange(xfb[0], 640*576*2);
|
|
DCInvalidateRange(xfb[1], 640*576*2);
|
|
xfb[0] = (u32 *) MEM_K0_TO_K1 (xfb[0]);
|
|
xfb[1] = (u32 *) MEM_K0_TO_K1 (xfb[1]);
|
|
|
|
GXRModeObj *rmode = FindVideoMode();
|
|
SetupVideoMode(rmode);
|
|
|
|
LWP_CreateThread (&vbthread, vbgetback, NULL, vbstack, TSTACK, 68);
|
|
|
|
// Initialise GX
|
|
GXColor background = { 0, 0, 0, 0xff };
|
|
memset (gp_fifo, 0, DEFAULT_FIFO_SIZE);
|
|
GX_Init (&gp_fifo, DEFAULT_FIFO_SIZE);
|
|
GX_SetCopyClear (background, 0x00ffffff);
|
|
GX_SetDispCopyGamma (GX_GM_1_0);
|
|
GX_SetCullMode (GX_CULL_NONE);
|
|
GX_CopyDisp (xfb[whichfb], GX_TRUE); // reset xfb
|
|
GX_Flush();
|
|
}
|
|
|
|
|
|
static inline void UpdateScaling()
|
|
{
|
|
int xscale;
|
|
int yscale;
|
|
|
|
float TvAspectRatio;
|
|
float GameboyAspectRatio;
|
|
float MaxStretchRatio = 1.6f;
|
|
|
|
if (GCSettings.scaling == 1)
|
|
MaxStretchRatio = 1.3f;
|
|
else if (GCSettings.scaling == 2)
|
|
MaxStretchRatio = 1.6f;
|
|
else
|
|
MaxStretchRatio = 1.0f;
|
|
|
|
#ifdef HW_RVL
|
|
if (CONF_GetAspectRatio() == CONF_ASPECT_16_9)
|
|
TvAspectRatio = 16.0f/9.0f;
|
|
else
|
|
TvAspectRatio = 4.0f/3.0f;
|
|
#else
|
|
if (GCSettings.scaling == 3)
|
|
TvAspectRatio = 16.0f/9.0f;
|
|
else
|
|
TvAspectRatio = 4.0f/3.0f;
|
|
#endif
|
|
|
|
GameboyAspectRatio = ((vwidth * 1.0) / vheight);
|
|
|
|
if (TvAspectRatio>GameboyAspectRatio)
|
|
{
|
|
yscale = 240; // half of TV resolution 640x480
|
|
float StretchRatio = TvAspectRatio/GameboyAspectRatio;
|
|
if (StretchRatio > MaxStretchRatio)
|
|
StretchRatio = MaxStretchRatio;
|
|
xscale = 240.0f*GameboyAspectRatio*StretchRatio * ((4.0f/3.0f)/TvAspectRatio);
|
|
}
|
|
else
|
|
{
|
|
xscale = 320; // half of TV resolution 640x480
|
|
float StretchRatio = GameboyAspectRatio/TvAspectRatio;
|
|
if (StretchRatio > MaxStretchRatio)
|
|
StretchRatio = MaxStretchRatio;
|
|
yscale = 320.0f/GameboyAspectRatio*StretchRatio / ((4.0f/3.0f)/TvAspectRatio);
|
|
}
|
|
|
|
// change zoom
|
|
float zoomHor, zoomVert;
|
|
int fixed;
|
|
if (cartridgeType == 2) // GBA
|
|
{
|
|
zoomHor = GCSettings.gbaZoomHor;
|
|
zoomVert = GCSettings.gbaZoomVert;
|
|
fixed = GCSettings.gbaFixed;
|
|
}
|
|
else
|
|
{
|
|
zoomHor = GCSettings.gbZoomHor;
|
|
zoomVert = GCSettings.gbZoomVert;
|
|
fixed = GCSettings.gbFixed;
|
|
}
|
|
|
|
if (fixed) {
|
|
xscale = 320;
|
|
yscale = 240;
|
|
} else {
|
|
xscale *= zoomHor;
|
|
yscale *= zoomVert;
|
|
}
|
|
|
|
#ifdef HW_RVL
|
|
if (fixed && CONF_GetAspectRatio() == CONF_ASPECT_16_9 && (*(u32*)(0xCD8005A0) >> 16) == 0xCAFE) // Wii U
|
|
{
|
|
/* vWii widescreen patch by tueidj */
|
|
write32(0xd8006a0, fixed ? 0x30000002 : 0x30000004), mask32(0xd8006a8, 0, 2);
|
|
}
|
|
#endif
|
|
|
|
// Set new aspect
|
|
square[0] = square[9] = -xscale + GCSettings.xshift;
|
|
square[3] = square[6] = xscale + GCSettings.xshift;
|
|
square[1] = square[4] = yscale - GCSettings.yshift;
|
|
square[7] = square[10] = -yscale - GCSettings.yshift;
|
|
DCFlushRange (square, 32); // update memory BEFORE the GPU accesses it!
|
|
|
|
draw_init ();
|
|
|
|
memset(&view, 0, sizeof(Mtx));
|
|
guLookAt(view, &cam.pos, &cam.up, &cam.view);
|
|
if (fixed) {
|
|
int ratio = fixed % 10;
|
|
bool widescreen = fixed / 10;
|
|
|
|
float vw = vwidth * ratio;
|
|
if (widescreen) vw /= 4.0 / 3.0;
|
|
float vh = vheight * ratio;
|
|
|
|
// 240p adjustment
|
|
if (GCSettings.videomode == 5 || GCSettings.videomode == 6) vw *= 2;
|
|
|
|
float vx = (vmode->fbWidth - vw) / 2;
|
|
float vy = (vmode->efbHeight - vh) / 2;
|
|
GX_SetViewport(vx, vy, vw, vh, 0, 1);
|
|
} else {
|
|
GX_SetViewport(0, 0, vmode->fbWidth, vmode->efbHeight, 0, 1);
|
|
}
|
|
|
|
updateScaling = 0;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* ResetVideo_Emu
|
|
*
|
|
* Reset the video/rendering mode for the emulator rendering
|
|
****************************************************************************/
|
|
void
|
|
ResetVideo_Emu ()
|
|
{
|
|
Mtx44 p;
|
|
GXRModeObj * rmode = FindVideoMode();
|
|
|
|
SetupVideoMode(rmode); // reconfigure VI
|
|
|
|
// reconfigure GX
|
|
GX_SetViewport (0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
|
|
GX_SetDispCopyYScale ((f32) rmode->xfbHeight / (f32) rmode->efbHeight);
|
|
GX_SetScissor (0, 0, rmode->fbWidth, rmode->efbHeight);
|
|
|
|
GX_SetDispCopySrc (0, 0, rmode->fbWidth, rmode->efbHeight);
|
|
GX_SetDispCopyDst (rmode->fbWidth, rmode->xfbHeight);
|
|
u8 sharp[7] = {0,0,21,22,21,0,0};
|
|
u8 soft[7] = {8,8,10,12,10,8,8};
|
|
u8* vfilter =
|
|
GCSettings.render == 3 ? sharp
|
|
: GCSettings.render == 4 ? soft
|
|
: rmode->vfilter;
|
|
GX_SetCopyFilter (rmode->aa, rmode->sample_pattern, (GCSettings.render != 2) ? GX_TRUE : GX_FALSE, vfilter); // deflickering filter only for filtered mode
|
|
|
|
GX_SetFieldMode (rmode->field_rendering, ((rmode->viHeight == 2 * rmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
|
|
|
|
if (rmode->aa)
|
|
GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
|
|
else
|
|
GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
|
|
|
|
GX_SetCullMode (GX_CULL_NONE);
|
|
GX_SetDispCopyGamma (GX_GM_1_0);
|
|
GX_SetBlendMode(GX_BM_BLEND,GX_BL_DSTALPHA,GX_BL_INVSRCALPHA,GX_LO_CLEAR);
|
|
|
|
guOrtho(p, 480/2, -(480/2), -(640/2), 640/2, 100, 1000); // matrix, t, b, l, r, n, f
|
|
GX_LoadProjectionMtx (p, GX_ORTHOGRAPHIC);
|
|
|
|
// reinitialize texture
|
|
GX_InvalidateTexAll ();
|
|
GX_InitTexObj (&texobj, texturemem, vwidth, vheight, GX_TF_RGB565, GX_CLAMP, GX_CLAMP, GX_FALSE); // initialize the texture obj we are going to use
|
|
if (GCSettings.render == 2)
|
|
GX_InitTexObjLOD(&texobj,GX_NEAR,GX_NEAR_MIP_NEAR,2.5,9.0,0.0,GX_FALSE,GX_FALSE,GX_ANISO_1); // original/unfiltered video mode: force texture filtering OFF
|
|
|
|
GX_Flush();
|
|
draw_init();
|
|
|
|
// set aspect ratio
|
|
updateScaling = 1;
|
|
}
|
|
|
|
void GX_Render_Init(int width, int height)
|
|
{
|
|
if (texturemem)
|
|
free(texturemem);
|
|
|
|
/*** Allocate 32byte aligned texture memory ***/
|
|
texturesize = (width * height) * 2;
|
|
|
|
texturemem = (u8 *) memalign(32, texturesize);
|
|
|
|
memset(texturemem, 0, texturesize);
|
|
|
|
/*** Setup for first call to scaler ***/
|
|
vwidth = width;
|
|
vheight = height;
|
|
}
|
|
|
|
bool borderAreaEmpty(const u16* buffer) {
|
|
u16 reference = buffer[0];
|
|
for (int y=0; y<40; y++) {
|
|
for (int x=0; x<256; x++) {
|
|
if (buffer[258*y + x] != reference) return false;
|
|
}
|
|
}
|
|
for (int y=40; y<184; y++) {
|
|
for (int x=0; x<48; x++) {
|
|
if (buffer[258*y + x] != reference) return false;
|
|
}
|
|
for (int x=208; x<224; x++) {
|
|
if (buffer[258*y + x] != reference) return false;
|
|
}
|
|
}
|
|
for (int y=184; y<224; y++) {
|
|
for (int x=0; x<256; x++) {
|
|
if (buffer[258*y + x] != reference) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* GX_Render
|
|
*
|
|
* Pass in a buffer, width and height to update as a tiled RGB565 texture
|
|
* (2 bytes per pixel)
|
|
****************************************************************************/
|
|
void GX_Render(int gbWidth, int gbHeight, u8 * buffer)
|
|
{
|
|
int borderWidth = InitialBorder ? InitialBorderWidth : gbWidth;
|
|
int borderHeight = InitialBorder ? InitialBorderHeight : gbHeight;
|
|
|
|
int h, w;
|
|
int gbPitch = gbWidth * 2 + 4;
|
|
long long int *dst = (long long int *) texturemem; // Pointer in 8-byte units / 4-pixel units
|
|
long long int *src1 = (long long int *) buffer;
|
|
long long int *src2 = (long long int *) (buffer + gbPitch);
|
|
long long int *src3 = (long long int *) (buffer + (gbPitch << 1));
|
|
long long int *src4 = (long long int *) (buffer + (gbPitch * 3));
|
|
int srcrowpitch = (gbPitch >> 3) * 3;
|
|
int srcrowadjust = ( gbPitch % 8 ) << 2;
|
|
int dstrowpitch = borderWidth - gbWidth;
|
|
|
|
vwidth = borderWidth;
|
|
vheight = borderHeight;
|
|
|
|
int vwid2 = (gbWidth >> 2);
|
|
char *ra = NULL;
|
|
|
|
// Ensure previous vb has complete
|
|
while ((LWP_ThreadIsSuspended (vbthread) == 0) || (copynow == GX_TRUE))
|
|
usleep (50);
|
|
|
|
whichfb ^= 1;
|
|
|
|
if(updateScaling)
|
|
UpdateScaling();
|
|
|
|
// clear texture objects
|
|
GX_InvVtxCache();
|
|
GX_InvalidateTexAll();
|
|
GX_SetTevOp(GX_TEVSTAGE0, GX_DECAL);
|
|
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
|
|
|
|
if (gbWidth == 256 && gbHeight == 224 && !SGBBorderLoadedFromGame) {
|
|
if (borderAreaEmpty((u16*)buffer)) {
|
|
// TODO: don't paint empty SGB border
|
|
} else {
|
|
// don't try to load the default border anymore
|
|
SGBBorderLoadedFromGame = true;
|
|
SaveSGBBorderIfNoneExists(buffer);
|
|
}
|
|
}
|
|
|
|
// The InitialBorder, if any, should already be properly tiled
|
|
if (InitialBorder) {
|
|
memcpy(dst, InitialBorder, borderWidth * borderHeight * 2);
|
|
|
|
int rows_to_skip = (borderHeight - gbHeight) / 2;
|
|
if (rows_to_skip > 0) dst += rows_to_skip * borderWidth / 4;
|
|
dst += (borderWidth - gbWidth) / 2;
|
|
}
|
|
|
|
for (h = 0; h < gbHeight; h += 4)
|
|
{
|
|
for (w = 0; w < vwid2; ++w)
|
|
{
|
|
*dst++ = *src1++;
|
|
*dst++ = *src2++;
|
|
*dst++ = *src3++;
|
|
*dst++ = *src4++;
|
|
}
|
|
|
|
src1 += srcrowpitch;
|
|
src2 += srcrowpitch;
|
|
src3 += srcrowpitch;
|
|
src4 += srcrowpitch;
|
|
dst += dstrowpitch;
|
|
|
|
if ( srcrowadjust )
|
|
{
|
|
ra = (char *)src1;
|
|
src1 = (long long int *)(ra + srcrowadjust);
|
|
ra = (char *)src2;
|
|
src2 = (long long int *)(ra + srcrowadjust);
|
|
ra = (char *)src3;
|
|
src3 = (long long int *)(ra + srcrowadjust);
|
|
ra = (char *)src4;
|
|
src4 = (long long int *)(ra + srcrowadjust);
|
|
}
|
|
}
|
|
|
|
// load texture into GX
|
|
DCFlushRange(texturemem, texturesize);
|
|
|
|
GX_SetNumChans(1);
|
|
GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
|
|
GX_SetColorUpdate(GX_TRUE);
|
|
GX_LoadTexObj(&texobj, GX_TEXMAP0);
|
|
|
|
draw_square(view); // render textured quad
|
|
#ifdef HW_RVL
|
|
draw_cursor(view); // render cursor
|
|
#endif
|
|
GX_DrawDone();
|
|
|
|
if(ScreenshotRequested)
|
|
{
|
|
ScreenshotRequested = 0;
|
|
TakeScreenshot();
|
|
ConfigRequested = 1;
|
|
}
|
|
|
|
// EFB is ready to be copied into XFB
|
|
VIDEO_SetNextFramebuffer(xfb[whichfb]);
|
|
VIDEO_Flush();
|
|
copynow = GX_TRUE;
|
|
|
|
// Return to caller, don't waste time waiting for vb
|
|
LWP_ResumeThread (vbthread);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* TakeScreenshot
|
|
*
|
|
* Copies the current screen into a GX texture
|
|
***************************************************************************/
|
|
void TakeScreenshot()
|
|
{
|
|
IMGCTX pngContext = PNGU_SelectImageFromBuffer(savebuffer);
|
|
|
|
if (pngContext != NULL)
|
|
{
|
|
gameScreenPngSize = PNGU_EncodeFromEFB(pngContext, vmode->fbWidth, vmode->efbHeight);
|
|
PNGU_ReleaseImageContext(pngContext);
|
|
gameScreenPng = (u8 *)malloc(gameScreenPngSize);
|
|
memcpy(gameScreenPng, savebuffer, gameScreenPngSize);
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* ResetVideo_Menu
|
|
*
|
|
* Reset the video/rendering mode for the menu
|
|
****************************************************************************/
|
|
void
|
|
ResetVideo_Menu ()
|
|
{
|
|
#ifdef HW_RVL
|
|
if (CONF_GetAspectRatio() == CONF_ASPECT_16_9 && (*(u32*)(0xCD8005A0) >> 16) == 0xCAFE) // Wii U
|
|
{
|
|
/* vWii widescreen patch by tueidj */
|
|
write32(0xd8006a0, 0x30000004), mask32(0xd8006a8, 0, 2);
|
|
}
|
|
#endif
|
|
|
|
Mtx44 p;
|
|
f32 yscale;
|
|
u32 xfbHeight;
|
|
GXRModeObj * rmode = FindVideoMode();
|
|
|
|
SetupVideoMode(rmode); // reconfigure VI
|
|
|
|
// clears the bg to color and clears the z buffer
|
|
GXColor background = {0, 0, 0, 255};
|
|
GX_SetCopyClear (background, 0x00ffffff);
|
|
|
|
yscale = GX_GetYScaleFactor(vmode->efbHeight,vmode->xfbHeight);
|
|
xfbHeight = GX_SetDispCopyYScale(yscale);
|
|
GX_SetScissor(0,0,vmode->fbWidth,vmode->efbHeight);
|
|
GX_SetDispCopySrc(0,0,vmode->fbWidth,vmode->efbHeight);
|
|
GX_SetDispCopyDst(vmode->fbWidth,xfbHeight);
|
|
GX_SetCopyFilter(vmode->aa,vmode->sample_pattern,GX_TRUE,vmode->vfilter);
|
|
GX_SetFieldMode(vmode->field_rendering,((vmode->viHeight==2*vmode->xfbHeight)?GX_ENABLE:GX_DISABLE));
|
|
|
|
if (vmode->aa)
|
|
GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
|
|
else
|
|
GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
|
|
|
|
// setup the vertex descriptor
|
|
// tells the flipper to expect direct data
|
|
GX_ClearVtxDesc();
|
|
GX_InvVtxCache ();
|
|
GX_InvalidateTexAll();
|
|
|
|
GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
|
|
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
|
|
GX_SetVtxDesc (GX_VA_CLR0, GX_DIRECT);
|
|
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
|
|
GX_SetVtxAttrFmt (GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
|
|
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
|
|
GX_SetZMode (GX_FALSE, GX_LEQUAL, GX_TRUE);
|
|
|
|
GX_SetNumChans(1);
|
|
GX_SetNumTexGens(1);
|
|
GX_SetTevOp (GX_TEVSTAGE0, GX_PASSCLR);
|
|
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
|
|
GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
|
|
|
|
guMtxIdentity(GXmodelView2D);
|
|
guMtxTransApply (GXmodelView2D, GXmodelView2D, 0.0F, 0.0F, -50.0F);
|
|
GX_LoadPosMtxImm(GXmodelView2D,GX_PNMTX0);
|
|
|
|
guOrtho(p,0,479,0,639,0,300);
|
|
GX_LoadProjectionMtx(p, GX_ORTHOGRAPHIC);
|
|
|
|
GX_SetViewport(0,0,vmode->fbWidth,vmode->efbHeight,0,1);
|
|
GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
|
|
GX_SetAlphaUpdate(GX_TRUE);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Menu_Render
|
|
*
|
|
* Renders everything current sent to GX, and flushes video
|
|
***************************************************************************/
|
|
void Menu_Render()
|
|
{
|
|
whichfb ^= 1; // flip framebuffer
|
|
GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
|
|
GX_SetColorUpdate(GX_TRUE);
|
|
GX_CopyDisp(xfb[whichfb],GX_TRUE);
|
|
GX_DrawDone();
|
|
VIDEO_SetNextFramebuffer(xfb[whichfb]);
|
|
VIDEO_Flush();
|
|
VIDEO_WaitVSync();
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Menu_DrawImg
|
|
*
|
|
* Draws the specified image on screen using GX
|
|
***************************************************************************/
|
|
void Menu_DrawImg(f32 xpos, f32 ypos, u16 width, u16 height, u8 data[],
|
|
f32 degrees, f32 scaleX, f32 scaleY, u8 alpha)
|
|
{
|
|
if(data == NULL)
|
|
return;
|
|
|
|
GXTexObj texObj;
|
|
|
|
GX_InitTexObj(&texObj, data, width,height, GX_TF_RGBA8,GX_CLAMP, GX_CLAMP,GX_FALSE);
|
|
GX_LoadTexObj(&texObj, GX_TEXMAP0);
|
|
GX_InvalidateTexAll();
|
|
|
|
GX_SetTevOp (GX_TEVSTAGE0, GX_MODULATE);
|
|
GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);
|
|
|
|
Mtx m,m1,m2, mv;
|
|
width >>= 1;
|
|
height >>= 1;
|
|
|
|
guMtxIdentity (m1);
|
|
guMtxScaleApply(m1,m1,scaleX,scaleY,1.0);
|
|
guVector axis = (guVector) {0 , 0, 1 };
|
|
guMtxRotAxisDeg (m2, &axis, degrees);
|
|
guMtxConcat(m2,m1,m);
|
|
|
|
guMtxTransApply(m,m, xpos+width,ypos+height,0);
|
|
guMtxConcat (GXmodelView2D, m, mv);
|
|
GX_LoadPosMtxImm (mv, GX_PNMTX0);
|
|
|
|
GX_Begin(GX_QUADS, GX_VTXFMT0,4);
|
|
GX_Position3f32(-width, -height, 0);
|
|
GX_Color4u8(0xFF,0xFF,0xFF,alpha);
|
|
GX_TexCoord2f32(0, 0);
|
|
|
|
GX_Position3f32(width, -height, 0);
|
|
GX_Color4u8(0xFF,0xFF,0xFF,alpha);
|
|
GX_TexCoord2f32(1, 0);
|
|
|
|
GX_Position3f32(width, height, 0);
|
|
GX_Color4u8(0xFF,0xFF,0xFF,alpha);
|
|
GX_TexCoord2f32(1, 1);
|
|
|
|
GX_Position3f32(-width, height, 0);
|
|
GX_Color4u8(0xFF,0xFF,0xFF,alpha);
|
|
GX_TexCoord2f32(0, 1);
|
|
GX_End();
|
|
GX_LoadPosMtxImm (GXmodelView2D, GX_PNMTX0);
|
|
|
|
GX_SetTevOp (GX_TEVSTAGE0, GX_PASSCLR);
|
|
GX_SetVtxDesc (GX_VA_TEX0, GX_NONE);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Menu_DrawRectangle
|
|
*
|
|
* Draws a rectangle at the specified coordinates using GX
|
|
***************************************************************************/
|
|
void Menu_DrawRectangle(f32 x, f32 y, f32 width, f32 height, GXColor color, u8 filled)
|
|
{
|
|
long n = 4;
|
|
f32 x2 = x+width;
|
|
f32 y2 = y+height;
|
|
guVector v[] = {{x,y,0.0f}, {x2,y,0.0f}, {x2,y2,0.0f}, {x,y2,0.0f}, {x,y,0.0f}};
|
|
u8 fmt = GX_TRIANGLEFAN;
|
|
|
|
if(!filled)
|
|
{
|
|
fmt = GX_LINESTRIP;
|
|
n = 5;
|
|
}
|
|
|
|
GX_Begin(fmt, GX_VTXFMT0, n);
|
|
for(long i=0; i<n; ++i)
|
|
{
|
|
GX_Position3f32(v[i].x, v[i].y, v[i].z);
|
|
GX_Color4u8(color.r, color.g, color.b, color.a);
|
|
}
|
|
GX_End();
|
|
}
|