OSDN Git Service

draw: don't crash on vertex buffer overflow
[android-x86/external-mesa.git] / src / gallium / drivers / llvmpipe / lp_draw_arrays.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 /* Author:
29  *    Brian Paul
30  *    Keith Whitwell
31  */
32
33
34 #include "pipe/p_defines.h"
35 #include "pipe/p_context.h"
36 #include "util/u_prim.h"
37
38 #include "lp_context.h"
39 #include "lp_state.h"
40 #include "lp_query.h"
41
42 #include "draw/draw_context.h"
43
44
45
46 /**
47  * Draw vertex arrays, with optional indexing, optional instancing.
48  * All the other drawing functions are implemented in terms of this function.
49  * Basically, map the vertex buffers (and drawing surfaces), then hand off
50  * the drawing to the 'draw' module.
51  */
52 static void
53 llvmpipe_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
54 {
55    struct llvmpipe_context *lp = llvmpipe_context(pipe);
56    struct draw_context *draw = lp->draw;
57    const void *mapped_indices = NULL;
58    unsigned i;
59
60    if (!llvmpipe_check_render_cond(lp))
61       return;
62
63    if (lp->dirty)
64       llvmpipe_update_derived( lp );
65
66    /*
67     * Map vertex buffers
68     */
69    for (i = 0; i < lp->num_vertex_buffers; i++) {
70       const void *buf = lp->vertex_buffer[i].user_buffer;
71       size_t size = ~0;
72       if (!buf) {
73          if (!lp->vertex_buffer[i].buffer) {
74             continue;
75          }
76          buf = llvmpipe_resource_data(lp->vertex_buffer[i].buffer);
77          size = lp->vertex_buffer[i].buffer->width0;
78       }
79       draw_set_mapped_vertex_buffer(draw, i, buf, size);
80    }
81
82    /* Map index buffer, if present */
83    if (info->indexed) {
84       mapped_indices = lp->index_buffer.user_buffer;
85       if (!mapped_indices)
86          mapped_indices = llvmpipe_resource_data(lp->index_buffer.buffer);
87
88       draw_set_indexes(draw,
89                        (ubyte *) mapped_indices + lp->index_buffer.offset,
90                        lp->index_buffer.index_size);
91    }
92
93    for (i = 0; i < lp->num_so_targets; i++) {
94       void *buf = 0;
95       if (lp->so_targets[i]) {
96          buf = llvmpipe_resource(lp->so_targets[i]->target.buffer)->data;
97          lp->so_targets[i]->mapping = buf;
98       }
99    }
100    draw_set_mapped_so_targets(draw, lp->num_so_targets,
101                               lp->so_targets);
102
103    llvmpipe_prepare_vertex_sampling(lp,
104                                     lp->num_sampler_views[PIPE_SHADER_VERTEX],
105                                     lp->sampler_views[PIPE_SHADER_VERTEX]);
106    llvmpipe_prepare_geometry_sampling(lp,
107                                       lp->num_sampler_views[PIPE_SHADER_GEOMETRY],
108                                       lp->sampler_views[PIPE_SHADER_GEOMETRY]);
109    if (lp->gs && !lp->gs->shader.tokens) {
110       /* we have an empty geometry shader with stream output, so
111          attach the stream output info to the current vertex shader */
112       if (lp->vs) {
113          draw_vs_attach_so(lp->vs->draw_data, &lp->gs->shader.stream_output);
114       }
115    }
116    draw_collect_pipeline_statistics(draw,
117                                     lp->active_statistics_queries > 0);
118
119    /* draw! */
120    draw_vbo(draw, info);
121
122    /*
123     * unmap vertex/index buffers
124     */
125    for (i = 0; i < lp->num_vertex_buffers; i++) {
126       draw_set_mapped_vertex_buffer(draw, i, NULL, 0);
127    }
128    if (mapped_indices) {
129       draw_set_indexes(draw, NULL, 0);
130    }
131    draw_set_mapped_so_targets(draw, 0, NULL);
132
133    if (lp->gs && !lp->gs->shader.tokens) {
134       /* we have attached stream output to the vs for rendering,
135          now lets reset it */
136       if (lp->vs) {
137          draw_vs_reset_so(lp->vs->draw_data);
138       }
139    }
140    
141    llvmpipe_cleanup_vertex_sampling(lp);
142    llvmpipe_cleanup_geometry_sampling(lp);
143
144    /*
145     * TODO: Flush only when a user vertex/index buffer is present
146     * (or even better, modify draw module to do this
147     * internally when this condition is seen?)
148     */
149    draw_flush(draw);
150 }
151
152
153 void
154 llvmpipe_init_draw_funcs(struct llvmpipe_context *llvmpipe)
155 {
156    llvmpipe->pipe.draw_vbo = llvmpipe_draw_vbo;
157 }