13464ebd5Sriastradh/* Display a cleared blue window.  This demo has no dependencies on
23464ebd5Sriastradh * any utility code, just the graw interface and gallium.
33464ebd5Sriastradh */
43464ebd5Sriastradh
53464ebd5Sriastradh#include <stdio.h>
6af69d88dSmrg#include "graw_util.h"
73464ebd5Sriastradh
8af69d88dSmrgstatic struct graw_info info;
93464ebd5Sriastradh
103464ebd5Sriastradhstatic const int WIDTH = 300;
113464ebd5Sriastradhstatic const int HEIGHT = 300;
123464ebd5Sriastradh
133464ebd5Sriastradh
143464ebd5Sriastradhstruct vertex {
153464ebd5Sriastradh   float position[4];
163464ebd5Sriastradh   float color[4];
173464ebd5Sriastradh};
183464ebd5Sriastradh
19af69d88dSmrgstatic boolean FlatShade = FALSE;
20af69d88dSmrg
21af69d88dSmrg
223464ebd5Sriastradhstatic struct vertex vertices[3] =
233464ebd5Sriastradh{
243464ebd5Sriastradh   {
253464ebd5Sriastradh      { 0.0f, -0.9f, 0.0f, 1.0f },
263464ebd5Sriastradh      { 1.0f, 0.0f, 0.0f, 1.0f }
273464ebd5Sriastradh   },
283464ebd5Sriastradh   {
293464ebd5Sriastradh      { -0.9f, 0.9f, 0.0f, 1.0f },
303464ebd5Sriastradh      { 0.0f, 1.0f, 0.0f, 1.0f }
313464ebd5Sriastradh   },
323464ebd5Sriastradh   {
333464ebd5Sriastradh      { 0.9f, 0.9f, 0.0f, 1.0f },
343464ebd5Sriastradh      { 0.0f, 0.0f, 1.0f, 1.0f }
353464ebd5Sriastradh   }
363464ebd5Sriastradh};
373464ebd5Sriastradh
383464ebd5Sriastradh
393464ebd5Sriastradhstatic void set_vertices( void )
403464ebd5Sriastradh{
413464ebd5Sriastradh   struct pipe_vertex_element ve[2];
423464ebd5Sriastradh   struct pipe_vertex_buffer vbuf;
433464ebd5Sriastradh   void *handle;
443464ebd5Sriastradh
453464ebd5Sriastradh   memset(ve, 0, sizeof ve);
463464ebd5Sriastradh
473464ebd5Sriastradh   ve[0].src_offset = Offset(struct vertex, position);
483464ebd5Sriastradh   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
493464ebd5Sriastradh   ve[1].src_offset = Offset(struct vertex, color);
503464ebd5Sriastradh   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
513464ebd5Sriastradh
52af69d88dSmrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
53af69d88dSmrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
543464ebd5Sriastradh
55af69d88dSmrg   memset(&vbuf, 0, sizeof vbuf);
563464ebd5Sriastradh
573464ebd5Sriastradh   vbuf.stride = sizeof( struct vertex );
583464ebd5Sriastradh   vbuf.buffer_offset = 0;
5901e04c3fSmrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
60af69d88dSmrg                                              PIPE_BIND_VERTEX_BUFFER,
61af69d88dSmrg                                              PIPE_USAGE_DEFAULT,
62af69d88dSmrg                                              sizeof(vertices),
63af69d88dSmrg                                              vertices);
643464ebd5Sriastradh
657ec681f3Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
663464ebd5Sriastradh}
673464ebd5Sriastradh
68af69d88dSmrg
693464ebd5Sriastradhstatic void set_vertex_shader( void )
703464ebd5Sriastradh{
713464ebd5Sriastradh   void *handle;
723464ebd5Sriastradh   const char *text =
733464ebd5Sriastradh      "VERT\n"
743464ebd5Sriastradh      "DCL IN[0]\n"
753464ebd5Sriastradh      "DCL IN[1]\n"
763464ebd5Sriastradh      "DCL OUT[0], POSITION\n"
773464ebd5Sriastradh      "DCL OUT[1], COLOR\n"
783464ebd5Sriastradh      "  0: MOV OUT[1], IN[1]\n"
793464ebd5Sriastradh      "  1: MOV OUT[0], IN[0]\n"
803464ebd5Sriastradh      "  2: END\n";
813464ebd5Sriastradh
82af69d88dSmrg   handle = graw_parse_vertex_shader(info.ctx, text);
83af69d88dSmrg   info.ctx->bind_vs_state(info.ctx, handle);
843464ebd5Sriastradh}
853464ebd5Sriastradh
86af69d88dSmrg
873464ebd5Sriastradhstatic void set_fragment_shader( void )
883464ebd5Sriastradh{
893464ebd5Sriastradh   void *handle;
903464ebd5Sriastradh   const char *text =
913464ebd5Sriastradh      "FRAG\n"
923464ebd5Sriastradh      "DCL IN[0], COLOR, LINEAR\n"
933464ebd5Sriastradh      "DCL OUT[0], COLOR\n"
943464ebd5Sriastradh      "  0: MOV OUT[0], IN[0]\n"
953464ebd5Sriastradh      "  1: END\n";
963464ebd5Sriastradh
97af69d88dSmrg   handle = graw_parse_fragment_shader(info.ctx, text);
98af69d88dSmrg   info.ctx->bind_fs_state(info.ctx, handle);
993464ebd5Sriastradh}
1003464ebd5Sriastradh
1013464ebd5Sriastradh
1023464ebd5Sriastradhstatic void draw( void )
1033464ebd5Sriastradh{
104af69d88dSmrg   union pipe_color_union clear_color = { {1,0,1,1} };
1053464ebd5Sriastradh
1067ec681f3Smrg   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
107af69d88dSmrg   util_draw_arrays(info.ctx, PIPE_PRIM_TRIANGLES, 0, 3);
108af69d88dSmrg   info.ctx->flush(info.ctx, NULL, 0);
1093464ebd5Sriastradh
110af69d88dSmrg   graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
1113464ebd5Sriastradh
112af69d88dSmrg   graw_util_flush_front(&info);
1133464ebd5Sriastradh}
1143464ebd5Sriastradh
1153464ebd5Sriastradh
1163464ebd5Sriastradhstatic void init( void )
1173464ebd5Sriastradh{
118af69d88dSmrg   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
1193464ebd5Sriastradh      exit(1);
1203464ebd5Sriastradh
121af69d88dSmrg   graw_util_default_state(&info, FALSE);
1223464ebd5Sriastradh
1233464ebd5Sriastradh   {
1243464ebd5Sriastradh      struct pipe_rasterizer_state rasterizer;
1253464ebd5Sriastradh      void *handle;
1263464ebd5Sriastradh      memset(&rasterizer, 0, sizeof rasterizer);
1273464ebd5Sriastradh      rasterizer.cull_face = PIPE_FACE_NONE;
128af69d88dSmrg      rasterizer.half_pixel_center = 1;
129af69d88dSmrg      rasterizer.bottom_edge_rule = 1;
130af69d88dSmrg      rasterizer.flatshade = FlatShade;
13101e04c3fSmrg      rasterizer.depth_clip_near = 1;
13201e04c3fSmrg      rasterizer.depth_clip_far = 1;
133af69d88dSmrg      handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
134af69d88dSmrg      info.ctx->bind_rasterizer_state(info.ctx, handle);
1353464ebd5Sriastradh   }
1363464ebd5Sriastradh
137af69d88dSmrg
138af69d88dSmrg   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
139af69d88dSmrg
1403464ebd5Sriastradh   set_vertices();
1413464ebd5Sriastradh   set_vertex_shader();
1423464ebd5Sriastradh   set_fragment_shader();
1433464ebd5Sriastradh}
1443464ebd5Sriastradh
1453464ebd5Sriastradhstatic void args(int argc, char *argv[])
1463464ebd5Sriastradh{
1473464ebd5Sriastradh   int i;
1483464ebd5Sriastradh
149af69d88dSmrg   for (i = 1; i < argc; ) {
1503464ebd5Sriastradh      if (graw_parse_args(&i, argc, argv)) {
151af69d88dSmrg         /* ok */
152af69d88dSmrg      }
153af69d88dSmrg      else if (strcmp(argv[i], "-f") == 0) {
154af69d88dSmrg         FlatShade = TRUE;
155af69d88dSmrg         i++;
156af69d88dSmrg      }
157af69d88dSmrg      else {
158af69d88dSmrg         printf("Invalid arg %s\n", argv[i]);
159af69d88dSmrg         exit(1);
1603464ebd5Sriastradh      }
1613464ebd5Sriastradh   }
1623464ebd5Sriastradh}
1633464ebd5Sriastradh
1643464ebd5Sriastradhint main( int argc, char *argv[] )
1653464ebd5Sriastradh{
1663464ebd5Sriastradh   args(argc, argv);
1673464ebd5Sriastradh   init();
1683464ebd5Sriastradh
1693464ebd5Sriastradh   graw_set_display_func( draw );
1703464ebd5Sriastradh   graw_main_loop();
1713464ebd5Sriastradh   return 0;
1723464ebd5Sriastradh}
173