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