ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
main.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (c) 2020 h4570 Sandro Sobczyński <sandro.sobczynski@gmail.com>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 # VU1 and libpacket2 showcase.
10 */
11 
12 #include <kernel.h>
13 #include <malloc.h>
14 #include <tamtypes.h>
15 #include <gs_psm.h>
16 #include <dma.h>
17 #include <packet2.h>
18 #include <packet2_utils.h>
19 #include <graph.h>
20 #include <draw.h>
21 #include "zbyszek.c"
22 #include "mesh_data.c"
23 
24 // ---
25 // Variables declared as global for tutorial only!
26 // ---
27 
29 extern unsigned char zbyszek[];
30 
51 extern u32 VU1Draw3D_CodeStart __attribute__((section(".vudata")));
52 extern u32 VU1Draw3D_CodeEnd __attribute__((section(".vudata")));
53 
54 VECTOR object_rotation = {0.00f, 0.00f, 0.00f, 1.00f};
55 VECTOR camera_position = {140.00f, 140.00f, 40.00f, 1.00f};
56 VECTOR camera_rotation = {0.00f, 0.00f, 0.00f, 1.00f};
58 
65 packet2_t *vif_packets[2] __attribute__((aligned(64)));
67 
70 
71 u8 context = 0;
72 
75 
81 
87 
92 VECTOR *c_verts __attribute__((aligned(128))), *c_sts __attribute__((aligned(128)));
93 
95 void calculate_cube(texbuffer_t *t_texbuff)
96 {
97  packet2_add_float(zbyszek_packet, 2048.0F); // scale
98  packet2_add_float(zbyszek_packet, 2048.0F); // scale
99  packet2_add_float(zbyszek_packet, ((float)0xFFFFFF) / 32.0F); // scale
100  packet2_add_s32(zbyszek_packet, faces_count); // vertex count
105  u8 j = 0; // RGBA
106  for (j = 0; j < 4; j++)
108 }
109 
111 void draw_cube(VECTOR t_object_position, texbuffer_t *t_texbuff)
112 {
113  create_local_world(local_world, t_object_position, object_rotation);
118 
119  // Add matrix at the beggining of VU mem (skip TOP)
121 
122  u32 vif_added_bytes = 0; // zero because now we will use TOP register (double buffer)
123  // we don't wan't to unpack at 8 + beggining of buffer, but at
124  // the beggining of the buffer
125 
126  // Merge packets
128  vif_added_bytes += packet2_get_qw_count(zbyszek_packet);
129 
130  // Add vertices
132  vif_added_bytes += faces_count; // one VECTOR is size of qword
133 
134  // Add sts
136  vif_added_bytes += faces_count;
137 
142 
143  // Switch packet, so we can proceed during DMA transfer
144  context = !context;
145 }
146 
148 void init_gs(framebuffer_t *t_frame, zbuffer_t *t_z, texbuffer_t *t_texbuff)
149 {
150  // Define a 32-bit 640x512 framebuffer.
151  t_frame->width = 640;
152  t_frame->height = 512;
153  t_frame->mask = 0;
154  t_frame->psm = GS_PSM_32;
155  t_frame->address = graph_vram_allocate(t_frame->width, t_frame->height, t_frame->psm, GRAPH_ALIGN_PAGE);
156 
157  // Enable the zbuffer.
158  t_z->enable = DRAW_ENABLE;
159  t_z->mask = 0;
161  t_z->zsm = GS_ZBUF_32;
162  t_z->address = graph_vram_allocate(t_frame->width, t_frame->height, t_z->zsm, GRAPH_ALIGN_PAGE);
163 
164  // Allocate some vram for the texture buffer
165  t_texbuff->width = 128;
166  t_texbuff->psm = GS_PSM_24;
167  t_texbuff->address = graph_vram_allocate(128, 128, GS_PSM_24, GRAPH_ALIGN_BLOCK);
168 
169  // Initialize the screen and tie the first framebuffer to the read circuits.
170  graph_initialize(t_frame->address, t_frame->width, t_frame->height, t_frame->psm, 0, 0);
171 }
172 
175 {
177 
178  // This will setup a default drawing environment.
179  packet2_update(packet2, draw_setup_environment(packet2->next, 0, t_frame, t_z));
180 
181  // Now reset the primitive origin to 2048-width/2,2048-height/2.
182  packet2_update(packet2, draw_primitive_xyoffset(packet2->next, 0, (2048 - 320), (2048 - 256)));
183 
184  // Finish setting up the environment.
185  packet2_update(packet2, draw_finish(packet2->next));
186 
187  // Now send the packet, no need to wait since it's the first.
189  dma_wait_fast();
190 
191  packet2_free(packet2);
192 }
193 
196 {
198  packet2_update(packet2, draw_texture_transfer(packet2->next, zbyszek, 128, 128, GS_PSM_24, texbuf->address, texbuf->width));
199  packet2_update(packet2, draw_texture_flush(packet2->next));
201  dma_wait_fast();
202  packet2_free(packet2);
203 }
204 
207 {
209 
210  // Clear framebuffer but don't update zbuffer.
211  packet2_update(clear, draw_disable_tests(clear->next, 0, z));
212  packet2_update(clear, draw_clear(clear->next, 0, 2048.0f - 320.0f, 2048.0f - 256.0f, frame->width, frame->height, 0x40, 0x40, 0x40));
213  packet2_update(clear, draw_enable_tests(clear->next, 0, z));
214  packet2_update(clear, draw_finish(clear->next));
215 
216  // Now send our current dma chain.
217  dma_wait_fast();
219 
220  packet2_free(clear);
221 
222  // Wait for scene to finish drawing
224 }
225 
227 {
229  lod.max_level = 0;
232  lod.l = 0;
233  lod.k = 0;
234 
236  clut.start = 0;
237  clut.psm = 0;
239  clut.address = 0;
240 
241  // Define the triangle primitive we want to use.
250 
251  t_texbuff->info.width = draw_log2(128);
252  t_texbuff->info.height = draw_log2(128);
254  t_texbuff->info.function = TEXTURE_FUNCTION_DECAL;
255 }
256 
257 void render(framebuffer_t *t_frame, zbuffer_t *t_z, texbuffer_t *t_texbuff)
258 {
259  int i, j;
260 
261  set_lod_clut_prim_tex_buff(t_texbuff);
262 
267  c_verts = (VECTOR *)memalign(128, sizeof(VECTOR) * faces_count);
268  c_sts = (VECTOR *)memalign(128, sizeof(VECTOR) * faces_count);
269 
270  VECTOR c_zbyszek_position;
271 
272  for (i = 0; i < faces_count; i++)
273  {
274  c_verts[i][0] = vertices[faces[i]][0];
275  c_verts[i][1] = vertices[faces[i]][1];
276  c_verts[i][2] = vertices[faces[i]][2];
277  c_verts[i][3] = vertices[faces[i]][3];
278 
279  c_sts[i][0] = sts[faces[i]][0];
280  c_sts[i][1] = sts[faces[i]][1];
281  c_sts[i][2] = sts[faces[i]][2];
282  c_sts[i][3] = sts[faces[i]][3];
283  }
284 
285  // Create the view_screen matrix.
286  create_view_screen(view_screen, graph_aspect_ratio(), -3.00f, 3.00f, -3.00f, 3.00f, 1.00f, 2000.00f);
287  calculate_cube(t_texbuff);
288 
289  // The main loop...
290  for (;;)
291  {
292  // Spin the cube a bit.
293  object_rotation[0] += 0.008f;
294  while (object_rotation[0] > 3.14f)
295  {
296  object_rotation[0] -= 6.28f;
297  }
298  object_rotation[1] += 0.012f;
299  while (object_rotation[1] > 3.14f)
300  {
301  object_rotation[1] -= 6.28f;
302  }
303 
304  camera_position[2] += .5F;
305  camera_rotation[2] += 0.002f;
306  if (camera_position[2] >= 400.0F)
307  {
308  camera_position[2] = 40.0F;
309  camera_rotation[2] = 0.00f;
310  }
311 
312  clear_screen(t_frame, t_z);
313 
314  for (i = 0; i < 8; i++)
315  {
316  c_zbyszek_position[0] = i * 40.0F;
317  for (j = 0; j < 8; j++)
318  {
319  c_zbyszek_position[1] = j * 40.0F;
320  draw_cube(c_zbyszek_position, t_texbuff);
321  }
322  }
323 
325  }
326 }
327 
329 {
331  packet2_utils_vu_add_double_buffer(packet2, 8, 496);
335  packet2_free(packet2);
336 }
337 
339 {
340  u32 packet_size =
342  packet2_t *packet2 = packet2_create(packet_size, P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
347  packet2_free(packet2);
348 }
349 
350 int main(int argc, char **argv)
351 {
352 
353  // Init DMA channels.
358 
359  // Initialize vif packets
363 
366 
367  // The buffers to be used.
368  framebuffer_t frame;
369  zbuffer_t z;
370  texbuffer_t texbuff;
371 
372  // Init the GS, framebuffer, zbuffer, and texture buffer.
373  init_gs(&frame, &z, &texbuff);
374 
375  // Init the drawing environment and framebuffer.
376  init_drawing_environment(&frame, &z);
377 
378  // Load the texture into vram.
379  send_texture(&texbuff);
380 
381  // Render textured cube
382  render(&frame, &z, &texbuff);
383 
387 
388  // Sleep
389  SleepThread();
390 
391  // End program.
392  return 0;
393 }
VECTOR vertices[24]
Definition: mesh_data.c:31
void dma_wait_fast(void)
Definition: dma.c:115
int dma_channel_wait(int channel, int timeout)
Definition: dma.c:130
#define DMA_CHANNEL_VIF1
Definition: dma.h:23
int dma_channel_initialize(int channel, void *handler, int flags)
Definition: dma.c:58
#define DMA_CHANNEL_GIF
Definition: dma.h:24
void dma_channel_send_packet2(packet2_t *packet2, int channel, u8 flush_cache)
Definition: dma.c:157
void dma_channel_fast_waits(int channel)
Definition: dma.c:108
#define DRAW_STQ2_REGLIST
Definition: draw3d.h:55
void set_lod_clut_prim_tex_buff(texbuffer_t *t_texbuff)
Definition: main.c:226
packet2_t * vif_packets[2]
Definition: main.c:65
MATRIX world_view
Definition: main.c:57
void render(framebuffer_t *t_frame, zbuffer_t *t_z, texbuffer_t *t_texbuff)
Definition: main.c:257
packet2_t * curr_vif_packet
Definition: main.c:66
void vu1_upload_micro_program()
Definition: main.c:338
MATRIX local_screen
Definition: main.c:57
VECTOR camera_position
Definition: main.c:55
VECTOR * c_sts
Definition: main.c:92
int main(int argc, char **argv)
Definition: main.c:350
unsigned char zbyszek[]
VECTOR * c_verts
Definition: main.c:92
void send_texture(texbuffer_t *texbuf)
Definition: main.c:195
MATRIX local_world
Definition: main.c:57
packet2_t * zbyszek_packet
Definition: main.c:69
VECTOR object_rotation
Definition: main.c:54
void init_drawing_environment(framebuffer_t *t_frame, zbuffer_t *t_z)
Definition: main.c:174
void draw_cube(VECTOR t_object_position, texbuffer_t *t_texbuff)
Definition: main.c:111
u32 VU1Draw3D_CodeStart
void calculate_cube(texbuffer_t *t_texbuff)
Definition: main.c:95
lod_t lod
Definition: main.c:86
MATRIX view_screen
Definition: main.c:57
void init_gs(framebuffer_t *t_frame, zbuffer_t *t_z, texbuffer_t *t_texbuff)
Definition: main.c:148
prim_t prim
Definition: main.c:74
VECTOR camera_rotation
Definition: main.c:56
void clear_screen(framebuffer_t *frame, zbuffer_t *z)
Definition: main.c:206
u8 context
Definition: main.c:71
void vu1_set_double_buffer_settings()
Definition: main.c:328
u32 VU1Draw3D_CodeEnd
clutbuffer_t clut
Definition: main.c:80
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
qword_t * draw_texture_transfer(qword_t *q, void *src, int width, int height, int psm, int dest, int dest_width)
Definition: draw.c:223
void draw_wait_finish(void)
Definition: draw.c:200
#define DRAW_ENABLE
Definition: draw.h:25
qword_t * draw_finish(qword_t *q)
Definition: draw.c:188
qword_t * draw_clear(qword_t *q, int context, float x, float y, float width, float height, int r, int g, int b)
Definition: draw.c:148
qword_t * draw_texture_flush(qword_t *q)
Definition: draw.c:208
#define DRAW_DISABLE
Definition: draw.h:24
unsigned char draw_log2(unsigned int x)
Definition: draw.c:348
#define CLUT_NO_LOAD
Definition: draw_buffers.h:26
#define TEXTURE_FUNCTION_DECAL
Definition: draw_buffers.h:17
#define TEXTURE_COMPONENTS_RGB
Definition: draw_buffers.h:12
#define CLUT_STORAGE_MODE1
Definition: draw_buffers.h:22
#define PRIM_SHADE_GOURAUD
#define PRIM_UNFIXED
#define PRIM_MAP_ST
#define PRIM_TRIANGLE
qword_t * draw_primitive_xyoffset(qword_t *q, int context, float x, float y)
#define LOD_MIN_NEAREST
Definition: draw_sampling.h:18
#define LOD_USE_K
Definition: draw_sampling.h:13
#define LOD_MAG_NEAREST
Definition: draw_sampling.h:16
qword_t * draw_disable_tests(qword_t *q, int context, zbuffer_t *z)
Definition: draw.c:120
qword_t * draw_enable_tests(qword_t *q, int context, zbuffer_t *z)
Definition: draw.c:134
#define ZTEST_METHOD_GREATER_EQUAL
Definition: draw_tests.h:34
int graph_initialize(int fbp, int width, int height, int psm, int x, int y)
Definition: graph.c:6
void graph_wait_vsync(void)
Definition: graph.c:99
float graph_aspect_ratio(void)
Definition: graph_mode.c:397
#define GRAPH_ALIGN_BLOCK
Definition: graph_vram.h:16
int graph_vram_allocate(int width, int height, int psm, int alignment)
Definition: graph_vram.c:7
#define GRAPH_ALIGN_PAGE
Definition: graph_vram.h:13
@ P2_MODE_NORMAL
Definition: packet2_types.h:33
@ P2_MODE_CHAIN
Definition: packet2_types.h:34
@ P2_TYPE_NORMAL
Definition: packet2_types.h:41
static void packet2_utils_gs_add_texbuff_clut(packet2_t *packet2, texbuffer_t *texbuff, clutbuffer_t *clut)
static void packet2_utils_vu_add_double_buffer(packet2_t *packet2, u16 base, u16 offset)
Definition: packet2_utils.h:49
static void packet2_utils_gs_add_lod(packet2_t *packet2, lod_t *lod)
static void packet2_utils_gs_add_prim_giftag(packet2_t *packet2, prim_t *prim, u32 loops_count, u32 nreg, u8 nreg_count, u8 context)
static u32 packet2_utils_get_packet_size_for_program(u32 *start, u32 *end)
static void packet2_utils_vu_add_unpack_data(packet2_t *packet2, u32 t_dest_address, void *t_data, u32 t_size, u8 t_use_top)
Definition: packet2_utils.h:67
static void packet2_utils_vu_add_start_program(packet2_t *packet2, u32 addr)
static void packet2_utils_vu_add_end_tag(packet2_t *packet2)
static void packet2_utils_gif_add_set(packet2_t *packet2, u32 loops_count)
void packet2_vif_add_micro_program(packet2_t *packet2, u32 dest, u32 *start, u32 *end)
Definition: packet2_vif.c:14
void packet2_free(packet2_t *packet2)
Definition: packet2.c:81
void packet2_reset(packet2_t *packet2, u8 clear_mem)
Definition: packet2.c:88
static void packet2_add_u32(packet2_t *packet2, const u32 val)
Definition: packet2.h:134
static void packet2_add_s32(packet2_t *packet2, const s32 val)
Definition: packet2.h:141
packet2_t * packet2_create(u16 qwords, enum Packet2Type type, enum Packet2Mode mode, u8 tte)
Definition: packet2.c:25
static void packet2_update(packet2_t *packet2, qword_t *qw)
Definition: packet2.h:89
static u32 packet2_get_qw_count(packet2_t *packet2)
Definition: packet2.h:200
static void packet2_add_float(packet2_t *packet2, const float val)
Definition: packet2.h:148
#define GS_ZBUF_32
Definition: gs_psm.h:42
#define GS_PSM_32
Definition: gs_psm.h:11
#define GS_PSM_24
Definition: gs_psm.h:13
s32 SleepThread(void)
float MATRIX[16]
Definition: math3d.h:23
void create_local_screen(MATRIX local_screen, MATRIX local_world, MATRIX world_view, MATRIX view_screen)
Definition: math3d.c:434
void create_local_world(MATRIX local_world, VECTOR translation, VECTOR rotation)
Definition: math3d.c:377
void create_world_view(MATRIX world_view, VECTOR translation, VECTOR rotation)
Definition: math3d.c:394
float VECTOR[4]
Definition: math3d.h:21
void create_view_screen(MATRIX view_screen, float aspect, float left, float right, float bottom, float top, float near, float far)
Definition: math3d.c:416
unsigned int load_method
Definition: draw_buffers.h:68
unsigned int start
Definition: draw_buffers.h:67
unsigned int storage_mode
Definition: draw_buffers.h:66
unsigned int psm
Definition: draw_buffers.h:65
unsigned int address
Definition: draw_buffers.h:64
unsigned int address
Definition: draw_buffers.h:41
unsigned int height
Definition: draw_buffers.h:43
unsigned int psm
Definition: draw_buffers.h:44
unsigned int mask
Definition: draw_buffers.h:45
unsigned int width
Definition: draw_buffers.h:42
unsigned char mag_filter
Definition: draw_sampling.h:42
float k
Definition: draw_sampling.h:46
unsigned char l
Definition: draw_sampling.h:45
unsigned char calculation
Definition: draw_sampling.h:40
unsigned char min_filter
Definition: draw_sampling.h:43
unsigned char max_level
Definition: draw_sampling.h:41
qword_t * base
qword_t * next
unsigned char type
unsigned char blending
unsigned char antialiasing
unsigned char colorfix
unsigned char shading
unsigned char fogging
unsigned char mapping_type
unsigned char mapping
unsigned int address
Definition: draw_buffers.h:57
unsigned int width
Definition: draw_buffers.h:58
unsigned int psm
Definition: draw_buffers.h:59
texinfo_t info
Definition: draw_buffers.h:60
unsigned char height
Definition: draw_buffers.h:35
unsigned char function
Definition: draw_buffers.h:37
unsigned char components
Definition: draw_buffers.h:36
unsigned char width
Definition: draw_buffers.h:34
unsigned int mask
Definition: draw_buffers.h:53
unsigned int method
Definition: draw_buffers.h:50
unsigned int address
Definition: draw_buffers.h:51
unsigned int enable
Definition: draw_buffers.h:49
unsigned int zsm
Definition: draw_buffers.h:52
#define NULL
Definition: tamtypes.h:91
unsigned int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23
int faces[36]
Definition: mesh_data.c:18
VECTOR sts[24]
Definition: mesh_data.c:61
int faces_count
Definition: mesh_data.c:12