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
123464ebd5Sriastradh#include <stdio.h>              /* for fread(), etc */
133464ebd5Sriastradh
143464ebd5Sriastradh#include "util/u_inlines.h"
153464ebd5Sriastradh#include "util/u_memory.h"      /* Offset() */
163464ebd5Sriastradh#include "util/u_draw_quad.h"
173464ebd5Sriastradh#include "util/u_box.h"
183464ebd5Sriastradh
193464ebd5Sriastradhstatic const char *filename = NULL;
203464ebd5Sriastradhunsigned show_fps = 0;
213464ebd5Sriastradh
223464ebd5Sriastradh
233464ebd5Sriastradhstatic void usage(char *name)
243464ebd5Sriastradh{
253464ebd5Sriastradh   fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
26af69d88dSmrg#ifndef _WIN32
273464ebd5Sriastradh   fprintf(stderr, "\n" );
283464ebd5Sriastradh   fprintf(stderr, "options:\n");
293464ebd5Sriastradh   fprintf(stderr, "    -fps  show frames per second\n");
303464ebd5Sriastradh#endif
313464ebd5Sriastradh}
323464ebd5Sriastradh
333464ebd5Sriastradh
343464ebd5Sriastradhenum pipe_format formats[] = {
35af69d88dSmrg   PIPE_FORMAT_RGBA8888_UNORM,
36af69d88dSmrg   PIPE_FORMAT_BGRA8888_UNORM,
373464ebd5Sriastradh   PIPE_FORMAT_NONE
383464ebd5Sriastradh};
393464ebd5Sriastradh
403464ebd5Sriastradhstatic const int WIDTH = 250;
413464ebd5Sriastradhstatic const int HEIGHT = 250;
423464ebd5Sriastradh
433464ebd5Sriastradhstatic struct pipe_screen *screen = NULL;
443464ebd5Sriastradhstatic struct pipe_context *ctx = NULL;
453464ebd5Sriastradhstatic struct pipe_resource *rttex = NULL;
463464ebd5Sriastradhstatic struct pipe_resource *constbuf = NULL;
473464ebd5Sriastradhstatic struct pipe_surface *surf = NULL;
483464ebd5Sriastradhstatic struct pipe_sampler_view *sv = NULL;
493464ebd5Sriastradhstatic void *sampler = NULL;
503464ebd5Sriastradhstatic void *window = NULL;
513464ebd5Sriastradhstatic struct pipe_resource *samptex = NULL;
523464ebd5Sriastradh
533464ebd5Sriastradhstruct vertex {
543464ebd5Sriastradh   float position[4];
553464ebd5Sriastradh   float color[3];
563464ebd5Sriastradh};
573464ebd5Sriastradh
583464ebd5Sriastradh/* Draw a regular mesh
593464ebd5Sriastradh */
603464ebd5Sriastradh#define MESH_SZ 16
613464ebd5Sriastradhstatic struct vertex vertices[MESH_SZ * MESH_SZ];
623464ebd5Sriastradh
633464ebd5Sriastradhstatic float constants[] =
643464ebd5Sriastradh{  0.4, 0, 0,  1,
653464ebd5Sriastradh   1,   1, 1,  1,
663464ebd5Sriastradh   2,   2, 2,  2,
673464ebd5Sriastradh   4,   8, 16, 32,
683464ebd5Sriastradh
693464ebd5Sriastradh   3,  0, 0, 0,
703464ebd5Sriastradh   0, .5, 0, 0,
713464ebd5Sriastradh   0,  0, 1, 0,
723464ebd5Sriastradh   0,  0, 0, 1,
733464ebd5Sriastradh
743464ebd5Sriastradh   1, 0, 0, 0.5,
753464ebd5Sriastradh   0, 1, 0, 0.5,
763464ebd5Sriastradh   0, 0, 1, 0,
773464ebd5Sriastradh   0, 0, 0, 1,
783464ebd5Sriastradh};
793464ebd5Sriastradh
803464ebd5Sriastradhstatic void init_fs_constbuf( void )
813464ebd5Sriastradh{
823464ebd5Sriastradh   struct pipe_resource templat;
833464ebd5Sriastradh   struct pipe_box box;
843464ebd5Sriastradh
8501e04c3fSmrg   memset(&templat, 0, sizeof(templat));
863464ebd5Sriastradh   templat.target = PIPE_BUFFER;
873464ebd5Sriastradh   templat.format = PIPE_FORMAT_R8_UNORM;
883464ebd5Sriastradh   templat.width0 = sizeof(constants);
893464ebd5Sriastradh   templat.height0 = 1;
903464ebd5Sriastradh   templat.depth0 = 1;
913464ebd5Sriastradh   templat.array_size = 1;
923464ebd5Sriastradh   templat.last_level = 0;
933464ebd5Sriastradh   templat.bind = PIPE_BIND_CONSTANT_BUFFER;
943464ebd5Sriastradh
953464ebd5Sriastradh   constbuf = screen->resource_create(screen,
963464ebd5Sriastradh                                      &templat);
973464ebd5Sriastradh   if (constbuf == NULL)
983464ebd5Sriastradh      exit(4);
993464ebd5Sriastradh
1003464ebd5Sriastradh
1013464ebd5Sriastradh   u_box_2d(0,0,sizeof(constants),1, &box);
1023464ebd5Sriastradh
10301e04c3fSmrg   ctx->buffer_subdata(ctx, constbuf,
1047ec681f3Smrg                       PIPE_MAP_WRITE,
10501e04c3fSmrg                       0, sizeof(constants), constants);
1063464ebd5Sriastradh
107af69d88dSmrg   pipe_set_constant_buffer(ctx,
108af69d88dSmrg                            PIPE_SHADER_VERTEX, 0,
1093464ebd5Sriastradh                            constbuf);
1103464ebd5Sriastradh}
1113464ebd5Sriastradh
1123464ebd5Sriastradh
1133464ebd5Sriastradhstatic void set_viewport( float x, float y,
1143464ebd5Sriastradh                          float width, float height,
11501e04c3fSmrg                          float zNear, float zFar)
1163464ebd5Sriastradh{
11701e04c3fSmrg   float z = zFar;
1183464ebd5Sriastradh   float half_width = (float)width / 2.0f;
1193464ebd5Sriastradh   float half_height = (float)height / 2.0f;
12001e04c3fSmrg   float half_depth = ((float)zFar - (float)zNear) / 2.0f;
1213464ebd5Sriastradh   struct pipe_viewport_state vp;
1223464ebd5Sriastradh
1233464ebd5Sriastradh   vp.scale[0] = half_width;
1243464ebd5Sriastradh   vp.scale[1] = half_height;
1253464ebd5Sriastradh   vp.scale[2] = half_depth;
1263464ebd5Sriastradh
1273464ebd5Sriastradh   vp.translate[0] = half_width + x;
1283464ebd5Sriastradh   vp.translate[1] = half_height + y;
1293464ebd5Sriastradh   vp.translate[2] = half_depth + z;
1303464ebd5Sriastradh
1317ec681f3Smrg   vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
1327ec681f3Smrg   vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
1337ec681f3Smrg   vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
1347ec681f3Smrg   vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
1357ec681f3Smrg
136af69d88dSmrg   ctx->set_viewport_states( ctx, 0, 1, &vp );
1373464ebd5Sriastradh}
1383464ebd5Sriastradh
1393464ebd5Sriastradhstatic void set_vertices( void )
1403464ebd5Sriastradh{
1413464ebd5Sriastradh   struct pipe_vertex_element ve[2];
1423464ebd5Sriastradh   struct pipe_vertex_buffer vbuf;
1433464ebd5Sriastradh   void *handle;
1443464ebd5Sriastradh   int x,y;
1453464ebd5Sriastradh
1463464ebd5Sriastradh   memset(ve, 0, sizeof ve);
1473464ebd5Sriastradh
1483464ebd5Sriastradh   ve[0].src_offset = Offset(struct vertex, position);
1493464ebd5Sriastradh   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1503464ebd5Sriastradh   ve[1].src_offset = Offset(struct vertex, color);
1513464ebd5Sriastradh   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1523464ebd5Sriastradh
1533464ebd5Sriastradh   handle = ctx->create_vertex_elements_state(ctx, 2, ve);
1543464ebd5Sriastradh   ctx->bind_vertex_elements_state(ctx, handle);
1553464ebd5Sriastradh
1563464ebd5Sriastradh   for (x = 0; x < MESH_SZ; x++) {
1573464ebd5Sriastradh      for (y = 0; y < MESH_SZ; y++) {
1583464ebd5Sriastradh         int i = y * MESH_SZ + x;
1593464ebd5Sriastradh         vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0;
1603464ebd5Sriastradh         vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0;
1613464ebd5Sriastradh         vertices[i].position[2] = 0;
1623464ebd5Sriastradh         vertices[i].position[3] = 1.0;
1633464ebd5Sriastradh
1643464ebd5Sriastradh         vertices[i].color[0] = .5;
1653464ebd5Sriastradh         vertices[i].color[1] = (float)x / (float)MESH_SZ;
1663464ebd5Sriastradh         vertices[i].color[2] = (float)y / (float)MESH_SZ;
1673464ebd5Sriastradh      }
1683464ebd5Sriastradh   }
1693464ebd5Sriastradh
170af69d88dSmrg   memset(&vbuf, 0, sizeof vbuf);
171af69d88dSmrg
1723464ebd5Sriastradh   vbuf.stride = sizeof( struct vertex );
1733464ebd5Sriastradh   vbuf.buffer_offset = 0;
17401e04c3fSmrg   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
175af69d88dSmrg                                              PIPE_BIND_VERTEX_BUFFER,
176af69d88dSmrg                                              PIPE_USAGE_DEFAULT,
177af69d88dSmrg                                              sizeof(vertices),
178af69d88dSmrg                                              vertices);
1793464ebd5Sriastradh
1807ec681f3Smrg   ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);
1813464ebd5Sriastradh}
1823464ebd5Sriastradh
1833464ebd5Sriastradhstatic void set_vertex_shader( void )
1843464ebd5Sriastradh{
1853464ebd5Sriastradh   FILE *f;
1863464ebd5Sriastradh   char buf[50000];
1873464ebd5Sriastradh   void *handle;
1883464ebd5Sriastradh   int sz;
1893464ebd5Sriastradh
1903464ebd5Sriastradh   if ((f = fopen(filename, "r")) == NULL) {
1913464ebd5Sriastradh      fprintf(stderr, "Couldn't open %s\n", filename);
1923464ebd5Sriastradh      exit(1);
1933464ebd5Sriastradh   }
1943464ebd5Sriastradh
1953464ebd5Sriastradh   sz = fread(buf, 1, sizeof(buf), f);
1963464ebd5Sriastradh   if (!feof(f)) {
1973464ebd5Sriastradh      printf("file too long\n");
1983464ebd5Sriastradh      exit(1);
1993464ebd5Sriastradh   }
2003464ebd5Sriastradh   printf("%.*s\n", sz, buf);
2013464ebd5Sriastradh   buf[sz] = 0;
2023464ebd5Sriastradh
2033464ebd5Sriastradh   handle = graw_parse_vertex_shader(ctx, buf);
2043464ebd5Sriastradh   ctx->bind_vs_state(ctx, handle);
2053464ebd5Sriastradh   fclose(f);
2063464ebd5Sriastradh}
2073464ebd5Sriastradh
2083464ebd5Sriastradhstatic void set_fragment_shader( void )
2093464ebd5Sriastradh{
2103464ebd5Sriastradh   void *handle;
2113464ebd5Sriastradh   const char *text =
2123464ebd5Sriastradh      "FRAG\n"
2133464ebd5Sriastradh      "DCL IN[0], COLOR, LINEAR\n"
2143464ebd5Sriastradh      "DCL OUT[0], COLOR\n"
2153464ebd5Sriastradh      "  0: MOV OUT[0], IN[0]\n"
2163464ebd5Sriastradh      "  1: END\n";
2173464ebd5Sriastradh
2183464ebd5Sriastradh   handle = graw_parse_fragment_shader(ctx, text);
2193464ebd5Sriastradh   ctx->bind_fs_state(ctx, handle);
2203464ebd5Sriastradh}
2213464ebd5Sriastradh
2223464ebd5Sriastradh
2233464ebd5Sriastradh
2243464ebd5Sriastradhstatic void draw( void )
2253464ebd5Sriastradh{
226af69d88dSmrg   union pipe_color_union clear_color = { {.1,.3,.5,0} };
2273464ebd5Sriastradh
2287ec681f3Smrg   ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
22901e04c3fSmrg   util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, ARRAY_SIZE(vertices));
230af69d88dSmrg   ctx->flush(ctx, NULL, 0);
2313464ebd5Sriastradh
2323464ebd5Sriastradh   graw_save_surface_to_file(ctx, surf, NULL);
2333464ebd5Sriastradh
2347ec681f3Smrg   screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);
2353464ebd5Sriastradh}
2363464ebd5Sriastradh
2373464ebd5Sriastradh#define SIZE 16
2383464ebd5Sriastradh
2393464ebd5Sriastradhstatic void init_tex( void )
2403464ebd5Sriastradh{
2413464ebd5Sriastradh   struct pipe_sampler_view sv_template;
2423464ebd5Sriastradh   struct pipe_sampler_state sampler_desc;
2433464ebd5Sriastradh   struct pipe_resource templat;
2443464ebd5Sriastradh   struct pipe_box box;
2453464ebd5Sriastradh   ubyte tex2d[SIZE][SIZE][4];
2463464ebd5Sriastradh   int s, t;
2473464ebd5Sriastradh
2483464ebd5Sriastradh#if (SIZE != 2)
2493464ebd5Sriastradh   for (s = 0; s < SIZE; s++) {
2503464ebd5Sriastradh      for (t = 0; t < SIZE; t++) {
2513464ebd5Sriastradh         if (0) {
2523464ebd5Sriastradh            int x = (s ^ t) & 1;
2533464ebd5Sriastradh	    tex2d[t][s][0] = (x) ? 0 : 63;
2543464ebd5Sriastradh	    tex2d[t][s][1] = (x) ? 0 : 128;
2553464ebd5Sriastradh	    tex2d[t][s][2] = 0;
2563464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
2573464ebd5Sriastradh         }
2583464ebd5Sriastradh         else {
2593464ebd5Sriastradh            int x = ((s ^ t) >> 2) & 1;
2603464ebd5Sriastradh	    tex2d[t][s][0] = s*255/(SIZE-1);
2613464ebd5Sriastradh	    tex2d[t][s][1] = t*255/(SIZE-1);
2623464ebd5Sriastradh	    tex2d[t][s][2] = (x) ? 0 : 128;
2633464ebd5Sriastradh	    tex2d[t][s][3] = 0xff;
2643464ebd5Sriastradh         }
2653464ebd5Sriastradh      }
2663464ebd5Sriastradh   }
2673464ebd5Sriastradh#else
2683464ebd5Sriastradh   tex2d[0][0][0] = 0;
2693464ebd5Sriastradh   tex2d[0][0][1] = 255;
2703464ebd5Sriastradh   tex2d[0][0][2] = 255;
2713464ebd5Sriastradh   tex2d[0][0][3] = 0;
2723464ebd5Sriastradh
2733464ebd5Sriastradh   tex2d[0][1][0] = 0;
2743464ebd5Sriastradh   tex2d[0][1][1] = 0;
2753464ebd5Sriastradh   tex2d[0][1][2] = 255;
2763464ebd5Sriastradh   tex2d[0][1][3] = 255;
2773464ebd5Sriastradh
2783464ebd5Sriastradh   tex2d[1][0][0] = 255;
2793464ebd5Sriastradh   tex2d[1][0][1] = 255;
2803464ebd5Sriastradh   tex2d[1][0][2] = 0;
2813464ebd5Sriastradh   tex2d[1][0][3] = 255;
2823464ebd5Sriastradh
2833464ebd5Sriastradh   tex2d[1][1][0] = 255;
2843464ebd5Sriastradh   tex2d[1][1][1] = 0;
2853464ebd5Sriastradh   tex2d[1][1][2] = 0;
2863464ebd5Sriastradh   tex2d[1][1][3] = 255;
2873464ebd5Sriastradh#endif
2883464ebd5Sriastradh
28901e04c3fSmrg   memset(&templat, 0, sizeof(templat));
2903464ebd5Sriastradh   templat.target = PIPE_TEXTURE_2D;
2913464ebd5Sriastradh   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
2923464ebd5Sriastradh   templat.width0 = SIZE;
2933464ebd5Sriastradh   templat.height0 = SIZE;
2943464ebd5Sriastradh   templat.depth0 = 1;
2953464ebd5Sriastradh   templat.array_size = 1;
2963464ebd5Sriastradh   templat.last_level = 0;
2973464ebd5Sriastradh   templat.bind = PIPE_BIND_SAMPLER_VIEW;
2983464ebd5Sriastradh
2993464ebd5Sriastradh
3003464ebd5Sriastradh   samptex = screen->resource_create(screen,
3013464ebd5Sriastradh                                 &templat);
3023464ebd5Sriastradh   if (samptex == NULL)
3033464ebd5Sriastradh      exit(4);
3043464ebd5Sriastradh
3053464ebd5Sriastradh   u_box_2d(0,0,SIZE,SIZE, &box);
3063464ebd5Sriastradh
30701e04c3fSmrg   ctx->texture_subdata(ctx,
30801e04c3fSmrg                        samptex,
30901e04c3fSmrg                        0,
3107ec681f3Smrg                        PIPE_MAP_WRITE,
31101e04c3fSmrg                        &box,
31201e04c3fSmrg                        tex2d,
31301e04c3fSmrg                        sizeof tex2d[0],
31401e04c3fSmrg                        sizeof tex2d);
3153464ebd5Sriastradh
3163464ebd5Sriastradh   /* Possibly read back & compare against original data:
3173464ebd5Sriastradh    */
3183464ebd5Sriastradh   if (0)
3193464ebd5Sriastradh   {
3203464ebd5Sriastradh      struct pipe_transfer *t;
3213464ebd5Sriastradh      uint32_t *ptr;
3227ec681f3Smrg      ptr = pipe_texture_map(ctx, samptex,
323af69d88dSmrg                              0, 0, /* level, layer */
3247ec681f3Smrg                              PIPE_MAP_READ,
325af69d88dSmrg                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
3263464ebd5Sriastradh
3273464ebd5Sriastradh      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
3283464ebd5Sriastradh         assert(0);
3293464ebd5Sriastradh         exit(9);
3303464ebd5Sriastradh      }
3313464ebd5Sriastradh
3327ec681f3Smrg      ctx->texture_unmap(ctx, t);
3333464ebd5Sriastradh   }
3343464ebd5Sriastradh
3353464ebd5Sriastradh   memset(&sv_template, 0, sizeof sv_template);
3363464ebd5Sriastradh   sv_template.format = samptex->format;
3373464ebd5Sriastradh   sv_template.texture = samptex;
3383464ebd5Sriastradh   sv_template.swizzle_r = 0;
3393464ebd5Sriastradh   sv_template.swizzle_g = 1;
3403464ebd5Sriastradh   sv_template.swizzle_b = 2;
3413464ebd5Sriastradh   sv_template.swizzle_a = 3;
3423464ebd5Sriastradh   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
3433464ebd5Sriastradh   if (sv == NULL)
3443464ebd5Sriastradh      exit(5);
3453464ebd5Sriastradh
3467ec681f3Smrg   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv);
3473464ebd5Sriastradh
3483464ebd5Sriastradh
3493464ebd5Sriastradh   memset(&sampler_desc, 0, sizeof sampler_desc);
3503464ebd5Sriastradh   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
3513464ebd5Sriastradh   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
3523464ebd5Sriastradh   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
3533464ebd5Sriastradh   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
3543464ebd5Sriastradh   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
3553464ebd5Sriastradh   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
3563464ebd5Sriastradh   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
3573464ebd5Sriastradh   sampler_desc.compare_func = 0;
3583464ebd5Sriastradh   sampler_desc.normalized_coords = 1;
3593464ebd5Sriastradh   sampler_desc.max_anisotropy = 0;
3603464ebd5Sriastradh
3613464ebd5Sriastradh   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
3623464ebd5Sriastradh   if (sampler == NULL)
3633464ebd5Sriastradh      exit(6);
3643464ebd5Sriastradh
365af69d88dSmrg   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
3663464ebd5Sriastradh
3673464ebd5Sriastradh}
3683464ebd5Sriastradh
3693464ebd5Sriastradhstatic void init( void )
3703464ebd5Sriastradh{
3713464ebd5Sriastradh   struct pipe_framebuffer_state fb;
3723464ebd5Sriastradh   struct pipe_resource templat;
3733464ebd5Sriastradh   struct pipe_surface surf_tmpl;
3743464ebd5Sriastradh   int i;
3753464ebd5Sriastradh
3763464ebd5Sriastradh   /* It's hard to say whether window or screen should be created
3773464ebd5Sriastradh    * first.  Different environments would prefer one or the other.
3783464ebd5Sriastradh    *
3793464ebd5Sriastradh    * Also, no easy way of querying supported formats if the screen
3803464ebd5Sriastradh    * cannot be created first.
3813464ebd5Sriastradh    */
3823464ebd5Sriastradh   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
3833464ebd5Sriastradh      screen = graw_create_window_and_screen(0, 0, 300, 300,
3843464ebd5Sriastradh                                             formats[i],
3853464ebd5Sriastradh                                             &window);
3863464ebd5Sriastradh      if (window && screen)
3873464ebd5Sriastradh         break;
3883464ebd5Sriastradh   }
3893464ebd5Sriastradh   if (!screen || !window) {
3903464ebd5Sriastradh      fprintf(stderr, "Unable to create window\n");
3913464ebd5Sriastradh      exit(1);
3923464ebd5Sriastradh   }
3933464ebd5Sriastradh
39401e04c3fSmrg   ctx = screen->context_create(screen, NULL, 0);
3953464ebd5Sriastradh   if (ctx == NULL)
3963464ebd5Sriastradh      exit(3);
3973464ebd5Sriastradh
39801e04c3fSmrg   memset(&templat, 0, sizeof(templat));
3993464ebd5Sriastradh   templat.target = PIPE_TEXTURE_2D;
4003464ebd5Sriastradh   templat.format = formats[i];
4013464ebd5Sriastradh   templat.width0 = WIDTH;
4023464ebd5Sriastradh   templat.height0 = HEIGHT;
4033464ebd5Sriastradh   templat.depth0 = 1;
4043464ebd5Sriastradh   templat.array_size = 1;
4053464ebd5Sriastradh   templat.last_level = 0;
4063464ebd5Sriastradh   templat.bind = (PIPE_BIND_RENDER_TARGET |
4073464ebd5Sriastradh                   PIPE_BIND_DISPLAY_TARGET);
4083464ebd5Sriastradh
4093464ebd5Sriastradh   rttex = screen->resource_create(screen,
4103464ebd5Sriastradh                                 &templat);
4113464ebd5Sriastradh   if (rttex == NULL)
4123464ebd5Sriastradh      exit(4);
4133464ebd5Sriastradh
4143464ebd5Sriastradh   surf_tmpl.format = templat.format;
4153464ebd5Sriastradh   surf_tmpl.u.tex.level = 0;
4163464ebd5Sriastradh   surf_tmpl.u.tex.first_layer = 0;
4173464ebd5Sriastradh   surf_tmpl.u.tex.last_layer = 0;
4183464ebd5Sriastradh   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
4193464ebd5Sriastradh   if (surf == NULL)
4203464ebd5Sriastradh      exit(5);
4213464ebd5Sriastradh
4223464ebd5Sriastradh   memset(&fb, 0, sizeof fb);
4233464ebd5Sriastradh   fb.nr_cbufs = 1;
4243464ebd5Sriastradh   fb.width = WIDTH;
4253464ebd5Sriastradh   fb.height = HEIGHT;
4263464ebd5Sriastradh   fb.cbufs[0] = surf;
4273464ebd5Sriastradh
4283464ebd5Sriastradh   ctx->set_framebuffer_state(ctx, &fb);
4293464ebd5Sriastradh
4303464ebd5Sriastradh   {
4313464ebd5Sriastradh      struct pipe_blend_state blend;
4323464ebd5Sriastradh      void *handle;
4333464ebd5Sriastradh      memset(&blend, 0, sizeof blend);
4343464ebd5Sriastradh      blend.rt[0].colormask = PIPE_MASK_RGBA;
4353464ebd5Sriastradh      handle = ctx->create_blend_state(ctx, &blend);
4363464ebd5Sriastradh      ctx->bind_blend_state(ctx, handle);
4373464ebd5Sriastradh   }
4383464ebd5Sriastradh
4393464ebd5Sriastradh   {
4403464ebd5Sriastradh      struct pipe_depth_stencil_alpha_state depthstencil;
4413464ebd5Sriastradh      void *handle;
4423464ebd5Sriastradh      memset(&depthstencil, 0, sizeof depthstencil);
4433464ebd5Sriastradh      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
4443464ebd5Sriastradh      ctx->bind_depth_stencil_alpha_state(ctx, handle);
4453464ebd5Sriastradh   }
4463464ebd5Sriastradh
4473464ebd5Sriastradh   {
4483464ebd5Sriastradh      struct pipe_rasterizer_state rasterizer;
4493464ebd5Sriastradh      void *handle;
4503464ebd5Sriastradh      memset(&rasterizer, 0, sizeof rasterizer);
4513464ebd5Sriastradh      rasterizer.cull_face = PIPE_FACE_NONE;
4523464ebd5Sriastradh      rasterizer.point_size = 8.0;
453af69d88dSmrg      rasterizer.half_pixel_center = 1;
454af69d88dSmrg      rasterizer.bottom_edge_rule = 1;
45501e04c3fSmrg      rasterizer.depth_clip_near = 1;
45601e04c3fSmrg      rasterizer.depth_clip_far = 1;
4573464ebd5Sriastradh      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
4583464ebd5Sriastradh      ctx->bind_rasterizer_state(ctx, handle);
4593464ebd5Sriastradh   }
4603464ebd5Sriastradh
4613464ebd5Sriastradh   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
4623464ebd5Sriastradh
4633464ebd5Sriastradh   init_tex();
4643464ebd5Sriastradh   init_fs_constbuf();
4653464ebd5Sriastradh
4663464ebd5Sriastradh   set_vertices();
4673464ebd5Sriastradh   set_vertex_shader();
4683464ebd5Sriastradh   set_fragment_shader();
4693464ebd5Sriastradh}
4703464ebd5Sriastradh
4713464ebd5Sriastradhstatic void args(int argc, char *argv[])
4723464ebd5Sriastradh{
4733464ebd5Sriastradh   int i;
4743464ebd5Sriastradh
4753464ebd5Sriastradh   for (i = 1; i < argc;) {
4763464ebd5Sriastradh      if (graw_parse_args(&i, argc, argv)) {
4773464ebd5Sriastradh         continue;
4783464ebd5Sriastradh      }
4793464ebd5Sriastradh      if (strcmp(argv[i], "-fps") == 0) {
4803464ebd5Sriastradh         show_fps = 1;
4813464ebd5Sriastradh         i++;
4823464ebd5Sriastradh      }
4833464ebd5Sriastradh      else if (i == argc - 1) {
4843464ebd5Sriastradh         filename = argv[i];
4853464ebd5Sriastradh         i++;
4863464ebd5Sriastradh      }
4873464ebd5Sriastradh      else {
4883464ebd5Sriastradh         usage(argv[0]);
4893464ebd5Sriastradh         exit(1);
4903464ebd5Sriastradh      }
4913464ebd5Sriastradh   }
4923464ebd5Sriastradh
4933464ebd5Sriastradh   if (!filename) {
4943464ebd5Sriastradh      usage(argv[0]);
4953464ebd5Sriastradh      exit(1);
4963464ebd5Sriastradh   }
4973464ebd5Sriastradh}
4983464ebd5Sriastradh
4993464ebd5Sriastradhint main( int argc, char *argv[] )
5003464ebd5Sriastradh{
5013464ebd5Sriastradh   args(argc,argv);
5023464ebd5Sriastradh   init();
5033464ebd5Sriastradh
5043464ebd5Sriastradh   graw_set_display_func( draw );
5053464ebd5Sriastradh   graw_main_loop();
5063464ebd5Sriastradh   return 0;
5073464ebd5Sriastradh}
508