Home | History | Annotate | Download | only in vbo
      1 /*
      2  * mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2006  Brian Paul   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 "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 /**
     26  * \file vbo_context.h
     27  * \brief VBO builder module datatypes and definitions.
     28  * \author Keith Whitwell
     29  */
     30 
     31 
     32 /**
     33  * \mainpage The VBO builder module
     34  *
     35  * This module hooks into the GL dispatch table and catches all vertex
     36  * building and drawing commands, such as glVertex3f, glBegin and
     37  * glDrawArrays.  The module stores all incoming vertex data as arrays
     38  * in GL vertex buffer objects (VBOs), and translates all drawing
     39  * commands into calls to a driver supplied DrawPrimitives() callback.
     40  *
     41  * The module captures both immediate mode and display list drawing,
     42  * and manages the allocation, reference counting and deallocation of
     43  * vertex buffer objects itself.
     44  *
     45  * The DrawPrimitives() callback can be either implemented by the
     46  * driver itself or hooked to the tnl module's _tnl_draw_primitives()
     47  * function for hardware without tnl capablilties or during fallbacks.
     48  */
     49 
     50 
     51 #ifndef _VBO_CONTEXT_H
     52 #define _VBO_CONTEXT_H
     53 
     54 #include "vbo.h"
     55 #include "vbo_attrib.h"
     56 #include "vbo_exec.h"
     57 #include "vbo_save.h"
     58 
     59 #include "main/macros.h"
     60 
     61 #ifdef __cplusplus
     62 extern "C" {
     63 #endif
     64 
     65 struct vbo_context {
     66    struct gl_vertex_array currval[VBO_ATTRIB_MAX];
     67 
     68    /** Map VERT_ATTRIB_x to VBO_ATTRIB_y */
     69    GLuint map_vp_none[VERT_ATTRIB_MAX];
     70    GLuint map_vp_arb[VERT_ATTRIB_MAX];
     71 
     72    struct vbo_exec_context exec;
     73    struct vbo_save_context save;
     74 
     75    /* Callback into the driver.  This must always succeed, the driver
     76     * is responsible for initiating any fallback actions required:
     77     */
     78    vbo_draw_func draw_prims;
     79 
     80    /* Optional callback for indirect draws. This allows multidraws to not be
     81     * broken up, as well as for the actual count to be passed in as a separate
     82     * indirect parameter.
     83     */
     84    vbo_indirect_draw_func draw_indirect_prims;
     85 };
     86 
     87 
     88 static inline struct vbo_context *vbo_context(struct gl_context *ctx)
     89 {
     90    return ctx->vbo_context;
     91 }
     92 
     93 
     94 /**
     95  * Return VP_x token to indicate whether we're running fixed-function
     96  * vertex transformation, an NV vertex program or ARB vertex program/shader.
     97  */
     98 static inline enum vp_mode
     99 get_program_mode( struct gl_context *ctx )
    100 {
    101    if (!ctx->VertexProgram._Current)
    102       return VP_NONE;
    103    else if (ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram)
    104       return VP_NONE;
    105    else
    106       return VP_ARB;
    107 }
    108 
    109 
    110 /**
    111  * This is called by glBegin, glDrawArrays and glDrawElements (and
    112  * variations of those calls).  When we transition from immediate mode
    113  * drawing to array drawing we need to invalidate the array state.
    114  *
    115  * glBegin/End builds vertex arrays.  Those arrays may look identical
    116  * to glDrawArrays arrays except that the position of the elements may
    117  * be different.  For example, arrays of (position3v, normal3f) vs. arrays
    118  * of (normal3f, position3f).  So we need to make sure we notify drivers
    119  * that arrays may be changing.
    120  */
    121 static inline void
    122 vbo_draw_method(struct vbo_context *vbo, gl_draw_method method)
    123 {
    124    struct gl_context *ctx = vbo->exec.ctx;
    125 
    126    if (ctx->Array.DrawMethod != method) {
    127       switch (method) {
    128       case DRAW_ARRAYS:
    129          ctx->Array._DrawArrays = vbo->exec.array.inputs;
    130          break;
    131       case DRAW_BEGIN_END:
    132          ctx->Array._DrawArrays = vbo->exec.vtx.inputs;
    133          break;
    134       case DRAW_DISPLAY_LIST:
    135          ctx->Array._DrawArrays = vbo->save.inputs;
    136          break;
    137       default:
    138          assert(0);
    139       }
    140 
    141       ctx->NewDriverState |= ctx->DriverFlags.NewArray;
    142       ctx->Array.DrawMethod = method;
    143    }
    144 }
    145 
    146 /**
    147  * Return if format is integer. The immediate mode commands only emit floats
    148  * for non-integer types, thus everything else is integer.
    149  */
    150 static inline GLboolean
    151 vbo_attrtype_to_integer_flag(GLenum format)
    152 {
    153    switch (format) {
    154    case GL_FLOAT:
    155    case GL_DOUBLE:
    156       return GL_FALSE;
    157    case GL_INT:
    158    case GL_UNSIGNED_INT:
    159       return GL_TRUE;
    160    default:
    161       assert(0);
    162       return GL_FALSE;
    163    }
    164 }
    165 
    166 static inline GLboolean
    167 vbo_attrtype_to_double_flag(GLenum format)
    168 {
    169    switch (format) {
    170    case GL_FLOAT:
    171    case GL_INT:
    172    case GL_UNSIGNED_INT:
    173       return GL_FALSE;
    174    case GL_DOUBLE:
    175       return GL_TRUE;
    176    default:
    177       assert(0);
    178       return GL_FALSE;
    179    }
    180 }
    181 
    182 /**
    183  * Return default component values for the given format.
    184  * The return type is an array of fi_types, because that's how we declare
    185  * the vertex storage : floats , integers or unsigned integers.
    186  */
    187 static inline const fi_type *
    188 vbo_get_default_vals_as_union(GLenum format)
    189 {
    190    static const GLfloat default_float[4] = { 0, 0, 0, 1 };
    191    static const GLint default_int[4] = { 0, 0, 0, 1 };
    192 
    193    switch (format) {
    194    case GL_FLOAT:
    195       return (fi_type *)default_float;
    196    case GL_INT:
    197    case GL_UNSIGNED_INT:
    198       return (fi_type *)default_int;
    199    default:
    200       assert(0);
    201       return NULL;
    202    }
    203 }
    204 
    205 
    206 /**
    207  * Compute the max number of vertices which can be stored in
    208  * a vertex buffer, given the current vertex size, and the amount
    209  * of space already used.
    210  */
    211 static inline unsigned
    212 vbo_compute_max_verts(const struct vbo_exec_context *exec)
    213 {
    214    unsigned n = (VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
    215       (exec->vtx.vertex_size * sizeof(GLfloat));
    216    if (n == 0)
    217       return 0;
    218    /* Subtract one so we're always sure to have room for an extra
    219     * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.
    220     */
    221    n--;
    222    return n;
    223 }
    224 
    225 
    226 #ifdef __cplusplus
    227 } // extern "C"
    228 #endif
    229 
    230 #endif
    231