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