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"
12 #include <stdio.h> /* for fread(), etc */
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
*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
;
58 /* Draw a regular mesh
61 static struct vertex vertices
[MESH_SZ
* MESH_SZ
];
63 static float constants
[] =
80 static void init_fs_constbuf( void )
82 struct pipe_resource templat
;
85 templat
.target
= PIPE_BUFFER
;
86 templat
.format
= PIPE_FORMAT_R8_UNORM
;
87 templat
.width0
= sizeof(constants
);
90 templat
.last_level
= 0;
91 templat
.nr_samples
= 1;
92 templat
.bind
= PIPE_BIND_CONSTANT_BUFFER
;
94 constbuf
= screen
->resource_create(screen
,
100 u_box_2d(0,0,sizeof(constants
),1, &box
);
102 ctx
->transfer_inline_write(ctx
,
112 ctx
->set_constant_buffer(ctx
,
113 PIPE_SHADER_FRAGMENT
, 0,
118 static void set_viewport( float x
, float y
,
119 float width
, float height
,
120 float near
, float far
)
123 float half_width
= (float)width
/ 2.0f
;
124 float half_height
= (float)height
/ 2.0f
;
125 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
126 struct pipe_viewport_state vp
;
128 vp
.scale
[0] = half_width
;
129 vp
.scale
[1] = half_height
;
130 vp
.scale
[2] = half_depth
;
133 vp
.translate
[0] = half_width
+ x
;
134 vp
.translate
[1] = half_height
+ y
;
135 vp
.translate
[2] = half_depth
+ z
;
136 vp
.translate
[3] = 0.0f
;
138 ctx
->set_viewport_state( ctx
, &vp
);
141 static void set_vertices( void )
143 struct pipe_vertex_element ve
[2];
144 struct pipe_vertex_buffer vbuf
;
148 memset(ve
, 0, sizeof ve
);
150 ve
[0].src_offset
= Offset(struct vertex
, position
);
151 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
152 ve
[1].src_offset
= Offset(struct vertex
, color
);
153 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
155 handle
= ctx
->create_vertex_elements_state(ctx
, 2, ve
);
156 ctx
->bind_vertex_elements_state(ctx
, handle
);
158 for (x
= 0; x
< MESH_SZ
; x
++) {
159 for (y
= 0; y
< MESH_SZ
; y
++) {
160 int i
= y
* MESH_SZ
+ x
;
161 vertices
[i
].position
[0] = ((float)x
)/MESH_SZ
* 2.0 - 1.0;
162 vertices
[i
].position
[1] = ((float)y
)/MESH_SZ
* 2.0 - 1.0;
163 vertices
[i
].position
[2] = 0;
164 vertices
[i
].position
[3] = 1.0;
166 vertices
[i
].color
[0] = .5;
167 vertices
[i
].color
[1] = (float)x
/ (float)MESH_SZ
;
168 vertices
[i
].color
[2] = (float)y
/ (float)MESH_SZ
;
172 vbuf
.stride
= sizeof( struct vertex
);
173 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
174 vbuf
.buffer_offset
= 0;
175 vbuf
.buffer
= screen
->user_buffer_create(screen
,
178 PIPE_BIND_VERTEX_BUFFER
);
180 ctx
->set_vertex_buffers(ctx
, 1, &vbuf
);
183 static void set_vertex_shader( void )
190 if ((f
= fopen(filename
, "r")) == NULL
) {
191 fprintf(stderr
, "Couldn't open %s\n", filename
);
195 sz
= fread(buf
, 1, sizeof(buf
), f
);
197 printf("file too long\n");
200 printf("%.*s\n", sz
, buf
);
203 handle
= graw_parse_vertex_shader(ctx
, buf
);
204 ctx
->bind_vs_state(ctx
, handle
);
208 static void set_fragment_shader( void )
213 "DCL IN[0], COLOR, LINEAR\n"
214 "DCL OUT[0], COLOR\n"
215 " 0: MOV OUT[0], IN[0]\n"
218 handle
= graw_parse_fragment_shader(ctx
, text
);
219 ctx
->bind_fs_state(ctx
, handle
);
224 static void draw( void )
226 float clear_color
[4] = {.1,.3,.5,0};
228 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
229 util_draw_arrays(ctx
, PIPE_PRIM_POINTS
, 0, Elements(vertices
));
230 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
232 graw_save_surface_to_file(ctx
, surf
, NULL
);
234 screen
->flush_frontbuffer(screen
, surf
, window
);
239 static void init_tex( void )
241 struct pipe_sampler_view sv_template
;
242 struct pipe_sampler_state sampler_desc
;
243 struct pipe_resource templat
;
245 ubyte tex2d
[SIZE
][SIZE
][4];
249 for (s
= 0; s
< SIZE
; s
++) {
250 for (t
= 0; t
< SIZE
; t
++) {
253 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
254 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
256 tex2d
[t
][s
][3] = 0xff;
259 int x
= ((s
^ t
) >> 2) & 1;
260 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
261 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
262 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
263 tex2d
[t
][s
][3] = 0xff;
269 tex2d
[0][0][1] = 255;
270 tex2d
[0][0][2] = 255;
275 tex2d
[0][1][2] = 255;
276 tex2d
[0][1][3] = 255;
278 tex2d
[1][0][0] = 255;
279 tex2d
[1][0][1] = 255;
281 tex2d
[1][0][3] = 255;
283 tex2d
[1][1][0] = 255;
286 tex2d
[1][1][3] = 255;
289 templat
.target
= PIPE_TEXTURE_2D
;
290 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
291 templat
.width0
= SIZE
;
292 templat
.height0
= SIZE
;
294 templat
.last_level
= 0;
295 templat
.nr_samples
= 1;
296 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
299 samptex
= screen
->resource_create(screen
,
304 u_box_2d(0,0,SIZE
,SIZE
, &box
);
306 ctx
->transfer_inline_write(ctx
,
315 /* Possibly read back & compare against original data:
319 struct pipe_transfer
*t
;
321 t
= pipe_get_transfer(ctx
, samptex
,
322 0, 0, 0, /* face, level, zslice */
324 0, 0, SIZE
, SIZE
); /* x, y, width, height */
326 ptr
= ctx
->transfer_map(ctx
, t
);
328 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
333 ctx
->transfer_unmap(ctx
, t
);
335 ctx
->transfer_destroy(ctx
, t
);
338 memset(&sv_template
, 0, sizeof sv_template
);
339 sv_template
.format
= samptex
->format
;
340 sv_template
.texture
= samptex
;
341 sv_template
.first_level
= 0;
342 sv_template
.last_level
= 0;
343 sv_template
.swizzle_r
= 0;
344 sv_template
.swizzle_g
= 1;
345 sv_template
.swizzle_b
= 2;
346 sv_template
.swizzle_a
= 3;
347 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
351 ctx
->set_fragment_sampler_views(ctx
, 1, &sv
);
354 memset(&sampler_desc
, 0, sizeof sampler_desc
);
355 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
356 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
357 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
358 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
359 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
360 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
361 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
362 sampler_desc
.compare_func
= 0;
363 sampler_desc
.normalized_coords
= 1;
364 sampler_desc
.max_anisotropy
= 0;
366 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
370 ctx
->bind_fragment_sampler_states(ctx
, 1, &sampler
);
374 static void init( void )
376 struct pipe_framebuffer_state fb
;
377 struct pipe_resource templat
;
380 /* It's hard to say whether window or screen should be created
381 * first. Different environments would prefer one or the other.
383 * Also, no easy way of querying supported formats if the screen
384 * cannot be created first.
387 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
390 screen
= graw_create_window_and_screen(0,0,WIDTH
,HEIGHT
,
395 ctx
= screen
->context_create(screen
, NULL
);
399 templat
.target
= PIPE_TEXTURE_2D
;
400 templat
.format
= formats
[i
];
401 templat
.width0
= WIDTH
;
402 templat
.height0
= HEIGHT
;
404 templat
.last_level
= 0;
405 templat
.nr_samples
= 1;
406 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
407 PIPE_BIND_DISPLAY_TARGET
);
409 rttex
= screen
->resource_create(screen
,
414 surf
= screen
->get_tex_surface(screen
, rttex
, 0, 0, 0,
415 PIPE_BIND_RENDER_TARGET
|
416 PIPE_BIND_DISPLAY_TARGET
);
420 memset(&fb
, 0, sizeof fb
);
426 ctx
->set_framebuffer_state(ctx
, &fb
);
429 struct pipe_blend_state blend
;
431 memset(&blend
, 0, sizeof blend
);
432 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
433 handle
= ctx
->create_blend_state(ctx
, &blend
);
434 ctx
->bind_blend_state(ctx
, handle
);
438 struct pipe_depth_stencil_alpha_state depthstencil
;
440 memset(&depthstencil
, 0, sizeof depthstencil
);
441 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
442 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
446 struct pipe_rasterizer_state rasterizer
;
448 memset(&rasterizer
, 0, sizeof rasterizer
);
449 rasterizer
.cull_face
= PIPE_FACE_NONE
;
450 rasterizer
.point_size
= 8.0;
451 rasterizer
.gl_rasterization_rules
= 1;
452 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
453 ctx
->bind_rasterizer_state(ctx
, handle
);
456 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
463 set_fragment_shader();
466 static void args(int argc
, char *argv
[])
470 for (i
= 1; i
< argc
;) {
471 if (graw_parse_args(&i
, argc
, argv
)) {
474 if (strcmp(argv
[i
], "-fps") == 0) {
478 else if (i
== argc
- 1) {
494 int main( int argc
, char *argv
[] )
499 graw_set_display_func( draw
);