Sony PlayStation2 Study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.2 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. /**
  11. * @file Helper functions for packet2.
  12. * @defgroup packet2_utils Utils
  13. * Useful functions and examples for libpacket2.
  14. * @ingroup packet2
  15. * @{
  16. */
  17. #ifndef __PACKET2_UTILS_H__
  18. #define __PACKET2_UTILS_H__
  19. #include "packet2.h"
  20. #include "packet2_chain.h"
  21. #include "packet2_vif.h"
  22. #include <tamtypes.h>
  23. #include <draw3d.h>
  24. #include <draw_buffers.h>
  25. #include <draw_sampling.h>
  26. #include <gs_gp.h>
  27. #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)))
  28. #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))
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. // ----
  34. // VU
  35. // ----
  36. /**
  37. * Add VU buffers size settings.
  38. * @param t_base Base address (qw)
  39. * @param t_offset Offset address (qw)
  40. */
  41. static inline void packet2_utils_vu_add_double_buffer(packet2_t *packet2, u16 base, u16 offset)
  42. {
  43. packet2_chain_open_cnt(packet2, 0, 0, 0);
  44. packet2_vif_base(packet2, base, 0);
  45. packet2_vif_offset(packet2, offset, 0);
  46. packet2_chain_close_tag(packet2);
  47. }
  48. /**
  49. * Add data via REF tag and UNPACK.
  50. * Please be sure that data is aligned,
  51. * and there is no any open UNPACK/DIRECT.
  52. * @param packet2 Pointer to packet2.
  53. * @param t_dest_address Destination address.
  54. * @param t_data Data pointer.
  55. * @param t_size Size in quadwords.
  56. * @param t_use_top Unpack to current double buffer?
  57. */
  58. 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)
  59. {
  60. packet2_chain_ref(packet2, t_data, t_size, 0, 0, 0);
  61. packet2_vif_stcycl(packet2, 0, 0x0101, 0);
  62. packet2_vif_open_unpack(packet2, P2_UNPACK_V4_32, t_dest_address, t_use_top, 0, 1, 0);
  63. packet2_vif_close_unpack_manual(packet2, t_size);
  64. }
  65. /**
  66. * Open CNT tag + VU unpack.
  67. * @param packet2 Pointer to packet2.
  68. * @param t_dest_address Destination address.
  69. * @param t_use_top Unpack to current double buffer?
  70. */
  71. static inline void packet2_utils_vu_open_unpack(packet2_t *packet2, u32 t_dest_address, u8 t_use_top)
  72. {
  73. packet2_chain_open_cnt(packet2, 0, 0, 0);
  74. packet2_vif_stcycl(packet2, 0, 0x0101, 0);
  75. packet2_vif_open_unpack(packet2, P2_UNPACK_V4_32, t_dest_address, t_use_top, 0, 1, 0);
  76. }
  77. /**
  78. * Close CNT tag + VU unpack.
  79. * @returns Unpacked qwords count
  80. */
  81. static inline u32 packet2_utils_vu_close_unpack(packet2_t *packet2)
  82. {
  83. packet2_vif_pad128(packet2);
  84. packet2_chain_close_tag(packet2);
  85. return packet2_vif_close_unpack_auto(packet2, 0, 0x0101);
  86. }
  87. /**
  88. * Continue VU micro program (from --cont line).
  89. * Adds FLUSH and MSCNT VIF opcodes.
  90. */
  91. static inline void packet2_utils_vu_add_continue_program(packet2_t *packet2)
  92. {
  93. packet2_chain_open_cnt(packet2, 0, 0, 0);
  94. packet2_vif_flush(packet2, 0);
  95. packet2_vif_mscnt(packet2, 0);
  96. packet2_chain_close_tag(packet2);
  97. }
  98. /**
  99. * Start VU micro program.
  100. * Adds FLUSH and MSCAL VIF opcodes.
  101. */
  102. static inline void packet2_utils_vu_add_start_program(packet2_t *packet2, u32 addr)
  103. {
  104. packet2_chain_open_cnt(packet2, 0, 0, 0);
  105. packet2_vif_flush(packet2, 0);
  106. packet2_vif_mscal(packet2, addr, 0);
  107. packet2_chain_close_tag(packet2);
  108. }
  109. /** Add aligned END tag (with nops). */
  110. static inline void packet2_utils_vu_add_end_tag(packet2_t *packet2)
  111. {
  112. packet2_chain_open_end(packet2, 0, 0);
  113. packet2_vif_nop(packet2, 0);
  114. packet2_vif_nop(packet2, 0);
  115. packet2_chain_close_tag(packet2);
  116. }
  117. /**
  118. * Get program size in qws
  119. * @param start Start address.
  120. * @param end End address.
  121. * @returns Packet size in qws for specified program (instructions/256 + 1qw for tolerance).
  122. */
  123. static inline u32 packet2_utils_get_packet_size_for_program(u32 *start, u32 *end)
  124. {
  125. // Count instructions
  126. u32 count = (end - start) / 2;
  127. if (count & 1)
  128. count++;
  129. return (count >> 8) + 1;
  130. }
  131. // ----
  132. // GIF
  133. // ----
  134. /**
  135. * Add set GIFTag which will be
  136. * sent from VU1 via XGKICK instruction.
  137. * @param packet2 Pointer to packet2.
  138. * @param loops_count How many GIF tags there will be?
  139. */
  140. static inline void packet2_utils_gif_add_set(packet2_t *packet2, u32 loops_count)
  141. {
  142. packet2_add_2x_s64(packet2, GIF_SET_TAG(loops_count, 0, 0, 0, GIF_FLG_PACKED, 1), GIF_REG_AD);
  143. }
  144. // ----
  145. // GS
  146. // ----
  147. /**
  148. * Add lod GIFTag which will be
  149. * sent from VU1 via XGKICK instruction.
  150. * @param packet2 Pointer to packet2.
  151. * @param lod Pointer to lod settings.
  152. */
  153. static inline void packet2_utils_gs_add_lod(packet2_t *packet2, lod_t *lod)
  154. {
  155. packet2_add_2x_s64(
  156. packet2,
  157. GS_SET_TEX1(
  158. lod->calculation,
  159. lod->max_level,
  160. lod->mag_filter,
  161. lod->min_filter,
  162. lod->mipmap_select,
  163. lod->l,
  164. (int)(lod->k * 16.0F)),
  165. GS_REG_TEX1);
  166. }
  167. /**
  168. * Add texture buffer / clut GIFTag which will be
  169. * sent from VU1 via XGKICK instruction.
  170. * @param packet2 Pointer to packet2.
  171. * @param texbuff Pointer to texture buffer.
  172. * @param clut Pointer to clut buffer.
  173. */
  174. static inline void packet2_utils_gs_add_texbuff_clut(packet2_t *packet2, texbuffer_t *texbuff, clutbuffer_t *clut)
  175. {
  176. packet2_add_2x_s64(
  177. packet2,
  178. GS_SET_TEX0(
  179. texbuff->address >> 6,
  180. texbuff->width >> 6,
  181. texbuff->psm,
  182. texbuff->info.width,
  183. texbuff->info.height,
  184. texbuff->info.components,
  185. texbuff->info.function,
  186. clut->address >> 6,
  187. clut->psm,
  188. clut->storage_mode,
  189. clut->start,
  190. clut->load_method),
  191. GS_REG_TEX0);
  192. }
  193. /**
  194. * Add draw finish event GIFTag which will be sent from VU1 via
  195. * XGKICK instruction.
  196. * Used for synchronization via draw_wait_finish()
  197. * @param packet2 Pointer to packet2.
  198. */
  199. static inline void packet2_utils_gs_add_draw_finish_giftag(packet2_t *packet2)
  200. {
  201. packet2_add_2x_s64(packet2, 1, GS_REG_FINISH);
  202. }
  203. /**
  204. * Add drawing (prim) GIFTag which will be sent from VU1 via
  205. * XGKICK instruction.
  206. * @param packet2 Pointer to packet2.
  207. * @param prim Pointer to prim settings.
  208. * @param loops_count GIF tag loops count.
  209. * @param nreg What types of data we will use?
  210. * @param nreg_count How many types there are in nreg?
  211. * @param context Drawing context
  212. */
  213. 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)
  214. {
  215. packet2_add_2x_s64(
  216. packet2,
  217. VU_GS_GIFTAG(
  218. loops_count, // Information for GS. Amount of loops
  219. 1,
  220. 1,
  221. VU_GS_PRIM(
  222. prim->type,
  223. prim->shading,
  224. prim->mapping,
  225. prim->fogging,
  226. prim->blending,
  227. prim->antialiasing,
  228. prim->mapping_type,
  229. context, // context
  230. prim->colorfix),
  231. 0,
  232. nreg_count),
  233. nreg);
  234. }
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif /* __PACKET2_UTILS_H__ */
  239. /** @} */ // end of packet2_utils subgroup