Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  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. /* Author:
  26.  *    Keith Whitwell <keith@tungstengraphics.com>
  27.  */
  28.  
  29. #include "main/glheader.h"
  30. #include "main/bufferobj.h"
  31. #include "main/context.h"
  32. #include "main/imports.h"
  33. #include "main/mtypes.h"
  34. #include "main/macros.h"
  35. #include "main/light.h"
  36. #include "main/state.h"
  37.  
  38. #include "vbo_context.h"
  39.  
  40.  
  41. /**
  42.  * After playback, copy everything but the position from the
  43.  * last vertex to the saved state
  44.  */
  45. static void
  46. _playback_copy_to_current(struct gl_context *ctx,
  47.                           const struct vbo_save_vertex_list *node)
  48. {
  49.    struct vbo_context *vbo = vbo_context(ctx);
  50.    GLfloat vertex[VBO_ATTRIB_MAX * 4];
  51.    GLfloat *data;
  52.    GLuint i, offset;
  53.  
  54.    if (node->current_size == 0)
  55.       return;
  56.  
  57.    if (node->current_data) {
  58.       data = node->current_data;
  59.    }
  60.    else {
  61.       data = vertex;
  62.  
  63.       if (node->count)
  64.          offset = (node->buffer_offset +
  65.                    (node->count-1) * node->vertex_size * sizeof(GLfloat));
  66.       else
  67.          offset = node->buffer_offset;
  68.  
  69.       ctx->Driver.GetBufferSubData( ctx, offset,
  70.                                     node->vertex_size * sizeof(GLfloat),
  71.                                     data, node->vertex_store->bufferobj );
  72.  
  73.       data += node->attrsz[0]; /* skip vertex position */
  74.    }
  75.  
  76.    for (i = VBO_ATTRIB_POS+1 ; i < VBO_ATTRIB_MAX ; i++) {
  77.       if (node->attrsz[i]) {
  78.          GLfloat *current = (GLfloat *)vbo->currval[i].Ptr;
  79.          GLfloat tmp[4];
  80.  
  81.          COPY_CLEAN_4V_TYPE_AS_FLOAT(tmp,
  82.                                      node->attrsz[i],
  83.                                      data,
  84.                                      node->attrtype[i]);
  85.          
  86.          if (node->attrtype[i] != vbo->currval[i].Type ||
  87.              memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
  88.             memcpy(current, tmp, 4 * sizeof(GLfloat));
  89.  
  90.             vbo->currval[i].Size = node->attrsz[i];
  91.             vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
  92.             vbo->currval[i].Type = node->attrtype[i];
  93.             vbo->currval[i].Integer =
  94.                   vbo_attrtype_to_integer_flag(node->attrtype[i]);
  95.  
  96.             if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
  97.                 i <= VBO_ATTRIB_LAST_MATERIAL)
  98.                ctx->NewState |= _NEW_LIGHT;
  99.  
  100.             ctx->NewState |= _NEW_CURRENT_ATTRIB;
  101.          }
  102.  
  103.          data += node->attrsz[i];
  104.       }
  105.    }
  106.  
  107.    /* Colormaterial -- this kindof sucks.
  108.     */
  109.    if (ctx->Light.ColorMaterialEnabled) {
  110.       _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
  111.    }
  112.  
  113.    /* CurrentExecPrimitive
  114.     */
  115.    if (node->prim_count) {
  116.       const struct _mesa_prim *prim = &node->prim[node->prim_count - 1];
  117.       if (prim->end)
  118.          ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
  119.       else
  120.          ctx->Driver.CurrentExecPrimitive = prim->mode;
  121.    }
  122. }
  123.  
  124.  
  125.  
  126. /**
  127.  * Treat the vertex storage as a VBO, define vertex arrays pointing
  128.  * into it:
  129.  */
  130. static void vbo_bind_vertex_list(struct gl_context *ctx,
  131.                                  const struct vbo_save_vertex_list *node)
  132. {
  133.    struct vbo_context *vbo = vbo_context(ctx);
  134.    struct vbo_save_context *save = &vbo->save;
  135.    struct gl_client_array *arrays = save->arrays;
  136.    GLuint buffer_offset = node->buffer_offset;
  137.    const GLuint *map;
  138.    GLuint attr;
  139.    GLubyte node_attrsz[VBO_ATTRIB_MAX];  /* copy of node->attrsz[] */
  140.    GLenum node_attrtype[VBO_ATTRIB_MAX];  /* copy of node->attrtype[] */
  141.    GLbitfield64 varying_inputs = 0x0;
  142.  
  143.    memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
  144.    memcpy(node_attrtype, node->attrtype, sizeof(node->attrtype));
  145.  
  146.    /* Install the default (ie Current) attributes first, then overlay
  147.     * all active ones.
  148.     */
  149.    switch (get_program_mode(ctx)) {
  150.    case VP_NONE:
  151.       for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
  152.          save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
  153.       }
  154.       for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
  155.          save->inputs[VERT_ATTRIB_GENERIC(attr)] =
  156.             &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+attr];
  157.       }
  158.       map = vbo->map_vp_none;
  159.       break;
  160.    case VP_ARB:
  161.       for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
  162.          save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS+attr];
  163.       }
  164.       for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
  165.          save->inputs[VERT_ATTRIB_GENERIC(attr)] =
  166.             &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
  167.       }
  168.       map = vbo->map_vp_arb;
  169.  
  170.       /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
  171.        * In that case we effectively need to route the data from
  172.        * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
  173.        */
  174.       if ((ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_POS) == 0 &&
  175.           (ctx->VertexProgram._Current->Base.InputsRead & VERT_BIT_GENERIC0)) {
  176.          save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[0];
  177.          node_attrsz[VERT_ATTRIB_GENERIC0] = node_attrsz[0];
  178.          node_attrtype[VERT_ATTRIB_GENERIC0] = node_attrtype[0];
  179.          node_attrsz[0] = 0;
  180.       }
  181.       break;
  182.    default:
  183.       assert(0);
  184.    }
  185.  
  186.    for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
  187.       const GLuint src = map[attr];
  188.  
  189.       if (node_attrsz[src]) {
  190.          /* override the default array set above */
  191.          save->inputs[attr] = &arrays[attr];
  192.  
  193.          arrays[attr].Ptr = (const GLubyte *) NULL + buffer_offset;
  194.          arrays[attr].Size = node_attrsz[src];
  195.          arrays[attr].StrideB = node->vertex_size * sizeof(GLfloat);
  196.          arrays[attr].Stride = node->vertex_size * sizeof(GLfloat);
  197.          arrays[attr].Type = node_attrtype[src];
  198.          arrays[attr].Integer =
  199.                vbo_attrtype_to_integer_flag(node_attrtype[src]);
  200.          arrays[attr].Format = GL_RGBA;
  201.          arrays[attr].Enabled = 1;
  202.          arrays[attr]._ElementSize = arrays[attr].Size * sizeof(GLfloat);
  203.          _mesa_reference_buffer_object(ctx,
  204.                                        &arrays[attr].BufferObj,
  205.                                        node->vertex_store->bufferobj);
  206.          arrays[attr]._MaxElement = node->count; /* ??? */
  207.          
  208.          assert(arrays[attr].BufferObj->Name);
  209.  
  210.          buffer_offset += node_attrsz[src] * sizeof(GLfloat);
  211.          varying_inputs |= VERT_BIT(attr);
  212.       }
  213.    }
  214.  
  215.    _mesa_set_varying_vp_inputs( ctx, varying_inputs );
  216.    ctx->NewDriverState |= ctx->DriverFlags.NewArray;
  217. }
  218.  
  219.  
  220. static void
  221. vbo_save_loopback_vertex_list(struct gl_context *ctx,
  222.                               const struct vbo_save_vertex_list *list)
  223. {
  224.    const char *buffer =
  225.       ctx->Driver.MapBufferRange(ctx, 0,
  226.                                  list->vertex_store->bufferobj->Size,
  227.                                  GL_MAP_READ_BIT, /* ? */
  228.                                  list->vertex_store->bufferobj);
  229.  
  230.    vbo_loopback_vertex_list(ctx,
  231.                             (const GLfloat *)(buffer + list->buffer_offset),
  232.                             list->attrsz,
  233.                             list->prim,
  234.                             list->prim_count,
  235.                             list->wrap_count,
  236.                             list->vertex_size);
  237.  
  238.    ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj);
  239. }
  240.  
  241.  
  242. /**
  243.  * Execute the buffer and save copied verts.
  244.  * This is called from the display list code when executing
  245.  * a drawing command.
  246.  */
  247. void
  248. vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
  249. {
  250.    const struct vbo_save_vertex_list *node =
  251.       (const struct vbo_save_vertex_list *) data;
  252.    struct vbo_save_context *save = &vbo_context(ctx)->save;
  253.    GLboolean remap_vertex_store = GL_FALSE;
  254.  
  255.    if (save->vertex_store && save->vertex_store->buffer) {
  256.       /* The vertex store is currently mapped but we're about to replay
  257.        * a display list.  This can happen when a nested display list is
  258.        * being build with GL_COMPILE_AND_EXECUTE.
  259.        * We never want to have mapped vertex buffers when we're drawing.
  260.        * Unmap the vertex store, execute the list, then remap the vertex
  261.        * store.
  262.        */
  263.       vbo_save_unmap_vertex_store(ctx, save->vertex_store);
  264.       remap_vertex_store = GL_TRUE;
  265.    }
  266.  
  267.    FLUSH_CURRENT(ctx, 0);
  268.  
  269.    if (node->prim_count > 0) {
  270.  
  271.       if (_mesa_inside_begin_end(ctx) && node->prim[0].begin) {
  272.          /* Error: we're about to begin a new primitive but we're already
  273.           * inside a glBegin/End pair.
  274.           */
  275.          _mesa_error(ctx, GL_INVALID_OPERATION,
  276.                      "draw operation inside glBegin/End");
  277.          goto end;
  278.       }
  279.       else if (save->replay_flags) {
  280.          /* Various degnerate cases: translate into immediate mode
  281.           * calls rather than trying to execute in place.
  282.           */
  283.          vbo_save_loopback_vertex_list( ctx, node );
  284.  
  285.          goto end;
  286.       }
  287.      
  288.       if (ctx->NewState)
  289.          _mesa_update_state( ctx );
  290.  
  291.       /* XXX also need to check if shader enabled, but invalid */
  292.       if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
  293.           (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
  294.          _mesa_error(ctx, GL_INVALID_OPERATION,
  295.                      "glBegin (invalid vertex/fragment program)");
  296.          return;
  297.       }
  298.  
  299.       vbo_bind_vertex_list( ctx, node );
  300.  
  301.       vbo_draw_method(vbo_context(ctx), DRAW_DISPLAY_LIST);
  302.  
  303.       /* Again...
  304.        */
  305.       if (ctx->NewState)
  306.          _mesa_update_state( ctx );
  307.  
  308.       if (node->count > 0) {
  309.          vbo_context(ctx)->draw_prims(ctx,
  310.                                       node->prim,
  311.                                       node->prim_count,
  312.                                       NULL,
  313.                                       GL_TRUE,
  314.                                       0,    /* Node is a VBO, so this is ok */
  315.                                       node->count - 1,
  316.                                       NULL);
  317.       }
  318.    }
  319.  
  320.    /* Copy to current?
  321.     */
  322.    _playback_copy_to_current( ctx, node );
  323.  
  324. end:
  325.    if (remap_vertex_store) {
  326.       save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
  327.    }
  328. }
  329.