implement get_tiled_pitch_size
[ps3freebsd_ps3gpu_test.git] / util.c
bloba3d7e009e7f30e2461f5c19d8fab20b556cce6f0
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));
148 uint32_t
149 get_tiled_pitch_size(uint32_t pitch)
151 static const uint32_t tile_pitches[] = {
152 0x00000000, 0x00000200, 0x00000300, 0x00000400,
153 0x00000500, 0x00000600, 0x00000700, 0x00000800,
154 0x00000a00, 0x00000c00, 0x00000d00, 0x00000e00,
155 0x00001000, 0x00001400, 0x00001800, 0x00001a00,
156 0x00001c00, 0x00002000, 0x00002800, 0x00003000,
157 0x00003400, 0x00003800, 0x00004000, 0x00005000,
158 0x00006000, 0x00006800, 0x00007000, 0x00008000,
159 0x0000a000, 0x0000c000, 0x0000d000, 0x0000e000,
160 0x00010000,
163 int i;
165 for (i = 0; i < ARRAY_SIZE(tile_pitches) - 1; i++) {
166 if ((tile_pitches[i] < pitch) && (pitch <= tile_pitches[i + 1]))
167 return (tile_pitches[i + 1]);
170 return (0);
174 save_image(const char *filename, const char *buf, int len)
176 FILE *fp;
177 int nwritten;
178 int err = 0;
180 fp = fopen(filename, "w");
181 if (!fp)
182 return (-1);
184 nwritten = fwrite(buf, 1, len, fp);
185 if (nwritten != len)
186 err = -1;
188 fclose(fp);
190 return (err);
193 void
194 dump_fifo(FILE *fp, const uint32_t *fifo, unsigned int word_count)
196 int i = 0;
198 while (word_count--) {
199 fprintf(fp, "%08x: %08x\n", i * 4, fifo[i]);
200 i++;
205 set_reference(uint32_t *fifo, uint32_t val)
207 int i = 0;
209 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF);
210 fifo[i++] = val;
212 return (i);
216 transfer_data(uint32_t *fifo, uint32_t src, uint32_t dst,
217 uint32_t dst_offset, int32_t dst_pitch, uint32_t src_offset, int32_t src_pitch,
218 uint32_t row_length, uint32_t row_count)
220 int i = 0;
221 int h;
223 fifo[i++] = PS3GPU_MTH_HDR(2, 1, 0x184);
224 fifo[i++] = src;
225 fifo[i++] = dst;
227 while (row_count) {
228 h = row_count;
229 if (h > 2047)
230 h = 2047;
232 fifo[i++] = PS3GPU_MTH_HDR(8, 1, 0x30c);
233 fifo[i++] = src_offset;
234 fifo[i++] = dst_offset;
235 fifo[i++] = src_pitch;
236 fifo[i++] = dst_pitch;
237 fifo[i++] = row_length;
238 fifo[i++] = h;
239 fifo[i++] = 0x00000101;
240 fifo[i++] = 0x00000000;
242 src_offset += h * src_pitch;
243 dst_offset += h * dst_pitch;
244 row_count -= h;
247 fifo[i++] = PS3GPU_MTH_HDR(1, 1, 0x310);
248 fifo[i++] = 0x00000000;
250 return (i);
254 transfer_inline(uint32_t *fifo, uint32_t dst, uint32_t dst_offset,
255 const uint32_t *data, uint32_t word_count)
257 int odd = word_count & 1;
258 int i = 0;
260 fifo[i++] = PS3GPU_MTH_HDR(1, 3, 0x188);
261 fifo[i++] = dst;
263 fifo[i++] = PS3GPU_MTH_HDR(1, 3, 0x30c);
264 fifo[i++] = dst_offset & ~0x3f;
266 fifo[i++] = PS3GPU_MTH_HDR(2, 3, 0x300);
267 fifo[i++] = 0x0000000b;
268 fifo[i++] = 0x10001000;
270 fifo[i++] = PS3GPU_MTH_HDR(3, 5, 0x304);
271 fifo[i++] = (dst_offset >> 2) & 0xf;
272 fifo[i++] = 0x00010000 | word_count;
273 fifo[i++] = 0x00010000 | word_count;
275 fifo[i++] = PS3GPU_MTH_HDR((word_count + 1) & ~1, 5, 0x400);
277 while (word_count--)
278 fifo[i++] = *data++;
280 if (odd)
281 fifo[i++] = 0x00000000;
283 return (i);
287 flip_display_buffer(uint32_t *fifo, uint8_t channel_id, uint8_t buffer_id,
288 uint8_t head)
290 int i = 0;
292 /* reset flip label */
294 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
295 fifo[i++]= (head << 4);
297 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x6c);
298 fifo[i++] = 0x00000001;
300 /* 0xcafebabe flip method */
302 fifo[i++] = PS3GPU_MTH_HDR(1, 7, 0x920 + (head << 2));
303 fifo[i++] = 0x80000000 | (channel_id << 8) | buffer_id;
305 return (i);
309 set_depth_mask(uint32_t *fifo, uint32_t mask)
311 int i = 0;
313 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_DEPTH_MASK);
314 fifo[i++] = mask;
316 return (i);
320 set_color_mask(uint32_t *fifo, uint32_t mask)
322 int i = 0;
324 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_COLOR_MASK);
325 fifo[i++] = mask;
327 return (i);
331 set_color_mask_mrt(uint32_t *fifo, uint32_t mask)
333 int i = 0;
335 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_COLOR_MASK_MRT);
336 fifo[i++] = mask;
338 return (i);
342 set_clear_color(uint32_t *fifo, uint32_t color)
344 int i = 0;
346 fifo[i++] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_CLEAR_COLOR);
347 fifo[i++] = color;
349 return (i);
353 set_scissor(uint32_t *fifo, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
355 int i = 0;
357 fifo[i++] = PS3GPU_MTH_HDR(2, 0, PS3GPU_MTH_ADDR_SCISSOR);
358 fifo[i++] = (w << 16) | x;
359 fifo[i++] = (h << 16) | y;
361 return (i);
365 set_front_poly_mode(uint32_t *fifo, uint32_t mode)
367 int i = 0;
369 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1828);
370 fifo[i++] = mode;
372 return (i);
376 set_shade_mode(uint32_t *fifo, uint32_t mode)
378 int i = 0;
380 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x368);
381 fifo[i++] = mode;
383 return (i);
387 blend_enable(uint32_t *fifo, uint32_t enable)
389 int i = 0;
391 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x310);
392 fifo[i++] = enable;
394 return (i);
398 clear_surface(uint32_t *fifo, uint32_t mask)
400 int i = 0;
402 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d94);
403 fifo[i++] = mask;
405 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x100);
406 fifo[i++] = 0x00000000;
408 return (i);
412 set_surface(uint32_t *fifo, const struct surface_desc *sd)
414 int i = 0;
416 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x194);
417 fifo[i++] = sd->sd_color_loc[0];
419 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x18c);
420 fifo[i++] = sd->sd_color_loc[1];
422 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1b4);
423 fifo[i++] = sd->sd_color_loc[2];
424 fifo[i++] = sd->sd_color_loc[3];
426 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x198);
427 fifo[i++] = sd->sd_depth_loc;
429 fifo[i++] = PS3GPU_MTH_HDR(6, 0, 0x208);
430 fifo[i++] = ((uint8_t) log2(sd->sd_h) << 24) | ((uint8_t) log2(sd->sd_w) << 16) |
431 (0x0 << 12) | (0x1 << 8) |
432 (sd->sd_depth_fmt << 5) | sd->sd_color_fmt;
433 fifo[i++] = sd->sd_color_pitch[0];
434 fifo[i++] = sd->sd_color_off[0];
435 fifo[i++] = sd->sd_depth_off;
436 fifo[i++] = sd->sd_color_off[1];
437 fifo[i++] = sd->sd_color_pitch[1];
439 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x22c);
440 fifo[i++] = sd->sd_depth_pitch;
442 fifo[i++] = PS3GPU_MTH_HDR(4, 0, 0x280);
443 fifo[i++] = sd->sd_color_pitch[2];
444 fifo[i++] = sd->sd_color_pitch[3];
445 fifo[i++] = sd->sd_color_off[2];
446 fifo[i++] = sd->sd_color_off[3];
448 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x220);
449 fifo[i++] = sd->sd_color_target;
451 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x2b8);
452 fifo[i++] = (sd->sd_y << 16) | sd->sd_x;
454 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x200);
455 fifo[i++] = (sd->sd_w << 16) | sd->sd_x;
456 fifo[i++] = (sd->sd_h << 16) | sd->sd_y;
458 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d88);
459 fifo[i++] = (0x0 << 16) | (0x1 << 12) |
460 (sd->sd_h - ((sd->sd_h >> 12) & 0x1));
462 return (i);
466 wait_label(uint32_t *fifo, uint32_t index, uint32_t val)
468 int i = 0;
470 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
471 fifo[i++] = index << 4;
473 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x68);
474 fifo[i++] = val;
476 return (i);
480 write_label(uint32_t *fifo, uint32_t index, uint32_t val)
482 int i = 0;
484 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x64);
485 fifo[i++] = index << 4;
487 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x6c);
488 fifo[i++] = val;
490 return (i);
494 write_backend_label(uint32_t *fifo, uint32_t index, uint32_t val)
496 int i = 0;
498 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d6c);
499 fifo[i++] = index << 4;
501 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d70);
502 fifo[i++] = (val & 0xff00ff00) | ((val & 0xff) << 16) | ((val >> 16) & 0xff);
504 return (i);
508 write_texture_label(uint32_t *fifo, uint32_t index, uint32_t val)
510 int i = 0;
512 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d6c);
513 fifo[i++] = index << 4;
515 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d74);
516 fifo[i++] = val;
518 return (i);
522 set_viewport(uint32_t *fifo, uint16_t x, uint16_t y, uint16_t w, uint16_t h,
523 float zmin, float zmax, const float offset[4], const float scale[4])
525 int i = 0;
527 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0xa00);
528 fifo[i++] = (w << 16) | x;
529 fifo[i++] = (h << 16) | y;
531 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x394);
532 fifo[i++] = *(uint32_t *) &zmin;
533 fifo[i++] = *(uint32_t *) &zmax;
535 fifo[i++] = PS3GPU_MTH_HDR(8, 0, 0xa20);
536 fifo[i++] = *(uint32_t *) &offset[0];
537 fifo[i++] = *(uint32_t *) &offset[1];
538 fifo[i++] = *(uint32_t *) &offset[2];
539 fifo[i++] = *(uint32_t *) &offset[3];
540 fifo[i++] = *(uint32_t *) &scale[0];
541 fifo[i++] = *(uint32_t *) &scale[1];
542 fifo[i++] = *(uint32_t *) &scale[2];
543 fifo[i++] = *(uint32_t *) &scale[3];
545 fifo[i++] = PS3GPU_MTH_HDR(8, 0, 0xa20);
546 fifo[i++] = *(uint32_t *) &offset[0];
547 fifo[i++] = *(uint32_t *) &offset[1];
548 fifo[i++] = *(uint32_t *) &offset[2];
549 fifo[i++] = *(uint32_t *) &offset[3];
550 fifo[i++] = *(uint32_t *) &scale[0];
551 fifo[i++] = *(uint32_t *) &scale[1];
552 fifo[i++] = *(uint32_t *) &scale[2];
553 fifo[i++] = *(uint32_t *) &scale[3];
555 return (i);
559 load_vertex_prg(uint32_t *fifo, uint16_t slot, const uint32_t *prg, int instr_count)
561 int i = 0;
562 int j;
564 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1e9c);
565 fifo[i++] = slot;
567 while (instr_count >= 32) {
568 fifo[i++] = PS3GPU_MTH_HDR(32, 0, 0xb80);
570 for (j = 0; j < 32; j++) {
571 fifo[i++] = *prg++;
572 fifo[i++] = *prg++;
573 fifo[i++] = *prg++;
574 fifo[i++] = *prg++;
577 instr_count -= 32;
580 if (instr_count > 0) {
581 fifo[i++] = PS3GPU_MTH_HDR(instr_count << 2, 0, 0xb80);
583 for (j = 0; j < instr_count; j++) {
584 fifo[i++] = *prg++;
585 fifo[i++] = *prg++;
586 fifo[i++] = *prg++;
587 fifo[i++] = *prg++;
591 return (i);
595 set_vertex_prg_reg_count(uint32_t *fifo, unsigned int count)
597 int i = 0;
598 uint32_t val;
600 val = (count <= 32) ? 0x0020ffff : 0x0030ffff;
602 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ef8);
603 fifo[i++] = val;
605 return (i);
609 set_vertex_prg_start_slot(uint32_t *fifo, uint16_t slot)
611 int i = 0;
613 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ea0);
614 fifo[i++] = slot;
616 return (i);
620 set_vertex_prg_const(uint32_t *fifo, uint32_t start, uint32_t count,
621 const float *val)
623 int i = 0;
624 int j;
626 while (count >= 32) {
627 fifo[i++] = PS3GPU_MTH_HDR(33, 0, 0x1efc);
628 fifo[i++] = start;
630 for (j = 0; j < 32; j++)
631 fifo[i++] = *(uint32_t *) val++;
633 count -= 32;
634 start += 8;
637 if (count > 0) {
638 fifo[i++] = PS3GPU_MTH_HDR(count + 1, 0, 0x1efc);
639 fifo[i++] = start;
641 for (j = 0; j < count; j++)
642 fifo[i++] = *(uint32_t *) val++;
645 return (i);
649 set_vertex_attr_inmask(uint32_t *fifo, uint32_t mask)
651 int i = 0;
653 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ff0);
654 fifo[i++] = mask;
656 return (i);
660 set_vertex_attr_outmask(uint32_t *fifo, uint32_t mask)
662 int i = 0;
664 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1ff4);
665 fifo[i++] = mask;
667 return (i);
671 set_frag_prg(uint32_t *fifo, uint8_t location, uint32_t offset)
673 int i = 0;
675 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x8e4);
676 fifo[i++] = offset | location;
678 return (i);
682 frag_prg_ctrl(uint32_t *fifo, uint8_t reg_count, uint8_t replace_txp_with_tex,
683 uint8_t pixel_kill, uint8_t output_from_h0, uint8_t depth_replace)
685 int i = 0;
687 if (reg_count <= 1)
688 reg_count = 2;
690 if (depth_replace)
691 depth_replace = 0x7;
693 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1d60);
694 fifo[i++] = (reg_count << 24) | (replace_txp_with_tex << 15) | 0x00000400 |
695 (pixel_kill << 7) | (!output_from_h0 << 6) | (depth_replace << 1);
697 return (i);
701 draw_begin(uint32_t *fifo, uint32_t mode)
703 int i = 0;
705 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(3, 0, 0x1714);
706 fifo[i++] = 0x00000000;
707 fifo[i++] = 0x00000000;
708 fifo[i++] = 0x00000000;
710 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
711 fifo[i++] = mode;
713 return (i);
717 draw_end(uint32_t *fifo)
719 int i = 0;
721 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
722 fifo[i++] = 0x00000000;
724 return (i);
728 set_vertex_data_arrfmt(uint32_t *fifo, uint8_t reg, uint16_t freq,
729 uint8_t stride, uint8_t size, uint8_t type)
731 int i = 0;
733 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1740 + (reg << 4));
734 fifo[i++] = (freq << 16) | (stride << 8) | (size << 4) | type;
736 return (i);
740 set_vertex_data_2f(uint32_t *fifo, uint8_t reg, const float val[2])
742 int i = 0;
744 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1880 + (reg << 3));
745 fifo[i++] = *(uint32_t *) &val[0];
746 fifo[i++] = *(uint32_t *) &val[1];
748 return (i);
752 set_vertex_data_4f(uint32_t *fifo, uint8_t reg, const float val[4])
754 int i = 0;
756 fifo[i++] = PS3GPU_MTH_HDR(4, 0, 0x1c00 + (reg << 4));
757 fifo[i++] = *(uint32_t *) &val[0];
758 fifo[i++] = *(uint32_t *) &val[1];
759 fifo[i++] = *(uint32_t *) &val[2];
760 fifo[i++] = *(uint32_t *) &val[3];
762 return (i);
766 set_vertex_data_arr(uint32_t *fifo, uint8_t reg,
767 uint16_t freq, uint8_t stride, uint8_t size, uint8_t type,
768 uint8_t location, uint32_t offset)
770 int i = 0;
772 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1740 + (reg << 2));
773 fifo[i++] = (freq << 16) | (stride << 8) | (size << 4) | type;
775 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1680 + (reg << 2));
776 fifo[i++] = (location << 31) | offset;
778 return (i);
782 draw_arrays(uint32_t *fifo, uint32_t mode, uint32_t first, uint32_t count)
784 int i = 0;
785 int j;
787 /* draw begin */
789 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(3, 0, 0x1714);
790 fifo[i++] = 0x00000000;
791 fifo[i++] = 0x00000000;
792 fifo[i++] = 0x00000000;
794 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
795 fifo[i++] = mode;
797 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1814);
798 fifo[i++] = (((count - 1) & 0xff) << 24) | first;
800 first += ((count - 1) & 0xff) + 1;
801 count -= ((count - 1) & 0xff) + 1;
803 while (count >= (0x7ff * 0x100)) {
804 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(0x7ff, 0, 0x1814);
806 for (j = 0; j < 0x7ff; j++) {
807 fifo[i++] = ((0x100 - 1) << 24) | first;
808 first += 0x100;
811 count -= 0x7ff * 0x100;
814 if (count) {
815 fifo[i++] = 0x40000000 | PS3GPU_MTH_HDR(count, 0, 0x1814);
817 for (j = 0; j < count; j++) {
818 fifo[i++] = ((0x100 - 1) << 24) | first;
819 first += 0x100;
823 /* draw end */
825 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1808);
826 fifo[i++] = 0x00000000;
828 return (i);
832 invalidate_vertex_cache(uint32_t *fifo)
834 int i = 0;
836 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1710);
837 fifo[i++] = 0x00000000;
839 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
840 fifo[i++] = 0x00000000;
842 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
843 fifo[i++] = 0x00000000;
845 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1714);
846 fifo[i++] = 0x00000000;
848 return (i);
852 set_texture(uint32_t *fifo, uint8_t index, const struct texture_desc *td)
854 int i = 0;
856 fifo[i++] = PS3GPU_MTH_HDR(2, 0, 0x1a00 + (index << 5));
857 fifo[i++] = td->td_off;
858 fifo[i++] = (td->td_mipmap << 16) | (td->td_fmt << 8) |
859 (td->td_dimension << 4) | (td->td_border << 3) |
860 (td->td_cubemap << 2) | td->td_loc;
862 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a18 + (index << 5));
863 fifo[i++] = (td->td_w << 16) | td->td_h;
865 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1840 + (index << 2));
866 fifo[i++] = (td->td_depth << 20) | td->td_pitch;
868 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a10 + (index << 5));
869 fifo[i++] = td->td_remap;
871 return (i);
875 texture_ctrl(uint32_t *fifo, uint8_t index, uint8_t enable,
876 uint16_t min_lod, uint16_t max_lod, uint8_t max_aniso)
878 int i = 0;
880 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a0c + (index << 5));
881 fifo[i++] = (enable << 31) | ((min_lod & 0xfff) << 19) |
882 ((max_lod & 0xfff) << 7) | (max_aniso << 4);
884 return (i);
888 set_texture_addr(uint32_t *fifo, uint8_t index,
889 uint8_t wrap_s, uint8_t wrap_t, uint8_t wrap_r, uint8_t unsigned_remap,
890 uint8_t zfunc, uint8_t gamma)
892 int i = 0;
894 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a08 + (index << 5));
895 fifo[i++] = (zfunc << 28) | (gamma << 20) | (wrap_r << 16) |
896 (unsigned_remap << 12) | (wrap_t << 8) | wrap_s;
898 return (i);
902 set_texture_filter(uint32_t *fifo, uint8_t index,
903 uint16_t bias, uint8_t min, uint8_t mag, uint8_t conv)
905 int i = 0;
907 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1a14 + (index << 5));
908 fifo[i++] = (mag << 24) | (min << 16) | (conv << 13) | (bias & 0x1fff);
910 return (i);
914 invalidate_texture_cache(uint32_t *fifo, uint32_t val)
916 int i = 0;
918 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1fd8);
919 fifo[i++] = val;
921 return (i);
925 set_timestamp(uint32_t *fifo, uint32_t index)
927 int i = 0;
929 fifo[i++] = PS3GPU_MTH_HDR(1, 0, 0x1800);
930 fifo[i++] = 0x01000000 | index << 4;
932 return (i);