Home | History | Annotate | Download | only in graw
      1 /* Test the TGSI_SEMANTIC_POSITION fragment shader input.
      2  * Plus properties for upper-left vs. lower-left origin and
      3  * center integer vs. half-integer;
      4  */
      5 
      6 #include <stdio.h>
      7 
      8 #include "graw_util.h"
      9 
     10 
     11 static int width = 300;
     12 static int height = 300;
     13 
     14 static struct graw_info info;
     15 
     16 struct vertex {
     17    float position[4];
     18    float color[4];
     19 };
     20 
     21 /* Note: the upper-left vertex is pushed to the left a bit to
     22  * make sure we can spot upside-down rendering.
     23  */
     24 static struct vertex vertices[] =
     25 {
     26    {
     27       {-0.95, -0.95, 0.5, 1.0 },
     28       { 0, 0, 0, 1 }
     29    },
     30 
     31    {
     32       { 0.85, -0.95, 0.5, 1.0 },
     33       { 0, 0, 0, 1 }
     34    },
     35 
     36    {
     37       { 0.95,  0.95, 0.5, 1.0 },
     38       { 0, 0, 0, 1 }
     39    },
     40 
     41    {
     42       {-0.95,  0.95, 0.5, 1.0 },
     43       { 0, 0, 0, 1 }
     44    }
     45 };
     46 
     47 #define NUM_VERTS (sizeof(vertices) / sizeof(vertices[0]))
     48 
     49 
     50 static void
     51 set_vertices(void)
     52 {
     53    struct pipe_vertex_element ve[2];
     54    struct pipe_vertex_buffer vbuf;
     55    void *handle;
     56 
     57    memset(ve, 0, sizeof ve);
     58 
     59    ve[0].src_offset = Offset(struct vertex, position);
     60    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
     61    ve[1].src_offset = Offset(struct vertex, color);
     62    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
     63 
     64    handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
     65    info.ctx->bind_vertex_elements_state(info.ctx, handle);
     66 
     67    memset(&vbuf, 0, sizeof vbuf);
     68 
     69    vbuf.stride = sizeof(struct vertex);
     70    vbuf.buffer_offset = 0;
     71    vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
     72                                               PIPE_BIND_VERTEX_BUFFER,
     73                                               PIPE_USAGE_DEFAULT,
     74                                               sizeof(vertices),
     75                                               vertices);
     76 
     77    info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
     78 }
     79 
     80 
     81 static void
     82 set_vertex_shader(void)
     83 {
     84    void *handle;
     85    const char *text =
     86       "VERT\n"
     87       "DCL IN[0]\n"
     88       "DCL IN[1]\n"
     89       "DCL OUT[0], POSITION\n"
     90       "DCL OUT[1], GENERIC[0]\n"
     91       "  0: MOV OUT[0], IN[0]\n"
     92       "  1: MOV OUT[1], IN[1]\n"
     93       "  2: END\n";
     94 
     95    handle = graw_parse_vertex_shader(info.ctx, text);
     96    info.ctx->bind_vs_state(info.ctx, handle);
     97 }
     98 
     99 
    100 static void
    101 set_fragment_shader(int mode)
    102 {
    103    void *handle;
    104 
    105    const char *origin_upper_left_text =
    106       "FRAG\n"
    107       "PROPERTY FS_COORD_ORIGIN UPPER_LEFT\n"  /* upper-left = black corner */
    108       "DCL IN[0], POSITION, LINEAR\n"
    109       "DCL OUT[0], COLOR\n"
    110       "DCL TEMP[0]\n"
    111       "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 }\n"
    112       "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 }\n"
    113       " 0: MOV TEMP[0], IN[0] \n"
    114       " 1: MOV TEMP[0].zw, IMM[1].xxxx \n"
    115       " 2: MUL OUT[0], TEMP[0], IMM[0] \n"
    116       " 3: END\n";
    117 
    118    const char *origin_lower_left_text =
    119       "FRAG\n"
    120       "PROPERTY FS_COORD_ORIGIN LOWER_LEFT\n"  /* lower-left = black corner */
    121       "DCL IN[0], POSITION, LINEAR\n"
    122       "DCL OUT[0], COLOR\n"
    123       "DCL TEMP[0]\n"
    124       "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 }\n"
    125       "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 }\n"
    126       " 0: MOV TEMP[0], IN[0] \n"
    127       " 1: MOV TEMP[0].zw, IMM[1].xxxx \n"
    128       " 2: MUL OUT[0], TEMP[0], IMM[0] \n"
    129       " 3: END\n";
    130 
    131    /* Test fragcoord center integer vs. half integer */
    132    const char *center_integer_text =
    133       "FRAG\n"
    134       "PROPERTY FS_COORD_PIXEL_CENTER INTEGER \n"       /* pixels are black */
    135       "DCL IN[0], POSITION, LINEAR \n"
    136       "DCL OUT[0], COLOR \n"
    137       "DCL TEMP[0] \n"
    138       "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 } \n"
    139       "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 } \n"
    140       "0: FRC TEMP[0], IN[0]  \n"
    141       "1: MOV TEMP[0].zw, IMM[1].xxxx \n"
    142       "2: MOV OUT[0], TEMP[0] \n"
    143       "3: END \n";
    144 
    145    const char *center_half_integer_text =
    146       "FRAG\n"
    147       "PROPERTY FS_COORD_PIXEL_CENTER HALF_INTEGER \n"  /* pixels are olive colored */
    148       "DCL IN[0], POSITION, LINEAR \n"
    149       "DCL OUT[0], COLOR \n"
    150       "DCL TEMP[0] \n"
    151       "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 } \n"
    152       "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 } \n"
    153       "0: FRC TEMP[0], IN[0]  \n"
    154       "1: MOV TEMP[0].zw, IMM[1].xxxx \n"
    155       "2: MOV OUT[0], TEMP[0] \n"
    156       "3: END \n";
    157 
    158    const char *text;
    159 
    160    if (mode == 0)
    161       text = origin_upper_left_text;
    162    else if (mode == 1)
    163       text = origin_lower_left_text;
    164    else if (mode == 2)
    165       text = center_integer_text;
    166    else
    167       text = center_half_integer_text;
    168 
    169    handle = graw_parse_fragment_shader(info.ctx, text);
    170    info.ctx->bind_fs_state(info.ctx, handle);
    171 }
    172 
    173 
    174 static void
    175 draw(void)
    176 {
    177    union pipe_color_union clear_color;
    178 
    179    clear_color.f[0] = 0.25;
    180    clear_color.f[1] = 0.25;
    181    clear_color.f[2] = 0.25;
    182    clear_color.f[3] = 1.0;
    183 
    184    info.ctx->clear(info.ctx,
    185               PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
    186               &clear_color, 1.0, 0);
    187    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
    188    info.ctx->flush(info.ctx, NULL, 0);
    189 
    190 #if 0
    191    /* At the moment, libgraw leaks out/makes available some of the
    192     * symbols from gallium/auxiliary, including these debug helpers.
    193     * Will eventually want to bless some of these paths, and lock the
    194     * others down so they aren't accessible from test programs.
    195     *
    196     * This currently just happens to work on debug builds - a release
    197     * build will probably fail to link here:
    198     */
    199    debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
    200 #endif
    201 
    202    graw_util_flush_front(&info);
    203 }
    204 
    205 
    206 #if 0
    207 static void
    208 resize(int w, int h)
    209 {
    210    width = w;
    211    height = h;
    212 
    213    set_viewport(0, 0, width, height, 30, 1000);
    214 }
    215 #endif
    216 
    217 
    218 static void
    219 init(int mode)
    220 {
    221    if (!graw_util_create_window(&info, width, height, 1, TRUE))
    222       exit(1);
    223 
    224    graw_util_default_state(&info, TRUE);
    225 
    226    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
    227 
    228    set_vertices();
    229    set_vertex_shader();
    230    set_fragment_shader(mode);
    231 }
    232 
    233 
    234 int
    235 main(int argc, char *argv[])
    236 {
    237    int mode = argc > 1 ? atoi(argv[1]) : 0;
    238 
    239    switch (mode) {
    240    default:
    241    case 0:
    242       printf("frag coord origin upper-left (lower-left = black)\n");
    243       break;
    244    case 1:
    245       printf("frag coord origin lower-left (upper-left = black)\n");
    246       break;
    247    case 2:
    248       printf("frag coord center integer (all pixels black)\n");
    249       break;
    250    case 3:
    251       printf("frag coord center half-integer (all pixels olive color)\n");
    252       break;
    253    }
    254 
    255    init(mode);
    256 
    257    graw_set_display_func(draw);
    258    /*graw_set_reshape_func(resize);*/
    259    graw_main_loop();
    260    return 0;
    261 }
    262