Home | History | Annotate | Download | only in graw
      1 /* Test gallium occlusion queries.
      2  */
      3 
      4 #include <stdio.h>
      5 
      6 #include "graw_util.h"
      7 
      8 
      9 static int width = 300;
     10 static int height = 300;
     11 
     12 /* expected results of occlusion test (depndsd on window size) */
     13 static int expected1 = (int) ((300 * 0.9) * (300 * 0.9));
     14 static int expected2 = 420;
     15 
     16 
     17 static struct graw_info info;
     18 
     19 struct vertex {
     20    float position[4];
     21    float color[4];
     22 };
     23 
     24 #define z0 0.2
     25 #define z1 0.6
     26 
     27 static struct vertex obj1_vertices[4] =
     28 {
     29    {
     30       {-0.9, -0.9, z0, 1.0 },
     31       { 1, 0, 0, 1 }
     32    },
     33 
     34    {
     35       { 0.9, -0.9, z0, 1.0 },
     36       { 1, 0, 0, 1 }
     37    },
     38 
     39    {
     40       { 0.9,  0.9, z0, 1.0 },
     41       { 1, 0, 0, 1 }
     42    },
     43 
     44    {
     45       {-0.9,  0.9, z0, 1.0 },
     46       { 1, 0, 0, 1 }
     47    }
     48 };
     49 
     50 static struct vertex obj2_vertices[4] =
     51 {
     52    {
     53       { -0.2,  -0.2, z1, 1.0 },
     54       { 0, 0, 1, 1 }
     55    },
     56 
     57    {
     58       { 0.95, -0.2, z1, 1.0 },
     59       { 0, 0, 1, 1 }
     60    },
     61 
     62    {
     63       { 0.95,  0.2, z1, 1.0 },
     64       { 0, 0, 1, 1 }
     65    },
     66 
     67    {
     68       { -0.2, 0.2, z1, 1.0 },
     69       { 0, 0, 1, 1 }
     70    },
     71 };
     72 
     73 #define NUM_VERTS 4
     74 
     75 
     76 
     77 static void
     78 set_vertices(struct vertex *vertices, unsigned bytes)
     79 {
     80    struct pipe_vertex_element ve[2];
     81    struct pipe_vertex_buffer vbuf;
     82    void *handle;
     83 
     84    memset(ve, 0, sizeof ve);
     85 
     86    ve[0].src_offset = Offset(struct vertex, position);
     87    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
     88    ve[1].src_offset = Offset(struct vertex, color);
     89    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
     90 
     91    handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
     92    info.ctx->bind_vertex_elements_state(info.ctx, handle);
     93 
     94 
     95    vbuf.stride = sizeof(struct vertex);
     96    vbuf.buffer_offset = 0;
     97    vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
     98                                               PIPE_BIND_VERTEX_BUFFER,
     99                                               PIPE_USAGE_STATIC,
    100                                               bytes,
    101                                               vertices);
    102 
    103    info.ctx->set_vertex_buffers(info.ctx, 1, &vbuf);
    104 }
    105 
    106 
    107 static void
    108 set_vertex_shader(struct graw_info *info)
    109 {
    110    void *handle;
    111    const char *text =
    112       "VERT\n"
    113       "DCL IN[0]\n"
    114       "DCL IN[1]\n"
    115       "DCL OUT[0], POSITION\n"
    116       "DCL OUT[1], GENERIC[0]\n"
    117       "  0: MOV OUT[0], IN[0]\n"
    118       "  1: MOV OUT[1], IN[1]\n"
    119       "  2: END\n";
    120 
    121    handle = graw_parse_vertex_shader(info->ctx, text);
    122    if (!handle) {
    123       debug_printf("Failed to parse vertex shader\n");
    124       return;
    125    }
    126    info->ctx->bind_vs_state(info->ctx, handle);
    127 }
    128 
    129 
    130 static void
    131 set_fragment_shader(struct graw_info *info)
    132 {
    133    void *handle;
    134    const char *text =
    135       "FRAG\n"
    136       "DCL IN[0], GENERIC, LINEAR\n"
    137       "DCL OUT[0], COLOR\n"
    138       " 0: MOV OUT[0], IN[0]\n"
    139       " 1: END\n";
    140 
    141    handle = graw_parse_fragment_shader(info->ctx, text);
    142    if (!handle) {
    143       debug_printf("Failed to parse fragment shader\n");
    144       return;
    145    }
    146    info->ctx->bind_fs_state(info->ctx, handle);
    147 }
    148 
    149 
    150 static void
    151 draw(void)
    152 {
    153    int expected1_min = (int) (expected1 * 0.95);
    154    int expected1_max = (int) (expected1 * 1.05);
    155    int expected2_min = (int) (expected2 * 0.95);
    156    int expected2_max = (int) (expected2 * 1.05);
    157 
    158    union pipe_color_union clear_color;
    159 
    160    struct pipe_query *q1, *q2;
    161    uint64_t res1, res2;
    162 
    163    clear_color.f[0] = 0.25;
    164    clear_color.f[1] = 0.25;
    165    clear_color.f[2] = 0.25;
    166    clear_color.f[3] = 1.00;
    167 
    168    info.ctx->clear(info.ctx,
    169                    PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
    170                    &clear_color, 1.0, 0);
    171 
    172    q1 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER);
    173    q2 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER);
    174 
    175    /* draw first, large object */
    176    set_vertices(obj1_vertices, sizeof(obj1_vertices));
    177    info.ctx->begin_query(info.ctx, q1);
    178    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
    179    info.ctx->end_query(info.ctx, q1);
    180 
    181    /* draw second, small object behind first object */
    182    set_vertices(obj2_vertices, sizeof(obj2_vertices));
    183    info.ctx->begin_query(info.ctx, q2);
    184    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
    185    info.ctx->end_query(info.ctx, q2);
    186 
    187    info.ctx->get_query_result(info.ctx, q1, TRUE, &res1);
    188    info.ctx->get_query_result(info.ctx, q2, TRUE, &res2);
    189 
    190    printf("result1 = %lu  result2 = %lu\n", res1, res2);
    191    if (res1 < expected1_min || res1 > expected1_max)
    192       printf("  Failure: result1 should be near %d\n", expected1);
    193    if (res2 < expected2_min || res2 > expected2_max)
    194       printf("  Failure: result2 should be near %d\n", expected2);
    195 
    196    info.ctx->flush(info.ctx, NULL);
    197 
    198    graw_util_flush_front(&info);
    199 
    200    info.ctx->destroy_query(info.ctx, q1);
    201    info.ctx->destroy_query(info.ctx, q2);
    202 }
    203 
    204 
    205 #if 0
    206 static void
    207 resize(int w, int h)
    208 {
    209    width = w;
    210    height = h;
    211 
    212    graw_util_viewport(&info, 0, 0, width, height, 30, 1000);
    213 }
    214 #endif
    215 
    216 
    217 static void
    218 init(void)
    219 {
    220    if (!graw_util_create_window(&info, width, height, 1, TRUE))
    221       exit(1);
    222 
    223    graw_util_default_state(&info, TRUE);
    224 
    225    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
    226 
    227    set_vertex_shader(&info);
    228    set_fragment_shader(&info);
    229 }
    230 
    231 
    232 int
    233 main(int argc, char *argv[])
    234 {
    235    init();
    236 
    237    printf("The red quad should mostly occlude the blue quad.\n");
    238 
    239    graw_set_display_func(draw);
    240    /*graw_set_reshape_func(resize);*/
    241    graw_main_loop();
    242    return 0;
    243 }
    244