ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
packet2_utils.h
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 */
10 
19 #ifndef __PACKET2_UTILS_H__
20 #define __PACKET2_UTILS_H__
21 
22 #include "packet2.h"
23 #include "packet2_chain.h"
24 #include "packet2_vif.h"
25 
26 #include <tamtypes.h>
27 #include <draw3d.h>
28 #include <draw_buffers.h>
29 #include <draw_sampling.h>
30 #include <gs_gp.h>
31 
32 #define VU_GS_PRIM(PRIM, IIP, TME, FGE, ABE, AA1, FST, CTXT, FIX) (u128)(((FIX << 10) | (CTXT << 9) | (FST << 8) | (AA1 << 7) | (ABE << 6) | (FGE << 5) | (TME << 4) | (IIP << 3) | (PRIM)))
33 #define VU_GS_GIFTAG(NLOOP, EOP, PRE, PRIM, FLG, NREG) (((u64)(NREG) << 60) | ((u64)(FLG) << 58) | ((u64)(PRIM) << 47) | ((u64)(PRE) << 46) | (EOP << 15) | (NLOOP << 0))
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40  // ----
41  // VU
42  // ----
43 
49  static inline void packet2_utils_vu_add_double_buffer(packet2_t *packet2, u16 base, u16 offset)
50  {
51  packet2_chain_open_cnt(packet2, 0, 0, 0);
52  packet2_vif_base(packet2, base, 0);
53  packet2_vif_offset(packet2, offset, 0);
54  packet2_chain_close_tag(packet2);
55  }
56 
67  static inline void packet2_utils_vu_add_unpack_data(packet2_t *packet2, u32 t_dest_address, void *t_data, u32 t_size, u8 t_use_top)
68  {
69  packet2_chain_ref(packet2, t_data, t_size, 0, 0, 0);
70  packet2_vif_stcycl(packet2, 0, 0x0101, 0);
71  packet2_vif_open_unpack(packet2, P2_UNPACK_V4_32, t_dest_address, t_use_top, 0, 1, 0);
72  packet2_vif_close_unpack_manual(packet2, t_size);
73  }
74 
81  static inline void packet2_utils_vu_open_unpack(packet2_t *packet2, u32 t_dest_address, u8 t_use_top)
82  {
83  packet2_chain_open_cnt(packet2, 0, 0, 0);
84  packet2_vif_stcycl(packet2, 0, 0x0101, 0);
85  packet2_vif_open_unpack(packet2, P2_UNPACK_V4_32, t_dest_address, t_use_top, 0, 1, 0);
86  }
87 
93  {
94  packet2_vif_pad128(packet2);
95  packet2_chain_close_tag(packet2);
96  return packet2_vif_close_unpack_auto(packet2, 0, 0x0101);
97  }
98 
104  {
105  packet2_chain_open_cnt(packet2, 0, 0, 0);
106  packet2_vif_flush(packet2, 0);
107  packet2_vif_mscnt(packet2, 0);
108  packet2_chain_close_tag(packet2);
109  }
110 
115  static inline void packet2_utils_vu_add_start_program(packet2_t *packet2, u32 addr)
116  {
117  packet2_chain_open_cnt(packet2, 0, 0, 0);
118  packet2_vif_flush(packet2, 0);
119  packet2_vif_mscal(packet2, addr, 0);
120  packet2_chain_close_tag(packet2);
121  }
122 
124  static inline void packet2_utils_vu_add_end_tag(packet2_t *packet2)
125  {
126  packet2_chain_open_end(packet2, 0, 0);
127  packet2_vif_nop(packet2, 0);
128  packet2_vif_nop(packet2, 0);
129  packet2_chain_close_tag(packet2);
130  }
131 
139  {
140  // Count instructions
141  u32 count = (end - start) / 2;
142  if (count & 1)
143  count++;
144  return (count >> 8) + 1;
145  }
146 
147  // ----
148  // GIF
149  // ----
150 
157  static inline void packet2_utils_gif_add_set(packet2_t *packet2, u32 loops_count)
158  {
159  packet2_add_2x_s64(packet2, GIF_SET_TAG(loops_count, 0, 0, 0, GIF_FLG_PACKED, 1), GIF_REG_AD);
160  }
161 
162  // ----
163  // GS
164  // ----
165 
172  static inline void packet2_utils_gs_add_lod(packet2_t *packet2, lod_t *lod)
173  {
175  packet2,
176  GS_SET_TEX1(
177  lod->calculation,
178  lod->max_level,
179  lod->mag_filter,
180  lod->min_filter,
182  lod->l,
183  (int)(lod->k * 16.0F)),
184  GS_REG_TEX1);
185  }
186 
195  {
197  packet2,
198  GS_SET_TEX0(
199  texbuff->address >> 6,
200  texbuff->width >> 6,
201  texbuff->psm,
202  texbuff->info.width,
203  texbuff->info.height,
204  texbuff->info.components,
205  texbuff->info.function,
206  clut->address >> 6,
207  clut->psm,
209  clut->start,
210  clut->load_method),
211  GS_REG_TEX0);
212  }
213 
221  {
222  packet2_add_2x_s64(packet2, 1, GS_REG_FINISH);
223  }
224 
235  static inline void packet2_utils_gs_add_prim_giftag(packet2_t *packet2, prim_t *prim, u32 loops_count, u32 nreg, u8 nreg_count, u8 context)
236  {
238  packet2,
239  VU_GS_GIFTAG(
240  loops_count, // Information for GS. Amount of loops
241  1,
242  1,
243  VU_GS_PRIM(
244  prim->type,
245  prim->shading,
246  prim->mapping,
247  prim->fogging,
248  prim->blending,
251  context, // context
252  prim->colorfix),
253  0,
254  nreg_count),
255  nreg);
256  }
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif /* __PACKET2_UTILS_H__ */
263  // end of packet2_utils subgroup
lod_t lod
Definition: main.c:86
prim_t prim
Definition: main.c:74
u8 context
Definition: main.c:71
clutbuffer_t clut
Definition: main.c:80
#define GIF_SET_TAG(NLOOP, EOP, PRE, PRIM, FLG, NREG)
Definition: gif_tags.h:80
#define GIF_REG_AD
Definition: gif_tags.h:72
#define GIF_FLG_PACKED
Definition: gif_tags.h:35
static void packet2_chain_close_tag(packet2_t *packet2)
static void packet2_chain_ref(packet2_t *packet2, const void *ref_data, u32 qw_length, u8 irq, u8 spr, u32 pce)
static void packet2_chain_open_cnt(packet2_t *packet2, u8 irq, u32 pce, u8 spr)
static void packet2_chain_open_end(packet2_t *packet2, u8 irq, u32 pce)
@ P2_UNPACK_V4_32
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 void packet2_utils_gs_add_draw_finish_giftag(packet2_t *packet2)
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 u32 packet2_utils_vu_close_unpack(packet2_t *packet2)
Definition: packet2_utils.h:92
static void packet2_utils_vu_add_start_program(packet2_t *packet2, u32 addr)
static void packet2_utils_vu_add_continue_program(packet2_t *packet2)
static void packet2_utils_vu_add_end_tag(packet2_t *packet2)
static void packet2_utils_vu_open_unpack(packet2_t *packet2, u32 t_dest_address, u8 t_use_top)
Definition: packet2_utils.h:81
#define VU_GS_PRIM(PRIM, IIP, TME, FGE, ABE, AA1, FST, CTXT, FIX)
Definition: packet2_utils.h:32
static void packet2_utils_gif_add_set(packet2_t *packet2, u32 loops_count)
#define VU_GS_GIFTAG(NLOOP, EOP, PRE, PRIM, FLG, NREG)
Definition: packet2_utils.h:33
static void packet2_vif_mscal(packet2_t *packet2, u32 addr, u8 irq)
Definition: packet2_vif.h:232
static void packet2_vif_close_unpack_manual(packet2_t *packet2, u32 unpack_num)
Definition: packet2_vif.h:68
static void packet2_vif_base(packet2_t *packet2, u32 base, u8 irq)
Definition: packet2_vif.h:207
static void packet2_vif_pad128(packet2_t *packet2)
Definition: packet2_vif.h:153
static void packet2_vif_open_unpack(packet2_t *packet2, enum UnpackMode mode, u32 vuAddr, u8 dblBuffered, u8 masked, u8 usigned, u8 irq)
Definition: packet2_vif.h:50
static void packet2_vif_nop(packet2_t *packet2, u8 irq)
Definition: packet2_vif.h:135
static void packet2_vif_stcycl(packet2_t *packet2, u32 wl, u32 cl, u8 irq)
Definition: packet2_vif.h:181
static void packet2_vif_mscnt(packet2_t *packet2, u8 irq)
Definition: packet2_vif.h:244
static void packet2_vif_flush(packet2_t *packet2, u8 irq)
Definition: packet2_vif.h:219
static void packet2_vif_offset(packet2_t *packet2, u32 offset, u8 irq)
Definition: packet2_vif.h:194
u32 packet2_vif_close_unpack_auto(packet2_t *packet2, u32 wl, u32 cl)
Definition: packet2_vif.c:34
static void packet2_add_2x_s64(packet2_t *packet2, const s64 v1, const s64 v2)
Definition: packet2.h:114
#define GS_REG_TEX1
Definition: gs_gp.h:43
#define GS_SET_TEX0(TBA, TBW, PSM, TW, TH, TCC, TFNCT, CBA, CPSM, CSM, CSA, CLD)
Definition: gs_gp.h:281
#define GS_REG_TEX0
Definition: gs_gp.h:25
#define GS_REG_FINISH
Definition: gs_gp.h:143
#define GS_SET_TEX1(LCM, MXL, MMAG, MMIN, MTBA, L, K)
Definition: gs_gp.h:289
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 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 mipmap_select
Definition: draw_sampling.h:44
unsigned char min_filter
Definition: draw_sampling.h:43
unsigned char max_level
Definition: draw_sampling.h:41
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 u32
Definition: tamtypes.h:30
unsigned short u16
Definition: tamtypes.h:24
unsigned char u8
Definition: tamtypes.h:23