1bf215546Sopenharmony_ci/* Display a cleared blue window.  This demo has no dependencies on
2bf215546Sopenharmony_ci * any utility code, just the graw interface and gallium.
3bf215546Sopenharmony_ci */
4bf215546Sopenharmony_ci
5bf215546Sopenharmony_ci#include <stdio.h>
6bf215546Sopenharmony_ci#include "graw_util.h"
7bf215546Sopenharmony_ci
8bf215546Sopenharmony_cistatic struct graw_info info;
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_cistatic const int WIDTH = 300;
11bf215546Sopenharmony_cistatic const int HEIGHT = 300;
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_cistruct vertex {
15bf215546Sopenharmony_ci   float position[4];
16bf215546Sopenharmony_ci   float color[4];
17bf215546Sopenharmony_ci};
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_cistatic boolean FlatShade = FALSE;
20bf215546Sopenharmony_ci
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_cistatic struct vertex vertices[3] =
23bf215546Sopenharmony_ci{
24bf215546Sopenharmony_ci   {
25bf215546Sopenharmony_ci      { 0.0f, -0.9f, 0.0f, 1.0f },
26bf215546Sopenharmony_ci      { 1.0f, 0.0f, 0.0f, 1.0f }
27bf215546Sopenharmony_ci   },
28bf215546Sopenharmony_ci   {
29bf215546Sopenharmony_ci      { -0.9f, 0.9f, 0.0f, 1.0f },
30bf215546Sopenharmony_ci      { 0.0f, 1.0f, 0.0f, 1.0f }
31bf215546Sopenharmony_ci   },
32bf215546Sopenharmony_ci   {
33bf215546Sopenharmony_ci      { 0.9f, 0.9f, 0.0f, 1.0f },
34bf215546Sopenharmony_ci      { 0.0f, 0.0f, 1.0f, 1.0f }
35bf215546Sopenharmony_ci   }
36bf215546Sopenharmony_ci};
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistatic void set_vertices( void )
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   struct pipe_vertex_element ve[2];
42bf215546Sopenharmony_ci   struct pipe_vertex_buffer vbuf;
43bf215546Sopenharmony_ci   void *handle;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   memset(ve, 0, sizeof ve);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   ve[0].src_offset = Offset(struct vertex, position);
48bf215546Sopenharmony_ci   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
49bf215546Sopenharmony_ci   ve[1].src_offset = Offset(struct vertex, color);
50bf215546Sopenharmony_ci   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
53bf215546Sopenharmony_ci   info.ctx->bind_vertex_elements_state(info.ctx, handle);
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   memset(&vbuf, 0, sizeof vbuf);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   vbuf.stride = sizeof( struct vertex );
58bf215546Sopenharmony_ci   vbuf.buffer_offset = 0;
59bf215546Sopenharmony_ci   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
60bf215546Sopenharmony_ci                                              PIPE_BIND_VERTEX_BUFFER,
61bf215546Sopenharmony_ci                                              PIPE_USAGE_DEFAULT,
62bf215546Sopenharmony_ci                                              sizeof(vertices),
63bf215546Sopenharmony_ci                                              vertices);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cistatic void set_vertex_shader( void )
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   void *handle;
72bf215546Sopenharmony_ci   const char *text =
73bf215546Sopenharmony_ci      "VERT\n"
74bf215546Sopenharmony_ci      "DCL IN[0]\n"
75bf215546Sopenharmony_ci      "DCL IN[1]\n"
76bf215546Sopenharmony_ci      "DCL OUT[0], POSITION\n"
77bf215546Sopenharmony_ci      "DCL OUT[1], COLOR\n"
78bf215546Sopenharmony_ci      "  0: MOV OUT[1], IN[1]\n"
79bf215546Sopenharmony_ci      "  1: MOV OUT[0], IN[0]\n"
80bf215546Sopenharmony_ci      "  2: END\n";
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   handle = graw_parse_vertex_shader(info.ctx, text);
83bf215546Sopenharmony_ci   info.ctx->bind_vs_state(info.ctx, handle);
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_cistatic void set_fragment_shader( void )
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci   void *handle;
90bf215546Sopenharmony_ci   const char *text =
91bf215546Sopenharmony_ci      "FRAG\n"
92bf215546Sopenharmony_ci      "DCL IN[0], COLOR, LINEAR\n"
93bf215546Sopenharmony_ci      "DCL OUT[0], COLOR\n"
94bf215546Sopenharmony_ci      "  0: MOV OUT[0], IN[0]\n"
95bf215546Sopenharmony_ci      "  1: END\n";
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   handle = graw_parse_fragment_shader(info.ctx, text);
98bf215546Sopenharmony_ci   info.ctx->bind_fs_state(info.ctx, handle);
99bf215546Sopenharmony_ci}
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_cistatic void draw( void )
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   union pipe_color_union clear_color = { {1,0,1,1} };
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
107bf215546Sopenharmony_ci   util_draw_arrays(info.ctx, PIPE_PRIM_TRIANGLES, 0, 3);
108bf215546Sopenharmony_ci   info.ctx->flush(info.ctx, NULL, 0);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   graw_util_flush_front(&info);
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic void init( void )
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
119bf215546Sopenharmony_ci      exit(1);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   graw_util_default_state(&info, FALSE);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   {
124bf215546Sopenharmony_ci      struct pipe_rasterizer_state rasterizer;
125bf215546Sopenharmony_ci      void *handle;
126bf215546Sopenharmony_ci      memset(&rasterizer, 0, sizeof rasterizer);
127bf215546Sopenharmony_ci      rasterizer.cull_face = PIPE_FACE_NONE;
128bf215546Sopenharmony_ci      rasterizer.half_pixel_center = 1;
129bf215546Sopenharmony_ci      rasterizer.bottom_edge_rule = 1;
130bf215546Sopenharmony_ci      rasterizer.flatshade = FlatShade;
131bf215546Sopenharmony_ci      rasterizer.depth_clip_near = 1;
132bf215546Sopenharmony_ci      rasterizer.depth_clip_far = 1;
133bf215546Sopenharmony_ci      handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
134bf215546Sopenharmony_ci      info.ctx->bind_rasterizer_state(info.ctx, handle);
135bf215546Sopenharmony_ci   }
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   set_vertices();
141bf215546Sopenharmony_ci   set_vertex_shader();
142bf215546Sopenharmony_ci   set_fragment_shader();
143bf215546Sopenharmony_ci}
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cistatic void args(int argc, char *argv[])
146bf215546Sopenharmony_ci{
147bf215546Sopenharmony_ci   int i;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   for (i = 1; i < argc; ) {
150bf215546Sopenharmony_ci      if (graw_parse_args(&i, argc, argv)) {
151bf215546Sopenharmony_ci         /* ok */
152bf215546Sopenharmony_ci      }
153bf215546Sopenharmony_ci      else if (strcmp(argv[i], "-f") == 0) {
154bf215546Sopenharmony_ci         FlatShade = TRUE;
155bf215546Sopenharmony_ci         i++;
156bf215546Sopenharmony_ci      }
157bf215546Sopenharmony_ci      else {
158bf215546Sopenharmony_ci         printf("Invalid arg %s\n", argv[i]);
159bf215546Sopenharmony_ci         exit(1);
160bf215546Sopenharmony_ci      }
161bf215546Sopenharmony_ci   }
162bf215546Sopenharmony_ci}
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ciint main( int argc, char *argv[] )
165bf215546Sopenharmony_ci{
166bf215546Sopenharmony_ci   args(argc, argv);
167bf215546Sopenharmony_ci   init();
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci   graw_set_display_func( draw );
170bf215546Sopenharmony_ci   graw_main_loop();
171bf215546Sopenharmony_ci   return 0;
172bf215546Sopenharmony_ci}
173