Merge remote branch 'upstream/gallium-0.1' into nouveau-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/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 }
59
60 static void
61 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
62 {
63 struct pipe_winsys *ws = sp->pipe.winsys;
64 uint i;
65
66 /* really need to flush all prims since the vert/frag shaders const buffers
67 * are going away now.
68 */
69 draw_flush(sp->draw);
70
71 draw_set_mapped_constant_buffer(sp->draw, NULL);
72
73 for (i = 0; i < 2; i++) {
74 if (sp->constants[i].size)
75 ws->buffer_unmap(ws, sp->constants[i].buffer);
76 sp->mapped_constants[i] = NULL;
77 }
78 }
79
80
81 boolean
82 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
83 unsigned start, unsigned count)
84 {
85 return softpipe_draw_elements(pipe, NULL, 0, mode, start, count);
86 }
87
88
89
90 /**
91 * Draw vertex arrays, with optional indexing.
92 * Basically, map the vertex buffers (and drawing surfaces), then hand off
93 * the drawing to the 'draw' module.
94 *
95 * XXX should the element buffer be specified/bound with a separate function?
96 */
97 boolean
98 softpipe_draw_elements(struct pipe_context *pipe,
99 struct pipe_buffer *indexBuffer,
100 unsigned indexSize,
101 unsigned mode, unsigned start, unsigned count)
102 {
103 struct softpipe_context *sp = softpipe_context(pipe);
104 struct draw_context *draw = sp->draw;
105 unsigned i;
106
107 /* first, check that the primitive is not malformed. It is the
108 * state tracker's responsibility to do send only correctly formed
109 * primitives down. It currently isn't doing that though...
110 */
111 #if 1
112 count = draw_trim_prim( mode, count );
113 #else
114 if (!draw_validate_prim( mode, count ))
115 assert(0);
116 #endif
117
118
119 if (sp->dirty)
120 softpipe_update_derived( sp );
121
122 softpipe_map_surfaces(sp);
123 softpipe_map_constant_buffers(sp);
124
125 /*
126 * Map vertex buffers
127 */
128 for (i = 0; i < sp->num_vertex_buffers; i++) {
129 void *buf
130 = pipe->winsys->buffer_map(pipe->winsys,
131 sp->vertex_buffer[i].buffer,
132 PIPE_BUFFER_USAGE_CPU_READ);
133 draw_set_mapped_vertex_buffer(draw, i, buf);
134 }
135 /* Map index buffer, if present */
136 if (indexBuffer) {
137 void *mapped_indexes
138 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
139 PIPE_BUFFER_USAGE_CPU_READ);
140 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
141 }
142 else {
143 /* no index/element buffer */
144 draw_set_mapped_element_buffer(draw, 0, NULL);
145 }
146
147
148 /* draw! */
149 draw_arrays(draw, mode, start, count);
150
151 /*
152 * unmap vertex/index buffers - will cause draw module to flush
153 */
154 for (i = 0; i < sp->num_vertex_buffers; i++) {
155 draw_set_mapped_vertex_buffer(draw, i, NULL);
156 pipe->winsys->buffer_unmap(pipe->winsys, sp->vertex_buffer[i].buffer);
157 }
158 if (indexBuffer) {
159 draw_set_mapped_element_buffer(draw, 0, NULL);
160 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
161 }
162
163
164 /* Note: leave drawing surfaces mapped */
165 softpipe_unmap_constant_buffers(sp);
166
167 return TRUE;
168 }