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_draw_quad.h"
17 #include "util/u_box.h"
19 static const char *filename
= NULL
;
20 unsigned show_fps
= 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");
34 enum pipe_format formats
[] = {
35 PIPE_FORMAT_R8G8B8A8_UNORM
,
36 PIPE_FORMAT_B8G8R8A8_UNORM
,
40 static const int WIDTH
= 250;
41 static const int HEIGHT
= 250;
43 static struct pipe_screen
*screen
= NULL
;
44 static struct pipe_context
*ctx
= NULL
;
45 static struct pipe_resource
*rttex
= NULL
;
46 static struct pipe_resource
*constbuf1
= NULL
;
47 static struct pipe_resource
*constbuf2
= NULL
;
48 static struct pipe_surface
*surf
= NULL
;
49 static struct pipe_sampler_view
*sv
= NULL
;
50 static void *sampler
= NULL
;
51 static void *window
= NULL
;
52 static struct pipe_resource
*samptex
= NULL
;
60 /* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
61 * so that the final images are the same.
63 static struct vertex vertices
[] =
65 { { 0.9, 0.9, 0.0, 1.0 },
69 { { 0.9, -0.9, 0.0, 1.0 },
73 { {-0.9, 0.0, 0.0, 1.0 },
78 static float constants1
[] =
96 static float constants2
[] =
113 static void init_fs_constbuf( void )
115 struct pipe_resource templat
;
118 templat
.target
= PIPE_BUFFER
;
119 templat
.format
= PIPE_FORMAT_R8_UNORM
;
120 templat
.width0
= sizeof(constants1
);
123 templat
.last_level
= 0;
124 templat
.nr_samples
= 1;
125 templat
.bind
= PIPE_BIND_CONSTANT_BUFFER
;
127 constbuf1
= screen
->resource_create(screen
,
129 if (constbuf1
== NULL
)
132 constbuf2
= screen
->resource_create(screen
,
134 if (constbuf2
== NULL
)
139 u_box_2d(0,0,sizeof(constants1
),1, &box
);
141 ctx
->transfer_inline_write(ctx
,
151 ctx
->set_constant_buffer(ctx
,
152 PIPE_SHADER_FRAGMENT
, 0,
156 u_box_2d(0,0,sizeof(constants2
),1, &box
);
158 ctx
->transfer_inline_write(ctx
,
168 ctx
->set_constant_buffer(ctx
,
169 PIPE_SHADER_FRAGMENT
, 1,
175 static void set_viewport( float x
, float y
,
176 float width
, float height
,
177 float near
, float far
)
180 float half_width
= (float)width
/ 2.0f
;
181 float half_height
= (float)height
/ 2.0f
;
182 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
183 struct pipe_viewport_state vp
;
185 vp
.scale
[0] = half_width
;
186 vp
.scale
[1] = half_height
;
187 vp
.scale
[2] = half_depth
;
190 vp
.translate
[0] = half_width
+ x
;
191 vp
.translate
[1] = half_height
+ y
;
192 vp
.translate
[2] = half_depth
+ z
;
193 vp
.translate
[3] = 0.0f
;
195 ctx
->set_viewport_state( ctx
, &vp
);
198 static void set_vertices( void )
200 struct pipe_vertex_element ve
[3];
201 struct pipe_vertex_buffer vbuf
;
204 memset(ve
, 0, sizeof ve
);
206 ve
[0].src_offset
= Offset(struct vertex
, position
);
207 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
208 ve
[1].src_offset
= Offset(struct vertex
, color
);
209 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
210 ve
[2].src_offset
= Offset(struct vertex
, texcoord
);
211 ve
[2].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
213 handle
= ctx
->create_vertex_elements_state(ctx
, 3, ve
);
214 ctx
->bind_vertex_elements_state(ctx
, handle
);
217 vbuf
.stride
= sizeof( struct vertex
);
218 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
219 vbuf
.buffer_offset
= 0;
220 vbuf
.buffer
= screen
->user_buffer_create(screen
,
223 PIPE_BIND_VERTEX_BUFFER
);
225 ctx
->set_vertex_buffers(ctx
, 1, &vbuf
);
228 static void set_vertex_shader( void )
236 "DCL OUT[0], POSITION\n"
237 "DCL OUT[1], COLOR[0]\n"
238 "DCL OUT[2], GENERIC[0]\n"
239 " MOV OUT[0], IN[0]\n"
240 " MOV OUT[1], IN[1]\n"
241 " MOV OUT[2], IN[2]\n"
244 handle
= graw_parse_vertex_shader(ctx
, text
);
245 ctx
->bind_vs_state(ctx
, handle
);
248 static void set_fragment_shader( const char *filename
)
255 if ((f
= fopen(filename
, "r")) == NULL
) {
256 fprintf(stderr
, "Couldn't open %s\n", filename
);
260 sz
= fread(buf
, 1, sizeof(buf
), f
);
262 printf("file too long\n");
265 printf("%.*s\n", sz
, buf
);
268 handle
= graw_parse_fragment_shader(ctx
, buf
);
269 ctx
->bind_fs_state(ctx
, handle
);
274 static void draw( void )
276 float clear_color
[4] = {.1,.3,.5,0};
278 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
279 util_draw_arrays(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3);
280 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
283 /* At the moment, libgraw leaks out/makes available some of the
284 * symbols from gallium/auxiliary, including these debug helpers.
285 * Will eventually want to bless some of these paths, and lock the
286 * others down so they aren't accessible from test programs.
288 * This currently just happens to work on debug builds - a release
289 * build will probably fail to link here:
291 debug_dump_surface_bmp(ctx
, "result.bmp", surf
);
294 screen
->flush_frontbuffer(screen
, surf
, window
);
299 static void init_tex( void )
301 struct pipe_sampler_view sv_template
;
302 struct pipe_sampler_state sampler_desc
;
303 struct pipe_resource templat
;
305 ubyte tex2d
[SIZE
][SIZE
][4];
309 for (s
= 0; s
< SIZE
; s
++) {
310 for (t
= 0; t
< SIZE
; t
++) {
313 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
314 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
316 tex2d
[t
][s
][3] = 0xff;
319 int x
= ((s
^ t
) >> 2) & 1;
320 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
321 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
322 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
323 tex2d
[t
][s
][3] = 0xff;
329 tex2d
[0][0][1] = 255;
330 tex2d
[0][0][2] = 255;
335 tex2d
[0][1][2] = 255;
336 tex2d
[0][1][3] = 255;
338 tex2d
[1][0][0] = 255;
339 tex2d
[1][0][1] = 255;
341 tex2d
[1][0][3] = 255;
343 tex2d
[1][1][0] = 255;
346 tex2d
[1][1][3] = 255;
349 templat
.target
= PIPE_TEXTURE_2D
;
350 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
351 templat
.width0
= SIZE
;
352 templat
.height0
= SIZE
;
354 templat
.last_level
= 0;
355 templat
.nr_samples
= 1;
356 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
359 samptex
= screen
->resource_create(screen
,
364 u_box_2d(0,0,SIZE
,SIZE
, &box
);
366 ctx
->transfer_inline_write(ctx
,
375 /* Possibly read back & compare against original data:
379 struct pipe_transfer
*t
;
381 t
= pipe_get_transfer(ctx
, samptex
,
382 0, 0, 0, /* face, level, zslice */
384 0, 0, SIZE
, SIZE
); /* x, y, width, height */
386 ptr
= ctx
->transfer_map(ctx
, t
);
388 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
393 ctx
->transfer_unmap(ctx
, t
);
395 ctx
->transfer_destroy(ctx
, t
);
398 memset(&sv_template
, 0, sizeof sv_template
);
399 sv_template
.format
= samptex
->format
;
400 sv_template
.texture
= samptex
;
401 sv_template
.first_level
= 0;
402 sv_template
.last_level
= 0;
403 sv_template
.swizzle_r
= 0;
404 sv_template
.swizzle_g
= 1;
405 sv_template
.swizzle_b
= 2;
406 sv_template
.swizzle_a
= 3;
407 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
411 ctx
->set_fragment_sampler_views(ctx
, 1, &sv
);
414 memset(&sampler_desc
, 0, sizeof sampler_desc
);
415 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
416 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
417 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
418 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
419 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
420 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
421 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
422 sampler_desc
.compare_func
= 0;
423 sampler_desc
.normalized_coords
= 1;
424 sampler_desc
.max_anisotropy
= 0;
426 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
430 ctx
->bind_fragment_sampler_states(ctx
, 1, &sampler
);
434 static void init( void )
436 struct pipe_framebuffer_state fb
;
437 struct pipe_resource templat
;
440 /* It's hard to say whether window or screen should be created
441 * first. Different environments would prefer one or the other.
443 * Also, no easy way of querying supported formats if the screen
444 * cannot be created first.
447 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
450 screen
= graw_create_window_and_screen(0,0,WIDTH
,HEIGHT
,
455 ctx
= screen
->context_create(screen
, NULL
);
459 templat
.target
= PIPE_TEXTURE_2D
;
460 templat
.format
= formats
[i
];
461 templat
.width0
= WIDTH
;
462 templat
.height0
= HEIGHT
;
464 templat
.last_level
= 0;
465 templat
.nr_samples
= 1;
466 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
467 PIPE_BIND_DISPLAY_TARGET
);
469 rttex
= screen
->resource_create(screen
,
474 surf
= screen
->get_tex_surface(screen
, rttex
, 0, 0, 0,
475 PIPE_BIND_RENDER_TARGET
|
476 PIPE_BIND_DISPLAY_TARGET
);
480 memset(&fb
, 0, sizeof fb
);
486 ctx
->set_framebuffer_state(ctx
, &fb
);
489 struct pipe_blend_state blend
;
491 memset(&blend
, 0, sizeof blend
);
492 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
493 handle
= ctx
->create_blend_state(ctx
, &blend
);
494 ctx
->bind_blend_state(ctx
, handle
);
498 struct pipe_depth_stencil_alpha_state depthstencil
;
500 memset(&depthstencil
, 0, sizeof depthstencil
);
501 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
502 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
506 struct pipe_rasterizer_state rasterizer
;
508 memset(&rasterizer
, 0, sizeof rasterizer
);
509 rasterizer
.cull_face
= PIPE_FACE_NONE
;
510 rasterizer
.gl_rasterization_rules
= 1;
511 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
512 ctx
->bind_rasterizer_state(ctx
, handle
);
515 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
522 set_fragment_shader(filename
);
525 static void args(int argc
, char *argv
[])
529 for (i
= 1; i
< argc
; i
++) {
530 if (strcmp(argv
[i
], "-fps") == 0) {
533 else if (i
== argc
- 1) {
548 int main( int argc
, char *argv
[] )
553 graw_set_display_func( draw
);