OSDN Git Service

draw: don't crash on vertex buffer overflow
[android-x86/external-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 /**
49  * Set the (private) draw module's post-transformed vertex format when in
50  * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
51  */
52 static void
53 set_feedback_vertex_format(struct gl_context *ctx)
54 {
55 #if 0
56    struct st_context *st = st_context(ctx);
57    struct vertex_info vinfo;
58    GLuint i;
59
60    memset(&vinfo, 0, sizeof(vinfo));
61
62    if (ctx->RenderMode == GL_SELECT) {
63       assert(ctx->RenderMode == GL_SELECT);
64       vinfo.num_attribs = 1;
65       vinfo.format[0] = FORMAT_4F;
66       vinfo.interp_mode[0] = INTERP_LINEAR;
67    }
68    else {
69       /* GL_FEEDBACK, or glRasterPos */
70       /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
71       vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
72       for (i = 0; i < vinfo.num_attribs; i++) {
73          vinfo.format[i] = FORMAT_4F;
74          vinfo.interp_mode[i] = INTERP_LINEAR;
75       }
76    }
77
78    draw_set_vertex_info(st->draw, &vinfo);
79 #endif
80 }
81
82
83 /**
84  * Called by VBO to draw arrays when in selection or feedback mode and
85  * to implement glRasterPos.
86  * This is very much like the normal draw_vbo() function above.
87  * Look at code refactoring some day.
88  */
89 void
90 st_feedback_draw_vbo(struct gl_context *ctx,
91                      const struct _mesa_prim *prims,
92                      GLuint nr_prims,
93                      const struct _mesa_index_buffer *ib,
94                      GLboolean index_bounds_valid,
95                      GLuint min_index,
96                      GLuint max_index,
97                      struct gl_transform_feedback_object *tfb_vertcount)
98 {
99    struct st_context *st = st_context(ctx);
100    struct pipe_context *pipe = st->pipe;
101    struct draw_context *draw = st->draw;
102    const struct st_vertex_program *vp;
103    const struct pipe_shader_state *vs;
104    struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
105    struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
106    struct pipe_index_buffer ibuffer;
107    struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {NULL};
108    struct pipe_transfer *ib_transfer = NULL;
109    const struct gl_client_array **arrays = ctx->Array._DrawArrays;
110    GLuint attr, i;
111    const GLubyte *low_addr = NULL;
112    const void *mapped_indices = NULL;
113
114    assert(draw);
115
116    st_validate_state(st);
117
118    if (!index_bounds_valid)
119       vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index, nr_prims);
120
121    /* must get these after state validation! */
122    vp = st->vp;
123    vs = &st->vp_variant->tgsi;
124
125    if (!st->vp_variant->draw_shader) {
126       st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
127    }
128
129    /*
130     * Set up the draw module's state.
131     *
132     * We'd like to do this less frequently, but the normal state-update
133     * code sends state updates to the pipe, not to our private draw module.
134     */
135    assert(draw);
136    draw_set_viewport_state(draw, &st->state.viewport);
137    draw_set_clip_state(draw, &st->state.clip);
138    draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
139    draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
140    set_feedback_vertex_format(ctx);
141
142    /* Find the lowest address of the arrays we're drawing */
143    if (vp->num_inputs) {
144       low_addr = arrays[vp->index_to_input[0]]->Ptr;
145
146       for (attr = 1; attr < vp->num_inputs; attr++) {
147          const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
148          low_addr = MIN2(low_addr, start);
149       }
150    }
151
152    /* loop over TGSI shader inputs to determine vertex buffer
153     * and attribute info
154     */
155    for (attr = 0; attr < vp->num_inputs; attr++) {
156       const GLuint mesaAttr = vp->index_to_input[attr];
157       struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
158       void *map;
159
160       if (bufobj && bufobj->Name) {
161          /* Attribute data is in a VBO.
162           * Recall that for VBOs, the gl_client_array->Ptr field is
163           * really an offset from the start of the VBO, not a pointer.
164           */
165          struct st_buffer_object *stobj = st_buffer_object(bufobj);
166          assert(stobj->buffer);
167
168          vbuffers[attr].buffer = NULL;
169          vbuffers[attr].user_buffer = NULL;
170          pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
171          vbuffers[attr].buffer_offset = pointer_to_offset(low_addr);
172          velements[attr].src_offset = arrays[mesaAttr]->Ptr - low_addr;
173
174          /* map the attrib buffer */
175          map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
176                                PIPE_TRANSFER_READ,
177                                &vb_transfer[attr]);
178          draw_set_mapped_vertex_buffer(draw, attr, map,
179                                        vbuffers[attr].buffer->width0);
180       }
181       else {
182          vbuffers[attr].buffer = NULL;
183          vbuffers[attr].user_buffer = arrays[mesaAttr]->Ptr;
184          vbuffers[attr].buffer_offset = 0;
185          velements[attr].src_offset = 0;
186
187          draw_set_mapped_vertex_buffer(draw, attr, vbuffers[attr].user_buffer,
188                                        ~0);
189       }
190
191       /* common-case setup */
192       vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
193       velements[attr].instance_divisor = 0;
194       velements[attr].vertex_buffer_index = attr;
195       velements[attr].src_format = 
196          st_pipe_vertex_format(arrays[mesaAttr]->Type,
197                                arrays[mesaAttr]->Size,
198                                arrays[mesaAttr]->Format,
199                                arrays[mesaAttr]->Normalized,
200                                arrays[mesaAttr]->Integer);
201       assert(velements[attr].src_format);
202
203       /* tell draw about this attribute */
204 #if 0
205       draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
206 #endif
207    }
208
209    draw_set_vertex_buffers(draw, 0, vp->num_inputs, vbuffers);
210    draw_set_vertex_elements(draw, vp->num_inputs, velements);
211
212    memset(&ibuffer, 0, sizeof(ibuffer));
213    if (ib) {
214       struct gl_buffer_object *bufobj = ib->obj;
215
216       ibuffer.index_size = vbo_sizeof_ib_type(ib->type);
217       if (ibuffer.index_size == 0)
218          goto out_unref_vertex;
219
220       if (bufobj && bufobj->Name) {
221          struct st_buffer_object *stobj = st_buffer_object(bufobj);
222
223          pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
224          ibuffer.offset = pointer_to_offset(ib->ptr);
225
226          mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
227                                           PIPE_TRANSFER_READ, &ib_transfer);
228       }
229       else {
230          /* skip setting ibuffer.buffer as the draw module does not use it */
231          mapped_indices = ib->ptr;
232       }
233
234       draw_set_indexes(draw,
235                        (ubyte *) mapped_indices + ibuffer.offset,
236                        ibuffer.index_size);
237    }
238
239    /* set the constant buffer */
240    draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
241                                    st->state.constants[PIPE_SHADER_VERTEX].ptr,
242                                    st->state.constants[PIPE_SHADER_VERTEX].size);
243
244
245    /* draw here */
246    for (i = 0; i < nr_prims; i++) {
247       draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
248    }
249
250
251    /*
252     * unmap vertex/index buffers
253     */
254    if (ib) {
255       draw_set_indexes(draw, NULL, 0);
256       if (ib_transfer)
257          pipe_buffer_unmap(pipe, ib_transfer);
258       pipe_resource_reference(&ibuffer.buffer, NULL);
259    }
260
261  out_unref_vertex:
262    for (attr = 0; attr < vp->num_inputs; attr++) {
263       if (vb_transfer[attr])
264          pipe_buffer_unmap(pipe, vb_transfer[attr]);
265       draw_set_mapped_vertex_buffer(draw, attr, NULL, 0);
266       pipe_resource_reference(&vbuffers[attr].buffer, NULL);
267    }
268    draw_set_vertex_buffers(draw, 0, vp->num_inputs, NULL);
269 }