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 "frontend/graw.h"
6bf215546Sopenharmony_ci#include "pipe/p_screen.h"
7bf215546Sopenharmony_ci#include "pipe/p_context.h"
8bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h"
9bf215546Sopenharmony_ci#include "pipe/p_state.h"
10bf215546Sopenharmony_ci#include "pipe/p_defines.h"
11bf215546Sopenharmony_ci#include <stdio.h>              /* for fread(), etc */
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci#include "util/u_inlines.h"
14bf215546Sopenharmony_ci#include "util/u_memory.h"      /* Offset() */
15bf215546Sopenharmony_ci#include "util/u_draw_quad.h"
16bf215546Sopenharmony_ci#include "util/u_box.h"
17bf215546Sopenharmony_ci
18bf215546Sopenharmony_cistatic const char *filename = NULL;
19bf215546Sopenharmony_ciunsigned show_fps = 0;
20bf215546Sopenharmony_ci
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_cistatic void usage(char *name)
23bf215546Sopenharmony_ci{
24bf215546Sopenharmony_ci   fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
25bf215546Sopenharmony_ci#ifndef _WIN32
26bf215546Sopenharmony_ci   fprintf(stderr, "\n" );
27bf215546Sopenharmony_ci   fprintf(stderr, "options:\n");
28bf215546Sopenharmony_ci   fprintf(stderr, "    -fps  show frames per second\n");
29bf215546Sopenharmony_ci#endif
30bf215546Sopenharmony_ci}
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cienum pipe_format formats[] = {
34bf215546Sopenharmony_ci   PIPE_FORMAT_RGBA8888_UNORM,
35bf215546Sopenharmony_ci   PIPE_FORMAT_BGRA8888_UNORM,
36bf215546Sopenharmony_ci   PIPE_FORMAT_NONE
37bf215546Sopenharmony_ci};
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistatic const int WIDTH = 250;
40bf215546Sopenharmony_cistatic const int HEIGHT = 250;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cistatic struct pipe_screen *screen = NULL;
43bf215546Sopenharmony_cistatic struct pipe_context *ctx = NULL;
44bf215546Sopenharmony_cistatic struct pipe_resource *rttex = NULL;
45bf215546Sopenharmony_cistatic struct pipe_surface *surf = NULL;
46bf215546Sopenharmony_cistatic struct pipe_sampler_view *sv = NULL;
47bf215546Sopenharmony_cistatic void *sampler = NULL;
48bf215546Sopenharmony_cistatic void *window = NULL;
49bf215546Sopenharmony_cistatic struct pipe_resource *samptex = NULL;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cistruct vertex {
52bf215546Sopenharmony_ci   float position[4];
53bf215546Sopenharmony_ci   float color[4];
54bf215546Sopenharmony_ci   float texcoord[4];
55bf215546Sopenharmony_ci};
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci/* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
58bf215546Sopenharmony_ci * so that the final images are the same.
59bf215546Sopenharmony_ci */
60bf215546Sopenharmony_cistatic struct vertex vertices[] =
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   { { 0.9, 0.9, 0.0, 1.0 },
63bf215546Sopenharmony_ci     { 0, 0, 1, 1 },
64bf215546Sopenharmony_ci     { 1, 1, 0, 1 } },
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   { { 0.9,  -0.9, 0.0, 1.0 },
67bf215546Sopenharmony_ci     { 1, 0, 0, 1 },
68bf215546Sopenharmony_ci     { 1, -1, 0, 1 } },
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   { {-0.9,  0.0, 0.0, 1.0 },
71bf215546Sopenharmony_ci     { 0, 1, 0, 1 },
72bf215546Sopenharmony_ci     { -1, 0, 0, 1 } },
73bf215546Sopenharmony_ci};
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic float constants1[] =
76bf215546Sopenharmony_ci{  0.4, 0, 0,  1,
77bf215546Sopenharmony_ci   1,   1, 1,  1,
78bf215546Sopenharmony_ci   2,   2, 2,  2,
79bf215546Sopenharmony_ci   4,   8, 16, 32,
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   3,  0, 0, 0,
82bf215546Sopenharmony_ci   0, .5, 0, 0,
83bf215546Sopenharmony_ci   1,  0, 0, 1,
84bf215546Sopenharmony_ci   0,  0, 0, 1,
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   1, 0, 0, 0.5,
87bf215546Sopenharmony_ci   0, 1, 0, 0.5,
88bf215546Sopenharmony_ci   0, 0, 1, 0,
89bf215546Sopenharmony_ci   0, 0, 0, 1,
90bf215546Sopenharmony_ci};
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_cistatic float constants2[] =
94bf215546Sopenharmony_ci{  1, 0, 0,  1,
95bf215546Sopenharmony_ci   0, 1, 0,  1,
96bf215546Sopenharmony_ci   0, 0, 1,  1,
97bf215546Sopenharmony_ci   0, 0, 0,  0,
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   1,  1, 0, 1,
100bf215546Sopenharmony_ci   1, .5, 0, 1,
101bf215546Sopenharmony_ci   1,  0, 0, 1,
102bf215546Sopenharmony_ci   0,  0, 0, 1,
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   1, 0, 0, 0.5,
105bf215546Sopenharmony_ci   0, 1, 0, 0.5,
106bf215546Sopenharmony_ci   0, 0, 1, 0,
107bf215546Sopenharmony_ci   0, 0, 0, 1,
108bf215546Sopenharmony_ci};
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_cistatic void init_fs_constbuf( void )
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   struct pipe_constant_buffer cb1;
113bf215546Sopenharmony_ci   struct pipe_constant_buffer cb2;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   memset(&cb1, 0, sizeof cb1);
116bf215546Sopenharmony_ci   cb1.buffer_size = sizeof constants1;
117bf215546Sopenharmony_ci   cb1.user_buffer = constants1;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   ctx->set_constant_buffer(ctx,
120bf215546Sopenharmony_ci                            PIPE_SHADER_FRAGMENT, 0, false,
121bf215546Sopenharmony_ci                            &cb1);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   memset(&cb2, 0, sizeof cb2);
124bf215546Sopenharmony_ci   cb2.buffer_size = sizeof constants2;
125bf215546Sopenharmony_ci   cb2.user_buffer = constants2;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   ctx->set_constant_buffer(ctx,
128bf215546Sopenharmony_ci                            PIPE_SHADER_FRAGMENT, 1, false,
129bf215546Sopenharmony_ci                            &cb2);
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_cistatic void set_viewport( float x, float y,
134bf215546Sopenharmony_ci                          float width, float height,
135bf215546Sopenharmony_ci                          float zNear, float zFar)
136bf215546Sopenharmony_ci{
137bf215546Sopenharmony_ci   float z = zFar;
138bf215546Sopenharmony_ci   float half_width = (float)width / 2.0f;
139bf215546Sopenharmony_ci   float half_height = (float)height / 2.0f;
140bf215546Sopenharmony_ci   float half_depth = ((float)zFar - (float)zNear) / 2.0f;
141bf215546Sopenharmony_ci   struct pipe_viewport_state vp;
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   vp.scale[0] = half_width;
144bf215546Sopenharmony_ci   vp.scale[1] = half_height;
145bf215546Sopenharmony_ci   vp.scale[2] = half_depth;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   vp.translate[0] = half_width + x;
148bf215546Sopenharmony_ci   vp.translate[1] = half_height + y;
149bf215546Sopenharmony_ci   vp.translate[2] = half_depth + z;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
152bf215546Sopenharmony_ci   vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
153bf215546Sopenharmony_ci   vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
154bf215546Sopenharmony_ci   vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   ctx->set_viewport_states( ctx, 0, 1, &vp );
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistatic void set_vertices( void )
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   struct pipe_vertex_element ve[3];
162bf215546Sopenharmony_ci   struct pipe_vertex_buffer vbuf;
163bf215546Sopenharmony_ci   void *handle;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   memset(ve, 0, sizeof ve);
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   ve[0].src_offset = Offset(struct vertex, position);
168bf215546Sopenharmony_ci   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
169bf215546Sopenharmony_ci   ve[1].src_offset = Offset(struct vertex, color);
170bf215546Sopenharmony_ci   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
171bf215546Sopenharmony_ci   ve[2].src_offset = Offset(struct vertex, texcoord);
172bf215546Sopenharmony_ci   ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   handle = ctx->create_vertex_elements_state(ctx, 3, ve);
175bf215546Sopenharmony_ci   ctx->bind_vertex_elements_state(ctx, handle);
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   memset(&vbuf, 0, sizeof vbuf);
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   vbuf.stride = sizeof( struct vertex );
180bf215546Sopenharmony_ci   vbuf.buffer_offset = 0;
181bf215546Sopenharmony_ci   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
182bf215546Sopenharmony_ci                                              PIPE_BIND_VERTEX_BUFFER,
183bf215546Sopenharmony_ci                                              PIPE_USAGE_DEFAULT,
184bf215546Sopenharmony_ci                                              sizeof(vertices),
185bf215546Sopenharmony_ci                                              vertices);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistatic void set_vertex_shader( void )
191bf215546Sopenharmony_ci{
192bf215546Sopenharmony_ci   void *handle;
193bf215546Sopenharmony_ci   const char *text =
194bf215546Sopenharmony_ci      "VERT\n"
195bf215546Sopenharmony_ci      "DCL IN[0]\n"
196bf215546Sopenharmony_ci      "DCL IN[1]\n"
197bf215546Sopenharmony_ci      "DCL IN[2]\n"
198bf215546Sopenharmony_ci      "DCL OUT[0], POSITION\n"
199bf215546Sopenharmony_ci      "DCL OUT[1], COLOR[0]\n"
200bf215546Sopenharmony_ci      "DCL OUT[2], GENERIC[0]\n"
201bf215546Sopenharmony_ci      "  MOV OUT[0], IN[0]\n"
202bf215546Sopenharmony_ci      "  MOV OUT[1], IN[1]\n"
203bf215546Sopenharmony_ci      "  MOV OUT[2], IN[2]\n"
204bf215546Sopenharmony_ci      "  END\n";
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   handle = graw_parse_vertex_shader(ctx, text);
207bf215546Sopenharmony_ci   ctx->bind_vs_state(ctx, handle);
208bf215546Sopenharmony_ci}
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_cistatic void set_fragment_shader( const char *filename )
211bf215546Sopenharmony_ci{
212bf215546Sopenharmony_ci   FILE *f;
213bf215546Sopenharmony_ci   char buf[50000];
214bf215546Sopenharmony_ci   void *handle;
215bf215546Sopenharmony_ci   int sz;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   if ((f = fopen(filename, "r")) == NULL) {
218bf215546Sopenharmony_ci      fprintf(stderr, "Couldn't open %s\n", filename);
219bf215546Sopenharmony_ci      exit(1);
220bf215546Sopenharmony_ci   }
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   sz = fread(buf, 1, sizeof(buf), f);
223bf215546Sopenharmony_ci   if (!feof(f)) {
224bf215546Sopenharmony_ci      printf("file too long\n");
225bf215546Sopenharmony_ci      exit(1);
226bf215546Sopenharmony_ci   }
227bf215546Sopenharmony_ci   printf("%.*s\n", sz, buf);
228bf215546Sopenharmony_ci   buf[sz] = 0;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   handle = graw_parse_fragment_shader(ctx, buf);
231bf215546Sopenharmony_ci   ctx->bind_fs_state(ctx, handle);
232bf215546Sopenharmony_ci   fclose(f);
233bf215546Sopenharmony_ci}
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_cistatic void draw( void )
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci   union pipe_color_union clear_color = { {.1,.3,.5,0} };
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci   ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
241bf215546Sopenharmony_ci   util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
242bf215546Sopenharmony_ci   ctx->flush(ctx, NULL, 0);
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci   graw_save_surface_to_file(ctx, surf, NULL);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);
247bf215546Sopenharmony_ci}
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci#define SIZE 16
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_cistatic void init_tex( void )
252bf215546Sopenharmony_ci{
253bf215546Sopenharmony_ci   struct pipe_sampler_view sv_template;
254bf215546Sopenharmony_ci   struct pipe_sampler_state sampler_desc;
255bf215546Sopenharmony_ci   struct pipe_resource templat;
256bf215546Sopenharmony_ci   struct pipe_box box;
257bf215546Sopenharmony_ci   ubyte tex2d[SIZE][SIZE][4];
258bf215546Sopenharmony_ci   int s, t;
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci#if (SIZE != 2)
261bf215546Sopenharmony_ci   for (s = 0; s < SIZE; s++) {
262bf215546Sopenharmony_ci      for (t = 0; t < SIZE; t++) {
263bf215546Sopenharmony_ci         if (0) {
264bf215546Sopenharmony_ci            int x = (s ^ t) & 1;
265bf215546Sopenharmony_ci	    tex2d[t][s][0] = (x) ? 0 : 63;
266bf215546Sopenharmony_ci	    tex2d[t][s][1] = (x) ? 0 : 128;
267bf215546Sopenharmony_ci	    tex2d[t][s][2] = 0;
268bf215546Sopenharmony_ci	    tex2d[t][s][3] = 0xff;
269bf215546Sopenharmony_ci         }
270bf215546Sopenharmony_ci         else {
271bf215546Sopenharmony_ci            int x = ((s ^ t) >> 2) & 1;
272bf215546Sopenharmony_ci	    tex2d[t][s][0] = s*255/(SIZE-1);
273bf215546Sopenharmony_ci	    tex2d[t][s][1] = t*255/(SIZE-1);
274bf215546Sopenharmony_ci	    tex2d[t][s][2] = (x) ? 0 : 128;
275bf215546Sopenharmony_ci	    tex2d[t][s][3] = 0xff;
276bf215546Sopenharmony_ci         }
277bf215546Sopenharmony_ci      }
278bf215546Sopenharmony_ci   }
279bf215546Sopenharmony_ci#else
280bf215546Sopenharmony_ci   tex2d[0][0][0] = 0;
281bf215546Sopenharmony_ci   tex2d[0][0][1] = 255;
282bf215546Sopenharmony_ci   tex2d[0][0][2] = 255;
283bf215546Sopenharmony_ci   tex2d[0][0][3] = 0;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   tex2d[0][1][0] = 0;
286bf215546Sopenharmony_ci   tex2d[0][1][1] = 0;
287bf215546Sopenharmony_ci   tex2d[0][1][2] = 255;
288bf215546Sopenharmony_ci   tex2d[0][1][3] = 255;
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci   tex2d[1][0][0] = 255;
291bf215546Sopenharmony_ci   tex2d[1][0][1] = 255;
292bf215546Sopenharmony_ci   tex2d[1][0][2] = 0;
293bf215546Sopenharmony_ci   tex2d[1][0][3] = 255;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   tex2d[1][1][0] = 255;
296bf215546Sopenharmony_ci   tex2d[1][1][1] = 0;
297bf215546Sopenharmony_ci   tex2d[1][1][2] = 0;
298bf215546Sopenharmony_ci   tex2d[1][1][3] = 255;
299bf215546Sopenharmony_ci#endif
300bf215546Sopenharmony_ci
301bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
302bf215546Sopenharmony_ci   templat.target = PIPE_TEXTURE_2D;
303bf215546Sopenharmony_ci   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
304bf215546Sopenharmony_ci   templat.width0 = SIZE;
305bf215546Sopenharmony_ci   templat.height0 = SIZE;
306bf215546Sopenharmony_ci   templat.depth0 = 1;
307bf215546Sopenharmony_ci   templat.array_size = 1;
308bf215546Sopenharmony_ci   templat.last_level = 0;
309bf215546Sopenharmony_ci   templat.bind = PIPE_BIND_SAMPLER_VIEW;
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   samptex = screen->resource_create(screen,
313bf215546Sopenharmony_ci                                 &templat);
314bf215546Sopenharmony_ci   if (samptex == NULL)
315bf215546Sopenharmony_ci      exit(4);
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   u_box_2d(0,0,SIZE,SIZE, &box);
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci   ctx->texture_subdata(ctx,
320bf215546Sopenharmony_ci                        samptex,
321bf215546Sopenharmony_ci                        0,
322bf215546Sopenharmony_ci                        PIPE_MAP_WRITE,
323bf215546Sopenharmony_ci                        &box,
324bf215546Sopenharmony_ci                        tex2d,
325bf215546Sopenharmony_ci                        sizeof tex2d[0],
326bf215546Sopenharmony_ci                        sizeof tex2d);
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci   /* Possibly read back & compare against original data:
329bf215546Sopenharmony_ci    */
330bf215546Sopenharmony_ci   if (0)
331bf215546Sopenharmony_ci   {
332bf215546Sopenharmony_ci      struct pipe_transfer *t;
333bf215546Sopenharmony_ci      uint32_t *ptr;
334bf215546Sopenharmony_ci      ptr = pipe_texture_map(ctx, samptex,
335bf215546Sopenharmony_ci                              0, 0, /* level, layer */
336bf215546Sopenharmony_ci                              PIPE_MAP_READ,
337bf215546Sopenharmony_ci                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
340bf215546Sopenharmony_ci         assert(0);
341bf215546Sopenharmony_ci         exit(9);
342bf215546Sopenharmony_ci      }
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci      ctx->texture_unmap(ctx, t);
345bf215546Sopenharmony_ci   }
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   memset(&sv_template, 0, sizeof sv_template);
348bf215546Sopenharmony_ci   sv_template.format = samptex->format;
349bf215546Sopenharmony_ci   sv_template.texture = samptex;
350bf215546Sopenharmony_ci   sv_template.swizzle_r = 0;
351bf215546Sopenharmony_ci   sv_template.swizzle_g = 1;
352bf215546Sopenharmony_ci   sv_template.swizzle_b = 2;
353bf215546Sopenharmony_ci   sv_template.swizzle_a = 3;
354bf215546Sopenharmony_ci   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
355bf215546Sopenharmony_ci   if (sv == NULL)
356bf215546Sopenharmony_ci      exit(5);
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv);
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci   memset(&sampler_desc, 0, sizeof sampler_desc);
362bf215546Sopenharmony_ci   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
363bf215546Sopenharmony_ci   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
364bf215546Sopenharmony_ci   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
365bf215546Sopenharmony_ci   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
366bf215546Sopenharmony_ci   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
367bf215546Sopenharmony_ci   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
368bf215546Sopenharmony_ci   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
369bf215546Sopenharmony_ci   sampler_desc.compare_func = 0;
370bf215546Sopenharmony_ci   sampler_desc.normalized_coords = 1;
371bf215546Sopenharmony_ci   sampler_desc.max_anisotropy = 0;
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
374bf215546Sopenharmony_ci   if (sampler == NULL)
375bf215546Sopenharmony_ci      exit(6);
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
378bf215546Sopenharmony_ci
379bf215546Sopenharmony_ci}
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_cistatic void init( void )
382bf215546Sopenharmony_ci{
383bf215546Sopenharmony_ci   struct pipe_framebuffer_state fb;
384bf215546Sopenharmony_ci   struct pipe_resource templat;
385bf215546Sopenharmony_ci   struct pipe_surface surf_tmpl;
386bf215546Sopenharmony_ci   int i;
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci   /* It's hard to say whether window or screen should be created
389bf215546Sopenharmony_ci    * first.  Different environments would prefer one or the other.
390bf215546Sopenharmony_ci    *
391bf215546Sopenharmony_ci    * Also, no easy way of querying supported formats if the screen
392bf215546Sopenharmony_ci    * cannot be created first.
393bf215546Sopenharmony_ci    */
394bf215546Sopenharmony_ci   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
395bf215546Sopenharmony_ci      screen = graw_create_window_and_screen(0, 0, 300, 300,
396bf215546Sopenharmony_ci                                             formats[i],
397bf215546Sopenharmony_ci                                             &window);
398bf215546Sopenharmony_ci      if (window && screen)
399bf215546Sopenharmony_ci         break;
400bf215546Sopenharmony_ci   }
401bf215546Sopenharmony_ci   if (!screen || !window) {
402bf215546Sopenharmony_ci      fprintf(stderr, "Unable to create window\n");
403bf215546Sopenharmony_ci      exit(1);
404bf215546Sopenharmony_ci   }
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci   ctx = screen->context_create(screen, NULL, 0);
407bf215546Sopenharmony_ci   if (ctx == NULL)
408bf215546Sopenharmony_ci      exit(3);
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
411bf215546Sopenharmony_ci   templat.target = PIPE_TEXTURE_2D;
412bf215546Sopenharmony_ci   templat.format = formats[i];
413bf215546Sopenharmony_ci   templat.width0 = WIDTH;
414bf215546Sopenharmony_ci   templat.height0 = HEIGHT;
415bf215546Sopenharmony_ci   templat.depth0 = 1;
416bf215546Sopenharmony_ci   templat.array_size = 1;
417bf215546Sopenharmony_ci   templat.last_level = 0;
418bf215546Sopenharmony_ci   templat.bind = (PIPE_BIND_RENDER_TARGET |
419bf215546Sopenharmony_ci                   PIPE_BIND_DISPLAY_TARGET);
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci   rttex = screen->resource_create(screen,
422bf215546Sopenharmony_ci                                 &templat);
423bf215546Sopenharmony_ci   if (rttex == NULL)
424bf215546Sopenharmony_ci      exit(4);
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci   surf_tmpl.format = templat.format;
427bf215546Sopenharmony_ci   surf_tmpl.u.tex.level = 0;
428bf215546Sopenharmony_ci   surf_tmpl.u.tex.first_layer = 0;
429bf215546Sopenharmony_ci   surf_tmpl.u.tex.last_layer = 0;
430bf215546Sopenharmony_ci   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
431bf215546Sopenharmony_ci   if (surf == NULL)
432bf215546Sopenharmony_ci      exit(5);
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci   memset(&fb, 0, sizeof fb);
435bf215546Sopenharmony_ci   fb.nr_cbufs = 1;
436bf215546Sopenharmony_ci   fb.width = WIDTH;
437bf215546Sopenharmony_ci   fb.height = HEIGHT;
438bf215546Sopenharmony_ci   fb.cbufs[0] = surf;
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci   ctx->set_framebuffer_state(ctx, &fb);
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_ci   {
443bf215546Sopenharmony_ci      struct pipe_blend_state blend;
444bf215546Sopenharmony_ci      void *handle;
445bf215546Sopenharmony_ci      memset(&blend, 0, sizeof blend);
446bf215546Sopenharmony_ci      blend.rt[0].colormask = PIPE_MASK_RGBA;
447bf215546Sopenharmony_ci      handle = ctx->create_blend_state(ctx, &blend);
448bf215546Sopenharmony_ci      ctx->bind_blend_state(ctx, handle);
449bf215546Sopenharmony_ci   }
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci   {
452bf215546Sopenharmony_ci      struct pipe_depth_stencil_alpha_state depthstencil;
453bf215546Sopenharmony_ci      void *handle;
454bf215546Sopenharmony_ci      memset(&depthstencil, 0, sizeof depthstencil);
455bf215546Sopenharmony_ci      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
456bf215546Sopenharmony_ci      ctx->bind_depth_stencil_alpha_state(ctx, handle);
457bf215546Sopenharmony_ci   }
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_ci   {
460bf215546Sopenharmony_ci      struct pipe_rasterizer_state rasterizer;
461bf215546Sopenharmony_ci      void *handle;
462bf215546Sopenharmony_ci      memset(&rasterizer, 0, sizeof rasterizer);
463bf215546Sopenharmony_ci      rasterizer.cull_face = PIPE_FACE_NONE;
464bf215546Sopenharmony_ci      rasterizer.half_pixel_center = 1;
465bf215546Sopenharmony_ci      rasterizer.bottom_edge_rule = 1;
466bf215546Sopenharmony_ci      rasterizer.depth_clip_near = 1;
467bf215546Sopenharmony_ci      rasterizer.depth_clip_far = 1;
468bf215546Sopenharmony_ci      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
469bf215546Sopenharmony_ci      ctx->bind_rasterizer_state(ctx, handle);
470bf215546Sopenharmony_ci   }
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_ci   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci   init_tex();
475bf215546Sopenharmony_ci   init_fs_constbuf();
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci   set_vertices();
478bf215546Sopenharmony_ci   set_vertex_shader();
479bf215546Sopenharmony_ci   set_fragment_shader(filename);
480bf215546Sopenharmony_ci}
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_cistatic void args(int argc, char *argv[])
483bf215546Sopenharmony_ci{
484bf215546Sopenharmony_ci   int i;
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   for (i = 1; i < argc;) {
487bf215546Sopenharmony_ci      if (graw_parse_args(&i, argc, argv)) {
488bf215546Sopenharmony_ci         continue;
489bf215546Sopenharmony_ci      }
490bf215546Sopenharmony_ci      if (strcmp(argv[i], "-fps") == 0) {
491bf215546Sopenharmony_ci         show_fps = 1;
492bf215546Sopenharmony_ci         i++;
493bf215546Sopenharmony_ci      }
494bf215546Sopenharmony_ci      else if (i == argc - 1) {
495bf215546Sopenharmony_ci         filename = argv[i];
496bf215546Sopenharmony_ci         i++;
497bf215546Sopenharmony_ci      }
498bf215546Sopenharmony_ci      else {
499bf215546Sopenharmony_ci         usage(argv[0]);
500bf215546Sopenharmony_ci         exit(1);
501bf215546Sopenharmony_ci      }
502bf215546Sopenharmony_ci   }
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci   if (!filename) {
505bf215546Sopenharmony_ci      usage(argv[0]);
506bf215546Sopenharmony_ci      exit(1);
507bf215546Sopenharmony_ci   }
508bf215546Sopenharmony_ci}
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_ciint main( int argc, char *argv[] )
511bf215546Sopenharmony_ci{
512bf215546Sopenharmony_ci   args(argc,argv);
513bf215546Sopenharmony_ci   init();
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci   graw_set_display_func( draw );
516bf215546Sopenharmony_ci   graw_main_loop();
517bf215546Sopenharmony_ci   return 0;
518bf215546Sopenharmony_ci}
519