st_draw_feedback.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 #include "main/imports.h"
00029 #include "main/image.h"
00030 #include "main/macros.h"
00031 #include "shader/prog_uniform.h"
00032 
00033 #include "vbo/vbo.h"
00034 
00035 #include "st_context.h"
00036 #include "st_atom.h"
00037 #include "st_cb_bufferobjects.h"
00038 #include "st_draw.h"
00039 #include "st_program.h"
00040 
00041 #include "pipe/p_context.h"
00042 #include "pipe/p_defines.h"
00043 #include "pipe/p_inlines.h"
00044 
00045 #include "draw/draw_private.h"
00046 #include "draw/draw_context.h"
00047 
00048 
00049 #if FEATURE_feedback || FEATURE_drawpix
00050 
00055 static void
00056 set_feedback_vertex_format(GLcontext *ctx)
00057 {
00058 #if 0
00059    struct st_context *st = ctx->st;
00060    struct vertex_info vinfo;
00061    GLuint i;
00062 
00063    memset(&vinfo, 0, sizeof(vinfo));
00064 
00065    if (ctx->RenderMode == GL_SELECT) {
00066       assert(ctx->RenderMode == GL_SELECT);
00067       vinfo.num_attribs = 1;
00068       vinfo.format[0] = FORMAT_4F;
00069       vinfo.interp_mode[0] = INTERP_LINEAR;
00070    }
00071    else {
00072       /* GL_FEEDBACK, or glRasterPos */
00073       /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
00074       vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
00075       for (i = 0; i < vinfo.num_attribs; i++) {
00076          vinfo.format[i] = FORMAT_4F;
00077          vinfo.interp_mode[i] = INTERP_LINEAR;
00078       }
00079    }
00080 
00081    draw_set_vertex_info(st->draw, &vinfo);
00082 #endif
00083 }
00084 
00085 
00093 void
00094 st_feedback_draw_vbo(GLcontext *ctx,
00095                      const struct gl_client_array **arrays,
00096                      const struct _mesa_prim *prims,
00097                      GLuint nr_prims,
00098                      const struct _mesa_index_buffer *ib,
00099                      GLuint min_index,
00100                      GLuint max_index)
00101 {
00102    struct st_context *st = ctx->st;
00103    struct pipe_context *pipe = st->pipe;
00104    struct draw_context *draw = st->draw;
00105    const struct st_vertex_program *vp;
00106    const struct pipe_shader_state *vs;
00107    struct pipe_buffer *index_buffer_handle = 0;
00108    struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
00109    struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
00110    GLuint attr, i;
00111    ubyte *mapped_constants;
00112 
00113    assert(draw);
00114 
00115    st_validate_state(ctx->st);
00116 
00117    /* must get these after state validation! */
00118    vp = ctx->st->vp;
00119    vs = &st->vp->state;
00120 
00121    if (!st->vp->draw_shader) {
00122       st->vp->draw_shader = draw_create_vertex_shader(draw, vs);
00123    }
00124 
00125    /*
00126     * Set up the draw module's state.
00127     *
00128     * We'd like to do this less frequently, but the normal state-update
00129     * code sends state updates to the pipe, not to our private draw module.
00130     */
00131    assert(draw);
00132    draw_set_viewport_state(draw, &st->state.viewport);
00133    draw_set_clip_state(draw, &st->state.clip);
00134    draw_set_rasterizer_state(draw, &st->state.rasterizer);
00135    draw_bind_vertex_shader(draw, st->vp->draw_shader);
00136    set_feedback_vertex_format(ctx);
00137 
00138    /* loop over TGSI shader inputs to determine vertex buffer
00139     * and attribute info
00140     */
00141    for (attr = 0; attr < vp->num_inputs; attr++) {
00142       const GLuint mesaAttr = vp->index_to_input[attr];
00143       struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
00144       void *map;
00145 
00146       if (bufobj && bufobj->Name) {
00147          /* Attribute data is in a VBO.
00148           * Recall that for VBOs, the gl_client_array->Ptr field is
00149           * really an offset from the start of the VBO, not a pointer.
00150           */
00151          struct st_buffer_object *stobj = st_buffer_object(bufobj);
00152          assert(stobj->buffer);
00153 
00154          vbuffers[attr].buffer = NULL;
00155          pipe_buffer_reference(pipe->screen, &vbuffers[attr].buffer, stobj->buffer);
00156          vbuffers[attr].buffer_offset = (unsigned) arrays[0]->Ptr;/* in bytes */
00157          velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
00158       }
00159       else {
00160          /* attribute data is in user-space memory, not a VBO */
00161          uint bytes = (arrays[mesaAttr]->Size
00162                        * _mesa_sizeof_type(arrays[mesaAttr]->Type)
00163                        * (max_index + 1));
00164 
00165          /* wrap user data */
00166          vbuffers[attr].buffer
00167             = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
00168                                       bytes);
00169          vbuffers[attr].buffer_offset = 0;
00170          velements[attr].src_offset = 0;
00171       }
00172 
00173       /* common-case setup */
00174       vbuffers[attr].pitch = arrays[mesaAttr]->StrideB; /* in bytes */
00175       vbuffers[attr].max_index = max_index;
00176       velements[attr].vertex_buffer_index = attr;
00177       velements[attr].nr_components = arrays[mesaAttr]->Size;
00178       velements[attr].src_format = 
00179          st_pipe_vertex_format(arrays[mesaAttr]->Type,
00180                                arrays[mesaAttr]->Size,
00181                                arrays[mesaAttr]->Normalized);
00182       assert(velements[attr].src_format);
00183 
00184       /* tell draw about this attribute */
00185 #if 0
00186       draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
00187 #endif
00188 
00189       /* map the attrib buffer */
00190       map = pipe_buffer_map(pipe->screen, vbuffers[attr].buffer,
00191                             PIPE_BUFFER_USAGE_CPU_READ);
00192       draw_set_mapped_vertex_buffer(draw, attr, map);
00193    }
00194 
00195    draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
00196    draw_set_vertex_elements(draw, vp->num_inputs, velements);
00197 
00198    if (ib) {
00199       unsigned indexSize;
00200       struct gl_buffer_object *bufobj = ib->obj;
00201       struct st_buffer_object *stobj = st_buffer_object(bufobj);
00202       void *map;
00203 
00204       index_buffer_handle = stobj->buffer;
00205 
00206       switch (ib->type) {
00207       case GL_UNSIGNED_INT:
00208          indexSize = 4;
00209          break;
00210       case GL_UNSIGNED_SHORT:
00211          indexSize = 2;
00212          break;
00213       default:
00214          assert(0);
00215          return;
00216       }
00217 
00218       map = pipe_buffer_map(pipe->screen, index_buffer_handle,
00219                             PIPE_BUFFER_USAGE_CPU_READ);
00220       draw_set_mapped_element_buffer(draw, indexSize, map);
00221    }
00222    else {
00223       /* no index/element buffer */
00224       draw_set_mapped_element_buffer(draw, 0, NULL);
00225    }
00226 
00227 
00228    /* map constant buffers */
00229    mapped_constants = pipe_buffer_map(pipe->screen,
00230                                       st->state.constants[PIPE_SHADER_VERTEX].buffer,
00231                                       PIPE_BUFFER_USAGE_CPU_READ);
00232    draw_set_mapped_constant_buffer(st->draw, mapped_constants,
00233                                    st->state.constants[PIPE_SHADER_VERTEX].buffer->size);
00234 
00235 
00236    /* draw here */
00237    for (i = 0; i < nr_prims; i++) {
00238       draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
00239    }
00240 
00241 
00242    /* unmap constant buffers */
00243    pipe_buffer_unmap(pipe->screen, st->state.constants[PIPE_SHADER_VERTEX].buffer);
00244 
00245    /*
00246     * unmap vertex/index buffers
00247     */
00248    for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
00249       if (draw->pt.vertex_buffer[i].buffer) {
00250          pipe_buffer_unmap(pipe->screen, draw->pt.vertex_buffer[i].buffer);
00251          pipe_buffer_reference(pipe->screen, &draw->pt.vertex_buffer[i].buffer, NULL);
00252          draw_set_mapped_vertex_buffer(draw, i, NULL);
00253       }
00254    }
00255    if (ib) {
00256       pipe_buffer_unmap(pipe->screen, index_buffer_handle);
00257       draw_set_mapped_element_buffer(draw, 0, NULL);
00258    }
00259 }
00260 
00261 #endif /* FEATURE_feedback || FEATURE_drawpix */
00262 

Generated on Tue Sep 29 06:25:18 2009 for Gallium3D by  doxygen 1.5.4