sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
gsInline.h
Go to the documentation of this file.
1
5// ____ ___ | / _____ _____
6// | __ | |___/ | |
7// |___| ___| | \ __|__ | gsKit Open Source Project.
8// ----------------------------------------------------------------------
9// Copyright 2004 - Chris "Neovanglist" Gilbert <Neovanglist@LainOS.org>
10// Licenced under Academic Free License version 2.0
11// Review gsKit README & LICENSE files for further details.
12//
13// gsInline.h - Static Inline Routines
14//
15//
16
17#ifndef __GSINLINE_H__
18#define __GSINLINE_H__
19
20#include <stdio.h>
21#include <stdint.h>
22
23// Drawbuffer Heap Allocator, GSQUEUE version
24static inline void *_gsKit_heap_alloc(GSQUEUE *q, int qsize, int bsize, int type)
25{
26 void *p_heap;
27
28#ifdef GSKIT_DEBUG
29 if(((u32)q->pool_cur + bsize ) >= (u32)q->pool_max[q->dbuf])
30 {
31 printf("GSKIT: WARNING! HEAP OVERFLOW FOR RENDERQUEUE %p!!\n", q);
32 return NULL;
33 }
34#endif
35
36 if((q->tag_size + qsize) >= 65535)
37 {
38 *(u64 *)q->dma_tag = DMA_TAG(q->tag_size, 0, DMA_CNT, 0, 0, 0);
39 q->tag_size = 0;
40 q->dma_tag = q->pool_cur;
41 q->pool_cur = (u8*)q->pool_cur + 16;
42 }
43
44 if(type == GIF_AD || type != q->last_type || q->same_obj >= GS_GIF_BLOCKSIZE)
45 {
46 if(q->last_type != GIF_RESERVED && q->last_type != GIF_AD)
47 *(u64 *)q->last_tag = ((u64)q->same_obj | *(u64 *)q->last_tag);
48
49 qsize ++;
50 bsize += 16;
51 q->last_tag = q->pool_cur;
52 q->same_obj = 0;
53 }
54
55 q->same_obj++;
56 q->last_type = type;
57 q->tag_size += qsize;
58 p_heap = q->pool_cur;
59 q->pool_cur = (u8*)q->pool_cur + bsize;
60
61 return p_heap;
62}
63
65static inline void *gsKit_heap_alloc(GSGLOBAL *gsGlobal, int qsize, int bsize, int type)
66{
67 return _gsKit_heap_alloc(gsGlobal->CurQueue, qsize, bsize, type);
68}
69
70// Drawbuffer Heap Allocator (For Injected DMA_TAGs), GSQUEUE version
71static inline void *_gsKit_heap_alloc_dma(GSQUEUE *q, int qsize, int bsize)
72{
73 void *p_heap;
74
75#ifdef GSKIT_DEBUG
76 if(((u32)q->pool_cur + bsize ) >= (u32)q->pool_max[q->dbuf])
77 {
78 printf("GSKIT: WARNING! HEAP OVERFLOW FOR RENDERQUEUE %p!!\n", q);
79 return NULL;
80 }
81#endif
82
83 if(q->last_type != GIF_RESERVED && q->last_type != GIF_AD)
84 {
85 *(u64 *)q->last_tag = ((u64)q->same_obj | *(u64 *)q->last_tag);
86 }
87
88 *(u64 *)q->dma_tag = DMA_TAG(q->tag_size, 0, DMA_CNT, 0, 0, 0);
89 q->tag_size = 0;
90
92 q->same_obj = 0;
93
94 p_heap = q->pool_cur;
95 q->pool_cur = (u8*)q->pool_cur + bsize;
96 q->dma_tag = q->pool_cur;
97 q->pool_cur = (u8*)q->pool_cur + 16;
98
99 return p_heap;
100}
101
103static inline void *gsKit_heap_alloc_dma(GSGLOBAL *gsGlobal, int qsize, int bsize)
104{
105 return _gsKit_heap_alloc_dma(gsGlobal->CurQueue, qsize, bsize);
106}
107
108static inline int __gsKit_float_to_int_uv(float fuv, int imax)
109{
110 int iuv = (int)(fuv * 16.0f);
111
112 // Limit u/v to texture width/height
113
114 if (iuv < 0)
115 iuv = 0;
116
117 if (iuv > imax)
118 iuv = imax;
119
120 // Prevent overflow when using maximum size texture
121
122 if (iuv >= (1024 * 16))
123 iuv = (1024 * 16) - 1;
124
125 return iuv;
126}
127
128static inline int gsKit_float_to_int_u(const GSTEXTURE *Texture, float fu)
129{
130 return __gsKit_float_to_int_uv(fu, Texture->Width * 16);
131}
132
133static inline int gsKit_float_to_int_v(const GSTEXTURE *Texture, float fv)
134{
135 return __gsKit_float_to_int_uv(fv, Texture->Height * 16);
136}
137
138static inline int __gsKit_float_to_int_xy(float fxy, int offset)
139{
140 int ixy = (int)(fxy * 16.0f) + offset;
141
142 // Limit x/y to Primitive Coordinate System (0 to 4095.9375)
143
144 if (ixy < 0)
145 ixy = 0;
146
147 if (ixy >= (4096 * 16))
148 ixy = (4096 * 16) - 1;
149
150 return ixy;
151}
152
153static inline int gsKit_float_to_int_x(const GSGLOBAL *gsGlobal, float fx)
154{
155 return __gsKit_float_to_int_xy(fx, gsGlobal->OffsetX);
156}
157
158static inline int gsKit_float_to_int_y(const GSGLOBAL *gsGlobal, float fy)
159{
160 return __gsKit_float_to_int_xy(fy, gsGlobal->OffsetY);
161}
162
163static inline gs_xyz2 vertex_to_XYZ2(const GSGLOBAL *gsGlobal, float fx, float fy, int iz)
164{
165 gs_xyz2 res;
166
167 res.xyz.x = gsKit_float_to_int_x(gsGlobal, fx);
168 res.xyz.y = gsKit_float_to_int_y(gsGlobal, fy);
169 res.xyz.z = iz;
170 res.tag = GS_XYZ2;
171
172 return res;
173}
174
175// If STQ coordinates are used set q to 1.0f otherwise keep it as 0.0f
176static inline gs_rgbaq color_to_RGBAQ(uint8_t r, uint8_t g, uint8_t b, uint8_t a, float q)
177{
178 gs_rgbaq res;
179
180 res.color.components.r = r;
181 res.color.components.g = g;
182 res.color.components.b = b;
183 res.color.components.a = a;
184 res.color.q = q;
185 res.tag = GS_RGBAQ;
186
187 return res;
188}
189
190// If STQ coordinates are used set q to 1.0f otherwise keep it as 0.0f
191static inline gs_rgbaq rgba_to_RGBAQ(uint32_t rgba, float q)
192{
193 gs_rgbaq res;
194
195 res.color.components.rgba = rgba;
196 res.color.q = q;
197 res.tag = GS_RGBAQ;
198
199 return res;
200}
201
202static inline gs_rgbaq rgbaq_to_RGBAQ(uint64_t rgbaq)
203{
204 gs_rgbaq res;
205
206 res.color.rgbaq = rgbaq;
207 res.tag = GS_RGBAQ;
208
209 return res;
210}
211
212static inline gs_uv vertex_to_UV(const GSTEXTURE *Texture, float u, float v)
213{
214 gs_uv res;
215
216 int iu = gsKit_float_to_int_u(Texture, u);
217 int iv = gsKit_float_to_int_v(Texture, v);
218
219 res.coord.u = iu;
220 res.coord.v = iv;
221 res.tag = GS_UV;
222
223 return res;
224}
225
226static inline gs_stq vertex_to_STQ(float s, float t)
227{
228 gs_stq res;
229
230 res.st.s = s;
231 res.st.t = t;
232 res.tag = GS_ST;
233
234 return res;
235}
236
237static inline gs_texclut postion_to_TEXCLUT(u8 cbw, u8 cou, u8 cov)
238{
239 gs_texclut res;
240
241 res.specification.cbw = cbw;
242 res.specification.cou = cou;
243 res.specification.cov = cov;
244 res.tag = GS_TEXCLUT;
245
246 return res;
247}
248
249#endif /* __GSINLINE_H__ */
#define GS_GIF_BLOCKSIZE
GS GIF Data Transfer Blocksize.
Definition gsCore.h:32
#define GS_XYZ2
GS Vertex Coordinate Value.
Definition gsInit.h:166
#define GS_RGBAQ
GS Color in RGBAQ Format.
Definition gsInit.h:158
#define GS_ST
GS Texture Coordinate Value in Texture Coordinates.
Definition gsInit.h:160
#define GS_TEXCLUT
GS Texture CLUT Location.
Definition gsInit.h:198
#define GS_UV
GS Texture Coordinate Value in Texel Coordinates.
Definition gsInit.h:162
#define GIF_AD
GIF_TAG Address+Data Setting. (Used after GIF_TAG).
Definition gsInit.h:494
#define GIF_RESERVED
Reserved GIF Tag. This is never sent, only used for sanity checks.
Definition gsInit.h:492
GSQUEUE * CurQueue
Draw Queue (Current).
Definition gsInit.h:934
int OffsetX
X Window Offset.
Definition gsInit.h:921
int OffsetY
Y Window Offset.
Definition gsInit.h:922
void * dma_tag
Pointer to last DMA_TAG.
Definition gsInit.h:867
int same_obj
Number of times in a row the same REGLIST enabled object has been added (Used for NLOOP fixup later).
Definition gsInit.h:865
void * pool_max[2]
Pool Maximum Address (Used for bounds checking).
Definition gsInit.h:860
void * pool_cur
Current Pool Address (Offset from Base).
Definition gsInit.h:859
void * last_tag
Pointer to the GIF_TAG of the last object in this draw queue.
Definition gsInit.h:863
int dbuf
Which of the pool[2] buffers we are using.
Definition gsInit.h:871
int last_type
Last type of object in this draw queue.
Definition gsInit.h:862
u32 tag_size
Quadwords in current DMA_TAG.
Definition gsInit.h:868
u32 Height
Height of the Texture.
Definition gsInit.h:957
u32 Width
Width of the Texture.
Definition gsInit.h:956