scons: Fix freebsd8 build.
[mesa.git] / src / gallium / tests / graw / vs-test.c
1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
3 */
4
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
12 #include <stdio.h> /* for fread(), etc */
13
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_draw_quad.h"
18 #include "util/u_box.h"
19
20 static const char *filename = NULL;
21 unsigned show_fps = 0;
22
23
24 static void usage(char *name)
25 {
26 fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
27 #ifndef WIN32
28 fprintf(stderr, "\n" );
29 fprintf(stderr, "options:\n");
30 fprintf(stderr, " -fps show frames per second\n");
31 #endif
32 }
33
34
35 enum pipe_format formats[] = {
36 PIPE_FORMAT_R8G8B8A8_UNORM,
37 PIPE_FORMAT_B8G8R8A8_UNORM,
38 PIPE_FORMAT_NONE
39 };
40
41 static const int WIDTH = 250;
42 static const int HEIGHT = 250;
43
44 static struct pipe_screen *screen = NULL;
45 static struct pipe_context *ctx = NULL;
46 static struct pipe_resource *rttex = NULL;
47 static struct pipe_resource *constbuf = 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;
53
54 struct vertex {
55 float position[4];
56 float color[3];
57 };
58
59 /* Draw a regular mesh
60 */
61 #define MESH_SZ 16
62 static struct vertex vertices[MESH_SZ * MESH_SZ];
63
64 static float constants[] =
65 { 0.4, 0, 0, 1,
66 1, 1, 1, 1,
67 2, 2, 2, 2,
68 4, 8, 16, 32,
69
70 3, 0, 0, 0,
71 0, .5, 0, 0,
72 0, 0, 1, 0,
73 0, 0, 0, 1,
74
75 1, 0, 0, 0.5,
76 0, 1, 0, 0.5,
77 0, 0, 1, 0,
78 0, 0, 0, 1,
79 };
80
81 static void init_fs_constbuf( void )
82 {
83 struct pipe_resource templat;
84 struct pipe_box box;
85
86 templat.target = PIPE_BUFFER;
87 templat.format = PIPE_FORMAT_R8_UNORM;
88 templat.width0 = sizeof(constants);
89 templat.height0 = 1;
90 templat.depth0 = 1;
91 templat.last_level = 0;
92 templat.nr_samples = 1;
93 templat.bind = PIPE_BIND_CONSTANT_BUFFER;
94
95 constbuf = screen->resource_create(screen,
96 &templat);
97 if (constbuf == NULL)
98 exit(4);
99
100
101 u_box_2d(0,0,sizeof(constants),1, &box);
102
103 ctx->transfer_inline_write(ctx,
104 constbuf,
105 u_subresource(0,0),
106 PIPE_TRANSFER_WRITE,
107 &box,
108 constants,
109 sizeof constants,
110 sizeof constants);
111
112
113 ctx->set_constant_buffer(ctx,
114 PIPE_SHADER_FRAGMENT, 0,
115 constbuf);
116 }
117
118
119 static void set_viewport( float x, float y,
120 float width, float height,
121 float near, float far)
122 {
123 float z = far;
124 float half_width = (float)width / 2.0f;
125 float half_height = (float)height / 2.0f;
126 float half_depth = ((float)far - (float)near) / 2.0f;
127 struct pipe_viewport_state vp;
128
129 vp.scale[0] = half_width;
130 vp.scale[1] = half_height;
131 vp.scale[2] = half_depth;
132 vp.scale[3] = 1.0f;
133
134 vp.translate[0] = half_width + x;
135 vp.translate[1] = half_height + y;
136 vp.translate[2] = half_depth + z;
137 vp.translate[3] = 0.0f;
138
139 ctx->set_viewport_state( ctx, &vp );
140 }
141
142 static void set_vertices( void )
143 {
144 struct pipe_vertex_element ve[2];
145 struct pipe_vertex_buffer vbuf;
146 void *handle;
147 int x,y;
148
149 memset(ve, 0, sizeof ve);
150
151 ve[0].src_offset = Offset(struct vertex, position);
152 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
153 ve[1].src_offset = Offset(struct vertex, color);
154 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
155
156 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
157 ctx->bind_vertex_elements_state(ctx, handle);
158
159 for (x = 0; x < MESH_SZ; x++) {
160 for (y = 0; y < MESH_SZ; y++) {
161 int i = y * MESH_SZ + x;
162 vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0;
163 vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0;
164 vertices[i].position[2] = 0;
165 vertices[i].position[3] = 1.0;
166
167 vertices[i].color[0] = .5;
168 vertices[i].color[1] = (float)x / (float)MESH_SZ;
169 vertices[i].color[2] = (float)y / (float)MESH_SZ;
170 }
171 }
172
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,
177 vertices,
178 sizeof(vertices),
179 PIPE_BIND_VERTEX_BUFFER);
180
181 ctx->set_vertex_buffers(ctx, 1, &vbuf);
182 }
183
184 static void set_vertex_shader( void )
185 {
186 FILE *f;
187 char buf[50000];
188 void *handle;
189 int sz;
190
191 if ((f = fopen(filename, "r")) == NULL) {
192 fprintf(stderr, "Couldn't open %s\n", filename);
193 exit(1);
194 }
195
196 sz = fread(buf, 1, sizeof(buf), f);
197 if (!feof(f)) {
198 printf("file too long\n");
199 exit(1);
200 }
201 printf("%.*s\n", sz, buf);
202 buf[sz] = 0;
203
204 handle = graw_parse_vertex_shader(ctx, buf);
205 ctx->bind_vs_state(ctx, handle);
206 fclose(f);
207 }
208
209 static void set_fragment_shader( void )
210 {
211 void *handle;
212 const char *text =
213 "FRAG\n"
214 "DCL IN[0], COLOR, LINEAR\n"
215 "DCL OUT[0], COLOR\n"
216 " 0: MOV OUT[0], IN[0]\n"
217 " 1: END\n";
218
219 handle = graw_parse_fragment_shader(ctx, text);
220 ctx->bind_fs_state(ctx, handle);
221 }
222
223
224
225 static void draw( void )
226 {
227 float clear_color[4] = {.1,.3,.5,0};
228
229 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
230 util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, Elements(vertices));
231 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
232
233 #if 0
234 /* At the moment, libgraw leaks out/makes available some of the
235 * symbols from gallium/auxiliary, including these debug helpers.
236 * Will eventually want to bless some of these paths, and lock the
237 * others down so they aren't accessible from test programs.
238 *
239 * This currently just happens to work on debug builds - a release
240 * build will probably fail to link here:
241 */
242 debug_dump_surface_bmp(ctx, "result.bmp", surf);
243 #endif
244
245 screen->flush_frontbuffer(screen, surf, window);
246 }
247
248 #define SIZE 16
249
250 static void init_tex( void )
251 {
252 struct pipe_sampler_view sv_template;
253 struct pipe_sampler_state sampler_desc;
254 struct pipe_resource templat;
255 struct pipe_box box;
256 ubyte tex2d[SIZE][SIZE][4];
257 int s, t;
258
259 #if (SIZE != 2)
260 for (s = 0; s < SIZE; s++) {
261 for (t = 0; t < SIZE; t++) {
262 if (0) {
263 int x = (s ^ t) & 1;
264 tex2d[t][s][0] = (x) ? 0 : 63;
265 tex2d[t][s][1] = (x) ? 0 : 128;
266 tex2d[t][s][2] = 0;
267 tex2d[t][s][3] = 0xff;
268 }
269 else {
270 int x = ((s ^ t) >> 2) & 1;
271 tex2d[t][s][0] = s*255/(SIZE-1);
272 tex2d[t][s][1] = t*255/(SIZE-1);
273 tex2d[t][s][2] = (x) ? 0 : 128;
274 tex2d[t][s][3] = 0xff;
275 }
276 }
277 }
278 #else
279 tex2d[0][0][0] = 0;
280 tex2d[0][0][1] = 255;
281 tex2d[0][0][2] = 255;
282 tex2d[0][0][3] = 0;
283
284 tex2d[0][1][0] = 0;
285 tex2d[0][1][1] = 0;
286 tex2d[0][1][2] = 255;
287 tex2d[0][1][3] = 255;
288
289 tex2d[1][0][0] = 255;
290 tex2d[1][0][1] = 255;
291 tex2d[1][0][2] = 0;
292 tex2d[1][0][3] = 255;
293
294 tex2d[1][1][0] = 255;
295 tex2d[1][1][1] = 0;
296 tex2d[1][1][2] = 0;
297 tex2d[1][1][3] = 255;
298 #endif
299
300 templat.target = PIPE_TEXTURE_2D;
301 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
302 templat.width0 = SIZE;
303 templat.height0 = SIZE;
304 templat.depth0 = 1;
305 templat.last_level = 0;
306 templat.nr_samples = 1;
307 templat.bind = PIPE_BIND_SAMPLER_VIEW;
308
309
310 samptex = screen->resource_create(screen,
311 &templat);
312 if (samptex == NULL)
313 exit(4);
314
315 u_box_2d(0,0,SIZE,SIZE, &box);
316
317 ctx->transfer_inline_write(ctx,
318 samptex,
319 u_subresource(0,0),
320 PIPE_TRANSFER_WRITE,
321 &box,
322 tex2d,
323 sizeof tex2d[0],
324 sizeof tex2d);
325
326 /* Possibly read back & compare against original data:
327 */
328 if (0)
329 {
330 struct pipe_transfer *t;
331 uint32_t *ptr;
332 t = pipe_get_transfer(ctx, samptex,
333 0, 0, 0, /* face, level, zslice */
334 PIPE_TRANSFER_READ,
335 0, 0, SIZE, SIZE); /* x, y, width, height */
336
337 ptr = ctx->transfer_map(ctx, t);
338
339 if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
340 assert(0);
341 exit(9);
342 }
343
344 ctx->transfer_unmap(ctx, t);
345
346 ctx->transfer_destroy(ctx, t);
347 }
348
349 memset(&sv_template, 0, sizeof sv_template);
350 sv_template.format = samptex->format;
351 sv_template.texture = samptex;
352 sv_template.first_level = 0;
353 sv_template.last_level = 0;
354 sv_template.swizzle_r = 0;
355 sv_template.swizzle_g = 1;
356 sv_template.swizzle_b = 2;
357 sv_template.swizzle_a = 3;
358 sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
359 if (sv == NULL)
360 exit(5);
361
362 ctx->set_fragment_sampler_views(ctx, 1, &sv);
363
364
365 memset(&sampler_desc, 0, sizeof sampler_desc);
366 sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
367 sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
368 sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
369 sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
370 sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
371 sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
372 sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
373 sampler_desc.compare_func = 0;
374 sampler_desc.normalized_coords = 1;
375 sampler_desc.max_anisotropy = 0;
376
377 sampler = ctx->create_sampler_state(ctx, &sampler_desc);
378 if (sampler == NULL)
379 exit(6);
380
381 ctx->bind_fragment_sampler_states(ctx, 1, &sampler);
382
383 }
384
385 static void init( void )
386 {
387 struct pipe_framebuffer_state fb;
388 struct pipe_resource templat;
389 int i;
390
391 /* It's hard to say whether window or screen should be created
392 * first. Different environments would prefer one or the other.
393 *
394 * Also, no easy way of querying supported formats if the screen
395 * cannot be created first.
396 */
397 for (i = 0;
398 window == NULL && formats[i] != PIPE_FORMAT_NONE;
399 i++) {
400
401 screen = graw_create_window_and_screen(0,0,WIDTH,HEIGHT,
402 formats[i],
403 &window);
404 }
405
406 ctx = screen->context_create(screen, NULL);
407 if (ctx == NULL)
408 exit(3);
409
410 templat.target = PIPE_TEXTURE_2D;
411 templat.format = formats[i];
412 templat.width0 = WIDTH;
413 templat.height0 = HEIGHT;
414 templat.depth0 = 1;
415 templat.last_level = 0;
416 templat.nr_samples = 1;
417 templat.bind = (PIPE_BIND_RENDER_TARGET |
418 PIPE_BIND_DISPLAY_TARGET);
419
420 rttex = screen->resource_create(screen,
421 &templat);
422 if (rttex == NULL)
423 exit(4);
424
425 surf = screen->get_tex_surface(screen, rttex, 0, 0, 0,
426 PIPE_BIND_RENDER_TARGET |
427 PIPE_BIND_DISPLAY_TARGET);
428 if (surf == NULL)
429 exit(5);
430
431 memset(&fb, 0, sizeof fb);
432 fb.nr_cbufs = 1;
433 fb.width = WIDTH;
434 fb.height = HEIGHT;
435 fb.cbufs[0] = surf;
436
437 ctx->set_framebuffer_state(ctx, &fb);
438
439 {
440 struct pipe_blend_state blend;
441 void *handle;
442 memset(&blend, 0, sizeof blend);
443 blend.rt[0].colormask = PIPE_MASK_RGBA;
444 handle = ctx->create_blend_state(ctx, &blend);
445 ctx->bind_blend_state(ctx, handle);
446 }
447
448 {
449 struct pipe_depth_stencil_alpha_state depthstencil;
450 void *handle;
451 memset(&depthstencil, 0, sizeof depthstencil);
452 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
453 ctx->bind_depth_stencil_alpha_state(ctx, handle);
454 }
455
456 {
457 struct pipe_rasterizer_state rasterizer;
458 void *handle;
459 memset(&rasterizer, 0, sizeof rasterizer);
460 rasterizer.cull_face = PIPE_FACE_NONE;
461 rasterizer.point_size = 8.0;
462 rasterizer.gl_rasterization_rules = 1;
463 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
464 ctx->bind_rasterizer_state(ctx, handle);
465 }
466
467 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
468
469 init_tex();
470 init_fs_constbuf();
471
472 set_vertices();
473 set_vertex_shader();
474 set_fragment_shader();
475 }
476
477 static void args(int argc, char *argv[])
478 {
479 int i;
480
481 for (i = 1; i < argc; i++) {
482 if (strcmp(argv[i], "-fps") == 0) {
483 show_fps = 1;
484 }
485 else if (i == argc - 1) {
486 filename = argv[i];
487 }
488 else {
489 usage(argv[0]);
490 exit(1);
491 }
492 }
493
494 if (!filename) {
495 usage(argv[0]);
496 exit(1);
497 }
498 }
499
500 int main( int argc, char *argv[] )
501 {
502 args(argc,argv);
503 init();
504
505 graw_set_display_func( draw );
506 graw_main_loop();
507 return 0;
508 }