1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
5 #include "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_shader_tokens.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
11 #include <stdio.h> /* for fread(), etc */
13 #include "util/u_inlines.h"
14 #include "util/u_memory.h" /* Offset() */
15 #include "util/u_draw_quad.h"
16 #include "util/u_box.h"
18 static const char *filename
= NULL
;
19 unsigned show_fps
= 0;
20 unsigned draw_strip
= 0;
23 static void usage(char *name
)
25 fprintf(stderr
, "usage: %s [ options ] shader_filename\n", name
);
27 fprintf(stderr
, "\n" );
28 fprintf(stderr
, "options:\n");
29 fprintf(stderr
, " -fps show frames per second\n");
30 fprintf(stderr
, " -strip renders a triangle strip\n");
35 enum pipe_format formats
[] = {
36 PIPE_FORMAT_R8G8B8A8_UNORM
,
37 PIPE_FORMAT_B8G8R8A8_UNORM
,
41 static const int WIDTH
= 250;
42 static const int HEIGHT
= 250;
44 static struct pipe_screen
*screen
= NULL
;
45 static struct pipe_context
*ctx
= NULL
;
46 static struct pipe_resource
*rttex
= NULL
;
47 static struct pipe_resource
*constbuf1
= NULL
;
48 static struct pipe_resource
*constbuf2
= NULL
;
49 static struct pipe_surface
*surf
= NULL
;
50 static struct pipe_sampler_view
*sv
= NULL
;
51 static void *sampler
= NULL
;
52 static void *window
= NULL
;
53 static struct pipe_resource
*samptex
= NULL
;
62 /* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
63 * so that the final images are the same.
65 static struct vertex vertices
[] =
67 { { 0.9, 0.9, 0.0, 1.0 },
73 { { 0.9, -0.9, 0.0, 1.0 },
79 { {-0.9, 0.0, 0.0, 1.0 },
86 static struct vertex vertices_strip
[] =
88 { { 0.9, 0.9, 0.0, 1.0 },
94 { { 0.9, -0.9, 0.0, 1.0 },
100 { {-0.9, 0.9, 0.0, 1.0 },
106 { {-0.9, -0.9, 0.0, 1.0 },
113 static float constants1
[] =
131 static float constants2
[] =
149 static void init_fs_constbuf( void )
151 struct pipe_resource templat
;
153 memset(&templat
, 0, sizeof(templat
));
154 templat
.target
= PIPE_BUFFER
;
155 templat
.format
= PIPE_FORMAT_R8_UNORM
;
156 templat
.width0
= sizeof(constants1
);
159 templat
.array_size
= 1;
160 templat
.last_level
= 0;
161 templat
.nr_samples
= 1;
162 templat
.bind
= PIPE_BIND_CONSTANT_BUFFER
;
164 constbuf1
= screen
->resource_create(screen
, &templat
);
165 if (constbuf1
== NULL
)
167 constbuf2
= screen
->resource_create(screen
, &templat
);
168 if (constbuf2
== NULL
)
172 ctx
->buffer_subdata(ctx
, constbuf1
,
174 0, sizeof(constants1
), constants1
);
176 pipe_set_constant_buffer(ctx
,
177 PIPE_SHADER_GEOMETRY
, 0,
181 ctx
->buffer_subdata(ctx
, constbuf2
,
183 0, sizeof(constants2
), constants2
);
185 pipe_set_constant_buffer(ctx
,
186 PIPE_SHADER_GEOMETRY
, 1,
192 static void set_viewport( float x
, float y
,
193 float width
, float height
,
194 float zNear
, float zFar
)
197 float half_width
= (float)width
/ 2.0f
;
198 float half_height
= (float)height
/ 2.0f
;
199 float half_depth
= ((float)zFar
- (float)zNear
) / 2.0f
;
200 struct pipe_viewport_state vp
;
202 vp
.scale
[0] = half_width
;
203 vp
.scale
[1] = half_height
;
204 vp
.scale
[2] = half_depth
;
206 vp
.translate
[0] = half_width
+ x
;
207 vp
.translate
[1] = half_height
+ y
;
208 vp
.translate
[2] = half_depth
+ z
;
210 ctx
->set_viewport_states( ctx
, 0, 1, &vp
);
213 static void set_vertices( void )
215 struct pipe_vertex_element ve
[4];
216 struct pipe_vertex_buffer vbuf
;
219 memset(ve
, 0, sizeof ve
);
221 ve
[0].src_offset
= Offset(struct vertex
, position
);
222 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
223 ve
[1].src_offset
= Offset(struct vertex
, color
);
224 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
225 ve
[2].src_offset
= Offset(struct vertex
, texcoord
);
226 ve
[2].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
227 ve
[3].src_offset
= Offset(struct vertex
, generic
);
228 ve
[3].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
230 handle
= ctx
->create_vertex_elements_state(ctx
, 4, ve
);
231 ctx
->bind_vertex_elements_state(ctx
, handle
);
233 memset(&vbuf
, 0, sizeof vbuf
);
235 vbuf
.stride
= sizeof( struct vertex
);
236 vbuf
.buffer_offset
= 0;
238 vbuf
.buffer
.resource
= pipe_buffer_create_with_data(ctx
,
239 PIPE_BIND_VERTEX_BUFFER
,
241 sizeof(vertices_strip
),
244 vbuf
.buffer
.resource
= pipe_buffer_create_with_data(ctx
,
245 PIPE_BIND_VERTEX_BUFFER
,
251 ctx
->set_vertex_buffers(ctx
, 0, 1, &vbuf
);
254 static void set_vertex_shader( void )
263 "DCL OUT[0], POSITION\n"
264 "DCL OUT[1], COLOR[0]\n"
265 "DCL OUT[2], GENERIC[0]\n"
266 "DCL OUT[3], GENERIC[1]\n"
267 " MOV OUT[0], IN[0]\n"
268 " MOV OUT[1], IN[1]\n"
269 " MOV OUT[2], IN[2]\n"
270 " MOV OUT[3], IN[3]\n"
273 handle
= graw_parse_vertex_shader(ctx
, text
);
274 ctx
->bind_vs_state(ctx
, handle
);
277 static void set_fragment_shader( void )
282 "DCL IN[0], COLOR, LINEAR\n"
283 "DCL OUT[0], COLOR\n"
284 " 0: MOV OUT[0], IN[0]\n"
287 handle
= graw_parse_fragment_shader(ctx
, text
);
288 ctx
->bind_fs_state(ctx
, handle
);
292 static void set_geometry_shader( void )
299 if ((f
= fopen(filename
, "r")) == NULL
) {
300 fprintf(stderr
, "Couldn't open %s\n", filename
);
304 sz
= fread(buf
, 1, sizeof(buf
), f
);
306 printf("file too long\n");
309 printf("%.*s\n", sz
, buf
);
312 handle
= graw_parse_geometry_shader(ctx
, buf
);
313 ctx
->bind_gs_state(ctx
, handle
);
318 static void draw( void )
320 union pipe_color_union clear_color
= { {.1,.3,.5,0} };
322 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, &clear_color
, 0, 0);
324 util_draw_arrays(ctx
, PIPE_PRIM_TRIANGLE_STRIP
, 0, 4);
326 util_draw_arrays(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3);
328 ctx
->flush(ctx
, NULL
, 0);
330 graw_save_surface_to_file(ctx
, surf
, NULL
);
332 screen
->flush_frontbuffer(screen
, rttex
, 0, 0, window
, NULL
);
337 static void init_tex( void )
339 struct pipe_sampler_view sv_template
;
340 struct pipe_sampler_state sampler_desc
;
341 struct pipe_resource templat
;
343 ubyte tex2d
[SIZE
][SIZE
][4];
347 for (s
= 0; s
< SIZE
; s
++) {
348 for (t
= 0; t
< SIZE
; t
++) {
351 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
352 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
354 tex2d
[t
][s
][3] = 0xff;
357 int x
= ((s
^ t
) >> 2) & 1;
358 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
359 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
360 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
361 tex2d
[t
][s
][3] = 0xff;
367 tex2d
[0][0][1] = 255;
368 tex2d
[0][0][2] = 255;
373 tex2d
[0][1][2] = 255;
374 tex2d
[0][1][3] = 255;
376 tex2d
[1][0][0] = 255;
377 tex2d
[1][0][1] = 255;
379 tex2d
[1][0][3] = 255;
381 tex2d
[1][1][0] = 255;
384 tex2d
[1][1][3] = 255;
387 memset(&templat
, 0, sizeof(templat
));
388 templat
.target
= PIPE_TEXTURE_2D
;
389 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
390 templat
.width0
= SIZE
;
391 templat
.height0
= SIZE
;
393 templat
.array_size
= 1;
394 templat
.last_level
= 0;
395 templat
.nr_samples
= 1;
396 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
399 samptex
= screen
->resource_create(screen
,
404 u_box_2d(0,0,SIZE
,SIZE
, &box
);
406 ctx
->texture_subdata(ctx
,
415 /* Possibly read back & compare against original data:
419 struct pipe_transfer
*t
;
421 ptr
= pipe_transfer_map(ctx
, samptex
,
422 0, 0, /* level, layer */
424 0, 0, SIZE
, SIZE
, &t
); /* x, y, width, height */
426 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
431 ctx
->transfer_unmap(ctx
, t
);
434 memset(&sv_template
, 0, sizeof sv_template
);
435 sv_template
.format
= samptex
->format
;
436 sv_template
.texture
= samptex
;
437 sv_template
.swizzle_r
= 0;
438 sv_template
.swizzle_g
= 1;
439 sv_template
.swizzle_b
= 2;
440 sv_template
.swizzle_a
= 3;
441 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
445 ctx
->set_sampler_views(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sv
);
448 memset(&sampler_desc
, 0, sizeof sampler_desc
);
449 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
450 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
451 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
452 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
453 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
454 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
455 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
456 sampler_desc
.compare_func
= 0;
457 sampler_desc
.normalized_coords
= 1;
458 sampler_desc
.max_anisotropy
= 0;
460 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
464 ctx
->bind_sampler_states(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sampler
);
468 static void init( void )
470 struct pipe_framebuffer_state fb
;
471 struct pipe_resource templat
;
472 struct pipe_surface surf_tmpl
;
475 /* It's hard to say whether window or screen should be created
476 * first. Different environments would prefer one or the other.
478 * Also, no easy way of querying supported formats if the screen
479 * cannot be created first.
481 for (i
= 0; formats
[i
] != PIPE_FORMAT_NONE
; i
++) {
482 screen
= graw_create_window_and_screen(0, 0, 300, 300,
485 if (window
&& screen
)
488 if (!screen
|| !window
) {
489 fprintf(stderr
, "Unable to create window\n");
493 ctx
= screen
->context_create(screen
, NULL
, 0);
497 memset(&templat
, 0, sizeof(templat
));
498 templat
.target
= PIPE_TEXTURE_2D
;
499 templat
.format
= formats
[i
];
500 templat
.width0
= WIDTH
;
501 templat
.height0
= HEIGHT
;
503 templat
.array_size
= 1;
504 templat
.last_level
= 0;
505 templat
.nr_samples
= 1;
506 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
507 PIPE_BIND_DISPLAY_TARGET
);
509 rttex
= screen
->resource_create(screen
,
514 surf_tmpl
.format
= templat
.format
;
515 surf_tmpl
.u
.tex
.level
= 0;
516 surf_tmpl
.u
.tex
.first_layer
= 0;
517 surf_tmpl
.u
.tex
.last_layer
= 0;
518 surf
= ctx
->create_surface(ctx
, rttex
, &surf_tmpl
);
522 memset(&fb
, 0, sizeof fb
);
528 ctx
->set_framebuffer_state(ctx
, &fb
);
531 struct pipe_blend_state blend
;
533 memset(&blend
, 0, sizeof blend
);
534 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
535 handle
= ctx
->create_blend_state(ctx
, &blend
);
536 ctx
->bind_blend_state(ctx
, handle
);
540 struct pipe_depth_stencil_alpha_state depthstencil
;
542 memset(&depthstencil
, 0, sizeof depthstencil
);
543 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
544 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
548 struct pipe_rasterizer_state rasterizer
;
550 memset(&rasterizer
, 0, sizeof rasterizer
);
551 rasterizer
.cull_face
= PIPE_FACE_NONE
;
552 rasterizer
.half_pixel_center
= 1;
553 rasterizer
.bottom_edge_rule
= 1;
554 rasterizer
.depth_clip
= 1;
555 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
556 ctx
->bind_rasterizer_state(ctx
, handle
);
559 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
566 set_fragment_shader();
567 set_geometry_shader();
570 static void args(int argc
, char *argv
[])
574 for (i
= 1; i
< argc
;) {
575 if (graw_parse_args(&i
, argc
, argv
)) {
578 if (strcmp(argv
[i
], "-fps") == 0) {
582 else if (strcmp(argv
[i
], "-strip") == 0) {
586 else if (i
== argc
- 1) {
602 int main( int argc
, char *argv
[] )
607 graw_set_display_func( draw
);