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_inlines.h"
15#include "util/u_memory.h"      /* Offset() */
16#include "util/u_draw_quad.h"
17#include "util/u_box.h"
18
19static const char *filename = NULL;
20unsigned show_fps = 0;
21
22
23static 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
34enum pipe_format formats[] = {
35   PIPE_FORMAT_RGBA8888_UNORM,
36   PIPE_FORMAT_BGRA8888_UNORM,
37   PIPE_FORMAT_NONE
38};
39
40static const int WIDTH = 250;
41static const int HEIGHT = 250;
42
43static struct pipe_screen *screen = NULL;
44static struct pipe_context *ctx = NULL;
45static struct pipe_resource *rttex = NULL;
46static struct pipe_resource *constbuf = NULL;
47static struct pipe_surface *surf = NULL;
48static struct pipe_sampler_view *sv = NULL;
49static void *sampler = NULL;
50static void *window = NULL;
51static struct pipe_resource *samptex = NULL;
52
53struct vertex {
54   float position[4];
55   float color[3];
56};
57
58/* Draw a regular mesh
59 */
60#define MESH_SZ 16
61static struct vertex vertices[MESH_SZ * MESH_SZ];
62
63static 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
80static void init_fs_constbuf( void )
81{
82   struct pipe_resource templat;
83   struct pipe_box box;
84
85   memset(&templat, 0, sizeof(templat));
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.array_size = 1;
92   templat.last_level = 0;
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->buffer_subdata(ctx, constbuf,
104                       PIPE_TRANSFER_WRITE,
105                       0, sizeof(constants), constants);
106
107   pipe_set_constant_buffer(ctx,
108                            PIPE_SHADER_VERTEX, 0,
109                            constbuf);
110}
111
112
113static void set_viewport( float x, float y,
114                          float width, float height,
115                          float zNear, float zFar)
116{
117   float z = zFar;
118   float half_width = (float)width / 2.0f;
119   float half_height = (float)height / 2.0f;
120   float half_depth = ((float)zFar - (float)zNear) / 2.0f;
121   struct pipe_viewport_state vp;
122
123   vp.scale[0] = half_width;
124   vp.scale[1] = half_height;
125   vp.scale[2] = half_depth;
126
127   vp.translate[0] = half_width + x;
128   vp.translate[1] = half_height + y;
129   vp.translate[2] = half_depth + z;
130
131   ctx->set_viewport_states( ctx, 0, 1, &vp );
132}
133
134static void set_vertices( void )
135{
136   struct pipe_vertex_element ve[2];
137   struct pipe_vertex_buffer vbuf;
138   void *handle;
139   int x,y;
140
141   memset(ve, 0, sizeof ve);
142
143   ve[0].src_offset = Offset(struct vertex, position);
144   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
145   ve[1].src_offset = Offset(struct vertex, color);
146   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
147
148   handle = ctx->create_vertex_elements_state(ctx, 2, ve);
149   ctx->bind_vertex_elements_state(ctx, handle);
150
151   for (x = 0; x < MESH_SZ; x++) {
152      for (y = 0; y < MESH_SZ; y++) {
153         int i = y * MESH_SZ + x;
154         vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0;
155         vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0;
156         vertices[i].position[2] = 0;
157         vertices[i].position[3] = 1.0;
158
159         vertices[i].color[0] = .5;
160         vertices[i].color[1] = (float)x / (float)MESH_SZ;
161         vertices[i].color[2] = (float)y / (float)MESH_SZ;
162      }
163   }
164
165   memset(&vbuf, 0, sizeof vbuf);
166
167   vbuf.stride = sizeof( struct vertex );
168   vbuf.buffer_offset = 0;
169   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
170                                              PIPE_BIND_VERTEX_BUFFER,
171                                              PIPE_USAGE_DEFAULT,
172                                              sizeof(vertices),
173                                              vertices);
174
175   ctx->set_vertex_buffers(ctx, 0, 1, &vbuf);
176}
177
178static void set_vertex_shader( void )
179{
180   FILE *f;
181   char buf[50000];
182   void *handle;
183   int sz;
184
185   if ((f = fopen(filename, "r")) == NULL) {
186      fprintf(stderr, "Couldn't open %s\n", filename);
187      exit(1);
188   }
189
190   sz = fread(buf, 1, sizeof(buf), f);
191   if (!feof(f)) {
192      printf("file too long\n");
193      exit(1);
194   }
195   printf("%.*s\n", sz, buf);
196   buf[sz] = 0;
197
198   handle = graw_parse_vertex_shader(ctx, buf);
199   ctx->bind_vs_state(ctx, handle);
200   fclose(f);
201}
202
203static void set_fragment_shader( void )
204{
205   void *handle;
206   const char *text =
207      "FRAG\n"
208      "DCL IN[0], COLOR, LINEAR\n"
209      "DCL OUT[0], COLOR\n"
210      "  0: MOV OUT[0], IN[0]\n"
211      "  1: END\n";
212
213   handle = graw_parse_fragment_shader(ctx, text);
214   ctx->bind_fs_state(ctx, handle);
215}
216
217
218
219static void draw( void )
220{
221   union pipe_color_union clear_color = { {.1,.3,.5,0} };
222
223   ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
224   util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, ARRAY_SIZE(vertices));
225   ctx->flush(ctx, NULL, 0);
226
227   graw_save_surface_to_file(ctx, surf, NULL);
228
229   screen->flush_frontbuffer(screen, rttex, 0, 0, window, NULL);
230}
231
232#define SIZE 16
233
234static void init_tex( void )
235{
236   struct pipe_sampler_view sv_template;
237   struct pipe_sampler_state sampler_desc;
238   struct pipe_resource templat;
239   struct pipe_box box;
240   ubyte tex2d[SIZE][SIZE][4];
241   int s, t;
242
243#if (SIZE != 2)
244   for (s = 0; s < SIZE; s++) {
245      for (t = 0; t < SIZE; t++) {
246         if (0) {
247            int x = (s ^ t) & 1;
248	    tex2d[t][s][0] = (x) ? 0 : 63;
249	    tex2d[t][s][1] = (x) ? 0 : 128;
250	    tex2d[t][s][2] = 0;
251	    tex2d[t][s][3] = 0xff;
252         }
253         else {
254            int x = ((s ^ t) >> 2) & 1;
255	    tex2d[t][s][0] = s*255/(SIZE-1);
256	    tex2d[t][s][1] = t*255/(SIZE-1);
257	    tex2d[t][s][2] = (x) ? 0 : 128;
258	    tex2d[t][s][3] = 0xff;
259         }
260      }
261   }
262#else
263   tex2d[0][0][0] = 0;
264   tex2d[0][0][1] = 255;
265   tex2d[0][0][2] = 255;
266   tex2d[0][0][3] = 0;
267
268   tex2d[0][1][0] = 0;
269   tex2d[0][1][1] = 0;
270   tex2d[0][1][2] = 255;
271   tex2d[0][1][3] = 255;
272
273   tex2d[1][0][0] = 255;
274   tex2d[1][0][1] = 255;
275   tex2d[1][0][2] = 0;
276   tex2d[1][0][3] = 255;
277
278   tex2d[1][1][0] = 255;
279   tex2d[1][1][1] = 0;
280   tex2d[1][1][2] = 0;
281   tex2d[1][1][3] = 255;
282#endif
283
284   memset(&templat, 0, sizeof(templat));
285   templat.target = PIPE_TEXTURE_2D;
286   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
287   templat.width0 = SIZE;
288   templat.height0 = SIZE;
289   templat.depth0 = 1;
290   templat.array_size = 1;
291   templat.last_level = 0;
292   templat.bind = PIPE_BIND_SAMPLER_VIEW;
293
294
295   samptex = screen->resource_create(screen,
296                                 &templat);
297   if (samptex == NULL)
298      exit(4);
299
300   u_box_2d(0,0,SIZE,SIZE, &box);
301
302   ctx->texture_subdata(ctx,
303                        samptex,
304                        0,
305                        PIPE_TRANSFER_WRITE,
306                        &box,
307                        tex2d,
308                        sizeof tex2d[0],
309                        sizeof tex2d);
310
311   /* Possibly read back & compare against original data:
312    */
313   if (0)
314   {
315      struct pipe_transfer *t;
316      uint32_t *ptr;
317      ptr = pipe_transfer_map(ctx, samptex,
318                              0, 0, /* level, layer */
319                              PIPE_TRANSFER_READ,
320                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
321
322      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
323         assert(0);
324         exit(9);
325      }
326
327      ctx->transfer_unmap(ctx, t);
328   }
329
330   memset(&sv_template, 0, sizeof sv_template);
331   sv_template.format = samptex->format;
332   sv_template.texture = samptex;
333   sv_template.swizzle_r = 0;
334   sv_template.swizzle_g = 1;
335   sv_template.swizzle_b = 2;
336   sv_template.swizzle_a = 3;
337   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
338   if (sv == NULL)
339      exit(5);
340
341   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sv);
342
343
344   memset(&sampler_desc, 0, sizeof sampler_desc);
345   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
346   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
347   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
348   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
349   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
350   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
351   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
352   sampler_desc.compare_func = 0;
353   sampler_desc.normalized_coords = 1;
354   sampler_desc.max_anisotropy = 0;
355
356   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
357   if (sampler == NULL)
358      exit(6);
359
360   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
361
362}
363
364static void init( void )
365{
366   struct pipe_framebuffer_state fb;
367   struct pipe_resource templat;
368   struct pipe_surface surf_tmpl;
369   int i;
370
371   /* It's hard to say whether window or screen should be created
372    * first.  Different environments would prefer one or the other.
373    *
374    * Also, no easy way of querying supported formats if the screen
375    * cannot be created first.
376    */
377   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
378      screen = graw_create_window_and_screen(0, 0, 300, 300,
379                                             formats[i],
380                                             &window);
381      if (window && screen)
382         break;
383   }
384   if (!screen || !window) {
385      fprintf(stderr, "Unable to create window\n");
386      exit(1);
387   }
388
389   ctx = screen->context_create(screen, NULL, 0);
390   if (ctx == NULL)
391      exit(3);
392
393   memset(&templat, 0, sizeof(templat));
394   templat.target = PIPE_TEXTURE_2D;
395   templat.format = formats[i];
396   templat.width0 = WIDTH;
397   templat.height0 = HEIGHT;
398   templat.depth0 = 1;
399   templat.array_size = 1;
400   templat.last_level = 0;
401   templat.bind = (PIPE_BIND_RENDER_TARGET |
402                   PIPE_BIND_DISPLAY_TARGET);
403
404   rttex = screen->resource_create(screen,
405                                 &templat);
406   if (rttex == NULL)
407      exit(4);
408
409   surf_tmpl.format = templat.format;
410   surf_tmpl.u.tex.level = 0;
411   surf_tmpl.u.tex.first_layer = 0;
412   surf_tmpl.u.tex.last_layer = 0;
413   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
414   if (surf == NULL)
415      exit(5);
416
417   memset(&fb, 0, sizeof fb);
418   fb.nr_cbufs = 1;
419   fb.width = WIDTH;
420   fb.height = HEIGHT;
421   fb.cbufs[0] = surf;
422
423   ctx->set_framebuffer_state(ctx, &fb);
424
425   {
426      struct pipe_blend_state blend;
427      void *handle;
428      memset(&blend, 0, sizeof blend);
429      blend.rt[0].colormask = PIPE_MASK_RGBA;
430      handle = ctx->create_blend_state(ctx, &blend);
431      ctx->bind_blend_state(ctx, handle);
432   }
433
434   {
435      struct pipe_depth_stencil_alpha_state depthstencil;
436      void *handle;
437      memset(&depthstencil, 0, sizeof depthstencil);
438      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
439      ctx->bind_depth_stencil_alpha_state(ctx, handle);
440   }
441
442   {
443      struct pipe_rasterizer_state rasterizer;
444      void *handle;
445      memset(&rasterizer, 0, sizeof rasterizer);
446      rasterizer.cull_face = PIPE_FACE_NONE;
447      rasterizer.point_size = 8.0;
448      rasterizer.half_pixel_center = 1;
449      rasterizer.bottom_edge_rule = 1;
450      rasterizer.depth_clip_near = 1;
451      rasterizer.depth_clip_far = 1;
452      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
453      ctx->bind_rasterizer_state(ctx, handle);
454   }
455
456   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
457
458   init_tex();
459   init_fs_constbuf();
460
461   set_vertices();
462   set_vertex_shader();
463   set_fragment_shader();
464}
465
466static void args(int argc, char *argv[])
467{
468   int i;
469
470   for (i = 1; i < argc;) {
471      if (graw_parse_args(&i, argc, argv)) {
472         continue;
473      }
474      if (strcmp(argv[i], "-fps") == 0) {
475         show_fps = 1;
476         i++;
477      }
478      else if (i == argc - 1) {
479         filename = argv[i];
480         i++;
481      }
482      else {
483         usage(argv[0]);
484         exit(1);
485      }
486   }
487
488   if (!filename) {
489      usage(argv[0]);
490      exit(1);
491   }
492}
493
494int main( int argc, char *argv[] )
495{
496   args(argc,argv);
497   init();
498
499   graw_set_display_func( draw );
500   graw_main_loop();
501   return 0;
502}
503