Squashed commit of the following:
[profile/ivi/mesa.git] / src / mesa / state_tracker / st_draw_feedback.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "main/imports.h"
29 #include "main/image.h"
30 #include "main/macros.h"
31
32 #include "vbo/vbo.h"
33
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_cb_bufferobjects.h"
37 #include "st_draw.h"
38 #include "st_program.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "util/u_inlines.h"
43
44 #include "draw/draw_private.h"
45 #include "draw/draw_context.h"
46
47
48 #if FEATURE_feedback || FEATURE_drawpix
49
50 /**
51  * Set the (private) draw module's post-transformed vertex format when in
52  * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
53  */
54 static void
55 set_feedback_vertex_format(GLcontext *ctx)
56 {
57 #if 0
58    struct st_context *st = ctx->st;
59    struct vertex_info vinfo;
60    GLuint i;
61
62    memset(&vinfo, 0, sizeof(vinfo));
63
64    if (ctx->RenderMode == GL_SELECT) {
65       assert(ctx->RenderMode == GL_SELECT);
66       vinfo.num_attribs = 1;
67       vinfo.format[0] = FORMAT_4F;
68       vinfo.interp_mode[0] = INTERP_LINEAR;
69    }
70    else {
71       /* GL_FEEDBACK, or glRasterPos */
72       /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
73       vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
74       for (i = 0; i < vinfo.num_attribs; i++) {
75          vinfo.format[i] = FORMAT_4F;
76          vinfo.interp_mode[i] = INTERP_LINEAR;
77       }
78    }
79
80    draw_set_vertex_info(st->draw, &vinfo);
81 #endif
82 }
83
84
85 /**
86  * Called by VBO to draw arrays when in selection or feedback mode and
87  * to implement glRasterPos.
88  * This is very much like the normal draw_vbo() function above.
89  * Look at code refactoring some day.
90  * Might move this into the failover module some day.
91  */
92 void
93 st_feedback_draw_vbo(GLcontext *ctx,
94                      const struct gl_client_array **arrays,
95                      const struct _mesa_prim *prims,
96                      GLuint nr_prims,
97                      const struct _mesa_index_buffer *ib,
98                      GLboolean index_bounds_valid,
99                      GLuint min_index,
100                      GLuint max_index)
101 {
102    struct st_context *st = ctx->st;
103    struct pipe_context *pipe = st->pipe;
104    struct draw_context *draw = st->draw;
105    const struct st_vertex_program *vp;
106    const struct pipe_shader_state *vs;
107    struct pipe_resource *index_buffer_handle = 0;
108    struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
109    struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
110    struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
111    struct pipe_transfer *ib_transfer;
112    struct pipe_transfer *cb_transfer;
113    GLuint attr, i;
114    ubyte *mapped_constants;
115
116    assert(draw);
117
118    st_validate_state(ctx->st);
119
120    if (!index_bounds_valid)
121       vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
122
123    /* must get these after state validation! */
124    vp = ctx->st->vp;
125    vs = &st->vp_varient->tgsi;
126
127    if (!st->vp_varient->draw_shader) {
128       st->vp_varient->draw_shader = draw_create_vertex_shader(draw, vs);
129    }
130
131    /*
132     * Set up the draw module's state.
133     *
134     * We'd like to do this less frequently, but the normal state-update
135     * code sends state updates to the pipe, not to our private draw module.
136     */
137    assert(draw);
138    draw_set_viewport_state(draw, &st->state.viewport);
139    draw_set_clip_state(draw, &st->state.clip);
140    draw_set_rasterizer_state(draw, &st->state.rasterizer);
141    draw_bind_vertex_shader(draw, st->vp_varient->draw_shader);
142    set_feedback_vertex_format(ctx);
143
144    /* loop over TGSI shader inputs to determine vertex buffer
145     * and attribute info
146     */
147    for (attr = 0; attr < vp->num_inputs; attr++) {
148       const GLuint mesaAttr = vp->index_to_input[attr];
149       struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
150       void *map;
151
152       if (bufobj && bufobj->Name) {
153          /* Attribute data is in a VBO.
154           * Recall that for VBOs, the gl_client_array->Ptr field is
155           * really an offset from the start of the VBO, not a pointer.
156           */
157          struct st_buffer_object *stobj = st_buffer_object(bufobj);
158          assert(stobj->buffer);
159
160          vbuffers[attr].buffer = NULL;
161          pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
162          vbuffers[attr].buffer_offset = pointer_to_offset(arrays[0]->Ptr);
163          velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
164       }
165       else {
166          /* attribute data is in user-space memory, not a VBO */
167          uint bytes = (arrays[mesaAttr]->Size
168                        * _mesa_sizeof_type(arrays[mesaAttr]->Type)
169                        * (max_index + 1));
170
171          /* wrap user data */
172          vbuffers[attr].buffer
173             = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
174                                       bytes,
175                                       PIPE_BIND_VERTEX_BUFFER);
176          vbuffers[attr].buffer_offset = 0;
177          velements[attr].src_offset = 0;
178       }
179
180       /* common-case setup */
181       vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
182       vbuffers[attr].max_index = max_index;
183       velements[attr].instance_divisor = 0;
184       velements[attr].vertex_buffer_index = attr;
185       velements[attr].src_format = 
186          st_pipe_vertex_format(arrays[mesaAttr]->Type,
187                                arrays[mesaAttr]->Size,
188                                arrays[mesaAttr]->Format,
189                                arrays[mesaAttr]->Normalized);
190       assert(velements[attr].src_format);
191
192       /* tell draw about this attribute */
193 #if 0
194       draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
195 #endif
196
197       /* map the attrib buffer */
198       map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
199                             PIPE_TRANSFER_READ,
200                             &vb_transfer[attr]);
201       draw_set_mapped_vertex_buffer(draw, attr, map);
202    }
203
204    draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
205    draw_set_vertex_elements(draw, vp->num_inputs, velements);
206
207    if (ib) {
208       struct gl_buffer_object *bufobj = ib->obj;
209       unsigned indexSize;
210       void *map;
211
212       switch (ib->type) {
213       case GL_UNSIGNED_INT:
214          indexSize = 4;
215          break;
216       case GL_UNSIGNED_SHORT:
217          indexSize = 2;
218          break;
219       default:
220          assert(0);
221          return;
222       }
223
224       if (bufobj && bufobj->Name) {
225          struct st_buffer_object *stobj = st_buffer_object(bufobj);
226
227          index_buffer_handle = stobj->buffer;
228
229          map = pipe_buffer_map(pipe, index_buffer_handle,
230                                PIPE_TRANSFER_READ, &ib_transfer);
231
232          draw_set_mapped_element_buffer(draw, indexSize, map);
233       }
234       else {
235          draw_set_mapped_element_buffer(draw, indexSize, (void *) ib->ptr);
236          ib_transfer = NULL;
237       }
238    }
239    else {
240       /* no index/element buffer */
241       draw_set_mapped_element_buffer(draw, 0, NULL);
242    }
243
244
245    /* map constant buffers */
246    mapped_constants = pipe_buffer_map(pipe,
247                                       st->state.constants[PIPE_SHADER_VERTEX],
248                                       PIPE_TRANSFER_READ,
249                                       &cb_transfer);
250    draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
251                                    mapped_constants,
252                                    st->state.constants[PIPE_SHADER_VERTEX]->width0);
253
254
255    /* draw here */
256    for (i = 0; i < nr_prims; i++) {
257       draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
258    }
259
260
261    /* unmap constant buffers */
262    pipe_buffer_unmap(pipe, st->state.constants[PIPE_SHADER_VERTEX],
263                      cb_transfer);
264
265    /*
266     * unmap vertex/index buffers
267     */
268    for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
269       if (draw->pt.vertex_buffer[i].buffer) {
270          pipe_buffer_unmap(pipe, draw->pt.vertex_buffer[i].buffer, 
271                            vb_transfer[i]);
272          pipe_resource_reference(&draw->pt.vertex_buffer[i].buffer, NULL);
273          draw_set_mapped_vertex_buffer(draw, i, NULL);
274       }
275    }
276    if (index_buffer_handle) {
277       pipe_buffer_unmap(pipe, index_buffer_handle, ib_transfer);
278       draw_set_mapped_element_buffer(draw, 0, NULL);
279    }
280 }
281
282 #endif /* FEATURE_feedback || FEATURE_drawpix */
283