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 <unistd.h> /* for sleep() */
12 #include <stdio.h> /* for fread(), etc */
14 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
15 #include "util/u_inlines.h"
16 #include "util/u_memory.h" /* Offset() */
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
*constbuf
= NULL
;
47 static struct pipe_surface
*surf
= NULL
;
48 static struct pipe_sampler_view
*sv
= NULL
;
49 static void *sampler
= NULL
;
50 static void *window
= NULL
;
51 static struct pipe_resource
*samptex
= NULL
;
59 /* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
60 * so that the final images are the same.
62 static struct vertex vertices
[] =
64 { { 0.9, 0.9, 0.0, 1.0 },
68 { { 0.9, -0.9, 0.0, 1.0 },
72 { {-0.9, 0.0, 0.0, 1.0 },
77 static float constants
[] =
94 static void init_fs_constbuf( void )
96 struct pipe_resource templat
;
99 templat
.target
= PIPE_BUFFER
;
100 templat
.format
= PIPE_FORMAT_R8_UNORM
;
101 templat
.width0
= sizeof(constants
);
104 templat
.last_level
= 0;
105 templat
.nr_samples
= 1;
106 templat
.bind
= PIPE_BIND_CONSTANT_BUFFER
;
108 constbuf
= screen
->resource_create(screen
,
110 if (constbuf
== NULL
)
114 u_box_2d(0,0,sizeof(constants
),1, &box
);
116 ctx
->transfer_inline_write(ctx
,
126 ctx
->set_constant_buffer(ctx
,
127 PIPE_SHADER_FRAGMENT
, 0,
132 static void set_viewport( float x
, float y
,
133 float width
, float height
,
134 float near
, float far
)
137 float half_width
= (float)width
/ 2.0f
;
138 float half_height
= (float)height
/ 2.0f
;
139 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
140 struct pipe_viewport_state vp
;
142 vp
.scale
[0] = half_width
;
143 vp
.scale
[1] = half_height
;
144 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
;
150 vp
.translate
[3] = 0.0f
;
152 ctx
->set_viewport_state( ctx
, &vp
);
155 static void set_vertices( void )
157 struct pipe_vertex_element ve
[3];
158 struct pipe_vertex_buffer vbuf
;
161 memset(ve
, 0, sizeof ve
);
163 ve
[0].src_offset
= Offset(struct vertex
, position
);
164 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
165 ve
[1].src_offset
= Offset(struct vertex
, color
);
166 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
167 ve
[2].src_offset
= Offset(struct vertex
, texcoord
);
168 ve
[2].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
170 handle
= ctx
->create_vertex_elements_state(ctx
, 3, ve
);
171 ctx
->bind_vertex_elements_state(ctx
, handle
);
174 vbuf
.stride
= sizeof( struct vertex
);
175 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
176 vbuf
.buffer_offset
= 0;
177 vbuf
.buffer
= screen
->user_buffer_create(screen
,
180 PIPE_BIND_VERTEX_BUFFER
);
182 ctx
->set_vertex_buffers(ctx
, 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
);
230 static void draw( void )
232 float clear_color
[4] = {.1,.3,.5,0};
234 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
235 ctx
->draw_arrays(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3);
236 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
239 /* At the moment, libgraw leaks out/makes available some of the
240 * symbols from gallium/auxiliary, including these debug helpers.
241 * Will eventually want to bless some of these paths, and lock the
242 * others down so they aren't accessible from test programs.
244 * This currently just happens to work on debug builds - a release
245 * build will probably fail to link here:
247 debug_dump_surface_bmp(ctx
, "result.bmp", surf
);
250 screen
->flush_frontbuffer(screen
, surf
, window
);
255 static void init_tex( void )
257 struct pipe_sampler_view sv_template
;
258 struct pipe_sampler_state sampler_desc
;
259 struct pipe_resource templat
;
261 ubyte tex2d
[SIZE
][SIZE
][4];
265 for (s
= 0; s
< SIZE
; s
++) {
266 for (t
= 0; t
< SIZE
; t
++) {
269 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
270 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
272 tex2d
[t
][s
][3] = 0xff;
275 int x
= ((s
^ t
) >> 2) & 1;
276 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
277 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
278 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
279 tex2d
[t
][s
][3] = 0xff;
285 tex2d
[0][0][1] = 255;
286 tex2d
[0][0][2] = 255;
291 tex2d
[0][1][2] = 255;
292 tex2d
[0][1][3] = 255;
294 tex2d
[1][0][0] = 255;
295 tex2d
[1][0][1] = 255;
297 tex2d
[1][0][3] = 255;
299 tex2d
[1][1][0] = 255;
302 tex2d
[1][1][3] = 255;
305 templat
.target
= PIPE_TEXTURE_2D
;
306 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
307 templat
.width0
= SIZE
;
308 templat
.height0
= SIZE
;
310 templat
.last_level
= 0;
311 templat
.nr_samples
= 1;
312 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
315 samptex
= screen
->resource_create(screen
,
320 u_box_2d(0,0,SIZE
,SIZE
, &box
);
322 ctx
->transfer_inline_write(ctx
,
331 /* Possibly read back & compare against original data:
335 struct pipe_transfer
*t
;
337 t
= pipe_get_transfer(ctx
, samptex
,
338 0, 0, 0, /* face, level, zslice */
340 0, 0, SIZE
, SIZE
); /* x, y, width, height */
342 ptr
= ctx
->transfer_map(ctx
, t
);
344 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
349 ctx
->transfer_unmap(ctx
, t
);
351 ctx
->transfer_destroy(ctx
, t
);
354 memset(&sv_template
, 0, sizeof sv_template
);
355 sv_template
.format
= samptex
->format
;
356 sv_template
.texture
= samptex
;
357 sv_template
.first_level
= 0;
358 sv_template
.last_level
= 0;
359 sv_template
.swizzle_r
= 0;
360 sv_template
.swizzle_g
= 1;
361 sv_template
.swizzle_b
= 2;
362 sv_template
.swizzle_a
= 3;
363 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
367 ctx
->set_fragment_sampler_views(ctx
, 1, &sv
);
370 memset(&sampler_desc
, 0, sizeof sampler_desc
);
371 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
372 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
373 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
374 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
375 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
376 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
377 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
378 sampler_desc
.compare_func
= 0;
379 sampler_desc
.normalized_coords
= 1;
380 sampler_desc
.max_anisotropy
= 0;
382 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
386 ctx
->bind_fragment_sampler_states(ctx
, 1, &sampler
);
390 static void init( void )
392 struct pipe_framebuffer_state fb
;
393 struct pipe_resource templat
;
396 /* It's hard to say whether window or screen should be created
397 * first. Different environments would prefer one or the other.
399 * Also, no easy way of querying supported formats if the screen
400 * cannot be created first.
403 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
406 screen
= graw_create_window_and_screen(0,0,WIDTH
,HEIGHT
,
411 ctx
= screen
->context_create(screen
, NULL
);
415 templat
.target
= PIPE_TEXTURE_2D
;
416 templat
.format
= formats
[i
];
417 templat
.width0
= WIDTH
;
418 templat
.height0
= HEIGHT
;
420 templat
.last_level
= 0;
421 templat
.nr_samples
= 1;
422 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
423 PIPE_BIND_DISPLAY_TARGET
);
425 rttex
= screen
->resource_create(screen
,
430 surf
= screen
->get_tex_surface(screen
, rttex
, 0, 0, 0,
431 PIPE_BIND_RENDER_TARGET
|
432 PIPE_BIND_DISPLAY_TARGET
);
436 memset(&fb
, 0, sizeof fb
);
442 ctx
->set_framebuffer_state(ctx
, &fb
);
445 struct pipe_blend_state blend
;
447 memset(&blend
, 0, sizeof blend
);
448 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
449 handle
= ctx
->create_blend_state(ctx
, &blend
);
450 ctx
->bind_blend_state(ctx
, handle
);
454 struct pipe_depth_stencil_alpha_state depthstencil
;
456 memset(&depthstencil
, 0, sizeof depthstencil
);
457 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
458 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
462 struct pipe_rasterizer_state rasterizer
;
464 memset(&rasterizer
, 0, sizeof rasterizer
);
465 rasterizer
.cull_face
= PIPE_FACE_NONE
;
466 rasterizer
.gl_rasterization_rules
= 1;
467 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
468 ctx
->bind_rasterizer_state(ctx
, handle
);
471 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
478 set_fragment_shader(filename
);
481 static void args(int argc
, char *argv
[])
485 for (i
= 1; i
< argc
; i
++) {
486 if (strcmp(argv
[i
], "-fps") == 0) {
489 else if (i
== argc
- 1) {
504 int main( int argc
, char *argv
[] )
509 graw_set_display_func( draw
);