1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/****************************************************************************
* Generic GX Support for Emulators
* softdev 2007
* dhewg 2008
*
* This program 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* NGC GX Video Functions
*
* These are pretty standard functions to setup and use GX scaling.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "gx_supp.h"
#define DEFAULT_FIFO_SIZE (256 * 1024)
#define HASPECT 320
#define VASPECT 240
#ifdef __cplusplus
extern "C" {
#endif
/*** 2D ***/
static u32 whichfb;
static u32 *xfb[2];
GXRModeObj *vmode = NULL;
/*** 3D GX ***/
static u8 *gp_fifo;
/*** Texture memory ***/
static u8 *texturemem = NULL;
static u32 texturesize;
static GXTexObj texobj;
static Mtx view;
static u16 vwidth, vheight, oldvwidth, oldvheight;
static float tex_xT = 0.0f, tex_yT = 0.0f;
/* New texture based scaler */
typedef struct tagcamera {
Vector pos;
Vector up;
Vector view;
} camera;
static s16 square[] ATTRIBUTE_ALIGN(32) = {
-HASPECT, VASPECT, 0,
HASPECT, VASPECT, 0,
HASPECT, -VASPECT, 0,
-HASPECT, -VASPECT, 0,
};
static camera cam = {
{ 0.0f, 0.0f, 370.0f },
{ 0.0f, 0.5f, 0.0f },
{ 0.0f, 0.0f, -0.5f }
};
void GX_InitVideo() {
vmode = VIDEO_GetPreferredMode(NULL);
vmode->viWidth = 688;
vmode->viXOrigin = (VI_MAX_WIDTH_PAL - 688) / 2;
VIDEO_Configure(vmode);
xfb[0] = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer(vmode));
xfb[1] = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer(vmode));
gp_fifo = (u8 *) memalign(32, DEFAULT_FIFO_SIZE);
VIDEO_ClearFrameBuffer(vmode, xfb[0], COLOR_BLACK);
VIDEO_ClearFrameBuffer(vmode, xfb[1], COLOR_BLACK);
whichfb = 0;
VIDEO_SetNextFramebuffer(xfb[whichfb]);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
VIDEO_WaitVSync();
if (vmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();
}
void GX_SetTexTrans(float xT, float yT) {
tex_xT = xT;
tex_yT = yT;
}
void GX_SetCamPosZ(float f) {
cam.pos.z = f;
}
/****************************************************************************
* Scaler Support Functions
****************************************************************************/
static 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_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
GX_InvalidateTexAll();
GX_InitTexObj(&texobj, texturemem, vwidth, vheight, GX_TF_RGB565,
GX_CLAMP, GX_CLAMP, GX_FALSE);
}
static void draw_vert(u8 pos, u8 c, f32 s, f32 t) {
GX_Position1x8(pos);
GX_Color1x8(c);
GX_TexCoord2f32(s, t);
}
static void draw_square(Mtx v) {
Mtx m;
Mtx mv;
guMtxIdentity(m);
guMtxTransApply(m, m, tex_xT, tex_yT, -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();
}
/****************************************************************************
* StartGX
****************************************************************************/
void GX_Start(u16 width, u16 height, s16 haspect, s16 vaspect) {
static bool inited = false;
Mtx p;
GXColor gxbackground = { 0, 0, 0, 0xff };
/*** Set new aspect ***/
square[0] = square[9] = -haspect;
square[3] = square[6] = haspect;
square[1] = square[4] = vaspect;
square[7] = square[10] = -vaspect;
/*** Allocate 32byte aligned texture memory ***/
texturesize = (width * height) * 2;
if (texturemem)
free(texturemem);
texturemem = (u8 *) memalign(32, texturesize);
memset(texturemem, 0, texturesize);
/*** Setup for first call to scaler ***/
oldvwidth = oldvheight = 0;
if (inited)
return;
inited = true;
/*** Clear out FIFO area ***/
memset(gp_fifo, 0, DEFAULT_FIFO_SIZE);
/*** Initialise GX ***/
GX_Init(gp_fifo, DEFAULT_FIFO_SIZE);
GX_SetCopyClear(gxbackground, 0x00ffffff);
GX_SetViewport(0, 0, vmode->fbWidth, vmode->efbHeight, 0, 1);
GX_SetDispCopyYScale((f32) vmode->xfbHeight / (f32) vmode->efbHeight);
GX_SetScissor(0, 0, vmode->fbWidth, vmode->efbHeight);
GX_SetDispCopySrc(0, 0, vmode->fbWidth, vmode->efbHeight);
GX_SetDispCopyDst(vmode->fbWidth, vmode->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));
GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
GX_SetCullMode(GX_CULL_NONE);
GX_CopyDisp(xfb[whichfb ^ 1], GX_TRUE);
GX_SetDispCopyGamma(GX_GM_1_0);
guPerspective(p, 60, 1.33f, 10.0f, 1000.0f);
GX_LoadProjectionMtx(p, GX_PERSPECTIVE);
GX_Flush();
}
/****************************************************************************
* GX_Render
*
* Pass in a buffer, width and height to update as a tiled RGB565 texture
****************************************************************************/
void GX_Render(u16 width, u16 height, u8 *buffer, u16 pitch) {
u16 h, w;
u64 *dst = (u64 *) texturemem;
u64 *src1 = (u64 *) buffer;
u64 *src2 = (u64 *) (buffer + pitch);
u64 *src3 = (u64 *) (buffer + (pitch * 2));
u64 *src4 = (u64 *) (buffer + (pitch * 3));
u16 rowpitch = (pitch >> 3) * 3 + pitch % 8;
vwidth = width;
vheight = height;
whichfb ^= 1;
if ((oldvheight != vheight) || (oldvwidth != vwidth)) {
/** Update scaling **/
oldvwidth = vwidth;
oldvheight = vheight;
draw_init();
memset(&view, 0, sizeof(Mtx));
guLookAt(view, &cam.pos, &cam.up, &cam.view);
GX_SetViewport(0, 0, vmode->fbWidth, vmode->efbHeight, 0, 1);
}
GX_InvVtxCache();
GX_InvalidateTexAll();
GX_SetTevOp(GX_TEVSTAGE0, GX_DECAL);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
for (h = 0; h < vheight; h += 4) {
for (w = 0; w < (vwidth >> 2); w++) {
*dst++ = *src1++;
*dst++ = *src2++;
*dst++ = *src3++;
*dst++ = *src4++;
}
src1 += rowpitch;
src2 += rowpitch;
src3 += rowpitch;
src4 += rowpitch;
}
DCFlushRange(texturemem, texturesize);
GX_SetNumChans(1);
GX_LoadTexObj(&texobj, GX_TEXMAP0);
draw_square(view);
GX_DrawDone();
GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
GX_SetColorUpdate(GX_TRUE);
GX_CopyDisp(xfb[whichfb], GX_TRUE);
GX_Flush();
VIDEO_SetNextFramebuffer(xfb[whichfb]);
VIDEO_Flush();
}
#ifdef __cplusplus
}
#endif
|