Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 "pipe/p_winsys.h"
37 #include "pipe/p_inlines.h"
38
39 #include "sp_context.h"
40 #include "sp_state.h"
41
42 #include "draw/draw_context.h"
43
44
45
46 static void
47 softpipe_map_constant_buffers(struct softpipe_context *sp)
48 {
49 struct pipe_winsys *ws = sp->pipe.winsys;
50 uint i;
51 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
52 if (sp->constants[i].size)
53 sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
54 PIPE_BUFFER_USAGE_CPU_READ);
55 }
56
57 draw_set_mapped_constant_buffer(sp->draw,
58 sp->mapped_constants[PIPE_SHADER_VERTEX],
59 sp->constants[PIPE_SHADER_VERTEX].size);
60 }
61
62 static void
63 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
64 {
65 struct pipe_winsys *ws = sp->pipe.winsys;
66 uint i;
67
68 /* really need to flush all prims since the vert/frag shaders const buffers
69 * are going away now.
70 */
71 draw_flush(sp->draw);
72
73 draw_set_mapped_constant_buffer(sp->draw, NULL, 0);
74
75 for (i = 0; i < 2; i++) {
76 if (sp->constants[i].size)
77 ws->buffer_unmap(ws, sp->constants[i].buffer);
78 sp->mapped_constants[i] = NULL;
79 }
80 }
81
82
83 static unsigned reduced_prim[PIPE_PRIM_POLYGON + 1] = {
84 PIPE_PRIM_POINTS,
85 PIPE_PRIM_LINES,
86 PIPE_PRIM_LINES,
87 PIPE_PRIM_LINES,
88 PIPE_PRIM_TRIANGLES,
89 PIPE_PRIM_TRIANGLES,
90 PIPE_PRIM_TRIANGLES,
91 PIPE_PRIM_TRIANGLES,
92 PIPE_PRIM_TRIANGLES,
93 PIPE_PRIM_TRIANGLES
94 };
95
96
97 boolean
98 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
99 unsigned start, unsigned count)
100 {
101 return softpipe_draw_elements(pipe, NULL, 0, mode, start, count);
102 }
103
104
105
106 /**
107 * Draw vertex arrays, with optional indexing.
108 * Basically, map the vertex buffers (and drawing surfaces), then hand off
109 * the drawing to the 'draw' module.
110 *
111 * XXX should the element buffer be specified/bound with a separate function?
112 */
113
114 boolean
115 softpipe_draw_range_elements(struct pipe_context *pipe,
116 struct pipe_buffer *indexBuffer,
117 unsigned indexSize,
118 unsigned min_index,
119 unsigned max_index,
120 unsigned mode, unsigned start, unsigned count)
121 {
122 struct softpipe_context *sp = softpipe_context(pipe);
123 struct draw_context *draw = sp->draw;
124 unsigned i;
125
126 sp->reduced_api_prim = reduced_prim[mode];
127
128 if (sp->dirty)
129 softpipe_update_derived( sp );
130
131 softpipe_map_surfaces(sp);
132 softpipe_map_constant_buffers(sp);
133
134 /*
135 * Map vertex buffers
136 */
137 for (i = 0; i < sp->num_vertex_buffers; i++) {
138 void *buf
139 = pipe_buffer_map(pipe->screen,
140 sp->vertex_buffer[i].buffer,
141 PIPE_BUFFER_USAGE_CPU_READ);
142 draw_set_mapped_vertex_buffer(draw, i, buf);
143 }
144 /* Map index buffer, if present */
145 if (indexBuffer) {
146 void *mapped_indexes
147 = pipe_buffer_map(pipe->screen, indexBuffer,
148 PIPE_BUFFER_USAGE_CPU_READ);
149 draw_set_mapped_element_buffer_range(draw, indexSize,
150 min_index,
151 max_index,
152 mapped_indexes);
153 }
154 else {
155 /* no index/element buffer */
156 draw_set_mapped_element_buffer_range(draw, 0, start, start + count - 1, NULL);
157 }
158
159
160 /* draw! */
161 draw_arrays(draw, mode, start, count);
162
163 /*
164 * unmap vertex/index buffers - will cause draw module to flush
165 */
166 for (i = 0; i < sp->num_vertex_buffers; i++) {
167 draw_set_mapped_vertex_buffer(draw, i, NULL);
168 pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
169 }
170 if (indexBuffer) {
171 draw_set_mapped_element_buffer(draw, 0, NULL);
172 pipe_buffer_unmap(pipe->screen, indexBuffer);
173 }
174
175
176 /* Note: leave drawing surfaces mapped */
177 softpipe_unmap_constant_buffers(sp);
178
179 return TRUE;
180 }
181
182 boolean
183 softpipe_draw_elements(struct pipe_context *pipe,
184 struct pipe_buffer *indexBuffer,
185 unsigned indexSize,
186 unsigned mode, unsigned start, unsigned count)
187 {
188 return softpipe_draw_range_elements( pipe, indexBuffer,
189 indexSize,
190 0, 0xffffffff,
191 mode, start, count );
192 }
193
194
195
196 void
197 softpipe_set_edgeflags(struct pipe_context *pipe, const unsigned *edgeflags)
198 {
199 struct softpipe_context *sp = softpipe_context(pipe);
200 draw_set_edgeflags(sp->draw, edgeflags);
201 }
202