13464ebd5Sriastradh/* Display a cleared blue window.  This demo has no dependencies on
23464ebd5Sriastradh * any utility code, just the graw interface and gallium.
33464ebd5Sriastradh */
43464ebd5Sriastradh
5af69d88dSmrg#include "graw_util.h"
63464ebd5Sriastradh
73464ebd5Sriastradhstatic const int WIDTH = 300;
83464ebd5Sriastradhstatic const int HEIGHT = 300;
93464ebd5Sriastradh
10af69d88dSmrgstatic struct graw_info info;
11af69d88dSmrg
12af69d88dSmrg
13af69d88dSmrgstatic struct pipe_resource *texture = NULL;
143464ebd5Sriastradhstatic struct pipe_sampler_view *sv = NULL;
153464ebd5Sriastradhstatic void *sampler = NULL;
163464ebd5Sriastradh
173464ebd5Sriastradhstruct vertex {
183464ebd5Sriastradh   float position[4];
193464ebd5Sriastradh   float color[4];
203464ebd5Sriastradh};
213464ebd5Sriastradh
223464ebd5Sriastradhstatic struct vertex vertices[] =
233464ebd5Sriastradh{
243464ebd5Sriastradh   { { 0.9, -0.9, 0.0, 1.0 },
253464ebd5Sriastradh     { 1, 0, 0, 1 } },
263464ebd5Sriastradh
273464ebd5Sriastradh   { { 0.9,  0.9, 0.0, 1.0 },
283464ebd5Sriastradh     { 1, 1, 0, 1 } },
293464ebd5Sriastradh
303464ebd5Sriastradh   { {-0.9,  0.9, 0.0, 1.0 },
313464ebd5Sriastradh     { 0, 1, 0, 1 } },
323464ebd5Sriastradh
333464ebd5Sriastradh   { {-0.9,  -0.9, 0.0, 1.0 },
343464ebd5Sriastradh     { 0, 0, 0, 1 } },
353464ebd5Sriastradh};
363464ebd5Sriastradh
373464ebd5Sriastradh
383464ebd5Sriastradh
393464ebd5Sriastradh
403464ebd5Sriastradhstatic void set_vertices( void )
413464ebd5Sriastradh{
423464ebd5Sriastradh   struct pipe_vertex_element ve[2];
433464ebd5Sriastradh   struct pipe_vertex_buffer vbuf;
443464ebd5Sriastradh   void *handle;
453464ebd5Sriastradh
463464ebd5Sriastradh   memset(ve, 0, sizeof ve);
473464ebd5Sriastradh
483464ebd5Sriastradh   ve[0].src_offset = Offset(struct vertex, position);
493464ebd5Sriastradh   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
503464ebd5Sriastradh   ve[1].src_offset = Offset(struct vertex, color);
513464ebd5Sriastradh   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
523464ebd5Sriastradh
53af69d88dSmrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
54af69d88dSmrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
553464ebd5Sriastradh
56af69d88dSmrg   memset(&vbuf, 0, sizeof vbuf);
573464ebd5Sriastradh
583464ebd5Sriastradh   vbuf.stride = sizeof( struct vertex );
593464ebd5Sriastradh   vbuf.buffer_offset = 0;
6001e04c3fSmrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
61af69d88dSmrg                                              PIPE_BIND_VERTEX_BUFFER,
62af69d88dSmrg                                              PIPE_USAGE_DEFAULT,
63af69d88dSmrg                                              sizeof(vertices),
64af69d88dSmrg                                              vertices);
653464ebd5Sriastradh
667ec681f3Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
673464ebd5Sriastradh}
683464ebd5Sriastradh
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], GENERIC[0]\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
863464ebd5Sriastradhstatic void set_fragment_shader( void )
873464ebd5Sriastradh{
883464ebd5Sriastradh   void *handle;
893464ebd5Sriastradh   const char *text =
903464ebd5Sriastradh      "FRAG\n"
913464ebd5Sriastradh      "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
923464ebd5Sriastradh      "DCL OUT[0], COLOR\n"
933464ebd5Sriastradh      "DCL TEMP[0]\n"
943464ebd5Sriastradh      "DCL SAMP[0]\n"
9501e04c3fSmrg      "DCL SVIEW[0], 2D, FLOAT\n"
963464ebd5Sriastradh      "  0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
973464ebd5Sriastradh      "  1: MOV OUT[0], TEMP[0]\n"
983464ebd5Sriastradh      "  2: END\n";
993464ebd5Sriastradh
100af69d88dSmrg   handle = graw_parse_fragment_shader(info.ctx, text);
101af69d88dSmrg   info.ctx->bind_fs_state(info.ctx, handle);
1023464ebd5Sriastradh}
1033464ebd5Sriastradh
1043464ebd5Sriastradh
1053464ebd5Sriastradhstatic void draw( void )
1063464ebd5Sriastradh{
107af69d88dSmrg   union pipe_color_union clear_color = { {.5,.5,.5,1} };
1083464ebd5Sriastradh
1097ec681f3Smrg   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
110af69d88dSmrg   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
111af69d88dSmrg   info.ctx->flush(info.ctx, NULL, 0);
1123464ebd5Sriastradh
113af69d88dSmrg   graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
1143464ebd5Sriastradh
115af69d88dSmrg   graw_util_flush_front(&info);
1163464ebd5Sriastradh}
1173464ebd5Sriastradh
118af69d88dSmrg
1193464ebd5Sriastradh#define SIZE 16
1203464ebd5Sriastradh
1213464ebd5Sriastradhstatic void init_tex( void )
1223464ebd5Sriastradh{
1233464ebd5Sriastradh   ubyte tex2d[SIZE][SIZE][4];
1243464ebd5Sriastradh   int s, t;
1253464ebd5Sriastradh
1263464ebd5Sriastradh#if (SIZE != 2)
1273464ebd5Sriastradh   for (s = 0; s < SIZE; s++) {
1283464ebd5Sriastradh      for (t = 0; t < SIZE; t++) {
1293464ebd5Sriastradh         if (0) {
1303464ebd5Sriastradh            int x = (s ^ t) & 1;
1313464ebd5Sriastradh	    tex2d[t][s][0] = (x) ? 0 : 63;
1323464ebd5Sriastradh	    tex2d[t][s][1] = (x) ? 0 : 128;
1333464ebd5Sriastradh	    tex2d[t][s][2] = 0;
1343464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
1353464ebd5Sriastradh         }
1363464ebd5Sriastradh         else {
1373464ebd5Sriastradh            int x = ((s ^ t) >> 2) & 1;
1383464ebd5Sriastradh	    tex2d[t][s][0] = s*255/(SIZE-1);
1393464ebd5Sriastradh	    tex2d[t][s][1] = t*255/(SIZE-1);
1403464ebd5Sriastradh	    tex2d[t][s][2] = (x) ? 0 : 128;
1413464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
1423464ebd5Sriastradh         }
1433464ebd5Sriastradh      }
1443464ebd5Sriastradh   }
1453464ebd5Sriastradh#else
1463464ebd5Sriastradh   tex2d[0][0][0] = 0;
1473464ebd5Sriastradh   tex2d[0][0][1] = 255;
1483464ebd5Sriastradh   tex2d[0][0][2] = 255;
1493464ebd5Sriastradh   tex2d[0][0][3] = 0;
1503464ebd5Sriastradh
1513464ebd5Sriastradh   tex2d[0][1][0] = 0;
1523464ebd5Sriastradh   tex2d[0][1][1] = 0;
1533464ebd5Sriastradh   tex2d[0][1][2] = 255;
1543464ebd5Sriastradh   tex2d[0][1][3] = 255;
1553464ebd5Sriastradh
1563464ebd5Sriastradh   tex2d[1][0][0] = 255;
1573464ebd5Sriastradh   tex2d[1][0][1] = 255;
1583464ebd5Sriastradh   tex2d[1][0][2] = 0;
1593464ebd5Sriastradh   tex2d[1][0][3] = 255;
1603464ebd5Sriastradh
1613464ebd5Sriastradh   tex2d[1][1][0] = 255;
1623464ebd5Sriastradh   tex2d[1][1][1] = 0;
1633464ebd5Sriastradh   tex2d[1][1][2] = 0;
1643464ebd5Sriastradh   tex2d[1][1][3] = 255;
1653464ebd5Sriastradh#endif
1663464ebd5Sriastradh
167af69d88dSmrg   texture = graw_util_create_tex2d(&info, SIZE, SIZE,
168af69d88dSmrg                                    PIPE_FORMAT_B8G8R8A8_UNORM, tex2d);
1693464ebd5Sriastradh
170af69d88dSmrg   sv = graw_util_create_simple_sampler_view(&info, texture);
1717ec681f3Smrg   info.ctx->set_sampler_views(info.ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv);
1723464ebd5Sriastradh
173af69d88dSmrg   sampler = graw_util_create_simple_sampler(&info,
174af69d88dSmrg                                             PIPE_TEX_WRAP_REPEAT,
175af69d88dSmrg                                             PIPE_TEX_FILTER_NEAREST);
176af69d88dSmrg   info.ctx->bind_sampler_states(info.ctx, PIPE_SHADER_FRAGMENT,
177af69d88dSmrg                                 0, 1, &sampler);
1783464ebd5Sriastradh}
1793464ebd5Sriastradh
180af69d88dSmrg
1813464ebd5Sriastradhstatic void init( void )
1823464ebd5Sriastradh{
183af69d88dSmrg   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
1843464ebd5Sriastradh      exit(1);
1853464ebd5Sriastradh
186af69d88dSmrg   graw_util_default_state(&info, FALSE);
1873464ebd5Sriastradh
1883464ebd5Sriastradh   {
1893464ebd5Sriastradh      struct pipe_rasterizer_state rasterizer;
1903464ebd5Sriastradh      void *handle;
1913464ebd5Sriastradh      memset(&rasterizer, 0, sizeof rasterizer);
1923464ebd5Sriastradh      rasterizer.cull_face = PIPE_FACE_NONE;
193af69d88dSmrg      rasterizer.half_pixel_center = 1;
194af69d88dSmrg      rasterizer.bottom_edge_rule = 1;
19501e04c3fSmrg      rasterizer.depth_clip_near = 1;
19601e04c3fSmrg      rasterizer.depth_clip_far = 1;
197af69d88dSmrg      handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
198af69d88dSmrg      info.ctx->bind_rasterizer_state(info.ctx, handle);
1993464ebd5Sriastradh   }
2003464ebd5Sriastradh
201af69d88dSmrg   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
2023464ebd5Sriastradh
2033464ebd5Sriastradh   init_tex();
2043464ebd5Sriastradh
2053464ebd5Sriastradh   set_vertices();
2063464ebd5Sriastradh   set_vertex_shader();
2073464ebd5Sriastradh   set_fragment_shader();
2083464ebd5Sriastradh}
2093464ebd5Sriastradh
210af69d88dSmrg
2113464ebd5Sriastradhstatic void args(int argc, char *argv[])
2123464ebd5Sriastradh{
2133464ebd5Sriastradh   int i;
2143464ebd5Sriastradh
2153464ebd5Sriastradh   for (i = 1; i < argc;) {
2163464ebd5Sriastradh      if (graw_parse_args(&i, argc, argv)) {
2173464ebd5Sriastradh         continue;
2183464ebd5Sriastradh      }
2193464ebd5Sriastradh      exit(1);
2203464ebd5Sriastradh   }
2213464ebd5Sriastradh}
2223464ebd5Sriastradh
2233464ebd5Sriastradhint main( int argc, char *argv[] )
2243464ebd5Sriastradh{
2253464ebd5Sriastradh   args(argc, argv);
2263464ebd5Sriastradh   init();
2273464ebd5Sriastradh
2283464ebd5Sriastradh   graw_set_display_func( draw );
2293464ebd5Sriastradh   graw_main_loop();
2303464ebd5Sriastradh   return 0;
2313464ebd5Sriastradh}
232