add test label
[ps3freebsd_ps3gpu_test.git] / util.c
blob5f70745b355ee31afabf36c72935b08fe18ae6d0
1 /*-
2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD$
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <math.h>
34 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <unistd.h>
40 #include "ps3gpu_ctl.h"
41 #include "ps3gpu_mth.h"
42 #include "util.h"
44 int
45 setup_control(int fd, int context_id, unsigned long put, unsigned long get,
46 unsigned int ref)
48 struct ps3gpu_ctl_setup_control setup_control;
50 setup_control.context_id = context_id;
51 setup_control.put = put;
52 setup_control.get = get;
53 setup_control.ref = ref;
55 return (ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control));
58 int
59 memory_allocate(int fd, int context_id, int type, int size, int align,
60 unsigned long *handle, unsigned int *gpu_addr, void **map)
62 struct ps3gpu_ctl_memory_allocate memory_allocate;
63 struct ps3gpu_ctl_memory_free memory_free;
64 int err;
66 memory_allocate.context_id = context_id;
67 memory_allocate.type = type;
68 memory_allocate.size = size;
69 memory_allocate.align = align;
71 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
72 if (err < 0)
73 return (err);
75 if (handle)
76 *handle = memory_allocate.handle;
78 if (gpu_addr)
79 *gpu_addr = memory_allocate.gpu_addr;
81 if (map) {
82 *map = mmap(NULL, memory_allocate.size,
83 PROT_READ | PROT_WRITE, MAP_SHARED, fd, memory_allocate.handle);
84 if (*map == (void *) MAP_FAILED) {
85 memory_free.context_id = context_id;
86 memory_free.handle = memory_allocate.handle;
87 err = errno;
88 ioctl(fd, PS3GPU_CTL_MEMORY_FREE, &memory_free);
89 errno = err;
90 return (-1);
94 return (0);
97 int
98 display_buffer_set(int fd, int context_id, int buffer_id, int width, int height,
99 int pitch, unsigned long offset)
101 struct ps3gpu_ctl_display_buffer_set display_buffer_set;
103 display_buffer_set.context_id = context_id;
104 display_buffer_set.buffer_id = buffer_id;
105 display_buffer_set.width = width;
106 display_buffer_set.height = height;
107 display_buffer_set.pitch = pitch;
108 display_buffer_set.offset = offset;
110 return (ioctl(fd, PS3GPU_CTL_DISPLAY_BUFFER_SET, &display_buffer_set));
114 set_flip_mode(int fd, int context_id, int head, int mode)
116 struct ps3gpu_ctl_set_flip_mode set_flip_mode;
118 set_flip_mode.context_id = context_id;
119 set_flip_mode.head = head;
120 set_flip_mode.mode = mode;
122 return (ioctl(fd, PS3GPU_CTL_SET_FLIP_MODE, &set_flip_mode));
126 reset_flip_status(int fd, int context_id, int head)
128 struct ps3gpu_ctl_reset_flip_status reset_flip_status;
130 reset_flip_status.context_id = context_id;
131 reset_flip_status.head = head;
133 return (ioctl(fd, PS3GPU_CTL_RESET_FLIP_STATUS, &reset_flip_status));
137 flip(int fd, int context_id, int head, unsigned long offset)
139 struct ps3gpu_ctl_flip flip;
141 flip.context_id = context_id;
142 flip.head = head;
143 flip.offset = offset;
145 return (ioctl(fd, PS3GPU_CTL_FLIP, &flip));
149 save_image(const char *filename, const char *buf, int len)
151 FILE *fp;
152 int nwritten;
153 int err = 0;
155 fp = fopen(filename, "w");
156 if (!fp)
157 return (-1);
159 nwritten = fwrite(buf, 1, len, fp);
160 if (nwritten != len)
161 err = -1;
163 fclose(fp);
165 return (err);
168 void
169 dump_fifo(FILE *fp, const uint32_t *fifo, unsigned int word_count)
171 int i = 0;
173 while (word_count--) {
174 fprintf(fp, "%08x: %08x\n", i * 4, fifo[i]);
175 i++;
180 set_reference(uint32_t *fifo, uint32_t val)
182 int i = 0;
184 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF);
185 fifo[i++] = val;
187 return (i);
191 transfer_data(uint32_t *fifo, uint32_t src, uint32_t dst,
192 uint32_t dst_offset, int32_t dst_pitch, uint32_t src_offset, int32_t src_pitch,
193 uint32_t row_length, uint32_t row_count)
195 int i = 0;
196 int h;
198 fifo[i++] = PS3GPU_MTH_HDR(2, 1, 0x184);
199 fifo[i++] = src;
200 fifo[i++] = dst;
202 while (row_count) {
203 h = row_count;
204 if (h > 2047)
205 h = 2047;
207 fifo[i++] = PS3GPU_MTH_HDR(8, 1, 0x30c);
208 fifo[i++] = src_offset;
209 fifo[i++] = dst_offset;
210 fifo[i++] = src_pitch;
211 fifo[i++] = dst_pitch;
212 fifo[i++] = row_length;
213 fifo[i++] = h;
214 fifo[i++] = 0x00000101;
215 fifo[i++] = 0x00000000;
217 src_offset += h * src_pitch;
218 dst_offset += h * dst_pitch;
219 row_count -= h;
222 fifo[i++] = PS3GPU_MTH_HDR(1, 1, 0x310);
223 fifo[i++] = 0x00000000;
225 return (i);
229 transfer_inline(uint32_t *fifo, uint32_t dst, uint32_t dst_offset,
230 const uint32_t *data, uint32_t word_count)
232 int odd = word_count & 1;
233 int i = 0;
235 fifo[i++] = PS3GPU_MTH_HDR(1, 3, 0x188);
236 fifo[i++] = dst;
238 fifo[i++] = PS3GPU_MTH_HDR(1, 3, 0x30c);
239 fifo[i++] = dst_offset & ~0x3f;
241 fifo[i++] = PS3GPU_MTH_HDR(2, 3, 0x300);
242 fifo[i++] = 0x0000000b;
243 fifo[i++] = 0x10001000;
245 fifo[i++] = PS3GPU_MTH_HDR(3, 5, 0x304);
246 fifo[i++] = (dst_offset >> 2) & 0xf;
247 fifo[i++] = 0x00010000 | word_count;
248 fifo[i++] = 0x00010000 | word_count;
250 fifo[i++] = PS3GPU_MTH_HDR((word_count + 1) & ~1, 5, 0x400);
252 while (word_count--)
253 fifo[i++] = *data++;
255 if (odd)
256 fifo[i++] = 0x00000000;
258 return (i);
262 flip_display_buffer(uint32_t *fifo, uint8_t channel_id, uint8_t buffer_id,
263 uint8_t head)
265 int i = 0;
267 /* reset flip label */
269 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
270 fifo[i++]= (head << 4);
272 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x6c);
273 fifo[i++] = 0x00000001;
275 /* 0xcafebabe flip method */
277 fifo[i++] = PS3GPU_MTH_HDR(1, 7, 0x920 + (head << 2));
278 fifo[i++] = 0x80000000 | (channel_id << 8) | buffer_id;
280 return (i);
284 set_depth_mask(uint32_t *fifo, uint32_t mask)
286 int i = 0;
288 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_DEPTH_MASK);
289 fifo[i++] = mask;
291 return (i);
295 set_color_mask(uint32_t *fifo, uint32_t mask)
297 int i = 0;
299 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_COLOR_MASK);
300 fifo[i++] = mask;
302 return (i);
306 set_color_mask_mrt(uint32_t *fifo, uint32_t mask)
308 int i = 0;
310 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_COLOR_MASK_MRT);
311 fifo[i++] = mask;
313 return (i);
317 set_clear_color(uint32_t *fifo, uint32_t color)
319 int i = 0;
321 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_CLEAR_COLOR);
322 fifo[i++] = color;
324 return (i);
328 set_scissor(uint32_t *fifo, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
330 int i = 0;
332 fifo[i++] = PS3GPU_MTH_HDR(2, 0, PS3GPU_MTH_ADDR_SCISSOR);
333 fifo[i++] = (w << 16) | x;
334 fifo[i++] = (h << 16) | y;
336 return (i);
340 set_front_poly_mode(uint32_t *fifo, uint32_t mode)
342 int i = 0;
344 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1828);
345 fifo[i++] = mode;
347 return (i);
351 set_shade_mode(uint32_t *fifo, uint32_t mode)
353 int i = 0;
355 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x368);
356 fifo[i++] = mode;
358 return (i);
362 blend_enable(uint32_t *fifo, uint32_t enable)
364 int i = 0;
366 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x310);
367 fifo[i++] = enable;
369 return (i);
373 clear_surface(uint32_t *fifo, uint32_t mask)
375 int i = 0;
377 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d94);
378 fifo[i++] = mask;
380 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x100);
381 fifo[i++] = 0x00000000;
383 return (i);
387 set_surface(uint32_t *fifo, const struct surface_desc *sd)
389 int i = 0;
391 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x194);
392 fifo[i++] = sd->sd_color_loc[0];
394 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x18c);
395 fifo[i++] = sd->sd_color_loc[1];
397 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1b4);
398 fifo[i++] = sd->sd_color_loc[2];
399 fifo[i++] = sd->sd_color_loc[3];
401 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x198);
402 fifo[i++] = sd->sd_depth_loc;
404 fifo[i++] = PS3GPU_MTH_HDR(6, 0, 0x208);
405 fifo[i++] = ((uint8_t) log2(sd->sd_h) << 24) | ((uint8_t) log2(sd->sd_w) << 16) |
406 (0x0 << 12) | (0x1 << 8) |
407 (sd->sd_depth_fmt << 5) | sd->sd_color_fmt;
408 fifo[i++] = sd->sd_color_pitch[0];
409 fifo[i++] = sd->sd_color_off[0];
410 fifo[i++] = sd->sd_depth_off;
411 fifo[i++] = sd->sd_color_off[1];
412 fifo[i++] = sd->sd_color_pitch[1];
414 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x22c);
415 fifo[i++] = sd->sd_depth_pitch;
417 fifo[i++] = PS3GPU_MTH_HDR(4, 0, 0x280);
418 fifo[i++] = sd->sd_color_pitch[2];
419 fifo[i++] = sd->sd_color_pitch[3];
420 fifo[i++] = sd->sd_color_off[2];
421 fifo[i++] = sd->sd_color_off[3];
423 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x220);
424 fifo[i++] = sd->sd_color_target;
426 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x2b8);
427 fifo[i++] = (sd->sd_y << 16) | sd->sd_x;
429 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x200);
430 fifo[i++] = (sd->sd_w << 16) | sd->sd_x;
431 fifo[i++] = (sd->sd_h << 16) | sd->sd_y;
433 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d88);
434 fifo[i++] = (0x0 << 16) | (0x1 << 12) |
435 (sd->sd_h - ((sd->sd_h >> 12) & 0x1));
437 return (i);
441 wait_label(uint32_t *fifo, uint32_t index, uint32_t val)
443 int i = 0;
445 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
446 fifo[i++] = index << 4;
448 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x68);
449 fifo[i++] = val;
451 return (i);
455 write_label(uint32_t *fifo, uint32_t index, uint32_t val)
457 int i = 0;
459 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
460 fifo[i++] = index << 4;
462 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x6c);
463 fifo[i++] = val;
465 return (i);
469 write_backend_label(uint32_t *fifo, uint32_t index, uint32_t val)
471 int i = 0;
473 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d6c);
474 fifo[i++] = index << 4;
476 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d70);
477 fifo[i++] = (val & 0xff00ff00) | ((val & 0xff) << 16) | ((val >> 16) & 0xff);
479 return (i);
483 write_texture_label(uint32_t *fifo, uint32_t index, uint32_t val)
485 int i = 0;
487 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d6c);
488 fifo[i++] = index << 4;
490 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d74);
491 fifo[i++] = val;
493 return (i);
497 set_viewport(uint32_t *fifo, uint16_t x, uint16_t y, uint16_t w, uint16_t h,
498 float zmin, float zmax, const float offset[4], const float scale[4])
500 int i = 0;
502 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0xa00);
503 fifo[i++] = (w << 16) | x;
504 fifo[i++] = (h << 16) | y;
506 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x394);
507 fifo[i++] = *(uint32_t *) &zmin;
508 fifo[i++] = *(uint32_t *) &zmax;
510 fifo[i++] = PS3GPU_MTH_HDR(8, 0, 0xa20);
511 fifo[i++] = *(uint32_t *) &offset[0];
512 fifo[i++] = *(uint32_t *) &offset[1];
513 fifo[i++] = *(uint32_t *) &offset[2];
514 fifo[i++] = *(uint32_t *) &offset[3];
515 fifo[i++] = *(uint32_t *) &scale[0];
516 fifo[i++] = *(uint32_t *) &scale[1];
517 fifo[i++] = *(uint32_t *) &scale[2];
518 fifo[i++] = *(uint32_t *) &scale[3];
520 fifo[i++] = PS3GPU_MTH_HDR(8, 0, 0xa20);
521 fifo[i++] = *(uint32_t *) &offset[0];
522 fifo[i++] = *(uint32_t *) &offset[1];
523 fifo[i++] = *(uint32_t *) &offset[2];
524 fifo[i++] = *(uint32_t *) &offset[3];
525 fifo[i++] = *(uint32_t *) &scale[0];
526 fifo[i++] = *(uint32_t *) &scale[1];
527 fifo[i++] = *(uint32_t *) &scale[2];
528 fifo[i++] = *(uint32_t *) &scale[3];
530 return (i);
534 load_vertex_prg(uint32_t *fifo, uint16_t slot, const uint32_t *prg, int instr_count)
536 int i = 0;
537 int j;
539 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1e9c);
540 fifo[i++] = slot;
542 while (instr_count >= 32) {
543 fifo[i++] = PS3GPU_MTH_HDR(32, 0, 0xb80);
545 for (j = 0; j < 32; j++) {
546 fifo[i++] = *prg++;
547 fifo[i++] = *prg++;
548 fifo[i++] = *prg++;
549 fifo[i++] = *prg++;
552 instr_count -= 32;
555 if (instr_count > 0) {
556 fifo[i++] = PS3GPU_MTH_HDR(instr_count << 2, 0, 0xb80);
558 for (j = 0; j < instr_count; j++) {
559 fifo[i++] = *prg++;
560 fifo[i++] = *prg++;
561 fifo[i++] = *prg++;
562 fifo[i++] = *prg++;
566 return (i);
570 set_vertex_prg_reg_count(uint32_t *fifo, unsigned int count)
572 int i = 0;
573 uint32_t val;
575 val = (count <= 32) ? 0x0020ffff : 0x0030ffff;
577 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ef8);
578 fifo[i++] = val;
580 return (i);
584 set_vertex_prg_start_slot(uint32_t *fifo, uint16_t slot)
586 int i = 0;
588 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ea0);
589 fifo[i++] = slot;
591 return (i);
595 set_vertex_prg_const(uint32_t *fifo, uint32_t start, uint32_t count,
596 const float *val)
598 int i = 0;
599 int j;
601 while (count >= 32) {
602 fifo[i++] = PS3GPU_MTH_HDR(33, 0, 0x1efc);
603 fifo[i++] = start;
605 for (j = 0; j < 32; j++)
606 fifo[i++] = *(uint32_t *) val++;
608 count -= 32;
609 start += 8;
612 if (count > 0) {
613 fifo[i++] = PS3GPU_MTH_HDR(count + 1, 0, 0x1efc);
614 fifo[i++] = start;
616 for (j = 0; j < count; j++)
617 fifo[i++] = *(uint32_t *) val++;
620 return (i);
624 set_vertex_attr_inmask(uint32_t *fifo, uint32_t mask)
626 int i = 0;
628 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ff0);
629 fifo[i++] = mask;
631 return (i);
635 set_vertex_attr_outmask(uint32_t *fifo, uint32_t mask)
637 int i = 0;
639 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ff4);
640 fifo[i++] = mask;
642 return (i);
646 set_frag_prg(uint32_t *fifo, uint8_t location, uint32_t offset)
648 int i = 0;
650 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x8e4);
651 fifo[i++] = offset | location;
653 return (i);
657 frag_prg_ctrl(uint32_t *fifo, uint8_t reg_count, uint8_t replace_txp_with_tex,
658 uint8_t pixel_kill, uint8_t output_from_h0, uint8_t depth_replace)
660 int i = 0;
662 if (reg_count <= 1)
663 reg_count = 2;
665 if (depth_replace)
666 depth_replace = 0x7;
668 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d60);
669 fifo[i++] = (reg_count << 24) | (replace_txp_with_tex << 15) | 0x00000400 |
670 (pixel_kill << 7) | (!output_from_h0 << 6) | (depth_replace << 1);
672 return (i);
676 draw_begin(uint32_t *fifo, uint32_t mode)
678 int i = 0;
680 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(3, 0, 0x1714);
681 fifo[i++] = 0x00000000;
682 fifo[i++] = 0x00000000;
683 fifo[i++] = 0x00000000;
685 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
686 fifo[i++] = mode;
688 return (i);
692 draw_end(uint32_t *fifo)
694 int i = 0;
696 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
697 fifo[i++] = 0x00000000;
699 return (i);
703 set_vertex_data_arrfmt(uint32_t *fifo, uint8_t reg, uint16_t freq,
704 uint8_t stride, uint8_t size, uint8_t type)
706 int i = 0;
708 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1740 + (reg << 4));
709 fifo[i++] = (freq << 16) | (stride << 8) | (size << 4) | type;
711 return (i);
715 set_vertex_data_2f(uint32_t *fifo, uint8_t reg, const float val[2])
717 int i = 0;
719 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1880 + (reg << 3));
720 fifo[i++] = *(uint32_t *) &val[0];
721 fifo[i++] = *(uint32_t *) &val[1];
723 return (i);
727 set_vertex_data_4f(uint32_t *fifo, uint8_t reg, const float val[4])
729 int i = 0;
731 fifo[i++] = PS3GPU_MTH_HDR(4, 0, 0x1c00 + (reg << 4));
732 fifo[i++] = *(uint32_t *) &val[0];
733 fifo[i++] = *(uint32_t *) &val[1];
734 fifo[i++] = *(uint32_t *) &val[2];
735 fifo[i++] = *(uint32_t *) &val[3];
737 return (i);
741 set_vertex_data_arr(uint32_t *fifo, uint8_t reg,
742 uint16_t freq, uint8_t stride, uint8_t size, uint8_t type,
743 uint8_t location, uint32_t offset)
745 int i = 0;
747 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1740 + (reg << 2));
748 fifo[i++] = (freq << 16) | (stride << 8) | (size << 4) | type;
750 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1680 + (reg << 2));
751 fifo[i++] = (location << 31) | offset;
753 return (i);
757 draw_arrays(uint32_t *fifo, uint32_t mode, uint32_t first, uint32_t count)
759 int i = 0;
760 int j;
762 /* draw begin */
764 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(3, 0, 0x1714);
765 fifo[i++] = 0x00000000;
766 fifo[i++] = 0x00000000;
767 fifo[i++] = 0x00000000;
769 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
770 fifo[i++] = mode;
772 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1814);
773 fifo[i++] = (((count - 1) & 0xff) << 24) | first;
775 first += ((count - 1) & 0xff) + 1;
776 count -= ((count - 1) & 0xff) + 1;
778 while (count >= (0x7ff * 0x100)) {
779 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(0x7ff, 0, 0x1814);
781 for (j = 0; j < 0x7ff; j++) {
782 fifo[i++] = ((0x100 - 1) << 24) | first;
783 first += 0x100;
786 count -= 0x7ff * 0x100;
789 if (count) {
790 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(count, 0, 0x1814);
792 for (j = 0; j < count; j++) {
793 fifo[i++] = ((0x100 - 1) << 24) | first;
794 first += 0x100;
798 /* draw end */
800 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
801 fifo[i++] = 0x00000000;
803 return (i);
807 invalidate_vertex_cache(uint32_t *fifo)
809 int i = 0;
811 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1710);
812 fifo[i++] = 0x00000000;
814 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
815 fifo[i++] = 0x00000000;
817 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
818 fifo[i++] = 0x00000000;
820 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
821 fifo[i++] = 0x00000000;
823 return (i);
827 set_texture(uint32_t *fifo, uint8_t index, const struct texture_desc *td)
829 int i = 0;
831 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1a00 + (index << 5));
832 fifo[i++] = td->td_off;
833 fifo[i++] = (td->td_mipmap << 16) | (td->td_fmt << 8) |
834 (td->td_dimension << 4) | (td->td_border << 3) |
835 (td->td_cubemap << 2) | td->td_loc;
837 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a18 + (index << 5));
838 fifo[i++] = (td->td_w << 16) | td->td_h;
840 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1840 + (index << 2));
841 fifo[i++] = (td->td_depth << 20) | td->td_pitch;
843 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a10 + (index << 5));
844 fifo[i++] = td->td_remap;
846 return (i);
850 texture_ctrl(uint32_t *fifo, uint8_t index, uint8_t enable,
851 uint16_t min_lod, uint16_t max_lod, uint8_t max_aniso)
853 int i = 0;
855 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a0c + (index << 5));
856 fifo[i++] = (enable << 31) | ((min_lod & 0xfff) << 19) |
857 ((max_lod & 0xfff) << 7) | (max_aniso << 4);
859 return (i);
863 set_texture_addr(uint32_t *fifo, uint8_t index,
864 uint8_t wrap_s, uint8_t wrap_t, uint8_t wrap_r, uint8_t unsigned_remap,
865 uint8_t zfunc, uint8_t gamma)
867 int i = 0;
869 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a08 + (index << 5));
870 fifo[i++] = (zfunc << 28) | (gamma << 20) | (wrap_r << 16) |
871 (unsigned_remap << 12) | (wrap_t << 8) | wrap_s;
873 return (i);
877 set_texture_filter(uint32_t *fifo, uint8_t index,
878 uint16_t bias, uint8_t min, uint8_t mag, uint8_t conv)
880 int i = 0;
882 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a14 + (index << 5));
883 fifo[i++] = (mag << 24) | (min << 16) | (conv << 13) | (bias & 0x1fff);
885 return (i);
889 invalidate_texture_cache(uint32_t *fifo, uint32_t val)
891 int i = 0;
893 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1fd8);
894 fifo[i++] = val;
896 return (i);