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_debug.h" /* debug_dump_surface_bmp() */
14 #include "util/u_inlines.h"
15 #include "util/u_memory.h" /* Offset() */
16 #include "util/u_box.h"
18 static const char *filename = NULL;
19 unsigned show_fps = 0;
22 static void usage(char *name)
24 fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
26 fprintf(stderr, "\n" );
27 fprintf(stderr, "options:\n");
28 fprintf(stderr, " -fps show frames per second\n");
33 enum pipe_format formats[] = {
34 PIPE_FORMAT_R8G8B8A8_UNORM,
35 PIPE_FORMAT_B8G8R8A8_UNORM,
39 static const int WIDTH = 250;
40 static const int HEIGHT = 250;
42 static struct pipe_screen *screen = NULL;
43 static struct pipe_context *ctx = NULL;
44 static struct pipe_resource *rttex = NULL;
45 static struct pipe_resource *constbuf = NULL;
46 static struct pipe_surface *surf = NULL;
47 static struct pipe_sampler_view *sv = NULL;
48 static void *sampler = NULL;
49 static void *window = NULL;
50 static struct pipe_resource *samptex = NULL;
58 /* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
59 * so that the final images are the same.
61 static struct vertex vertices[] =
63 { { 0.9, 0.9, 0.0, 1.0 },
67 { { 0.9, -0.9, 0.0, 1.0 },
71 { {-0.9, 0.0, 0.0, 1.0 },
76 static float constants[] =
93 static void init_fs_constbuf( void )
95 struct pipe_resource templat;
98 templat.target = PIPE_BUFFER;
99 templat.format = PIPE_FORMAT_R8_UNORM;
100 templat.width0 = sizeof(constants);
103 templat.last_level = 0;
104 templat.nr_samples = 1;
105 templat.bind = PIPE_BIND_CONSTANT_BUFFER;
107 constbuf = screen->resource_create(screen,
109 if (constbuf == NULL)
113 u_box_2d(0,0,sizeof(constants),1, &box);
115 ctx->transfer_inline_write(ctx,
125 ctx->set_constant_buffer(ctx,
126 PIPE_SHADER_FRAGMENT, 0,
131 static void set_viewport( float x, float y,
132 float width, float height,
133 float near, float far)
136 float half_width = (float)width / 2.0f;
137 float half_height = (float)height / 2.0f;
138 float half_depth = ((float)far - (float)near) / 2.0f;
139 struct pipe_viewport_state vp;
141 vp.scale[0] = half_width;
142 vp.scale[1] = half_height;
143 vp.scale[2] = half_depth;
146 vp.translate[0] = half_width + x;
147 vp.translate[1] = half_height + y;
148 vp.translate[2] = half_depth + z;
149 vp.translate[3] = 0.0f;
151 ctx->set_viewport_state( ctx, &vp );
154 static void set_vertices( void )
156 struct pipe_vertex_element ve[3];
157 struct pipe_vertex_buffer vbuf;
160 memset(ve, 0, sizeof ve);
162 ve[0].src_offset = Offset(struct vertex, position);
163 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
164 ve[1].src_offset = Offset(struct vertex, color);
165 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
166 ve[2].src_offset = Offset(struct vertex, texcoord);
167 ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
169 handle = ctx->create_vertex_elements_state(ctx, 3, ve);
170 ctx->bind_vertex_elements_state(ctx, handle);
173 vbuf.stride = sizeof( struct vertex );
174 vbuf.max_index = sizeof(vertices) / vbuf.stride;
175 vbuf.buffer_offset = 0;
176 vbuf.buffer = screen->user_buffer_create(screen,
179 PIPE_BIND_VERTEX_BUFFER);
181 ctx->set_vertex_buffers(ctx, 1, &vbuf);
184 static void set_vertex_shader( void )
192 "DCL OUT[0], POSITION\n"
193 "DCL OUT[1], COLOR[0]\n"
194 "DCL OUT[2], GENERIC[0]\n"
195 " MOV OUT[0], IN[0]\n"
196 " MOV OUT[1], IN[1]\n"
197 " MOV OUT[2], IN[2]\n"
200 handle = graw_parse_vertex_shader(ctx, text);
201 ctx->bind_vs_state(ctx, handle);
204 static void set_fragment_shader( void )
209 "DCL IN[0], COLOR, LINEAR\n"
210 "DCL OUT[0], COLOR\n"
211 " 0: MOV OUT[0], IN[0]\n"
214 handle = graw_parse_fragment_shader(ctx, text);
215 ctx->bind_fs_state(ctx, handle);
219 static void set_geometry_shader( void )
226 if ((f = fopen(filename, "r")) == NULL) {
227 fprintf(stderr, "Couldn't open %s\n", filename);
231 sz = fread(buf, 1, sizeof(buf), f);
233 printf("file too long\n");
236 printf("%.*s\n", sz, buf);
239 handle = graw_parse_geometry_shader(ctx, buf);
240 ctx->bind_gs_state(ctx, handle);
245 static void draw( void )
247 float clear_color[4] = {.1,.3,.5,0};
249 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
250 ctx->draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
251 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
254 /* At the moment, libgraw leaks out/makes available some of the
255 * symbols from gallium/auxiliary, including these debug helpers.
256 * Will eventually want to bless some of these paths, and lock the
257 * others down so they aren't accessible from test programs.
259 * This currently just happens to work on debug builds - a release
260 * build will probably fail to link here:
262 debug_dump_surface_bmp(ctx, "result.bmp", surf);
265 screen->flush_frontbuffer(screen, surf, window);
270 static void init_tex( void )
272 struct pipe_sampler_view sv_template;
273 struct pipe_sampler_state sampler_desc;
274 struct pipe_resource templat;
276 ubyte tex2d[SIZE][SIZE][4];
280 for (s = 0; s < SIZE; s++) {
281 for (t = 0; t < SIZE; t++) {
284 tex2d[t][s][0] = (x) ? 0 : 63;
285 tex2d[t][s][1] = (x) ? 0 : 128;
287 tex2d[t][s][3] = 0xff;
290 int x = ((s ^ t) >> 2) & 1;
291 tex2d[t][s][0] = s*255/(SIZE-1);
292 tex2d[t][s][1] = t*255/(SIZE-1);
293 tex2d[t][s][2] = (x) ? 0 : 128;
294 tex2d[t][s][3] = 0xff;
300 tex2d[0][0][1] = 255;
301 tex2d[0][0][2] = 255;
306 tex2d[0][1][2] = 255;
307 tex2d[0][1][3] = 255;
309 tex2d[1][0][0] = 255;
310 tex2d[1][0][1] = 255;
312 tex2d[1][0][3] = 255;
314 tex2d[1][1][0] = 255;
317 tex2d[1][1][3] = 255;
320 templat.target = PIPE_TEXTURE_2D;
321 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
322 templat.width0 = SIZE;
323 templat.height0 = SIZE;
325 templat.last_level = 0;
326 templat.nr_samples = 1;
327 templat.bind = PIPE_BIND_SAMPLER_VIEW;
330 samptex = screen->resource_create(screen,
335 u_box_2d(0,0,SIZE,SIZE, &box);
337 ctx->transfer_inline_write(ctx,
346 /* Possibly read back & compare against original data:
350 struct pipe_transfer *t;
352 t = pipe_get_transfer(ctx, samptex,
353 0, 0, 0, /* face, level, zslice */
355 0, 0, SIZE, SIZE); /* x, y, width, height */
357 ptr = ctx->transfer_map(ctx, t);
359 if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
364 ctx->transfer_unmap(ctx, t);
366 ctx->transfer_destroy(ctx, t);
369 memset(&sv_template, 0, sizeof sv_template);
370 sv_template.format = samptex->format;
371 sv_template.texture = samptex;
372 sv_template.first_level = 0;
373 sv_template.last_level = 0;
374 sv_template.swizzle_r = 0;
375 sv_template.swizzle_g = 1;
376 sv_template.swizzle_b = 2;
377 sv_template.swizzle_a = 3;
378 sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
382 ctx->set_fragment_sampler_views(ctx, 1, &sv);
385 memset(&sampler_desc, 0, sizeof sampler_desc);
386 sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
387 sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
388 sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
389 sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
390 sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
391 sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
392 sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
393 sampler_desc.compare_func = 0;
394 sampler_desc.normalized_coords = 1;
395 sampler_desc.max_anisotropy = 0;
397 sampler = ctx->create_sampler_state(ctx, &sampler_desc);
401 ctx->bind_fragment_sampler_states(ctx, 1, &sampler);
405 static void init( void )
407 struct pipe_framebuffer_state fb;
408 struct pipe_resource templat;
411 /* It's hard to say whether window or screen should be created
412 * first. Different environments would prefer one or the other.
414 * Also, no easy way of querying supported formats if the screen
415 * cannot be created first.
418 window == NULL && formats[i] != PIPE_FORMAT_NONE;
421 screen = graw_create_window_and_screen(0,0,WIDTH,HEIGHT,
426 ctx = screen->context_create(screen, NULL);
430 templat.target = PIPE_TEXTURE_2D;
431 templat.format = formats[i];
432 templat.width0 = WIDTH;
433 templat.height0 = HEIGHT;
435 templat.last_level = 0;
436 templat.nr_samples = 1;
437 templat.bind = (PIPE_BIND_RENDER_TARGET |
438 PIPE_BIND_DISPLAY_TARGET);
440 rttex = screen->resource_create(screen,
445 surf = screen->get_tex_surface(screen, rttex, 0, 0, 0,
446 PIPE_BIND_RENDER_TARGET |
447 PIPE_BIND_DISPLAY_TARGET);
451 memset(&fb, 0, sizeof fb);
457 ctx->set_framebuffer_state(ctx, &fb);
460 struct pipe_blend_state blend;
462 memset(&blend, 0, sizeof blend);
463 blend.rt[0].colormask = PIPE_MASK_RGBA;
464 handle = ctx->create_blend_state(ctx, &blend);
465 ctx->bind_blend_state(ctx, handle);
469 struct pipe_depth_stencil_alpha_state depthstencil;
471 memset(&depthstencil, 0, sizeof depthstencil);
472 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
473 ctx->bind_depth_stencil_alpha_state(ctx, handle);
477 struct pipe_rasterizer_state rasterizer;
479 memset(&rasterizer, 0, sizeof rasterizer);
480 rasterizer.cull_face = PIPE_FACE_NONE;
481 rasterizer.gl_rasterization_rules = 1;
482 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
483 ctx->bind_rasterizer_state(ctx, handle);
486 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
493 set_fragment_shader();
494 set_geometry_shader();
497 static void args(int argc, char *argv[])
501 for (i = 1; i < argc; i++) {
502 if (strcmp(argv[i], "-fps") == 0) {
505 else if (i == argc - 1) {
520 int main( int argc, char *argv[] )
525 graw_set_display_func( draw );