1848b8605Smrg/* Display a cleared blue window.  This demo has no dependencies on
2848b8605Smrg * any utility code, just the graw interface and gallium.
3848b8605Smrg */
4848b8605Smrg
5848b8605Smrg#include "graw_util.h"
6848b8605Smrg
7848b8605Smrgstatic const int WIDTH = 300;
8848b8605Smrgstatic const int HEIGHT = 300;
9848b8605Smrg
10848b8605Smrgstatic struct graw_info info;
11848b8605Smrg
12848b8605Smrg
13848b8605Smrgstatic struct pipe_resource *texture = NULL;
14848b8605Smrgstatic struct pipe_sampler_view *sv = NULL;
15848b8605Smrgstatic void *sampler = NULL;
16848b8605Smrg
17848b8605Smrgstruct vertex {
18848b8605Smrg   float position[4];
19848b8605Smrg   float color[4];
20848b8605Smrg};
21848b8605Smrg
22848b8605Smrgstatic struct vertex vertices[] =
23848b8605Smrg{
24848b8605Smrg   { { 0.9, -0.9, 0.0, 1.0 },
25848b8605Smrg     { 1, 0, 0, 1 } },
26848b8605Smrg
27848b8605Smrg   { { 0.9,  0.9, 0.0, 1.0 },
28848b8605Smrg     { 1, 1, 0, 1 } },
29848b8605Smrg
30848b8605Smrg   { {-0.9,  0.9, 0.0, 1.0 },
31848b8605Smrg     { 0, 1, 0, 1 } },
32848b8605Smrg
33848b8605Smrg   { {-0.9,  -0.9, 0.0, 1.0 },
34848b8605Smrg     { 0, 0, 0, 1 } },
35848b8605Smrg};
36848b8605Smrg
37848b8605Smrg
38848b8605Smrg
39848b8605Smrg
40848b8605Smrgstatic void set_vertices( void )
41848b8605Smrg{
42848b8605Smrg   struct pipe_vertex_element ve[2];
43848b8605Smrg   struct pipe_vertex_buffer vbuf;
44848b8605Smrg   void *handle;
45848b8605Smrg
46848b8605Smrg   memset(ve, 0, sizeof ve);
47848b8605Smrg
48848b8605Smrg   ve[0].src_offset = Offset(struct vertex, position);
49848b8605Smrg   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
50848b8605Smrg   ve[1].src_offset = Offset(struct vertex, color);
51848b8605Smrg   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
52848b8605Smrg
53848b8605Smrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
54848b8605Smrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
55848b8605Smrg
56848b8605Smrg   memset(&vbuf, 0, sizeof vbuf);
57848b8605Smrg
58848b8605Smrg   vbuf.stride = sizeof( struct vertex );
59848b8605Smrg   vbuf.buffer_offset = 0;
60b8e80941Smrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
61848b8605Smrg                                              PIPE_BIND_VERTEX_BUFFER,
62848b8605Smrg                                              PIPE_USAGE_DEFAULT,
63848b8605Smrg                                              sizeof(vertices),
64848b8605Smrg                                              vertices);
65848b8605Smrg
66848b8605Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
67848b8605Smrg}
68848b8605Smrg
69848b8605Smrgstatic void set_vertex_shader( void )
70848b8605Smrg{
71848b8605Smrg   void *handle;
72848b8605Smrg   const char *text =
73848b8605Smrg      "VERT\n"
74848b8605Smrg      "DCL IN[0]\n"
75848b8605Smrg      "DCL IN[1]\n"
76848b8605Smrg      "DCL OUT[0], POSITION\n"
77848b8605Smrg      "DCL OUT[1], GENERIC[0]\n"
78848b8605Smrg      "  0: MOV OUT[1], IN[1]\n"
79848b8605Smrg      "  1: MOV OUT[0], IN[0]\n"
80848b8605Smrg      "  2: END\n";
81848b8605Smrg
82848b8605Smrg   handle = graw_parse_vertex_shader(info.ctx, text);
83848b8605Smrg   info.ctx->bind_vs_state(info.ctx, handle);
84848b8605Smrg}
85848b8605Smrg
86848b8605Smrgstatic void set_fragment_shader( void )
87848b8605Smrg{
88848b8605Smrg   void *handle;
89848b8605Smrg   const char *text =
90848b8605Smrg      "FRAG\n"
91848b8605Smrg      "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
92848b8605Smrg      "DCL OUT[0], COLOR\n"
93848b8605Smrg      "DCL TEMP[0]\n"
94848b8605Smrg      "DCL SAMP[0]\n"
95b8e80941Smrg      "DCL SVIEW[0], 2D, FLOAT\n"
96848b8605Smrg      "  0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
97848b8605Smrg      "  1: MOV OUT[0], TEMP[0]\n"
98848b8605Smrg      "  2: END\n";
99848b8605Smrg
100848b8605Smrg   handle = graw_parse_fragment_shader(info.ctx, text);
101848b8605Smrg   info.ctx->bind_fs_state(info.ctx, handle);
102848b8605Smrg}
103848b8605Smrg
104848b8605Smrg
105848b8605Smrgstatic void draw( void )
106848b8605Smrg{
107848b8605Smrg   union pipe_color_union clear_color = { {.5,.5,.5,1} };
108848b8605Smrg
109848b8605Smrg   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
110848b8605Smrg   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
111848b8605Smrg   info.ctx->flush(info.ctx, NULL, 0);
112848b8605Smrg
113848b8605Smrg   graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
114848b8605Smrg
115848b8605Smrg   graw_util_flush_front(&info);
116848b8605Smrg}
117848b8605Smrg
118848b8605Smrg
119848b8605Smrg#define SIZE 16
120848b8605Smrg
121848b8605Smrgstatic void init_tex( void )
122848b8605Smrg{
123848b8605Smrg   ubyte tex2d[SIZE][SIZE][4];
124848b8605Smrg   int s, t;
125848b8605Smrg
126848b8605Smrg#if (SIZE != 2)
127848b8605Smrg   for (s = 0; s < SIZE; s++) {
128848b8605Smrg      for (t = 0; t < SIZE; t++) {
129848b8605Smrg         if (0) {
130848b8605Smrg            int x = (s ^ t) & 1;
131848b8605Smrg	    tex2d[t][s][0] = (x) ? 0 : 63;
132848b8605Smrg	    tex2d[t][s][1] = (x) ? 0 : 128;
133848b8605Smrg	    tex2d[t][s][2] = 0;
134848b8605Smrg	    tex2d[t][s][3] = 0xff;
135848b8605Smrg         }
136848b8605Smrg         else {
137848b8605Smrg            int x = ((s ^ t) >> 2) & 1;
138848b8605Smrg	    tex2d[t][s][0] = s*255/(SIZE-1);
139848b8605Smrg	    tex2d[t][s][1] = t*255/(SIZE-1);
140848b8605Smrg	    tex2d[t][s][2] = (x) ? 0 : 128;
141848b8605Smrg	    tex2d[t][s][3] = 0xff;
142848b8605Smrg         }
143848b8605Smrg      }
144848b8605Smrg   }
145848b8605Smrg#else
146848b8605Smrg   tex2d[0][0][0] = 0;
147848b8605Smrg   tex2d[0][0][1] = 255;
148848b8605Smrg   tex2d[0][0][2] = 255;
149848b8605Smrg   tex2d[0][0][3] = 0;
150848b8605Smrg
151848b8605Smrg   tex2d[0][1][0] = 0;
152848b8605Smrg   tex2d[0][1][1] = 0;
153848b8605Smrg   tex2d[0][1][2] = 255;
154848b8605Smrg   tex2d[0][1][3] = 255;
155848b8605Smrg
156848b8605Smrg   tex2d[1][0][0] = 255;
157848b8605Smrg   tex2d[1][0][1] = 255;
158848b8605Smrg   tex2d[1][0][2] = 0;
159848b8605Smrg   tex2d[1][0][3] = 255;
160848b8605Smrg
161848b8605Smrg   tex2d[1][1][0] = 255;
162848b8605Smrg   tex2d[1][1][1] = 0;
163848b8605Smrg   tex2d[1][1][2] = 0;
164848b8605Smrg   tex2d[1][1][3] = 255;
165848b8605Smrg#endif
166848b8605Smrg
167848b8605Smrg   texture = graw_util_create_tex2d(&info, SIZE, SIZE,
168848b8605Smrg                                    PIPE_FORMAT_B8G8R8A8_UNORM, tex2d);
169848b8605Smrg
170848b8605Smrg   sv = graw_util_create_simple_sampler_view(&info, texture);
171848b8605Smrg   info.ctx->set_sampler_views(info.ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sv);
172848b8605Smrg
173848b8605Smrg   sampler = graw_util_create_simple_sampler(&info,
174848b8605Smrg                                             PIPE_TEX_WRAP_REPEAT,
175848b8605Smrg                                             PIPE_TEX_FILTER_NEAREST);
176848b8605Smrg   info.ctx->bind_sampler_states(info.ctx, PIPE_SHADER_FRAGMENT,
177848b8605Smrg                                 0, 1, &sampler);
178848b8605Smrg}
179848b8605Smrg
180848b8605Smrg
181848b8605Smrgstatic void init( void )
182848b8605Smrg{
183848b8605Smrg   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
184848b8605Smrg      exit(1);
185848b8605Smrg
186848b8605Smrg   graw_util_default_state(&info, FALSE);
187848b8605Smrg
188848b8605Smrg   {
189848b8605Smrg      struct pipe_rasterizer_state rasterizer;
190848b8605Smrg      void *handle;
191848b8605Smrg      memset(&rasterizer, 0, sizeof rasterizer);
192848b8605Smrg      rasterizer.cull_face = PIPE_FACE_NONE;
193848b8605Smrg      rasterizer.half_pixel_center = 1;
194848b8605Smrg      rasterizer.bottom_edge_rule = 1;
195b8e80941Smrg      rasterizer.depth_clip_near = 1;
196b8e80941Smrg      rasterizer.depth_clip_far = 1;
197848b8605Smrg      handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
198848b8605Smrg      info.ctx->bind_rasterizer_state(info.ctx, handle);
199848b8605Smrg   }
200848b8605Smrg
201848b8605Smrg   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
202848b8605Smrg
203848b8605Smrg   init_tex();
204848b8605Smrg
205848b8605Smrg   set_vertices();
206848b8605Smrg   set_vertex_shader();
207848b8605Smrg   set_fragment_shader();
208848b8605Smrg}
209848b8605Smrg
210848b8605Smrg
211848b8605Smrgstatic void args(int argc, char *argv[])
212848b8605Smrg{
213848b8605Smrg   int i;
214848b8605Smrg
215848b8605Smrg   for (i = 1; i < argc;) {
216848b8605Smrg      if (graw_parse_args(&i, argc, argv)) {
217848b8605Smrg         continue;
218848b8605Smrg      }
219848b8605Smrg      exit(1);
220848b8605Smrg   }
221848b8605Smrg}
222848b8605Smrg
223848b8605Smrgint main( int argc, char *argv[] )
224848b8605Smrg{
225848b8605Smrg   args(argc, argv);
226848b8605Smrg   init();
227848b8605Smrg
228848b8605Smrg   graw_set_display_func( draw );
229848b8605Smrg   graw_main_loop();
230848b8605Smrg   return 0;
231848b8605Smrg}
232