change back to gcc
[ps3freebsd_ps3gpu_test.git] / triangle.c
bloba8c0b08a83474db5dca772c85ef5f8b0fea90af4
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>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/uio.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/fbio.h>
40 #include <sys/consio.h>
41 #include <fcntl.h>
42 #include <unistd.h>
44 #include "ps3gpu_ctl.h"
45 #include "ps3gpu_mth.h"
46 #include "reset_gpu_state.h"
47 #include "util.h"
49 int
50 main(int argc, char **argv)
52 struct ps3gpu_ctl_context_allocate context_allocate;
53 struct ps3gpu_ctl_context_free context_free;
54 int context_id;
55 volatile uint32_t *control;
56 volatile uint8_t *driver_info;
57 uint32_t *fifo, *reset_gpu, *db[2], *zb, *fp;
58 unsigned long fifo_handle, reset_gpu_handle, db_handle[2], zb_handle, fp_handle;
59 unsigned int fifo_gaddr, reset_gpu_gaddr, db_gaddr[2], zb_gaddr, fp_gaddr;
60 int fd = -1;
61 int i, err;
63 /* Open GPU device */
65 fd = open(PS3GPU_DEV_PATH, O_RDWR);
66 if (fd < 0) {
67 perror("open");
68 goto done;
71 /* Create GPU context */
73 context_allocate.vram_size = 64; /* MB */
75 err = ioctl(fd, PS3GPU_CTL_CONTEXT_ALLOCATE, &context_allocate);
76 if (err < 0) {
77 perror("ioctl");
78 goto done;
81 context_id = context_allocate.context_id;
83 printf("context id %d\n", context_id);
84 printf("control handle 0x%lx size %d\n",
85 context_allocate.control_handle, context_allocate.control_size);
86 printf("driver_info handle 0x%lx size %d\n",
87 context_allocate.driver_info_handle, context_allocate.driver_info_size);
89 /* Map control registers */
91 control = mmap(NULL, context_allocate.control_size,
92 PROT_READ | PROT_WRITE, MAP_SHARED, fd, context_allocate.control_handle);
93 if (control == (void *) MAP_FAILED) {
94 perror("mmap");
95 goto done;
98 /* Map driver info */
100 driver_info = mmap(NULL, context_allocate.driver_info_size,
101 PROT_READ | PROT_WRITE, MAP_SHARED, fd, context_allocate.driver_info_handle);
102 if (driver_info == (void *) MAP_FAILED) {
103 perror("mmap");
104 goto done;
107 printf("channel id %d\n", get_channel_id(driver_info));
109 /* Allocate FIFO */
111 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_GART,
112 64 * 1024, 12, &fifo_handle, &fifo_gaddr, (void **) &fifo);
113 if (err < 0) {
114 perror("memory_allocate");
115 goto done;
118 printf("FIFO handle 0x%lx gpu addr 0x%08x\n",
119 fifo_handle, fifo_gaddr);
121 /* Setup FIFO */
123 err = setup_control(fd, context_id, fifo_handle, fifo_handle, 0xdeadbabe);
124 if (err < 0) {
125 perror("setup_control");
126 goto done;
129 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
130 control[0x10], control[0x11], control[0x12]);
132 /* Allocate FIFO for resetting GPU state */
134 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_GART,
135 4 * 1024, 12, &reset_gpu_handle, &reset_gpu_gaddr, (void **)&reset_gpu);
136 if (err < 0) {
137 perror("memory_allocate");
138 goto done;
141 printf("reset GPU state handle 0x%lx gpu addr 0x%08x\n",
142 reset_gpu_handle, reset_gpu_gaddr);
144 memcpy(reset_gpu, reset_gpu_state_3d, reset_gpu_state_3d_size);
146 /* Kick FIFO */
148 fifo[0] = PS3GPU_MTH_HDR(0, 0, reset_gpu_gaddr | PS3GPU_MTH_ADDR_CALL);
149 fifo[1] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF);
150 fifo[2] = 0xcafef00d;
152 control[0x10] = fifo_gaddr + 3 * sizeof(uint32_t);
154 err = wait_fifo_idle(control);
155 if (err < 0) {
156 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
157 control[0x10], control[0x11], control[0x12]);
158 dump_fifo(stderr, fifo, 0x400);
159 goto done;
162 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
163 control[0x10], control[0x11], control[0x12]);
165 /* Allocate display buffers */
167 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_VIDEO,
168 ROUNDUP(DISPLAY_HEIGHT * DISPLAY_PITCH, 4 * 1024), 12,
169 &db_handle[0], &db_gaddr[0], (void **) &db[0]);
170 if (err < 0) {
171 perror("memory_allocate");
172 goto done;
175 printf("DB0 handle 0x%lx gpu addr 0x%08x\n",
176 db_handle[0], db_gaddr[0]);
178 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_VIDEO,
179 ROUNDUP(DISPLAY_HEIGHT * DISPLAY_PITCH, 4 * 1024), 12,
180 &db_handle[1], &db_gaddr[1], (void **) &db[1]);
181 if (err < 0) {
182 perror("memory_allocate");
183 goto done;
186 printf("DB1 handle 0x%lx gpu addr 0x%08x\n",
187 db_handle[1], db_gaddr[1]);
189 /* Allocate depth buffer */
191 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_VIDEO,
192 ROUNDUP(DISPLAY_HEIGHT * DISPLAY_PITCH, 4 * 1024), 12,
193 &zb_handle, &zb_gaddr, (void **) &zb);
194 if (err < 0) {
195 perror("memory_allocate");
196 goto done;
199 printf("ZB handle 0x%lx gpu addr 0x%08x\n",
200 zb_handle, zb_gaddr);
202 /* Allocate fragment program */
204 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_VIDEO,
205 4 * 1024, 12, &fp_handle, &fp_gaddr, (void **) &fp);
206 if (err < 0) {
207 perror("memory_allocate");
208 goto done;
211 printf("FP handle 0x%lx gpu addr 0x%08x\n",
212 fp_handle, fp_gaddr);
214 /* Set display buffers */
216 err = display_buffer_set(fd, context_id, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT,
217 DISPLAY_PITCH, db_handle[0]);
218 if (err < 0) {
219 perror("display_buffer_set");
220 goto done;
223 err = display_buffer_set(fd, context_id, 1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
224 DISPLAY_PITCH, db_handle[1]);
225 if (err < 0) {
226 perror("display_buffer_set");
227 goto done;
230 const struct surface_desc surf_desc[] = {
231 /* display buffer 0 */
233 .sd_color_loc = { 0xfeed0000, 0xfeed0000, 0xfeed0000, 0xfeed0000 },
234 .sd_color_off = { db_gaddr[0], 0, 0, 0 },
235 .sd_color_pitch = { DISPLAY_PITCH, 64, 64, 64 },
236 .sd_color_fmt = 0x8,
237 .sd_color_target = 0x1,
238 .sd_depth_loc = 0xfeed0000,
239 .sd_depth_off = zb_gaddr,
240 .sd_depth_pitch = DISPLAY_PITCH,
241 .sd_depth_fmt = 0x2,
242 .sd_x = 0,
243 .sd_y = 0,
244 .sd_w = DISPLAY_WIDTH,
245 .sd_h = DISPLAY_HEIGHT,
247 /* display buffer 1 */
249 .sd_color_loc = { 0xfeed0000, 0xfeed0000, 0xfeed0000, 0xfeed0000 },
250 .sd_color_off = { db_gaddr[1], 0, 0, 0 },
251 .sd_color_pitch = { DISPLAY_PITCH, 64, 64, 64 },
252 .sd_color_fmt = 0x8,
253 .sd_color_target = 0x1,
254 .sd_depth_loc = 0xfeed0000,
255 .sd_depth_off = zb_gaddr,
256 .sd_depth_pitch = DISPLAY_PITCH,
257 .sd_depth_fmt = 0x2,
258 .sd_x = 0,
259 .sd_y = 0,
260 .sd_w = DISPLAY_WIDTH,
261 .sd_h = DISPLAY_HEIGHT,
265 const uint32_t clear_color[] = {
266 0xff404040,
267 0xffffffff,
270 const float vp_offset[] = { DISPLAY_WIDTH * 0.5f, DISPLAY_HEIGHT * 0.5f, 0.5f, 0.0f };
271 const float vp_scale[] = { DISPLAY_WIDTH * 0.5f, DISPLAY_HEIGHT * 0.5f, 0.5f, 0.0f };
273 const uint32_t vertex_prg[] = {
274 /* MOV o[0], v[0] */
275 0x401f9c6c, 0x0040000d, 0x8106c083, 0x6041ff80,
276 /* MOV o[1], v[3] */
277 0x401f9c6c, 0x0040030d, 0x8106c083, 0x6041ff85,
280 uint32_t frag_prg[] = {
281 /* MOVR R0, f[1] */
282 0x3e010100, 0xc8011c9d, 0xc8000001, 0xc8003fe1,
286 * (0.0, -0.5)
287 * /\
288 * / \
289 * / \
290 * / \
291 * (-0.5, 0.5) /________\ (0.5, 0.5)
295 const float triangle_pos[][4] = {
296 /* xyzw */
297 { 0.0f, -0.5f, -1.0f, 1.0f },
298 { -0.5f, 0.5f, -1.0f, 1.0f },
299 { 0.5f, 0.5f, -1.0f, 1.0f },
301 const float triangle_color[][4] = {
302 /* rgba */
303 { 1.0f, 0.0f, 0.0f, 1.0f },
304 { 0.0f, 1.0f, 0.0f, 1.0f },
305 { 0.0f, 0.0f, 1.0f, 1.0f },
308 err = setup_control(fd, context_id, fifo_handle, fifo_handle, 0xdeadbabe);
309 if (err < 0) {
310 perror("setup_control");
311 goto done;
314 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
315 control[0x10], control[0x11], control[0x12]);
317 /* Transfer fragment program to VRAM */
319 err += transfer_inline(fifo + err, 0xfeed0000, fp_gaddr,
320 frag_prg, ARRAY_SIZE(frag_prg));
322 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
324 err = wait_fifo_idle(control);
325 if (err < 0) {
326 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
327 control[0x10], control[0x11], control[0x12]);
328 dump_fifo(stderr, fifo, 0x400);
329 goto done;
332 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
333 control[0x10], control[0x11], control[0x12]);
335 for (i = 0; i < ARRAY_SIZE(surf_desc); i++) {
336 err = setup_control(fd, context_id, fifo_handle, fifo_handle, 0xdeadbabe);
337 if (err < 0) {
338 perror("setup_control");
339 goto done;
342 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
343 control[0x10], control[0x11], control[0x12]);
345 err += set_surface(fifo + err, &surf_desc[i]);
346 err += set_depth_mask(fifo + err, 0x00000000);
347 err += set_color_mask(fifo + err, 0x01010101);
348 err += set_color_mask_mrt(fifo + err, 0x00000000);
349 err += set_clear_color(fifo + err, clear_color[i]);
350 err += set_scissor(fifo + err, 0, 0, 4095, 4095);
351 err += clear_surface(fifo + err, 0x000000f1);
353 err += set_viewport(fifo + err, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT,
354 0.0f, 1.0f, vp_offset, vp_scale);
356 /* Set vertex shader */
358 err += load_vertex_prg(fifo + err, 0, vertex_prg, ARRAY_SIZE(vertex_prg) / 4);
359 err += set_vertex_prg_start_slot(fifo + err, 0);
360 err += set_vertex_prg_reg_count(fifo + err, 1);
361 err += set_vertex_attr_inmask(fifo + err, (1 << 3) | (1 << 0));
362 err += set_vertex_attr_outmask(fifo + err, (1 << 2) | (1 << 0));
364 /* Set fragment shader */
366 err += set_frag_prg(fifo + err, 0x1, fp_gaddr);
367 err += frag_prg_ctrl(fifo + err, 2, 0, 0, 0, 0);
369 err += set_front_poly_mode(fifo + err, 0x1b02);
370 err += set_shade_mode(fifo + err, 0x1d01);
372 /* register 0 - position */
373 err += set_vertex_data_arrfmt(fifo + err, 0, 0, 0, 0, 2);
374 /* register 3 - color */
375 err += set_vertex_data_arrfmt(fifo + err, 3, 0, 0, 0, 2);
377 err += draw_begin(fifo + err, 0x5);
378 err += set_vertex_data_4f(fifo + err, 3, triangle_color[0]);
379 err += set_vertex_data_4f(fifo + err, 0, triangle_pos[0]);
380 err += set_vertex_data_4f(fifo + err, 3, triangle_color[1]);
381 err += set_vertex_data_4f(fifo + err, 0, triangle_pos[1]);
382 err += set_vertex_data_4f(fifo + err, 3, triangle_color[2]);
383 err += set_vertex_data_4f(fifo + err, 0, triangle_pos[2]);
384 err += draw_end(fifo + err);
386 err += flip_display_buffer(fifo + err, get_channel_id(driver_info), i, 0);
389 * Label with index 0 (head 0) is set by LV1 to 0x00000000 when flip is complete.
390 * Let GPU wait for it.
393 err += wait_label(fifo + err, 0, 0x00000000);
395 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
397 err = wait_fifo_idle(control);
398 if (err < 0) {
399 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
400 control[0x10], control[0x11], control[0x12]);
401 dump_fifo(stderr, fifo, 0x400);
402 goto done;
405 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
406 control[0x10], control[0x11], control[0x12]);
408 usleep(1000000);
411 save_image("image.argb", (const char *) db[0], DISPLAY_PITCH * DISPLAY_HEIGHT);
413 /* Destroy GPU context */
415 context_free.context_id = context_id;
417 err = ioctl(fd, PS3GPU_CTL_CONTEXT_FREE, &context_free);
418 if (err < 0) {
419 perror("ioctl");
420 goto done;
423 done:
425 if (fd >= 0)
426 close(fd);
428 /* Restore console */
430 ioctl(0, SW_TEXT_80x25, NULL);
432 exit(0);