Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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