summary refs log tree commit diff
path: root/src/Graphics_Xbox360.c
blob: 71a4efb204d133e8c263c7d0c2cb2f16398e7640 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "Core.h"
#ifdef CC_BUILD_XBOX360
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Window.h"
#include <xenos/xe.h>
#include <xenos/edram.h>

#include "../misc/xbox360/ps_coloured.h"
#include "../misc/xbox360/vs_coloured.h"
#include "../misc/xbox360/ps_textured.h"
#include "../misc/xbox360/vs_textured.h"
static struct XenosShader* shdr_tex_vs;
static struct XenosShader* shdr_tex_ps;
static struct XenosShader* shdr_col_vs;
static struct XenosShader* shdr_col_ps;

static struct XenosDevice device;
static struct XenosDevice* xe;

static const struct XenosVBFFormat textured_vbf = {
3, {
	{ XE_USAGE_POSITION, 0, XE_TYPE_FLOAT4 },
	{ XE_USAGE_COLOR,    0, XE_TYPE_UBYTE4 },
	{ XE_USAGE_TEXCOORD, 0, XE_TYPE_FLOAT2 }
} };

static const struct XenosVBFFormat coloured_vbf = {
2, {
	{ XE_USAGE_POSITION, 0, XE_TYPE_FLOAT4 },
	{ XE_USAGE_COLOR,    0, XE_TYPE_UBYTE4 }
} };

static void CreateState(void) {
	xe = &device;
	Xe_Init(xe);
	edram_init(xe);
	
	struct XenosSurface* fb = Xe_GetFramebufferSurface(xe);
	Xe_SetRenderTarget(xe, fb);
}

static void CreateShaders(void) {
	shdr_tex_vs = Xe_LoadShaderFromMemory(xe, (void*)vs_textured);
	Xe_InstantiateShader(xe, shdr_tex_vs, 0);	 
	Xe_ShaderApplyVFetchPatches(xe, shdr_tex_vs, 0, &textured_vbf);
	shdr_tex_ps = Xe_LoadShaderFromMemory(xe, (void*)ps_textured);
	Xe_InstantiateShader(xe, shdr_tex_ps, 0);
    	
	shdr_col_vs = Xe_LoadShaderFromMemory(xe, (void*)vs_coloured);
	Xe_InstantiateShader(xe, shdr_col_vs, 0);	 
	Xe_ShaderApplyVFetchPatches(xe, shdr_col_vs, 0, &coloured_vbf);
	shdr_col_ps = Xe_LoadShaderFromMemory(xe, (void*)ps_coloured);
	Xe_InstantiateShader(xe, shdr_col_ps, 0);
}

void Gfx_Create(void) {
	if (!Gfx.Created) {
		CreateState();
		CreateShaders();	
	}
	Gfx.Created = true;
	Gfx_SetVertexFormat(VERTEX_FORMAT_COLOURED);
	
	Gfx.MaxTexWidth  = 1024;
	Gfx.MaxTexHeight = 1024;
	Gfx.MaxTexSize   = 512 * 512;
}

cc_bool Gfx_TryRestoreContext(void) {
	return true;
}

void Gfx_Free(void) {
	Gfx_FreeState();
}

static void Gfx_FreeState(void) { 
	FreeDefaultResources();
}

static void Gfx_RestoreState(void) {
	InitDefaultResources();
	Gfx_SetFaceCulling(false);
	SetAlphaBlend(false);

	Xe_SetAlphaFunc(xe, XE_CMP_GREATER);
	Xe_SetAlphaRef(xe,  0.5f);
	Xe_SetZFunc(xe,     XE_CMP_LESSEQUAL);
}


/*########################################################################################################################*
*---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/
static void SetTextureData(struct XenosSurface* xtex, int x, int y, const struct Bitmap* bmp, int rowWidth, int lvl) {
	void* dst = Xe_Surface_LockRect(xe, xtex, x, y, bmp->width, bmp->height, XE_LOCK_WRITE);

	CopyTextureData(dst, bmp->width * 4, bmp, rowWidth << 2);
	
	Xe_Surface_Unlock(xe, xtex);
}

static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) {
	struct XenosSurface* xtex = Xe_CreateTexture(xe, bmp->width, bmp->height, 1, XE_FMT_8888, 0);
	SetTextureData(xtex, 0, 0, bmp, rowWidth, 0);
	return xtex;
}

void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) {
	struct XenosSurface* xtex = (struct XenosSurface*)texId;
	SetTextureData(xtex, x, y, part, rowWidth, 0);
}

void Gfx_BindTexture(GfxResourceID texId) {
	struct XenosSurface* xtex = (struct XenosSurface*)texId;
	Xe_SetTexture(xe, 0, xtex);
}

void Gfx_DeleteTexture(GfxResourceID* texId) {
	struct XenosSurface* xtex = (struct XenosSurface*)(*texId);
	if (xtex) Xe_DestroyTexture(xe, xtex);
	*texId = NULL;
}

void Gfx_EnableMipmaps(void) { }  // TODO

void Gfx_DisableMipmaps(void) { } // TODO


/*########################################################################################################################*
*-----------------------------------------------------State management----------------------------------------------------*
*#########################################################################################################################*/
void Gfx_SetFaceCulling(cc_bool enabled) {
	Xe_SetCullMode(xe, enabled ? XE_CULL_CW : XE_CULL_NONE);
}

void Gfx_SetFog(cc_bool enabled) {
	// TODO
}

void Gfx_SetFogCol(PackedCol color) {
	// TODO
}

void Gfx_SetFogDensity(float value) {
	// TODO
}

void Gfx_SetFogEnd(float value) {
	// TODO
}

void Gfx_SetFogMode(FogFunc func) {
	// TODO
}

static void SetAlphaTest(cc_bool enabled) {
	Xe_SetAlphaTestEnable(xe, enabled);
}

static void SetAlphaBlend(cc_bool enabled) {
	if (enabled) {
		Xe_SetBlendControl(xe,
			XE_BLEND_SRCALPHA, XE_BLENDOP_ADD, XE_BLEND_INVSRCALPHA,
			XE_BLEND_SRCALPHA, XE_BLENDOP_ADD, XE_BLEND_INVSRCALPHA);
	} else {
		Xe_SetBlendControl(xe,
			XE_BLEND_ONE, XE_BLENDOP_ADD, XE_BLEND_ZERO,
			XE_BLEND_ONE, XE_BLENDOP_ADD, XE_BLEND_ZERO);
	}
}

void Gfx_SetAlphaArgBlend(cc_bool enabled) {
	// TODO
}

void Gfx_ClearColor(PackedCol color) { 
	Xe_SetClearColor(xe, color);
}

void Gfx_SetDepthTest(cc_bool enabled) {
	Xe_SetZEnable(xe, enabled);
}

void Gfx_SetDepthWrite(cc_bool enabled) {
	Xe_SetZWrite(xe, enabled);
}

static void SetColorWrite(cc_bool r, cc_bool g, cc_bool b, cc_bool a) {
	// TODO
}

void Gfx_DepthOnlyRendering(cc_bool depthOnly) {
	cc_bool enabled = !depthOnly;
	SetColorWrite(enabled & gfx_colorMask[0], enabled & gfx_colorMask[1], 
				  enabled & gfx_colorMask[2], enabled & gfx_colorMask[3]);
}


/*########################################################################################################################*
*-------------------------------------------------------Index buffers-----------------------------------------------------*
*#########################################################################################################################*/
GfxResourceID Gfx_CreateIb2(int count, Gfx_FillIBFunc fillFunc, void* obj) {
	int size = count * 2;
	struct XenosIndexBuffer* xib = Xe_CreateIndexBuffer(xe, size, XE_FMT_INDEX16);
	
	void* dst = Xe_IB_Lock(xe, xib, 0, size, XE_LOCK_WRITE);
	fillFunc((cc_uint16*)dst, count, obj);
	Xe_IB_Unlock(xe, xib);
	return xib;
}

void Gfx_BindIb(GfxResourceID ib) {
	struct XenosIndexBuffer* xib = (struct XenosIndexBuffer*)ib;
	Xe_SetIndices(xe, xib);
}

void Gfx_DeleteIb(GfxResourceID* ib) { 
	struct XenosIndexBuffer* xib = (struct XenosIndexBuffer*)(*ib);
	if (xib) Xe_DestroyIndexBuffer(xe, xib);
	*ib = NULL;
}


/*########################################################################################################################*
*------------------------------------------------------Vertex buffers-----------------------------------------------------*
*#########################################################################################################################*/
static GfxResourceID Gfx_AllocStaticVb(VertexFormat fmt, int count) {
	int size = count * strideSizes[fmt];
	return Xe_CreateVertexBuffer(xe, size);
}

void Gfx_DeleteVb(GfxResourceID* vb) {
	struct XenosVertexBuffer* xvb = (struct XenosVertexBuffer*)(*vb);
	if (xvb) Xe_DestroyVertexBuffer(xe, xvb);
	*vb = NULL;
}

void Gfx_BindVb(GfxResourceID vb) {
	struct XenosVertexBuffer* xvb = (struct XenosVertexBuffer*)vb;
	Xe_SetStreamSource(xe, 0, xvb, 0, gfx_stride);
}

void* Gfx_LockVb(GfxResourceID vb, VertexFormat fmt, int count) {
	struct XenosVertexBuffer* xvb = (struct XenosVertexBuffer*)vb;
	int size = count * strideSizes[fmt];
	return Xe_VB_Lock(xe, xvb, 0, size, XE_LOCK_WRITE);
}

void Gfx_UnlockVb(GfxResourceID vb) {
	struct XenosVertexBuffer* xvb = (struct XenosVertexBuffer*)vb;
	Xe_VB_Unlock(xe, xvb);
}


/*########################################################################################################################*
*--------------------------------------------------Dynamic vertex buffers-------------------------------------------------*
*#########################################################################################################################*/
static GfxResourceID Gfx_AllocDynamicVb(VertexFormat fmt, int maxVertices) {
	return Gfx_AllocStaticVb(fmt, maxVertices);
}

void Gfx_DeleteDynamicVb(GfxResourceID* vb) { Gfx_DeleteVb(vb); }

void Gfx_BindDynamicVb(GfxResourceID vb)    { Gfx_BindVb(vb); }

void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count) {
	return Gfx_LockVb(vb, fmt, count);
}

void Gfx_UnlockDynamicVb(GfxResourceID vb)  { Gfx_UnlockVb(vb); Gfx_BindVb(vb); }


/*########################################################################################################################*
*-----------------------------------------------------Vertex rendering----------------------------------------------------*
*#########################################################################################################################*/
void Gfx_SetVertexFormat(VertexFormat fmt) {
	if (fmt == gfx_format) return;
	gfx_format = fmt;
	gfx_stride = strideSizes[fmt];
	
	if (fmt == VERTEX_FORMAT_COLOURED) {
		Xe_SetTexture(xe, 0, NULL);
		Xe_SetShader(xe,  SHADER_TYPE_PIXEL,  shdr_col_ps, 0);
		Xe_SetShader(xe,  SHADER_TYPE_VERTEX, shdr_col_vs, 0);
	} else {
		Xe_SetShader(xe,  SHADER_TYPE_PIXEL,  shdr_tex_ps, 0);
		Xe_SetShader(xe,  SHADER_TYPE_VERTEX, shdr_tex_vs, 0);
	}
}

void Gfx_DrawVb_Lines(int verticesCount) {
	Xe_DrawPrimitive(xe, XE_PRIMTYPE_LINELIST, 0, verticesCount >> 1);
}

void Gfx_DrawVb_IndexedTris(int verticesCount) {
	Xe_DrawIndexedPrimitive(xe, XE_PRIMTYPE_TRIANGLELIST, // TODO QUADLIST instead?
		0, 0, verticesCount, 0, verticesCount >> 1);
}

void Gfx_DrawVb_IndexedTris_Range(int verticesCount, int startVertex) {
	Xe_DrawIndexedPrimitive(xe, XE_PRIMTYPE_TRIANGLELIST,
		startVertex, 0, verticesCount, 0, verticesCount >> 1);
}

void Gfx_DrawIndexedTris_T2fC4b(int verticesCount, int startVertex) {
	Xe_DrawIndexedPrimitive(xe, XE_PRIMTYPE_TRIANGLELIST,
		startVertex, 0, verticesCount, 0, verticesCount >> 1);
}


/*########################################################################################################################*
*---------------------------------------------------------Matrices--------------------------------------------------------*
*#########################################################################################################################*/
static struct Matrix _view, _proj, _mvp;

void Gfx_LoadMatrix(MatrixType type, const struct Matrix* matrix) {
	struct Matrix* dst = type == MATRIX_PROJECTION ? &_proj : &_view;
	*dst = *matrix;
	
	Matrix_Mul(&_mvp, &_view, &_proj);
	// TODO: Is this a global uniform, or does it need to be reloaded on shader change?
	Xe_SetVertexShaderConstantF(xe, 0, (float*)&_mvp, 4);
}

void Gfx_LoadIdentityMatrix(MatrixType type) {	
	Gfx_LoadMatrix(type, &Matrix_Identity);
}

void Gfx_EnableTextureOffset(float x, float y) {
	// TODO
}

void Gfx_DisableTextureOffset(void) {
	// TODO
}

void Gfx_CalcOrthoMatrix(struct Matrix* matrix, float width, float height, float zNear, float zFar) {
	// TODO verify this
	*matrix = Matrix_Identity;

	matrix->row1.x =  2.0f / width;
	matrix->row2.y = -2.0f / height;
	matrix->row3.z =  1.0f / (zNear - zFar);

	matrix->row4.x = -1.0f;
	matrix->row4.y =  1.0f;
	matrix->row4.z = zNear / (zNear - zFar);
}

static float Cotangent(float x) { return Math_CosF(x) / Math_SinF(x); }
void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, float zFar) {
	// TODO verify this
	float zNear = 0.1f;
	float c = Cotangent(0.5f * fov);
	*matrix = Matrix_Identity;

	matrix->row1.x =  c / aspect;
	matrix->row2.y =  c;
	matrix->row3.z = zFar / (zNear - zFar);
	matrix->row3.w = -1.0f;
	matrix->row4.z = (zNear * zFar) / (zNear - zFar);
	matrix->row4.w =  0.0f;
}


/*########################################################################################################################*
*-----------------------------------------------------------Misc----------------------------------------------------------*
*#########################################################################################################################*/
cc_result Gfx_TakeScreenshot(struct Stream* output) {
	return ERR_NOT_SUPPORTED;
}

void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) {
	gfx_minFrameMs = minFrameMs;
	gfx_vsync      = vsync;
}

void Gfx_BeginFrame(void) { 
}

void Gfx_ClearBuffers(GfxBuffers buffers) {
	// TODO clear only some buffers
	// Xe_Clear is just a Resolve anyways
}

void Gfx_EndFrame(void) {
	Xe_Resolve(xe);
	Xe_Sync(xe);
	
	if (gfx_minFrameMs) LimitFPS();
}

cc_bool Gfx_WarnIfNecessary(void) { return false; }

void Gfx_GetApiInfo(cc_string* info) {
	String_AppendConst(info, "-- Using XBox 360 --\n");
	PrintMaxTextureInfo(info);
}

void Gfx_OnWindowResize(void) {

}

void Gfx_SetViewport(int x, int y, int w, int h) { }
#endif