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;
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_RGBA8888_UNORM
,
35 PIPE_FORMAT_BGRA8888_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_surface
*surf
= NULL
;
46 static struct pipe_sampler_view
*sv
= NULL
;
47 static void *sampler
= NULL
;
48 static void *window
= NULL
;
49 static struct pipe_resource
*samptex
= NULL
;
57 /* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
58 * so that the final images are the same.
60 static struct vertex vertices
[] =
62 { { 0.9, 0.9, 0.0, 1.0 },
66 { { 0.9, -0.9, 0.0, 1.0 },
70 { {-0.9, 0.0, 0.0, 1.0 },
75 static float constants1
[] =
93 static float constants2
[] =
110 static void init_fs_constbuf( void )
112 struct pipe_constant_buffer cb1
;
113 struct pipe_constant_buffer cb2
;
115 memset(&cb1
, 0, sizeof cb1
);
116 cb1
.buffer_size
= sizeof constants1
;
117 cb1
.user_buffer
= constants1
;
119 ctx
->set_constant_buffer(ctx
,
120 PIPE_SHADER_FRAGMENT
, 0,
123 memset(&cb2
, 0, sizeof cb2
);
124 cb2
.buffer_size
= sizeof constants2
;
125 cb2
.user_buffer
= constants2
;
127 ctx
->set_constant_buffer(ctx
,
128 PIPE_SHADER_FRAGMENT
, 1,
133 static void set_viewport( float x
, float y
,
134 float width
, float height
,
135 float zNear
, float zFar
)
138 float half_width
= (float)width
/ 2.0f
;
139 float half_height
= (float)height
/ 2.0f
;
140 float half_depth
= ((float)zFar
- (float)zNear
) / 2.0f
;
141 struct pipe_viewport_state vp
;
143 vp
.scale
[0] = half_width
;
144 vp
.scale
[1] = half_height
;
145 vp
.scale
[2] = half_depth
;
147 vp
.translate
[0] = half_width
+ x
;
148 vp
.translate
[1] = half_height
+ y
;
149 vp
.translate
[2] = half_depth
+ z
;
151 ctx
->set_viewport_states( ctx
, 0, 1, &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
);
172 memset(&vbuf
, 0, sizeof vbuf
);
174 vbuf
.stride
= sizeof( struct vertex
);
175 vbuf
.buffer_offset
= 0;
176 vbuf
.buffer
.resource
= pipe_buffer_create_with_data(ctx
,
177 PIPE_BIND_VERTEX_BUFFER
,
182 ctx
->set_vertex_buffers(ctx
, 0, 1, &vbuf
);
185 static void set_vertex_shader( void )
193 "DCL OUT[0], POSITION\n"
194 "DCL OUT[1], COLOR[0]\n"
195 "DCL OUT[2], GENERIC[0]\n"
196 " MOV OUT[0], IN[0]\n"
197 " MOV OUT[1], IN[1]\n"
198 " MOV OUT[2], IN[2]\n"
201 handle
= graw_parse_vertex_shader(ctx
, text
);
202 ctx
->bind_vs_state(ctx
, handle
);
205 static void set_fragment_shader( const char *filename
)
212 if ((f
= fopen(filename
, "r")) == NULL
) {
213 fprintf(stderr
, "Couldn't open %s\n", filename
);
217 sz
= fread(buf
, 1, sizeof(buf
), f
);
219 printf("file too long\n");
222 printf("%.*s\n", sz
, buf
);
225 handle
= graw_parse_fragment_shader(ctx
, buf
);
226 ctx
->bind_fs_state(ctx
, handle
);
231 static void draw( void )
233 union pipe_color_union clear_color
= { {.1,.3,.5,0} };
235 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, &clear_color
, 0, 0);
236 util_draw_arrays(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3);
237 ctx
->flush(ctx
, NULL
, 0);
239 graw_save_surface_to_file(ctx
, surf
, NULL
);
241 screen
->flush_frontbuffer(screen
, rttex
, 0, 0, window
, NULL
);
246 static void init_tex( void )
248 struct pipe_sampler_view sv_template
;
249 struct pipe_sampler_state sampler_desc
;
250 struct pipe_resource templat
;
252 ubyte tex2d
[SIZE
][SIZE
][4];
256 for (s
= 0; s
< SIZE
; s
++) {
257 for (t
= 0; t
< SIZE
; t
++) {
260 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
261 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
263 tex2d
[t
][s
][3] = 0xff;
266 int x
= ((s
^ t
) >> 2) & 1;
267 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
268 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
269 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
270 tex2d
[t
][s
][3] = 0xff;
276 tex2d
[0][0][1] = 255;
277 tex2d
[0][0][2] = 255;
282 tex2d
[0][1][2] = 255;
283 tex2d
[0][1][3] = 255;
285 tex2d
[1][0][0] = 255;
286 tex2d
[1][0][1] = 255;
288 tex2d
[1][0][3] = 255;
290 tex2d
[1][1][0] = 255;
293 tex2d
[1][1][3] = 255;
296 memset(&templat
, 0, sizeof(templat
));
297 templat
.target
= PIPE_TEXTURE_2D
;
298 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
299 templat
.width0
= SIZE
;
300 templat
.height0
= SIZE
;
302 templat
.array_size
= 1;
303 templat
.last_level
= 0;
304 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
307 samptex
= screen
->resource_create(screen
,
312 u_box_2d(0,0,SIZE
,SIZE
, &box
);
314 ctx
->texture_subdata(ctx
,
323 /* Possibly read back & compare against original data:
327 struct pipe_transfer
*t
;
329 ptr
= pipe_transfer_map(ctx
, samptex
,
330 0, 0, /* level, layer */
332 0, 0, SIZE
, SIZE
, &t
); /* x, y, width, height */
334 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
339 ctx
->transfer_unmap(ctx
, t
);
342 memset(&sv_template
, 0, sizeof sv_template
);
343 sv_template
.format
= samptex
->format
;
344 sv_template
.texture
= samptex
;
345 sv_template
.swizzle_r
= 0;
346 sv_template
.swizzle_g
= 1;
347 sv_template
.swizzle_b
= 2;
348 sv_template
.swizzle_a
= 3;
349 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
353 ctx
->set_sampler_views(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sv
);
356 memset(&sampler_desc
, 0, sizeof sampler_desc
);
357 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
358 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
359 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
360 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
361 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
362 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
363 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
364 sampler_desc
.compare_func
= 0;
365 sampler_desc
.normalized_coords
= 1;
366 sampler_desc
.max_anisotropy
= 0;
368 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
372 ctx
->bind_sampler_states(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sampler
);
376 static void init( void )
378 struct pipe_framebuffer_state fb
;
379 struct pipe_resource templat
;
380 struct pipe_surface surf_tmpl
;
383 /* It's hard to say whether window or screen should be created
384 * first. Different environments would prefer one or the other.
386 * Also, no easy way of querying supported formats if the screen
387 * cannot be created first.
389 for (i
= 0; formats
[i
] != PIPE_FORMAT_NONE
; i
++) {
390 screen
= graw_create_window_and_screen(0, 0, 300, 300,
393 if (window
&& screen
)
396 if (!screen
|| !window
) {
397 fprintf(stderr
, "Unable to create window\n");
401 ctx
= screen
->context_create(screen
, NULL
, 0);
405 memset(&templat
, 0, sizeof(templat
));
406 templat
.target
= PIPE_TEXTURE_2D
;
407 templat
.format
= formats
[i
];
408 templat
.width0
= WIDTH
;
409 templat
.height0
= HEIGHT
;
411 templat
.array_size
= 1;
412 templat
.last_level
= 0;
413 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
414 PIPE_BIND_DISPLAY_TARGET
);
416 rttex
= screen
->resource_create(screen
,
421 surf_tmpl
.format
= templat
.format
;
422 surf_tmpl
.u
.tex
.level
= 0;
423 surf_tmpl
.u
.tex
.first_layer
= 0;
424 surf_tmpl
.u
.tex
.last_layer
= 0;
425 surf
= ctx
->create_surface(ctx
, rttex
, &surf_tmpl
);
429 memset(&fb
, 0, sizeof fb
);
435 ctx
->set_framebuffer_state(ctx
, &fb
);
438 struct pipe_blend_state blend
;
440 memset(&blend
, 0, sizeof blend
);
441 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
442 handle
= ctx
->create_blend_state(ctx
, &blend
);
443 ctx
->bind_blend_state(ctx
, handle
);
447 struct pipe_depth_stencil_alpha_state depthstencil
;
449 memset(&depthstencil
, 0, sizeof depthstencil
);
450 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
451 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
455 struct pipe_rasterizer_state rasterizer
;
457 memset(&rasterizer
, 0, sizeof rasterizer
);
458 rasterizer
.cull_face
= PIPE_FACE_NONE
;
459 rasterizer
.half_pixel_center
= 1;
460 rasterizer
.bottom_edge_rule
= 1;
461 rasterizer
.depth_clip_near
= 1;
462 rasterizer
.depth_clip_far
= 1;
463 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
464 ctx
->bind_rasterizer_state(ctx
, handle
);
467 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
474 set_fragment_shader(filename
);
477 static void args(int argc
, char *argv
[])
481 for (i
= 1; i
< argc
;) {
482 if (graw_parse_args(&i
, argc
, argv
)) {
485 if (strcmp(argv
[i
], "-fps") == 0) {
489 else if (i
== argc
- 1) {
505 int main( int argc
, char *argv
[] )
510 graw_set_display_func( draw
);