PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
packet2_utils.h
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
36extern "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);
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
92 static inline u32 packet2_utils_vu_close_unpack(packet2_t *packet2)
93 {
94 packet2_vif_pad128(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);
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);
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);
130 }
131
138 static inline u32 packet2_utils_get_packet_size_for_program(u32 *start, u32 *end)
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 {
174 packet2_add_2x_s64(
175 packet2,
176 GS_SET_TEX1(
177 lod->calculation,
178 lod->max_level,
179 lod->mag_filter,
180 lod->min_filter,
181 lod->mipmap_select,
182 lod->l,
183 (int)(lod->k * 16.0F)),
185 }
186
194 static inline void packet2_utils_gs_add_texbuff_clut(packet2_t *packet2, texbuffer_t *texbuff, clutbuffer_t *clut)
195 {
196 packet2_add_2x_s64(
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,
208 clut->storage_mode,
209 clut->start,
210 clut->load_method),
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 {
237 packet2_add_2x_s64(
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,
249 prim->antialiasing,
250 prim->mapping_type,
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
#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)
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)
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)
static u32 packet2_utils_vu_close_unpack(packet2_t *packet2)
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)
static void packet2_utils_gif_add_set(packet2_t *packet2, u32 loops_count)
static void packet2_vif_mscal(packet2_t *packet2, u32 addr, u8 irq)
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)
static void packet2_vif_pad128(packet2_t *packet2)
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)
static void packet2_vif_stcycl(packet2_t *packet2, u32 wl, u32 cl, u8 irq)
static void packet2_vif_mscnt(packet2_t *packet2, u8 irq)
static void packet2_vif_flush(packet2_t *packet2, u8 irq)
static void packet2_vif_offset(packet2_t *packet2, u32 offset, u8 irq)
u32 packet2_vif_close_unpack_auto(packet2_t *packet2, u32 wl, u32 cl)
Definition packet2_vif.c:34
#define GS_REG_TEX1
Definition gs_gp.h:43
#define GS_REG_TEX0
Definition gs_gp.h:25
#define GS_REG_FINISH
Definition gs_gp.h:143
u32 count
start sector of fragmented bd/file