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;
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 u_box_2d(0,0,sizeof(constants1),1, &box);
174 ctx->transfer_inline_write(ctx,
184 pipe_set_constant_buffer(ctx,
185 PIPE_SHADER_GEOMETRY, 0,
189 u_box_2d(0,0,sizeof(constants2),1, &box);
191 ctx->transfer_inline_write(ctx,
201 pipe_set_constant_buffer(ctx,
202 PIPE_SHADER_GEOMETRY, 1,
208 static void set_viewport( float x, float y,
209 float width, float height,
210 float near, float far)
213 float half_width = (float)width / 2.0f;
214 float half_height = (float)height / 2.0f;
215 float half_depth = ((float)far - (float)near) / 2.0f;
216 struct pipe_viewport_state vp;
218 vp.scale[0] = half_width;
219 vp.scale[1] = half_height;
220 vp.scale[2] = half_depth;
223 vp.translate[0] = half_width + x;
224 vp.translate[1] = half_height + y;
225 vp.translate[2] = half_depth + z;
226 vp.translate[3] = 0.0f;
228 ctx->set_viewport_state( ctx, &vp );
231 static void set_vertices( void )
233 struct pipe_vertex_element ve[4];
234 struct pipe_vertex_buffer vbuf;
237 memset(ve, 0, sizeof ve);
239 ve[0].src_offset = Offset(struct vertex, position);
240 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
241 ve[1].src_offset = Offset(struct vertex, color);
242 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
243 ve[2].src_offset = Offset(struct vertex, texcoord);
244 ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
245 ve[3].src_offset = Offset(struct vertex, generic);
246 ve[3].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
248 handle = ctx->create_vertex_elements_state(ctx, 4, ve);
249 ctx->bind_vertex_elements_state(ctx, handle);
251 vbuf.stride = sizeof( struct vertex );
252 vbuf.buffer_offset = 0;
254 vbuf.buffer = pipe_buffer_create_with_data(ctx,
255 PIPE_BIND_VERTEX_BUFFER,
257 sizeof(vertices_strip),
260 vbuf.buffer = pipe_buffer_create_with_data(ctx,
261 PIPE_BIND_VERTEX_BUFFER,
267 ctx->set_vertex_buffers(ctx, 1, &vbuf);
270 static void set_vertex_shader( void )
279 "DCL OUT[0], POSITION\n"
280 "DCL OUT[1], COLOR[0]\n"
281 "DCL OUT[2], GENERIC[0]\n"
282 "DCL OUT[3], GENERIC[1]\n"
283 " MOV OUT[0], IN[0]\n"
284 " MOV OUT[1], IN[1]\n"
285 " MOV OUT[2], IN[2]\n"
286 " MOV OUT[3], IN[3]\n"
289 handle = graw_parse_vertex_shader(ctx, text);
290 ctx->bind_vs_state(ctx, handle);
293 static void set_fragment_shader( void )
298 "DCL IN[0], COLOR, LINEAR\n"
299 "DCL OUT[0], COLOR\n"
300 " 0: MOV OUT[0], IN[0]\n"
303 handle = graw_parse_fragment_shader(ctx, text);
304 ctx->bind_fs_state(ctx, handle);
308 static void set_geometry_shader( void )
315 if ((f = fopen(filename, "r")) == NULL) {
316 fprintf(stderr, "Couldn't open %s\n", filename);
320 sz = fread(buf, 1, sizeof(buf), f);
322 printf("file too long\n");
325 printf("%.*s\n", sz, buf);
328 handle = graw_parse_geometry_shader(ctx, buf);
329 ctx->bind_gs_state(ctx, handle);
334 static void draw( void )
336 union pipe_color_union clear_color = { {.1,.3,.5,0} };
338 ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
340 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLE_STRIP, 0, 4);
342 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
344 ctx->flush(ctx, NULL);
346 graw_save_surface_to_file(ctx, surf, NULL);
348 screen->flush_frontbuffer(screen, rttex, 0, 0, window);
353 static void init_tex( void )
355 struct pipe_sampler_view sv_template;
356 struct pipe_sampler_state sampler_desc;
357 struct pipe_resource templat;
359 ubyte tex2d[SIZE][SIZE][4];
363 for (s = 0; s < SIZE; s++) {
364 for (t = 0; t < SIZE; t++) {
367 tex2d[t][s][0] = (x) ? 0 : 63;
368 tex2d[t][s][1] = (x) ? 0 : 128;
370 tex2d[t][s][3] = 0xff;
373 int x = ((s ^ t) >> 2) & 1;
374 tex2d[t][s][0] = s*255/(SIZE-1);
375 tex2d[t][s][1] = t*255/(SIZE-1);
376 tex2d[t][s][2] = (x) ? 0 : 128;
377 tex2d[t][s][3] = 0xff;
383 tex2d[0][0][1] = 255;
384 tex2d[0][0][2] = 255;
389 tex2d[0][1][2] = 255;
390 tex2d[0][1][3] = 255;
392 tex2d[1][0][0] = 255;
393 tex2d[1][0][1] = 255;
395 tex2d[1][0][3] = 255;
397 tex2d[1][1][0] = 255;
400 tex2d[1][1][3] = 255;
403 templat.target = PIPE_TEXTURE_2D;
404 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
405 templat.width0 = SIZE;
406 templat.height0 = SIZE;
408 templat.array_size = 1;
409 templat.last_level = 0;
410 templat.nr_samples = 1;
411 templat.bind = PIPE_BIND_SAMPLER_VIEW;
414 samptex = screen->resource_create(screen,
419 u_box_2d(0,0,SIZE,SIZE, &box);
421 ctx->transfer_inline_write(ctx,
430 /* Possibly read back & compare against original data:
434 struct pipe_transfer *t;
436 t = pipe_get_transfer(ctx, samptex,
437 0, 0, /* level, layer */
439 0, 0, SIZE, SIZE); /* x, y, width, height */
441 ptr = ctx->transfer_map(ctx, t);
443 if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
448 ctx->transfer_unmap(ctx, t);
450 ctx->transfer_destroy(ctx, t);
453 memset(&sv_template, 0, sizeof sv_template);
454 sv_template.format = samptex->format;
455 sv_template.texture = samptex;
456 sv_template.swizzle_r = 0;
457 sv_template.swizzle_g = 1;
458 sv_template.swizzle_b = 2;
459 sv_template.swizzle_a = 3;
460 sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
464 ctx->set_fragment_sampler_views(ctx, 1, &sv);
467 memset(&sampler_desc, 0, sizeof sampler_desc);
468 sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
469 sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
470 sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
471 sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
472 sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
473 sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
474 sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
475 sampler_desc.compare_func = 0;
476 sampler_desc.normalized_coords = 1;
477 sampler_desc.max_anisotropy = 0;
479 sampler = ctx->create_sampler_state(ctx, &sampler_desc);
483 ctx->bind_fragment_sampler_states(ctx, 1, &sampler);
487 static void init( void )
489 struct pipe_framebuffer_state fb;
490 struct pipe_resource templat;
491 struct pipe_surface surf_tmpl;
494 /* It's hard to say whether window or screen should be created
495 * first. Different environments would prefer one or the other.
497 * Also, no easy way of querying supported formats if the screen
498 * cannot be created first.
500 for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
501 screen = graw_create_window_and_screen(0, 0, 300, 300,
504 if (window && screen)
507 if (!screen || !window) {
508 fprintf(stderr, "Unable to create window\n");
512 ctx = screen->context_create(screen, NULL);
516 templat.target = PIPE_TEXTURE_2D;
517 templat.format = formats[i];
518 templat.width0 = WIDTH;
519 templat.height0 = HEIGHT;
521 templat.array_size = 1;
522 templat.last_level = 0;
523 templat.nr_samples = 1;
524 templat.bind = (PIPE_BIND_RENDER_TARGET |
525 PIPE_BIND_DISPLAY_TARGET);
527 rttex = screen->resource_create(screen,
532 surf_tmpl.format = templat.format;
533 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
534 surf_tmpl.u.tex.level = 0;
535 surf_tmpl.u.tex.first_layer = 0;
536 surf_tmpl.u.tex.last_layer = 0;
537 surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
541 memset(&fb, 0, sizeof fb);
547 ctx->set_framebuffer_state(ctx, &fb);
550 struct pipe_blend_state blend;
552 memset(&blend, 0, sizeof blend);
553 blend.rt[0].colormask = PIPE_MASK_RGBA;
554 handle = ctx->create_blend_state(ctx, &blend);
555 ctx->bind_blend_state(ctx, handle);
559 struct pipe_depth_stencil_alpha_state depthstencil;
561 memset(&depthstencil, 0, sizeof depthstencil);
562 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
563 ctx->bind_depth_stencil_alpha_state(ctx, handle);
567 struct pipe_rasterizer_state rasterizer;
569 memset(&rasterizer, 0, sizeof rasterizer);
570 rasterizer.cull_face = PIPE_FACE_NONE;
571 rasterizer.gl_rasterization_rules = 1;
572 rasterizer.depth_clip = 1;
573 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
574 ctx->bind_rasterizer_state(ctx, handle);
577 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
584 set_fragment_shader();
585 set_geometry_shader();
588 static void args(int argc, char *argv[])
592 for (i = 1; i < argc;) {
593 if (graw_parse_args(&i, argc, argv)) {
596 if (strcmp(argv[i], "-fps") == 0) {
600 else if (strcmp(argv[i], "-strip") == 0) {
604 else if (i == argc - 1) {
620 int main( int argc, char *argv[] )
625 graw_set_display_func( draw );