13464ebd5Sriastradh/* Display a cleared blue window.  This demo has no dependencies on
23464ebd5Sriastradh * any utility code, just the graw interface and gallium.
33464ebd5Sriastradh */
43464ebd5Sriastradh
57ec681f3Smrg#include "frontend/graw.h"
63464ebd5Sriastradh#include "pipe/p_screen.h"
73464ebd5Sriastradh#include "pipe/p_context.h"
83464ebd5Sriastradh#include "pipe/p_shader_tokens.h"
93464ebd5Sriastradh#include "pipe/p_state.h"
103464ebd5Sriastradh#include "pipe/p_defines.h"
113464ebd5Sriastradh#include <stdio.h>              /* for fread(), etc */
123464ebd5Sriastradh
133464ebd5Sriastradh#include "util/u_inlines.h"
143464ebd5Sriastradh#include "util/u_memory.h"      /* Offset() */
153464ebd5Sriastradh#include "util/u_draw_quad.h"
163464ebd5Sriastradh#include "util/u_box.h"
173464ebd5Sriastradh
183464ebd5Sriastradhstatic const char *filename = NULL;
193464ebd5Sriastradhunsigned show_fps = 0;
203464ebd5Sriastradh
213464ebd5Sriastradh
223464ebd5Sriastradhstatic void usage(char *name)
233464ebd5Sriastradh{
243464ebd5Sriastradh   fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
25af69d88dSmrg#ifndef _WIN32
263464ebd5Sriastradh   fprintf(stderr, "\n" );
273464ebd5Sriastradh   fprintf(stderr, "options:\n");
283464ebd5Sriastradh   fprintf(stderr, "    -fps  show frames per second\n");
293464ebd5Sriastradh#endif
303464ebd5Sriastradh}
313464ebd5Sriastradh
323464ebd5Sriastradh
333464ebd5Sriastradhenum pipe_format formats[] = {
34af69d88dSmrg   PIPE_FORMAT_RGBA8888_UNORM,
35af69d88dSmrg   PIPE_FORMAT_BGRA8888_UNORM,
363464ebd5Sriastradh   PIPE_FORMAT_NONE
373464ebd5Sriastradh};
383464ebd5Sriastradh
393464ebd5Sriastradhstatic const int WIDTH = 250;
403464ebd5Sriastradhstatic const int HEIGHT = 250;
413464ebd5Sriastradh
423464ebd5Sriastradhstatic struct pipe_screen *screen = NULL;
433464ebd5Sriastradhstatic struct pipe_context *ctx = NULL;
443464ebd5Sriastradhstatic struct pipe_resource *rttex = NULL;
453464ebd5Sriastradhstatic struct pipe_surface *surf = NULL;
463464ebd5Sriastradhstatic struct pipe_sampler_view *sv = NULL;
473464ebd5Sriastradhstatic void *sampler = NULL;
483464ebd5Sriastradhstatic void *window = NULL;
493464ebd5Sriastradhstatic struct pipe_resource *samptex = NULL;
503464ebd5Sriastradh
513464ebd5Sriastradhstruct vertex {
523464ebd5Sriastradh   float position[4];
533464ebd5Sriastradh   float color[4];
543464ebd5Sriastradh   float texcoord[4];
553464ebd5Sriastradh};
563464ebd5Sriastradh
573464ebd5Sriastradh/* Vertex data matches progs/fp/fp-tri.c, but flipped in Y dimension
583464ebd5Sriastradh * so that the final images are the same.
593464ebd5Sriastradh */
603464ebd5Sriastradhstatic struct vertex vertices[] =
613464ebd5Sriastradh{
623464ebd5Sriastradh   { { 0.9, 0.9, 0.0, 1.0 },
633464ebd5Sriastradh     { 0, 0, 1, 1 },
643464ebd5Sriastradh     { 1, 1, 0, 1 } },
653464ebd5Sriastradh
663464ebd5Sriastradh   { { 0.9,  -0.9, 0.0, 1.0 },
673464ebd5Sriastradh     { 1, 0, 0, 1 },
683464ebd5Sriastradh     { 1, -1, 0, 1 } },
693464ebd5Sriastradh
703464ebd5Sriastradh   { {-0.9,  0.0, 0.0, 1.0 },
713464ebd5Sriastradh     { 0, 1, 0, 1 },
723464ebd5Sriastradh     { -1, 0, 0, 1 } },
733464ebd5Sriastradh};
743464ebd5Sriastradh
753464ebd5Sriastradhstatic float constants1[] =
763464ebd5Sriastradh{  0.4, 0, 0,  1,
773464ebd5Sriastradh   1,   1, 1,  1,
783464ebd5Sriastradh   2,   2, 2,  2,
793464ebd5Sriastradh   4,   8, 16, 32,
803464ebd5Sriastradh
813464ebd5Sriastradh   3,  0, 0, 0,
823464ebd5Sriastradh   0, .5, 0, 0,
833464ebd5Sriastradh   1,  0, 0, 1,
843464ebd5Sriastradh   0,  0, 0, 1,
853464ebd5Sriastradh
863464ebd5Sriastradh   1, 0, 0, 0.5,
873464ebd5Sriastradh   0, 1, 0, 0.5,
883464ebd5Sriastradh   0, 0, 1, 0,
893464ebd5Sriastradh   0, 0, 0, 1,
903464ebd5Sriastradh};
913464ebd5Sriastradh
923464ebd5Sriastradh
933464ebd5Sriastradhstatic float constants2[] =
943464ebd5Sriastradh{  1, 0, 0,  1,
953464ebd5Sriastradh   0, 1, 0,  1,
963464ebd5Sriastradh   0, 0, 1,  1,
973464ebd5Sriastradh   0, 0, 0,  0,
983464ebd5Sriastradh
993464ebd5Sriastradh   1,  1, 0, 1,
1003464ebd5Sriastradh   1, .5, 0, 1,
1013464ebd5Sriastradh   1,  0, 0, 1,
1023464ebd5Sriastradh   0,  0, 0, 1,
1033464ebd5Sriastradh
1043464ebd5Sriastradh   1, 0, 0, 0.5,
1053464ebd5Sriastradh   0, 1, 0, 0.5,
1063464ebd5Sriastradh   0, 0, 1, 0,
1073464ebd5Sriastradh   0, 0, 0, 1,
1083464ebd5Sriastradh};
1093464ebd5Sriastradh
1103464ebd5Sriastradhstatic void init_fs_constbuf( void )
1113464ebd5Sriastradh{
112af69d88dSmrg   struct pipe_constant_buffer cb1;
113af69d88dSmrg   struct pipe_constant_buffer cb2;
1143464ebd5Sriastradh
115af69d88dSmrg   memset(&cb1, 0, sizeof cb1);
116af69d88dSmrg   cb1.buffer_size = sizeof constants1;
117af69d88dSmrg   cb1.user_buffer = constants1;
1183464ebd5Sriastradh
119af69d88dSmrg   ctx->set_constant_buffer(ctx,
1207ec681f3Smrg                            PIPE_SHADER_FRAGMENT, 0, false,
121af69d88dSmrg                            &cb1);
1223464ebd5Sriastradh
123af69d88dSmrg   memset(&cb2, 0, sizeof cb2);
124af69d88dSmrg   cb2.buffer_size = sizeof constants2;
125af69d88dSmrg   cb2.user_buffer = constants2;
1263464ebd5Sriastradh
127af69d88dSmrg   ctx->set_constant_buffer(ctx,
1287ec681f3Smrg                            PIPE_SHADER_FRAGMENT, 1, false,
129af69d88dSmrg                            &cb2);
1303464ebd5Sriastradh}
1313464ebd5Sriastradh
1323464ebd5Sriastradh
1333464ebd5Sriastradhstatic void set_viewport( float x, float y,
1343464ebd5Sriastradh                          float width, float height,
13501e04c3fSmrg                          float zNear, float zFar)
1363464ebd5Sriastradh{
13701e04c3fSmrg   float z = zFar;
1383464ebd5Sriastradh   float half_width = (float)width / 2.0f;
1393464ebd5Sriastradh   float half_height = (float)height / 2.0f;
14001e04c3fSmrg   float half_depth = ((float)zFar - (float)zNear) / 2.0f;
1413464ebd5Sriastradh   struct pipe_viewport_state vp;
1423464ebd5Sriastradh
1433464ebd5Sriastradh   vp.scale[0] = half_width;
1443464ebd5Sriastradh   vp.scale[1] = half_height;
1453464ebd5Sriastradh   vp.scale[2] = half_depth;
1463464ebd5Sriastradh
1473464ebd5Sriastradh   vp.translate[0] = half_width + x;
1483464ebd5Sriastradh   vp.translate[1] = half_height + y;
1493464ebd5Sriastradh   vp.translate[2] = half_depth + z;
1503464ebd5Sriastradh
1517ec681f3Smrg   vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
1527ec681f3Smrg   vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
1537ec681f3Smrg   vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
1547ec681f3Smrg   vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
1557ec681f3Smrg
156af69d88dSmrg   ctx->set_viewport_states( ctx, 0, 1, &vp );
1573464ebd5Sriastradh}
1583464ebd5Sriastradh
1593464ebd5Sriastradhstatic void set_vertices( void )
1603464ebd5Sriastradh{
1613464ebd5Sriastradh   struct pipe_vertex_element ve[3];
1623464ebd5Sriastradh   struct pipe_vertex_buffer vbuf;
1633464ebd5Sriastradh   void *handle;
1643464ebd5Sriastradh
1653464ebd5Sriastradh   memset(ve, 0, sizeof ve);
1663464ebd5Sriastradh
1673464ebd5Sriastradh   ve[0].src_offset = Offset(struct vertex, position);
1683464ebd5Sriastradh   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1693464ebd5Sriastradh   ve[1].src_offset = Offset(struct vertex, color);
1703464ebd5Sriastradh   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1713464ebd5Sriastradh   ve[2].src_offset = Offset(struct vertex, texcoord);
1723464ebd5Sriastradh   ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1733464ebd5Sriastradh
1743464ebd5Sriastradh   handle = ctx->create_vertex_elements_state(ctx, 3, ve);
1753464ebd5Sriastradh   ctx->bind_vertex_elements_state(ctx, handle);
1763464ebd5Sriastradh
177af69d88dSmrg   memset(&vbuf, 0, sizeof vbuf);
1783464ebd5Sriastradh
1793464ebd5Sriastradh   vbuf.stride = sizeof( struct vertex );
1803464ebd5Sriastradh   vbuf.buffer_offset = 0;
18101e04c3fSmrg   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
182af69d88dSmrg                                              PIPE_BIND_VERTEX_BUFFER,
183af69d88dSmrg                                              PIPE_USAGE_DEFAULT,
184af69d88dSmrg                                              sizeof(vertices),
185af69d88dSmrg                                              vertices);
1863464ebd5Sriastradh
1877ec681f3Smrg   ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);
1883464ebd5Sriastradh}
1893464ebd5Sriastradh
1903464ebd5Sriastradhstatic void set_vertex_shader( void )
1913464ebd5Sriastradh{
1923464ebd5Sriastradh   void *handle;
1933464ebd5Sriastradh   const char *text =
1943464ebd5Sriastradh      "VERT\n"
1953464ebd5Sriastradh      "DCL IN[0]\n"
1963464ebd5Sriastradh      "DCL IN[1]\n"
1973464ebd5Sriastradh      "DCL IN[2]\n"
1983464ebd5Sriastradh      "DCL OUT[0], POSITION\n"
1993464ebd5Sriastradh      "DCL OUT[1], COLOR[0]\n"
2003464ebd5Sriastradh      "DCL OUT[2], GENERIC[0]\n"
2013464ebd5Sriastradh      "  MOV OUT[0], IN[0]\n"
2023464ebd5Sriastradh      "  MOV OUT[1], IN[1]\n"
2033464ebd5Sriastradh      "  MOV OUT[2], IN[2]\n"
2043464ebd5Sriastradh      "  END\n";
2053464ebd5Sriastradh
2063464ebd5Sriastradh   handle = graw_parse_vertex_shader(ctx, text);
2073464ebd5Sriastradh   ctx->bind_vs_state(ctx, handle);
2083464ebd5Sriastradh}
2093464ebd5Sriastradh
2103464ebd5Sriastradhstatic void set_fragment_shader( const char *filename )
2113464ebd5Sriastradh{
2123464ebd5Sriastradh   FILE *f;
2133464ebd5Sriastradh   char buf[50000];
2143464ebd5Sriastradh   void *handle;
2153464ebd5Sriastradh   int sz;
2163464ebd5Sriastradh
2173464ebd5Sriastradh   if ((f = fopen(filename, "r")) == NULL) {
2183464ebd5Sriastradh      fprintf(stderr, "Couldn't open %s\n", filename);
2193464ebd5Sriastradh      exit(1);
2203464ebd5Sriastradh   }
2213464ebd5Sriastradh
2223464ebd5Sriastradh   sz = fread(buf, 1, sizeof(buf), f);
2233464ebd5Sriastradh   if (!feof(f)) {
2243464ebd5Sriastradh      printf("file too long\n");
2253464ebd5Sriastradh      exit(1);
2263464ebd5Sriastradh   }
2273464ebd5Sriastradh   printf("%.*s\n", sz, buf);
2283464ebd5Sriastradh   buf[sz] = 0;
2293464ebd5Sriastradh
2303464ebd5Sriastradh   handle = graw_parse_fragment_shader(ctx, buf);
2313464ebd5Sriastradh   ctx->bind_fs_state(ctx, handle);
2323464ebd5Sriastradh   fclose(f);
2333464ebd5Sriastradh}
2343464ebd5Sriastradh
2353464ebd5Sriastradh
2363464ebd5Sriastradhstatic void draw( void )
2373464ebd5Sriastradh{
238af69d88dSmrg   union pipe_color_union clear_color = { {.1,.3,.5,0} };
2393464ebd5Sriastradh
2407ec681f3Smrg   ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
2413464ebd5Sriastradh   util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
242af69d88dSmrg   ctx->flush(ctx, NULL, 0);
2433464ebd5Sriastradh
2443464ebd5Sriastradh   graw_save_surface_to_file(ctx, surf, NULL);
2453464ebd5Sriastradh
2467ec681f3Smrg   screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);
2473464ebd5Sriastradh}
2483464ebd5Sriastradh
2493464ebd5Sriastradh#define SIZE 16
2503464ebd5Sriastradh
2513464ebd5Sriastradhstatic void init_tex( void )
2523464ebd5Sriastradh{
2533464ebd5Sriastradh   struct pipe_sampler_view sv_template;
2543464ebd5Sriastradh   struct pipe_sampler_state sampler_desc;
2553464ebd5Sriastradh   struct pipe_resource templat;
2563464ebd5Sriastradh   struct pipe_box box;
2573464ebd5Sriastradh   ubyte tex2d[SIZE][SIZE][4];
2583464ebd5Sriastradh   int s, t;
2593464ebd5Sriastradh
2603464ebd5Sriastradh#if (SIZE != 2)
2613464ebd5Sriastradh   for (s = 0; s < SIZE; s++) {
2623464ebd5Sriastradh      for (t = 0; t < SIZE; t++) {
2633464ebd5Sriastradh         if (0) {
2643464ebd5Sriastradh            int x = (s ^ t) & 1;
2653464ebd5Sriastradh	    tex2d[t][s][0] = (x) ? 0 : 63;
2663464ebd5Sriastradh	    tex2d[t][s][1] = (x) ? 0 : 128;
2673464ebd5Sriastradh	    tex2d[t][s][2] = 0;
2683464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
2693464ebd5Sriastradh         }
2703464ebd5Sriastradh         else {
2713464ebd5Sriastradh            int x = ((s ^ t) >> 2) & 1;
2723464ebd5Sriastradh	    tex2d[t][s][0] = s*255/(SIZE-1);
2733464ebd5Sriastradh	    tex2d[t][s][1] = t*255/(SIZE-1);
2743464ebd5Sriastradh	    tex2d[t][s][2] = (x) ? 0 : 128;
2753464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
2763464ebd5Sriastradh         }
2773464ebd5Sriastradh      }
2783464ebd5Sriastradh   }
2793464ebd5Sriastradh#else
2803464ebd5Sriastradh   tex2d[0][0][0] = 0;
2813464ebd5Sriastradh   tex2d[0][0][1] = 255;
2823464ebd5Sriastradh   tex2d[0][0][2] = 255;
2833464ebd5Sriastradh   tex2d[0][0][3] = 0;
2843464ebd5Sriastradh
2853464ebd5Sriastradh   tex2d[0][1][0] = 0;
2863464ebd5Sriastradh   tex2d[0][1][1] = 0;
2873464ebd5Sriastradh   tex2d[0][1][2] = 255;
2883464ebd5Sriastradh   tex2d[0][1][3] = 255;
2893464ebd5Sriastradh
2903464ebd5Sriastradh   tex2d[1][0][0] = 255;
2913464ebd5Sriastradh   tex2d[1][0][1] = 255;
2923464ebd5Sriastradh   tex2d[1][0][2] = 0;
2933464ebd5Sriastradh   tex2d[1][0][3] = 255;
2943464ebd5Sriastradh
2953464ebd5Sriastradh   tex2d[1][1][0] = 255;
2963464ebd5Sriastradh   tex2d[1][1][1] = 0;
2973464ebd5Sriastradh   tex2d[1][1][2] = 0;
2983464ebd5Sriastradh   tex2d[1][1][3] = 255;
2993464ebd5Sriastradh#endif
3003464ebd5Sriastradh
30101e04c3fSmrg   memset(&templat, 0, sizeof(templat));
3023464ebd5Sriastradh   templat.target = PIPE_TEXTURE_2D;
3033464ebd5Sriastradh   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
3043464ebd5Sriastradh   templat.width0 = SIZE;
3053464ebd5Sriastradh   templat.height0 = SIZE;
3063464ebd5Sriastradh   templat.depth0 = 1;
3073464ebd5Sriastradh   templat.array_size = 1;
3083464ebd5Sriastradh   templat.last_level = 0;
3093464ebd5Sriastradh   templat.bind = PIPE_BIND_SAMPLER_VIEW;
3103464ebd5Sriastradh
3113464ebd5Sriastradh
3123464ebd5Sriastradh   samptex = screen->resource_create(screen,
3133464ebd5Sriastradh                                 &templat);
3143464ebd5Sriastradh   if (samptex == NULL)
3153464ebd5Sriastradh      exit(4);
3163464ebd5Sriastradh
3173464ebd5Sriastradh   u_box_2d(0,0,SIZE,SIZE, &box);
3183464ebd5Sriastradh
31901e04c3fSmrg   ctx->texture_subdata(ctx,
32001e04c3fSmrg                        samptex,
32101e04c3fSmrg                        0,
3227ec681f3Smrg                        PIPE_MAP_WRITE,
32301e04c3fSmrg                        &box,
32401e04c3fSmrg                        tex2d,
32501e04c3fSmrg                        sizeof tex2d[0],
32601e04c3fSmrg                        sizeof tex2d);
3273464ebd5Sriastradh
3283464ebd5Sriastradh   /* Possibly read back & compare against original data:
3293464ebd5Sriastradh    */
3303464ebd5Sriastradh   if (0)
3313464ebd5Sriastradh   {
3323464ebd5Sriastradh      struct pipe_transfer *t;
3333464ebd5Sriastradh      uint32_t *ptr;
3347ec681f3Smrg      ptr = pipe_texture_map(ctx, samptex,
335af69d88dSmrg                              0, 0, /* level, layer */
3367ec681f3Smrg                              PIPE_MAP_READ,
337af69d88dSmrg                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
3383464ebd5Sriastradh
3393464ebd5Sriastradh      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
3403464ebd5Sriastradh         assert(0);
3413464ebd5Sriastradh         exit(9);
3423464ebd5Sriastradh      }
3433464ebd5Sriastradh
3447ec681f3Smrg      ctx->texture_unmap(ctx, t);
3453464ebd5Sriastradh   }
3463464ebd5Sriastradh
3473464ebd5Sriastradh   memset(&sv_template, 0, sizeof sv_template);
3483464ebd5Sriastradh   sv_template.format = samptex->format;
3493464ebd5Sriastradh   sv_template.texture = samptex;
3503464ebd5Sriastradh   sv_template.swizzle_r = 0;
3513464ebd5Sriastradh   sv_template.swizzle_g = 1;
3523464ebd5Sriastradh   sv_template.swizzle_b = 2;
3533464ebd5Sriastradh   sv_template.swizzle_a = 3;
3543464ebd5Sriastradh   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
3553464ebd5Sriastradh   if (sv == NULL)
3563464ebd5Sriastradh      exit(5);
3573464ebd5Sriastradh
3587ec681f3Smrg   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv);
3593464ebd5Sriastradh
3603464ebd5Sriastradh
3613464ebd5Sriastradh   memset(&sampler_desc, 0, sizeof sampler_desc);
3623464ebd5Sriastradh   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
3633464ebd5Sriastradh   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
3643464ebd5Sriastradh   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
3653464ebd5Sriastradh   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
3663464ebd5Sriastradh   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
3673464ebd5Sriastradh   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
3683464ebd5Sriastradh   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
3693464ebd5Sriastradh   sampler_desc.compare_func = 0;
3703464ebd5Sriastradh   sampler_desc.normalized_coords = 1;
3713464ebd5Sriastradh   sampler_desc.max_anisotropy = 0;
3723464ebd5Sriastradh
3733464ebd5Sriastradh   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
3743464ebd5Sriastradh   if (sampler == NULL)
3753464ebd5Sriastradh      exit(6);
3763464ebd5Sriastradh
377af69d88dSmrg   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
3783464ebd5Sriastradh
3793464ebd5Sriastradh}
3803464ebd5Sriastradh
3813464ebd5Sriastradhstatic void init( void )
3823464ebd5Sriastradh{
3833464ebd5Sriastradh   struct pipe_framebuffer_state fb;
3843464ebd5Sriastradh   struct pipe_resource templat;
3853464ebd5Sriastradh   struct pipe_surface surf_tmpl;
3863464ebd5Sriastradh   int i;
3873464ebd5Sriastradh
3883464ebd5Sriastradh   /* It's hard to say whether window or screen should be created
3893464ebd5Sriastradh    * first.  Different environments would prefer one or the other.
3903464ebd5Sriastradh    *
3913464ebd5Sriastradh    * Also, no easy way of querying supported formats if the screen
3923464ebd5Sriastradh    * cannot be created first.
3933464ebd5Sriastradh    */
3943464ebd5Sriastradh   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
3953464ebd5Sriastradh      screen = graw_create_window_and_screen(0, 0, 300, 300,
3963464ebd5Sriastradh                                             formats[i],
3973464ebd5Sriastradh                                             &window);
3983464ebd5Sriastradh      if (window && screen)
3993464ebd5Sriastradh         break;
4003464ebd5Sriastradh   }
4013464ebd5Sriastradh   if (!screen || !window) {
4023464ebd5Sriastradh      fprintf(stderr, "Unable to create window\n");
4033464ebd5Sriastradh      exit(1);
4043464ebd5Sriastradh   }
4053464ebd5Sriastradh
40601e04c3fSmrg   ctx = screen->context_create(screen, NULL, 0);
4073464ebd5Sriastradh   if (ctx == NULL)
4083464ebd5Sriastradh      exit(3);
4093464ebd5Sriastradh
41001e04c3fSmrg   memset(&templat, 0, sizeof(templat));
4113464ebd5Sriastradh   templat.target = PIPE_TEXTURE_2D;
4123464ebd5Sriastradh   templat.format = formats[i];
4133464ebd5Sriastradh   templat.width0 = WIDTH;
4143464ebd5Sriastradh   templat.height0 = HEIGHT;
4153464ebd5Sriastradh   templat.depth0 = 1;
4163464ebd5Sriastradh   templat.array_size = 1;
4173464ebd5Sriastradh   templat.last_level = 0;
4183464ebd5Sriastradh   templat.bind = (PIPE_BIND_RENDER_TARGET |
4193464ebd5Sriastradh                   PIPE_BIND_DISPLAY_TARGET);
4203464ebd5Sriastradh
4213464ebd5Sriastradh   rttex = screen->resource_create(screen,
4223464ebd5Sriastradh                                 &templat);
4233464ebd5Sriastradh   if (rttex == NULL)
4243464ebd5Sriastradh      exit(4);
4253464ebd5Sriastradh
4263464ebd5Sriastradh   surf_tmpl.format = templat.format;
4273464ebd5Sriastradh   surf_tmpl.u.tex.level = 0;
4283464ebd5Sriastradh   surf_tmpl.u.tex.first_layer = 0;
4293464ebd5Sriastradh   surf_tmpl.u.tex.last_layer = 0;
4303464ebd5Sriastradh   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
4313464ebd5Sriastradh   if (surf == NULL)
4323464ebd5Sriastradh      exit(5);
4333464ebd5Sriastradh
4343464ebd5Sriastradh   memset(&fb, 0, sizeof fb);
4353464ebd5Sriastradh   fb.nr_cbufs = 1;
4363464ebd5Sriastradh   fb.width = WIDTH;
4373464ebd5Sriastradh   fb.height = HEIGHT;
4383464ebd5Sriastradh   fb.cbufs[0] = surf;
4393464ebd5Sriastradh
4403464ebd5Sriastradh   ctx->set_framebuffer_state(ctx, &fb);
4413464ebd5Sriastradh
4423464ebd5Sriastradh   {
4433464ebd5Sriastradh      struct pipe_blend_state blend;
4443464ebd5Sriastradh      void *handle;
4453464ebd5Sriastradh      memset(&blend, 0, sizeof blend);
4463464ebd5Sriastradh      blend.rt[0].colormask = PIPE_MASK_RGBA;
4473464ebd5Sriastradh      handle = ctx->create_blend_state(ctx, &blend);
4483464ebd5Sriastradh      ctx->bind_blend_state(ctx, handle);
4493464ebd5Sriastradh   }
4503464ebd5Sriastradh
4513464ebd5Sriastradh   {
4523464ebd5Sriastradh      struct pipe_depth_stencil_alpha_state depthstencil;
4533464ebd5Sriastradh      void *handle;
4543464ebd5Sriastradh      memset(&depthstencil, 0, sizeof depthstencil);
4553464ebd5Sriastradh      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
4563464ebd5Sriastradh      ctx->bind_depth_stencil_alpha_state(ctx, handle);
4573464ebd5Sriastradh   }
4583464ebd5Sriastradh
4593464ebd5Sriastradh   {
4603464ebd5Sriastradh      struct pipe_rasterizer_state rasterizer;
4613464ebd5Sriastradh      void *handle;
4623464ebd5Sriastradh      memset(&rasterizer, 0, sizeof rasterizer);
4633464ebd5Sriastradh      rasterizer.cull_face = PIPE_FACE_NONE;
464af69d88dSmrg      rasterizer.half_pixel_center = 1;
465af69d88dSmrg      rasterizer.bottom_edge_rule = 1;
46601e04c3fSmrg      rasterizer.depth_clip_near = 1;
46701e04c3fSmrg      rasterizer.depth_clip_far = 1;
4683464ebd5Sriastradh      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
4693464ebd5Sriastradh      ctx->bind_rasterizer_state(ctx, handle);
4703464ebd5Sriastradh   }
4713464ebd5Sriastradh
4723464ebd5Sriastradh   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
4733464ebd5Sriastradh
4743464ebd5Sriastradh   init_tex();
4753464ebd5Sriastradh   init_fs_constbuf();
4763464ebd5Sriastradh
4773464ebd5Sriastradh   set_vertices();
4783464ebd5Sriastradh   set_vertex_shader();
4793464ebd5Sriastradh   set_fragment_shader(filename);
4803464ebd5Sriastradh}
4813464ebd5Sriastradh
4823464ebd5Sriastradhstatic void args(int argc, char *argv[])
4833464ebd5Sriastradh{
4843464ebd5Sriastradh   int i;
4853464ebd5Sriastradh
4863464ebd5Sriastradh   for (i = 1; i < argc;) {
4873464ebd5Sriastradh      if (graw_parse_args(&i, argc, argv)) {
4883464ebd5Sriastradh         continue;
4893464ebd5Sriastradh      }
4903464ebd5Sriastradh      if (strcmp(argv[i], "-fps") == 0) {
4913464ebd5Sriastradh         show_fps = 1;
4923464ebd5Sriastradh         i++;
4933464ebd5Sriastradh      }
4943464ebd5Sriastradh      else if (i == argc - 1) {
4953464ebd5Sriastradh         filename = argv[i];
4963464ebd5Sriastradh         i++;
4973464ebd5Sriastradh      }
4983464ebd5Sriastradh      else {
4993464ebd5Sriastradh         usage(argv[0]);
5003464ebd5Sriastradh         exit(1);
5013464ebd5Sriastradh      }
5023464ebd5Sriastradh   }
5033464ebd5Sriastradh
5043464ebd5Sriastradh   if (!filename) {
5053464ebd5Sriastradh      usage(argv[0]);
5063464ebd5Sriastradh      exit(1);
5073464ebd5Sriastradh   }
5083464ebd5Sriastradh}
5093464ebd5Sriastradh
5103464ebd5Sriastradhint main( int argc, char *argv[] )
5113464ebd5Sriastradh{
5123464ebd5Sriastradh   args(argc,argv);
5133464ebd5Sriastradh   init();
5143464ebd5Sriastradh
5153464ebd5Sriastradh   graw_set_display_func( draw );
5163464ebd5Sriastradh   graw_main_loop();
5173464ebd5Sriastradh   return 0;
5183464ebd5Sriastradh}
519