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 "frontend/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_MAP_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   vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
132   vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
133   vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
134   vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
135
136   ctx->set_viewport_states( ctx, 0, 1, &vp );
137}
138
139static void set_vertices( void )
140{
141   struct pipe_vertex_element ve[2];
142   struct pipe_vertex_buffer vbuf;
143   void *handle;
144   int x,y;
145
146   memset(ve, 0, sizeof ve);
147
148   ve[0].src_offset = Offset(struct vertex, position);
149   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
150   ve[1].src_offset = Offset(struct vertex, color);
151   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
152
153   handle = ctx->create_vertex_elements_state(ctx, 2, ve);
154   ctx->bind_vertex_elements_state(ctx, handle);
155
156   for (x = 0; x < MESH_SZ; x++) {
157      for (y = 0; y < MESH_SZ; y++) {
158         int i = y * MESH_SZ + x;
159         vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0;
160         vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0;
161         vertices[i].position[2] = 0;
162         vertices[i].position[3] = 1.0;
163
164         vertices[i].color[0] = .5;
165         vertices[i].color[1] = (float)x / (float)MESH_SZ;
166         vertices[i].color[2] = (float)y / (float)MESH_SZ;
167      }
168   }
169
170   memset(&vbuf, 0, sizeof vbuf);
171
172   vbuf.stride = sizeof( struct vertex );
173   vbuf.buffer_offset = 0;
174   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
175                                              PIPE_BIND_VERTEX_BUFFER,
176                                              PIPE_USAGE_DEFAULT,
177                                              sizeof(vertices),
178                                              vertices);
179
180   ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);
181}
182
183static 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
208static 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
224static void draw( void )
225{
226   union pipe_color_union clear_color = { {.1,.3,.5,0} };
227
228   ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
229   util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, ARRAY_SIZE(vertices));
230   ctx->flush(ctx, NULL, 0);
231
232   graw_save_surface_to_file(ctx, surf, NULL);
233
234   screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);
235}
236
237#define SIZE 16
238
239static void init_tex( void )
240{
241   struct pipe_sampler_view sv_template;
242   struct pipe_sampler_state sampler_desc;
243   struct pipe_resource templat;
244   struct pipe_box box;
245   ubyte tex2d[SIZE][SIZE][4];
246   int s, t;
247
248#if (SIZE != 2)
249   for (s = 0; s < SIZE; s++) {
250      for (t = 0; t < SIZE; t++) {
251         if (0) {
252            int x = (s ^ t) & 1;
253	    tex2d[t][s][0] = (x) ? 0 : 63;
254	    tex2d[t][s][1] = (x) ? 0 : 128;
255	    tex2d[t][s][2] = 0;
256	    tex2d[t][s][3] = 0xff;
257         }
258         else {
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;
264         }
265      }
266   }
267#else
268   tex2d[0][0][0] = 0;
269   tex2d[0][0][1] = 255;
270   tex2d[0][0][2] = 255;
271   tex2d[0][0][3] = 0;
272
273   tex2d[0][1][0] = 0;
274   tex2d[0][1][1] = 0;
275   tex2d[0][1][2] = 255;
276   tex2d[0][1][3] = 255;
277
278   tex2d[1][0][0] = 255;
279   tex2d[1][0][1] = 255;
280   tex2d[1][0][2] = 0;
281   tex2d[1][0][3] = 255;
282
283   tex2d[1][1][0] = 255;
284   tex2d[1][1][1] = 0;
285   tex2d[1][1][2] = 0;
286   tex2d[1][1][3] = 255;
287#endif
288
289   memset(&templat, 0, sizeof(templat));
290   templat.target = PIPE_TEXTURE_2D;
291   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
292   templat.width0 = SIZE;
293   templat.height0 = SIZE;
294   templat.depth0 = 1;
295   templat.array_size = 1;
296   templat.last_level = 0;
297   templat.bind = PIPE_BIND_SAMPLER_VIEW;
298
299
300   samptex = screen->resource_create(screen,
301                                 &templat);
302   if (samptex == NULL)
303      exit(4);
304
305   u_box_2d(0,0,SIZE,SIZE, &box);
306
307   ctx->texture_subdata(ctx,
308                        samptex,
309                        0,
310                        PIPE_MAP_WRITE,
311                        &box,
312                        tex2d,
313                        sizeof tex2d[0],
314                        sizeof tex2d);
315
316   /* Possibly read back & compare against original data:
317    */
318   if (0)
319   {
320      struct pipe_transfer *t;
321      uint32_t *ptr;
322      ptr = pipe_texture_map(ctx, samptex,
323                              0, 0, /* level, layer */
324                              PIPE_MAP_READ,
325                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
326
327      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
328         assert(0);
329         exit(9);
330      }
331
332      ctx->texture_unmap(ctx, t);
333   }
334
335   memset(&sv_template, 0, sizeof sv_template);
336   sv_template.format = samptex->format;
337   sv_template.texture = samptex;
338   sv_template.swizzle_r = 0;
339   sv_template.swizzle_g = 1;
340   sv_template.swizzle_b = 2;
341   sv_template.swizzle_a = 3;
342   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
343   if (sv == NULL)
344      exit(5);
345
346   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv);
347
348
349   memset(&sampler_desc, 0, sizeof sampler_desc);
350   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
351   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
352   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
353   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
354   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
355   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
356   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
357   sampler_desc.compare_func = 0;
358   sampler_desc.normalized_coords = 1;
359   sampler_desc.max_anisotropy = 0;
360
361   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
362   if (sampler == NULL)
363      exit(6);
364
365   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
366
367}
368
369static void init( void )
370{
371   struct pipe_framebuffer_state fb;
372   struct pipe_resource templat;
373   struct pipe_surface surf_tmpl;
374   int i;
375
376   /* It's hard to say whether window or screen should be created
377    * first.  Different environments would prefer one or the other.
378    *
379    * Also, no easy way of querying supported formats if the screen
380    * cannot be created first.
381    */
382   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
383      screen = graw_create_window_and_screen(0, 0, 300, 300,
384                                             formats[i],
385                                             &window);
386      if (window && screen)
387         break;
388   }
389   if (!screen || !window) {
390      fprintf(stderr, "Unable to create window\n");
391      exit(1);
392   }
393
394   ctx = screen->context_create(screen, NULL, 0);
395   if (ctx == NULL)
396      exit(3);
397
398   memset(&templat, 0, sizeof(templat));
399   templat.target = PIPE_TEXTURE_2D;
400   templat.format = formats[i];
401   templat.width0 = WIDTH;
402   templat.height0 = HEIGHT;
403   templat.depth0 = 1;
404   templat.array_size = 1;
405   templat.last_level = 0;
406   templat.bind = (PIPE_BIND_RENDER_TARGET |
407                   PIPE_BIND_DISPLAY_TARGET);
408
409   rttex = screen->resource_create(screen,
410                                 &templat);
411   if (rttex == NULL)
412      exit(4);
413
414   surf_tmpl.format = templat.format;
415   surf_tmpl.u.tex.level = 0;
416   surf_tmpl.u.tex.first_layer = 0;
417   surf_tmpl.u.tex.last_layer = 0;
418   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
419   if (surf == NULL)
420      exit(5);
421
422   memset(&fb, 0, sizeof fb);
423   fb.nr_cbufs = 1;
424   fb.width = WIDTH;
425   fb.height = HEIGHT;
426   fb.cbufs[0] = surf;
427
428   ctx->set_framebuffer_state(ctx, &fb);
429
430   {
431      struct pipe_blend_state blend;
432      void *handle;
433      memset(&blend, 0, sizeof blend);
434      blend.rt[0].colormask = PIPE_MASK_RGBA;
435      handle = ctx->create_blend_state(ctx, &blend);
436      ctx->bind_blend_state(ctx, handle);
437   }
438
439   {
440      struct pipe_depth_stencil_alpha_state depthstencil;
441      void *handle;
442      memset(&depthstencil, 0, sizeof depthstencil);
443      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
444      ctx->bind_depth_stencil_alpha_state(ctx, handle);
445   }
446
447   {
448      struct pipe_rasterizer_state rasterizer;
449      void *handle;
450      memset(&rasterizer, 0, sizeof rasterizer);
451      rasterizer.cull_face = PIPE_FACE_NONE;
452      rasterizer.point_size = 8.0;
453      rasterizer.half_pixel_center = 1;
454      rasterizer.bottom_edge_rule = 1;
455      rasterizer.depth_clip_near = 1;
456      rasterizer.depth_clip_far = 1;
457      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
458      ctx->bind_rasterizer_state(ctx, handle);
459   }
460
461   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
462
463   init_tex();
464   init_fs_constbuf();
465
466   set_vertices();
467   set_vertex_shader();
468   set_fragment_shader();
469}
470
471static void args(int argc, char *argv[])
472{
473   int i;
474
475   for (i = 1; i < argc;) {
476      if (graw_parse_args(&i, argc, argv)) {
477         continue;
478      }
479      if (strcmp(argv[i], "-fps") == 0) {
480         show_fps = 1;
481         i++;
482      }
483      else if (i == argc - 1) {
484         filename = argv[i];
485         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
499int 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}
508