OSDN Git Service

4378312bc3622ca0138a5415ae6b4192efb979c4
[android-x86/external-mesa.git] / src / gallium / drivers / softpipe / sp_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_inlines.h"
37 #include "util/u_prim.h"
38
39 #include "sp_context.h"
40 #include "sp_query.h"
41 #include "sp_state.h"
42 #include "sp_texture.h"
43
44 #include "draw/draw_context.h"
45
46 /**
47  * This function handles drawing indexed and non-indexed prims,
48  * instanced and non-instanced drawing, with or without min/max element
49  * indexes.
50  * All the other drawing functions are expressed in terms of this
51  * function.
52  *
53  * For non-indexed prims, indexBuffer should be NULL.
54  * For non-instanced drawing, instanceCount should be 1.
55  * When the min/max element indexes aren't known, minIndex should be 0
56  * and maxIndex should be ~0.
57  */
58 void
59 softpipe_draw_vbo(struct pipe_context *pipe,
60                   const struct pipe_draw_info *info)
61 {
62    struct softpipe_context *sp = softpipe_context(pipe);
63    struct draw_context *draw = sp->draw;
64    const void *mapped_indices = NULL;
65    unsigned i;
66
67    if (!softpipe_check_render_cond(sp))
68       return;
69
70    sp->reduced_api_prim = u_reduced_prim(info->mode);
71
72    if (sp->dirty) {
73       softpipe_update_derived(sp, sp->reduced_api_prim);
74    }
75
76    /* Map vertex buffers */
77    for (i = 0; i < sp->num_vertex_buffers; i++) {
78       const void *buf = sp->vertex_buffer[i].user_buffer;
79       if (!buf) {
80          if (!sp->vertex_buffer[i].buffer) {
81             continue;
82          }
83          buf = softpipe_resource(sp->vertex_buffer[i].buffer)->data;
84       }
85       draw_set_mapped_vertex_buffer(draw, i, buf);
86    }
87
88    /* Map index buffer, if present */
89    if (info->indexed) {
90       mapped_indices = sp->index_buffer.user_buffer;
91       if (!mapped_indices)
92          mapped_indices = softpipe_resource(sp->index_buffer.buffer)->data;
93
94       draw_set_indexes(draw,
95                        (ubyte *) mapped_indices + sp->index_buffer.offset,
96                        sp->index_buffer.index_size);
97    }
98
99
100    for (i = 0; i < sp->num_so_targets; i++) {
101       void *buf = softpipe_resource(sp->so_targets[i]->target.buffer)->data;
102       sp->so_targets[i]->mapping = buf;
103    }
104
105    draw_set_mapped_so_targets(draw, sp->num_so_targets,
106                               sp->so_targets);
107
108    draw_collect_pipeline_statistics(draw,
109                                     sp->active_statistics_queries > 0);
110
111    /* draw! */
112    draw_vbo(draw, info);
113
114    /* unmap vertex/index buffers - will cause draw module to flush */
115    for (i = 0; i < sp->num_vertex_buffers; i++) {
116       draw_set_mapped_vertex_buffer(draw, i, NULL);
117    }
118    if (mapped_indices) {
119       draw_set_indexes(draw, NULL, 0);
120    }
121
122    draw_set_mapped_so_targets(draw, 0, NULL);
123
124    /*
125     * TODO: Flush only when a user vertex/index buffer is present
126     * (or even better, modify draw module to do this
127     * internally when this condition is seen?)
128     */
129    draw_flush(draw);
130
131    /* Note: leave drawing surfaces mapped */
132    sp->dirty_render_cache = TRUE;
133 }