softpipe: fix flat shading provoking vertex for PIPE_PRIM_POLYGON
[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->api_prim = mode;
133 sp->reduced_api_prim = reduced_prim[mode];
134
135 if (sp->dirty)
136 softpipe_update_derived( sp );
137
138 softpipe_map_transfers(sp);
139 softpipe_map_constant_buffers(sp);
140
141 /*
142 * Map vertex buffers
143 */
144 for (i = 0; i < sp->num_vertex_buffers; i++) {
145 void *buf
146 = pipe_buffer_map(pipe->screen,
147 sp->vertex_buffer[i].buffer,
148 PIPE_BUFFER_USAGE_CPU_READ);
149 draw_set_mapped_vertex_buffer(draw, i, buf);
150 }
151 /* Map index buffer, if present */
152 if (indexBuffer) {
153 void *mapped_indexes
154 = pipe_buffer_map(pipe->screen, indexBuffer,
155 PIPE_BUFFER_USAGE_CPU_READ);
156 draw_set_mapped_element_buffer_range(draw, indexSize,
157 min_index,
158 max_index,
159 mapped_indexes);
160 }
161 else {
162 /* no index/element buffer */
163 draw_set_mapped_element_buffer_range(draw, 0, start, start + count - 1, NULL);
164 }
165
166
167 /* draw! */
168 draw_arrays(draw, mode, start, count);
169
170 /*
171 * unmap vertex/index buffers - will cause draw module to flush
172 */
173 for (i = 0; i < sp->num_vertex_buffers; i++) {
174 draw_set_mapped_vertex_buffer(draw, i, NULL);
175 pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
176 }
177 if (indexBuffer) {
178 draw_set_mapped_element_buffer(draw, 0, NULL);
179 pipe_buffer_unmap(pipe->screen, indexBuffer);
180 }
181
182
183 /* Note: leave drawing surfaces mapped */
184 softpipe_unmap_constant_buffers(sp);
185
186 return TRUE;
187 }
188
189 boolean
190 softpipe_draw_elements(struct pipe_context *pipe,
191 struct pipe_buffer *indexBuffer,
192 unsigned indexSize,
193 unsigned mode, unsigned start, unsigned count)
194 {
195 return softpipe_draw_range_elements( pipe, indexBuffer,
196 indexSize,
197 0, 0xffffffff,
198 mode, start, count );
199 }
200
201
202
203 void
204 softpipe_set_edgeflags(struct pipe_context *pipe, const unsigned *edgeflags)
205 {
206 struct softpipe_context *sp = softpipe_context(pipe);
207 draw_set_edgeflags(sp->draw, edgeflags);
208 }
209