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