Home | History | Annotate | Download | only in state_tracker
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 VMware, Inc.
      4  * All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sub license, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  **************************************************************************/
     27 
     28 /**
     29  * glRasterPos implementation.  Basically render a GL_POINT with our
     30  * private draw module.  Plug in a special "rasterpos" stage at the end
     31  * of the 'draw' pipeline to capture the results and update the current
     32  * raster pos attributes.
     33  *
     34  * Authors:
     35  *   Brian Paul
     36  */
     37 
     38 
     39 #include "main/imports.h"
     40 #include "main/macros.h"
     41 #include "main/feedback.h"
     42 #include "main/rastpos.h"
     43 
     44 #include "st_context.h"
     45 #include "st_atom.h"
     46 #include "st_draw.h"
     47 #include "st_cb_rasterpos.h"
     48 #include "draw/draw_context.h"
     49 #include "draw/draw_pipe.h"
     50 #include "vbo/vbo.h"
     51 
     52 
     53 /**
     54  * Our special drawing pipeline stage (replaces rasterization).
     55  */
     56 struct rastpos_stage
     57 {
     58    struct draw_stage stage;   /**< Base class */
     59    struct gl_context *ctx;            /**< Rendering context */
     60 
     61    /* vertex attrib info we can setup once and re-use */
     62    struct gl_vertex_array array[VERT_ATTRIB_MAX];
     63    const struct gl_vertex_array *arrays[VERT_ATTRIB_MAX];
     64    struct _mesa_prim prim;
     65 };
     66 
     67 
     68 static inline struct rastpos_stage *
     69 rastpos_stage( struct draw_stage *stage )
     70 {
     71    return (struct rastpos_stage *) stage;
     72 }
     73 
     74 static void
     75 rastpos_flush( struct draw_stage *stage, unsigned flags )
     76 {
     77    /* no-op */
     78 }
     79 
     80 static void
     81 rastpos_reset_stipple_counter( struct draw_stage *stage )
     82 {
     83    /* no-op */
     84 }
     85 
     86 static void
     87 rastpos_tri( struct draw_stage *stage, struct prim_header *prim )
     88 {
     89    /* should never get here */
     90    assert(0);
     91 }
     92 
     93 static void
     94 rastpos_line( struct draw_stage *stage, struct prim_header *prim )
     95 {
     96    /* should never get here */
     97    assert(0);
     98 }
     99 
    100 static void
    101 rastpos_destroy(struct draw_stage *stage)
    102 {
    103    free(stage);
    104 }
    105 
    106 
    107 /**
    108  * Update a raster pos attribute from the vertex result if it's present,
    109  * else copy the current attrib.
    110  */
    111 static void
    112 update_attrib(struct gl_context *ctx, const GLuint *outputMapping,
    113               const struct vertex_header *vert,
    114               GLfloat *dest,
    115               GLuint result, GLuint defaultAttrib)
    116 {
    117    const GLfloat *src;
    118    const GLuint k = outputMapping[result];
    119    if (k != ~0U)
    120       src = vert->data[k];
    121    else
    122       src = ctx->Current.Attrib[defaultAttrib];
    123    COPY_4V(dest, src);
    124 }
    125 
    126 
    127 /**
    128  * Normally, this function would render a GL_POINT.
    129  */
    130 static void
    131 rastpos_point(struct draw_stage *stage, struct prim_header *prim)
    132 {
    133    struct rastpos_stage *rs = rastpos_stage(stage);
    134    struct gl_context *ctx = rs->ctx;
    135    struct st_context *st = st_context(ctx);
    136    const GLfloat height = (GLfloat) ctx->DrawBuffer->Height;
    137    const GLuint *outputMapping = st->vertex_result_to_slot;
    138    const GLfloat *pos;
    139    GLuint i;
    140 
    141    /* if we get here, we didn't get clipped */
    142    ctx->Current.RasterPosValid = GL_TRUE;
    143 
    144    /* update raster pos */
    145    pos = prim->v[0]->data[0];
    146    ctx->Current.RasterPos[0] = pos[0];
    147    if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
    148       ctx->Current.RasterPos[1] = height - pos[1]; /* invert Y */
    149    else
    150       ctx->Current.RasterPos[1] = pos[1];
    151    ctx->Current.RasterPos[2] = pos[2];
    152    ctx->Current.RasterPos[3] = pos[3];
    153 
    154    /* update other raster attribs */
    155    update_attrib(ctx, outputMapping, prim->v[0],
    156                  ctx->Current.RasterColor,
    157                  VARYING_SLOT_COL0, VERT_ATTRIB_COLOR0);
    158 
    159    update_attrib(ctx, outputMapping, prim->v[0],
    160                  ctx->Current.RasterSecondaryColor,
    161                  VARYING_SLOT_COL1, VERT_ATTRIB_COLOR1);
    162 
    163    for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
    164       update_attrib(ctx, outputMapping, prim->v[0],
    165                     ctx->Current.RasterTexCoords[i],
    166                     VARYING_SLOT_TEX0 + i, VERT_ATTRIB_TEX0 + i);
    167    }
    168 
    169    if (ctx->RenderMode == GL_SELECT) {
    170       _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
    171    }
    172 }
    173 
    174 
    175 /**
    176  * Create rasterpos "drawing" stage.
    177  */
    178 static struct rastpos_stage *
    179 new_draw_rastpos_stage(struct gl_context *ctx, struct draw_context *draw)
    180 {
    181    struct rastpos_stage *rs = ST_CALLOC_STRUCT(rastpos_stage);
    182    GLuint i;
    183 
    184    rs->stage.draw = draw;
    185    rs->stage.next = NULL;
    186    rs->stage.point = rastpos_point;
    187    rs->stage.line = rastpos_line;
    188    rs->stage.tri = rastpos_tri;
    189    rs->stage.flush = rastpos_flush;
    190    rs->stage.destroy = rastpos_destroy;
    191    rs->stage.reset_stipple_counter = rastpos_reset_stipple_counter;
    192    rs->stage.destroy = rastpos_destroy;
    193    rs->ctx = ctx;
    194 
    195    for (i = 0; i < ARRAY_SIZE(rs->array); i++) {
    196       rs->array[i].Size = 4;
    197       rs->array[i].Type = GL_FLOAT;
    198       rs->array[i].Format = GL_RGBA;
    199       rs->array[i].StrideB = 0;
    200       rs->array[i].Ptr = (GLubyte *) ctx->Current.Attrib[i];
    201       rs->array[i].Normalized = GL_TRUE;
    202       rs->array[i].BufferObj = NULL;
    203       rs->arrays[i] = &rs->array[i];
    204    }
    205 
    206    rs->prim.mode = GL_POINTS;
    207    rs->prim.indexed = 0;
    208    rs->prim.begin = 1;
    209    rs->prim.end = 1;
    210    rs->prim.weak = 0;
    211    rs->prim.start = 0;
    212    rs->prim.count = 1;
    213 
    214    return rs;
    215 }
    216 
    217 
    218 static void
    219 st_RasterPos(struct gl_context *ctx, const GLfloat v[4])
    220 {
    221    struct st_context *st = st_context(ctx);
    222    struct draw_context *draw = st_get_draw_context(st);
    223    struct rastpos_stage *rs;
    224    const struct gl_vertex_array **saved_arrays = ctx->Array._DrawArrays;
    225 
    226    if (!st->draw)
    227       return;
    228 
    229    if (ctx->VertexProgram._Current == NULL ||
    230        ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) {
    231       /* No vertex shader/program is enabled, used the simple/fast fixed-
    232        * function implementation of RasterPos.
    233        */
    234       _mesa_RasterPos(ctx, v);
    235       return;
    236    }
    237 
    238    if (st->rastpos_stage) {
    239       /* get rastpos stage info */
    240       rs = rastpos_stage(st->rastpos_stage);
    241    }
    242    else {
    243       /* create rastpos draw stage */
    244       rs = new_draw_rastpos_stage(ctx, draw);
    245       st->rastpos_stage = &rs->stage;
    246    }
    247 
    248    /* plug our rastpos stage into the draw module */
    249    draw_set_rasterize_stage(st->draw, st->rastpos_stage);
    250 
    251    /* make sure everything's up to date */
    252    st_validate_state(st, ST_PIPELINE_RENDER);
    253 
    254    /* This will get set only if rastpos_point(), above, gets called */
    255    ctx->Current.RasterPosValid = GL_FALSE;
    256 
    257    /* All vertex attribs but position were previously initialized above.
    258     * Just plug in position pointer now.
    259     */
    260    rs->array[0].Ptr = (GLubyte *) v;
    261 
    262    /* Draw the point.
    263     *
    264     * Don't set DriverFlags.NewArray.
    265     * st_feedback_draw_vbo doesn't check for that flag. */
    266    ctx->Array._DrawArrays = rs->arrays;
    267    st_feedback_draw_vbo(ctx, &rs->prim, 1, NULL, GL_TRUE, 0, 1,
    268                         NULL, 0, NULL);
    269    ctx->Array._DrawArrays = saved_arrays;
    270 
    271    /* restore draw's rasterization stage depending on rendermode */
    272    if (ctx->RenderMode == GL_FEEDBACK) {
    273       draw_set_rasterize_stage(draw, st->feedback_stage);
    274    }
    275    else if (ctx->RenderMode == GL_SELECT) {
    276       draw_set_rasterize_stage(draw, st->selection_stage);
    277    }
    278 }
    279 
    280 
    281 
    282 void st_init_rasterpos_functions(struct dd_function_table *functions)
    283 {
    284    functions->RasterPos = st_RasterPos;
    285 }
    286