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