2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
40 #include <sys/consio.h>
44 #include "ps3gpu_ctl.h"
45 #include "ps3gpu_mth.h"
46 #include "reset_gpu_state.h"
50 main(int argc
, char **argv
)
52 struct ps3gpu_ctl_context_allocate context_allocate
;
53 struct ps3gpu_ctl_context_free context_free
;
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
;
65 fd
= open(PS3GPU_DEV_PATH
, O_RDWR
);
71 /* Create GPU context */
73 context_allocate
.vram_size
= 64; /* MB */
75 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_ALLOCATE
, &context_allocate
);
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
) {
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
) {
107 printf("channel id %d\n", get_channel_id(driver_info
));
111 err
= memory_allocate(fd
, context_id
, PS3GPU_CTL_MEMORY_TYPE_GART
,
112 64 * 1024, 12, &fifo_handle
, &fifo_gaddr
, (void **) &fifo
);
114 perror("memory_allocate");
118 printf("FIFO handle 0x%lx gpu addr 0x%08x\n",
119 fifo_handle
, fifo_gaddr
);
123 err
= setup_control(fd
, context_id
, fifo_handle
, fifo_handle
, 0xdeadbabe);
125 perror("setup_control");
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
);
137 perror("memory_allocate");
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
);
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
);
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);
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]);
171 perror("memory_allocate");
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]);
182 perror("memory_allocate");
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
);
195 perror("memory_allocate");
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
);
207 perror("memory_allocate");
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]);
219 perror("display_buffer_set");
223 err
= display_buffer_set(fd
, context_id
, 1, DISPLAY_WIDTH
, DISPLAY_HEIGHT
,
224 DISPLAY_PITCH
, db_handle
[1]);
226 perror("display_buffer_set");
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 },
237 .sd_color_target
= 0x1,
238 .sd_depth_loc
= 0xfeed0000,
239 .sd_depth_off
= zb_gaddr
,
240 .sd_depth_pitch
= DISPLAY_PITCH
,
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 },
253 .sd_color_target
= 0x1,
254 .sd_depth_loc
= 0xfeed0000,
255 .sd_depth_off
= zb_gaddr
,
256 .sd_depth_pitch
= DISPLAY_PITCH
,
260 .sd_w
= DISPLAY_WIDTH
,
261 .sd_h
= DISPLAY_HEIGHT
,
265 const uint32_t clear_color
[] = {
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
[] = {
275 0x401f9c6c, 0x0040000d, 0x8106c083, 0x6041ff80,
277 0x401f9c6c, 0x0040030d, 0x8106c083, 0x6041ff85,
280 uint32_t frag_prg
[] = {
282 0x3e010100, 0xc8011c9d, 0xc8000001, 0xc8003fe1,
291 * (-0.5, 0.5) /________\ (0.5, 0.5)
295 const float triangle_pos
[][4] = {
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] = {
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);
310 perror("setup_control");
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
);
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);
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);
338 perror("setup_control");
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
);
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);
405 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
406 control
[0x10], control
[0x11], control
[0x12]);
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
);
428 /* Restore console */
430 ioctl(0, SW_TEXT_80x25
, NULL
);