ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
draw.c
Go to the documentation of this file.
1 #include <dma_tags.h>
2 #include <gif_tags.h>
3 
4 #include <gs_privileged.h>
5 #include <gs_gp.h>
6 #include <gs_psm.h>
7 
8 #include <draw.h>
9 #include <draw2d.h>
10 
12 {
13 
14  // Change this if modifying the gif packet after the giftag.
15  int qword_count = 15;
16 
17  atest_t atest;
18  dtest_t dtest;
19  ztest_t ztest;
20  blend_t blend;
21  texwrap_t wrap;
22 
23  atest.enable = DRAW_ENABLE;
25  atest.compval = 0x00;
27 
28  dtest.enable = DRAW_DISABLE;
29  dtest.pass = DRAW_DISABLE;
30 
31  // Enable or Disable ZBuffer
32  if (z->enable)
33  {
34  ztest.enable = DRAW_ENABLE;
35  ztest.method = z->method;
36  }
37  else
38  {
39  z->mask = 1;
40  ztest.enable = DRAW_ENABLE;
42  }
43 
44  // Setup alpha blending
45  blend.color1 = BLEND_COLOR_SOURCE;
46  blend.color2 = BLEND_COLOR_DEST;
47  blend.alpha = BLEND_ALPHA_SOURCE;
48  blend.color3 = BLEND_COLOR_DEST;
49  blend.fixed_alpha = 0x80;
50 
51  // Setup whole texture clamping
52  wrap.horizontal = WRAP_CLAMP;
53  wrap.vertical = WRAP_CLAMP;
54  wrap.minu = wrap.maxu = 0;
55  wrap.minv = wrap.maxv = 0;
56 
57  // Begin packed gif data packet with another qword.
58  PACK_GIFTAG(q,GIF_SET_TAG(qword_count,0,0,0,GIF_FLG_PACKED,1),GIF_REG_AD);
59  q++;
60  // Framebuffer setting
61  PACK_GIFTAG(q, GS_SET_FRAME(frame->address>>11,frame->width>>6,frame->psm,frame->mask), GS_REG_FRAME + context);
62  q++;
63  // ZBuffer setting
65  q++;
66  // Override Primitive Control
68  q++;
69  // Primitive coordinate offsets
71  q++;
72  // Scissoring area
73  PACK_GIFTAG(q, GS_SET_SCISSOR(0,frame->width-1,0,frame->height-1), GS_REG_SCISSOR + context);
74  q++;
75  // Pixel testing
76  PACK_GIFTAG(q, GS_SET_TEST(atest.enable,atest.method,atest.compval,atest.keep,
77  dtest.enable,dtest.pass,
78  ztest.enable,ztest.method), GS_REG_TEST + context);
79  q++;
80  // Fog Color
82  q++;
83  // Per-pixel Alpha Blending (Blends if MSB of ALPHA is true)
85  q++;
86  // Alpha Blending
87  PACK_GIFTAG(q, GS_SET_ALPHA(blend.color1,blend.color2,blend.alpha,
88  blend.color3,blend.fixed_alpha), GS_REG_ALPHA + context);
89  q++;
90  // Dithering
92  q++;
93  PACK_GIFTAG(q, GS_SET_DIMX(4,2,5,3,0,6,1,7,5,3,4,2,1,7,0,6), GS_REG_DIMX);
94  q++;
95  // Color Clamp
97  q++;
98  // Alpha Correction
99  if ((frame->psm == GS_PSM_16) || (frame->psm == GS_PSM_16S))
100  {
102  q++;
103  }
104  else
105  {
107  q++;
108  }
109  // Texture wrapping/clamping
110  PACK_GIFTAG(q, GS_SET_CLAMP(wrap.horizontal,wrap.vertical,wrap.minu,
111  wrap.maxu,wrap.minv,wrap.maxv), GS_REG_CLAMP + context);
112  q++;
114  q++;
115 
116  return q;
117 
118 }
119 
121 {
122 
124  q++;
128  q++;
129 
130  return q;
131 
132 }
133 
135 {
136 
138  q++;
142  q++;
143 
144  return q;
145 
146 }
147 
148 qword_t *draw_clear(qword_t *q, int context, float x, float y, float width, float height, int r, int g, int b)
149 {
150 
151  rect_t rect;
152 
153  union{
154  float fvalue;
155  u32 ivalue;
156  } q0 = {
157  1.0f
158  };
159 
160  rect.v0.x = x;
161  rect.v0.y = y;
162  rect.v0.z = 0x00000000;
163 
164  rect.color.rgbaq = GS_SET_RGBAQ(r,g,b,0x80,q0.ivalue);
165 
166  rect.v1.x = x + width - 0.9375f;
167  rect.v1.y = y + height - 0.9375f;
168  rect.v1.z = 0x00000000;
169 
170  PACK_GIFTAG(q, GIF_SET_TAG(2,0,0,0,0,1), GIF_REG_AD);
171  q++;
173  q++;
174  PACK_GIFTAG(q, GS_SET_PRMODE(0,0,0,0,0,0,context,1), GS_REG_PRMODE);
175  q++;
176 
177  q = draw_rect_filled_strips(q, context, &rect);
178 
179  PACK_GIFTAG(q, GIF_SET_TAG(1,0,0,0,0,1), GIF_REG_AD);
180  q++;
182  q++;
183 
184  return q;
185 
186 }
187 
189 {
190 
192  q++;
194  q++;
195 
196  return q;
197 
198 }
199 
201 {
202 
203  while(!(*GS_REG_CSR & 2));
204  *GS_REG_CSR |= 2;
205 
206 }
207 
209 {
210 
211  // Flush texture buffer
212  DMATAG_END(q,2,0,0,0);
213  q++;
215  q++;
217  q++;
218 
219  return q;
220 
221 }
222 
223 qword_t *draw_texture_transfer(qword_t *q, void *src, int width, int height, int psm, int dest, int dest_width)
224 {
225 
226  int i;
227  int remaining;
228  int qwords = 0;
229 
230  switch (psm)
231  {
232  case GS_PSM_8:
233  {
234  qwords = (width*height)>>4;
235  break;
236  }
237 
238  case GS_PSM_32:
239  case GS_PSM_24:
240  {
241  qwords = (width*height)>>2;
242  break;
243  }
244 
245  case GS_PSM_4:
246  {
247  qwords = (width*height)>>5;
248  break;
249  }
250 
251  case GS_PSM_16:
252  case GS_PSM_16S:
253  {
254  qwords = (width*height)>>3;
255  break;
256  }
257 
258  default:
259  {
260  switch (psm)
261  {
262  case GS_PSM_8H:
263  {
264  qwords = (width*height)>>4;
265  break;
266  }
267 
268  case GS_PSMZ_32:
269  case GS_PSMZ_24:
270  {
271  qwords = (width*height)>>2;
272  break;
273  }
274 
275  case GS_PSMZ_16:
276  case GS_PSMZ_16S:
277  {
278  qwords = (width*height)>>3;
279  break;
280  }
281 
282  case GS_PSM_4HL:
283  case GS_PSM_4HH:
284  {
285  qwords = (width*height)>>5;
286  break;
287  }
288  }
289  break;
290  }
291  }
292 
293  // Determine number of iterations based on the number of qwords
294  // that can be handled per dmatag
295  i = qwords / GIF_BLOCK_SIZE;
296 
297  // Now calculate the remaining image data left over
298  remaining = qwords % GIF_BLOCK_SIZE;
299 
300  // Setup the transfer
301  DMATAG_CNT(q,5,0,0,0);
302  q++;
304  q++;
305  PACK_GIFTAG(q,GS_SET_BITBLTBUF(0,0,0,dest>>6,dest_width>>6,psm),GS_REG_BITBLTBUF);
306  q++;
308  q++;
309  PACK_GIFTAG(q,GS_SET_TRXREG(width,height),GS_REG_TRXREG);
310  q++;
312  q++;
313 
314 
315  while(i-- > 0)
316  {
317 
318  // Setup image data dma chain
319  DMATAG_CNT(q,1,0,0,0);
320  q++;
321  PACK_GIFTAG(q,GIF_SET_TAG(GIF_BLOCK_SIZE,0,0,0,2,0),0);
322  q++;
323  DMATAG_REF(q,GIF_BLOCK_SIZE,(unsigned int)src,0,0,0);
324  q++;
325 
326  //Now increment the address by the number of qwords in bytes
327  src += (GIF_BLOCK_SIZE*16);
328 
329  }
330 
331  if(remaining)
332  {
333 
334  // Setup remaining image data dma chain
335  DMATAG_CNT(q,1,0,0,0);
336  q++;
337  PACK_GIFTAG(q,GIF_SET_TAG(remaining,0,0,0,2,0),0);
338  q++;
339  DMATAG_REF(q,remaining,(unsigned int)src,0,0,0);
340  q++;
341 
342  }
343 
344  return q;
345 
346 }
347 
348 unsigned char draw_log2(unsigned int x)
349 {
350 
351  unsigned char res;
352 
353  __asm__ __volatile__ ("plzcw %0, %1\n\t" : "=r" (res) : "r" (x));
354 
355  res = 31 - (res + 1);
356  res += (x > (1<<res) ? 1 : 0);
357 
358  return res;
359 }
#define DMATAG_REF(Q, QWC, ADDR, SPR, W2, W3)
Definition: dma_tags.h:93
#define DMATAG_END(Q, QWC, SPR, W2, W3)
Definition: dma_tags.h:75
#define DMATAG_CNT(Q, QWC, SPR, W2, W3)
Definition: dma_tags.h:57
qword_t * draw_rect_filled_strips(qword_t *q, int context, rect_t *rect)
Definition: draw2d.c:248
u8 context
Definition: main.c:71
unsigned char draw_log2(unsigned int x)
Definition: draw.c:348
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
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
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_ENABLE
Definition: draw.h:25
#define DRAW_DISABLE
Definition: draw.h:24
#define BLEND_COLOR_DEST
Definition: draw_blending.h:15
#define BLEND_ALPHA_SOURCE
Definition: draw_blending.h:18
#define ALPHA_CORRECT_RGBA16
Definition: draw_blending.h:24
#define ALPHA_CORRECT_RGBA32
Definition: draw_blending.h:23
#define BLEND_COLOR_SOURCE
Definition: draw_blending.h:14
#define PRIM_OVERRIDE_ENABLE
#define PRIM_OVERRIDE_DISABLE
#define WRAP_CLAMP
Definition: draw_sampling.h:31
#define ALPHA_EXPAND_NORMAL
Definition: draw_sampling.h:36
#define ATEST_KEEP_FRAMEBUFFER
Definition: draw_tests.h:24
#define ATEST_METHOD_NOTEQUAL
Definition: draw_tests.h:20
#define ZTEST_METHOD_ALLPASS
Definition: draw_tests.h:33
#define ftoi4(F)
Definition: draw_types.h:15
#define PACK_GIFTAG(Q, D0, D1)
Definition: gif_tags.h:76
#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
#define GIF_BLOCK_SIZE
Definition: gif_tags.h:12
#define GS_SET_TEXA(A0, AM, A1)
Definition: gs_gp.h:300
#define GS_SET_PRMODECONT(CTRL)
Definition: gs_gp.h:250
#define GS_SET_TRXREG(W, H)
Definition: gs_gp.h:317
#define GS_REG_TRXREG
Definition: gs_gp.h:135
#define GS_REG_SCISSOR
Definition: gs_gp.h:87
#define GS_REG_DTHE
Definition: gs_gp.h:101
#define GS_REG_ZBUF
Definition: gs_gp.h:125
#define GS_REG_PRMODECONT
Definition: gs_gp.h:61
#define GS_SET_DTHE(ENABLE)
Definition: gs_gp.h:197
#define GS_SET_DIMX(D00, D01, D02, D03, D10, D11, D12, D13, D20, D21, D22, D23, D30, D31, D32, D33)
Definition: gs_gp.h:187
#define GS_SET_BITBLTBUF(SBA, SBW, SPSM, DBA, DBW, DPSM)
Definition: gs_gp.h:175
#define GS_SET_TRXPOS(SX, SY, DX, DY, DIR)
Definition: gs_gp.h:312
#define GS_REG_TEXFLUSH
Definition: gs_gp.h:85
#define GS_REG_FBA
Definition: gs_gp.h:113
#define GS_SET_PRMODE(IIP, TME, FGE, ABE, AA1, FST, CTXT, FIX)
Definition: gs_gp.h:244
#define GS_SET_XYOFFSET(X, Y)
Definition: gs_gp.h:323
#define GS_SET_RGBAQ(R, G, B, A, Q)
Definition: gs_gp.h:252
#define GS_SET_COLCLAMP(CLAMP)
Definition: gs_gp.h:185
#define GS_REG_TEXA
Definition: gs_gp.h:81
#define GS_SET_FRAME(FBA, FBW, PSM, FMSK)
Definition: gs_gp.h:209
#define GS_REG_CLAMP
Definition: gs_gp.h:31
#define GS_REG_TEST
Definition: gs_gp.h:105
#define GS_REG_DIMX
Definition: gs_gp.h:99
#define GS_SET_ZBUF(ZBA, ZSM, ZMSK)
Definition: gs_gp.h:334
#define GS_SET_SCISSOR(X0, X1, Y0, Y1)
Definition: gs_gp.h:259
#define GS_SET_TEST(ATEN, ATMETH, ATREF, ATFAIL, DATEN, DATMD, ZTEN, ZTMETH)
Definition: gs_gp.h:269
#define GS_SET_FOGCOL(R, G, B)
Definition: gs_gp.h:205
#define GS_SET_CLAMP(WMS, WMT, MINU, MAXU, MINV, MAXV)
Definition: gs_gp.h:180
#define GS_REG_FOGCOL
Definition: gs_gp.h:83
#define GS_REG_XYOFFSET
Definition: gs_gp.h:55
#define GS_REG_PABE
Definition: gs_gp.h:111
#define GS_SET_PABE(ENABLE)
Definition: gs_gp.h:235
#define GS_REG_TRXPOS
Definition: gs_gp.h:133
#define GS_REG_COLCLAMP
Definition: gs_gp.h:103
#define GS_REG_BITBLTBUF
Definition: gs_gp.h:131
#define GS_REG_PRMODE
Definition: gs_gp.h:63
#define GS_REG_FRAME
Definition: gs_gp.h:119
#define GS_SET_ALPHA(A, B, C, D, ALPHA)
Definition: gs_gp.h:170
#define GS_REG_FINISH
Definition: gs_gp.h:143
#define GS_SET_FBA(ALPHA)
Definition: gs_gp.h:199
#define GS_REG_TRXDIR
Definition: gs_gp.h:137
#define GS_SET_TRXDIR(DIR)
Definition: gs_gp.h:308
#define GS_REG_ALPHA
Definition: gs_gp.h:93
#define GS_REG_CSR
Definition: gs_privileged.h:45
#define GS_PSM_4HH
Definition: gs_psm.h:29
#define GS_PSM_4
Definition: gs_psm.h:23
#define GS_PSM_16S
Definition: gs_psm.h:17
#define GS_PSMZ_16S
Definition: gs_psm.h:37
#define GS_PSMZ_24
Definition: gs_psm.h:33
#define GS_PSMZ_32
Definition: gs_psm.h:31
#define GS_PSM_4HL
Definition: gs_psm.h:27
#define GS_PSM_8H
Definition: gs_psm.h:25
#define GS_PSM_32
Definition: gs_psm.h:11
#define GS_PSMZ_16
Definition: gs_psm.h:35
#define GS_PSM_24
Definition: gs_psm.h:13
#define GS_PSM_8
Definition: gs_psm.h:21
#define GS_PSM_16
Definition: gs_psm.h:15
#define GS_ENABLE
Definition: libgs.h:63
#define GS_DISABLE
Definition: libgs.h:62
s32 x
Definition: libmouse.c:34
s32 y
Definition: libmouse.c:34
unsigned char compval
Definition: draw_tests.h:40
unsigned char enable
Definition: draw_tests.h:38
unsigned char method
Definition: draw_tests.h:39
unsigned char keep
Definition: draw_tests.h:41
char alpha
Definition: draw_blending.h:29
unsigned char fixed_alpha
Definition: draw_blending.h:31
char color3
Definition: draw_blending.h:30
char color2
Definition: draw_blending.h:28
char color1
Definition: draw_blending.h:27
unsigned char pass
Definition: draw_tests.h:46
unsigned char enable
Definition: draw_tests.h:45
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
Definition: draw2d.h:31
vertex_t v1
Definition: draw2d.h:33
color_t color
Definition: draw2d.h:34
vertex_t v0
Definition: draw2d.h:32
unsigned char vertical
Definition: draw_sampling.h:60
unsigned char horizontal
Definition: draw_sampling.h:59
float y
Definition: draw_types.h:53
unsigned int z
Definition: draw_types.h:54
float x
Definition: draw_types.h:52
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
unsigned char method
Definition: draw_tests.h:51
unsigned char enable
Definition: draw_tests.h:50
unsigned int u32
Definition: tamtypes.h:30
u64 rgbaq
Definition: draw_types.h:40