Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / cell / ppu / cell_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 "cell_context.h"
40 #include "cell_draw_arrays.h"
41 #include "cell_state.h"
42 #include "cell_flush.h"
43
44 #include "draw/draw_context.h"
45
46
47
48 static void
49 cell_map_constant_buffers(struct cell_context *sp)
50 {
51 struct pipe_winsys *ws = sp->pipe.winsys;
52 uint i;
53 for (i = 0; i < 2; i++) {
54 if (sp->constants[i].size) {
55 sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
56 PIPE_BUFFER_USAGE_CPU_READ);
57 cell_flush_buffer_range(sp, sp->mapped_constants[i],
58 sp->constants[i].buffer->size);
59 }
60 }
61
62 draw_set_mapped_constant_buffer(sp->draw,
63 sp->mapped_constants[PIPE_SHADER_VERTEX],
64 sp->constants[PIPE_SHADER_VERTEX].size);
65 }
66
67 static void
68 cell_unmap_constant_buffers(struct cell_context *sp)
69 {
70 struct pipe_winsys *ws = sp->pipe.winsys;
71 uint i;
72 for (i = 0; i < 2; i++) {
73 if (sp->constants[i].size)
74 ws->buffer_unmap(ws, sp->constants[i].buffer);
75 sp->mapped_constants[i] = NULL;
76 }
77 }
78
79
80
81 /**
82 * Draw vertex arrays, with optional indexing.
83 * Basically, map the vertex buffers (and drawing surfaces), then hand off
84 * the drawing to the 'draw' module.
85 *
86 * XXX should the element buffer be specified/bound with a separate function?
87 */
88 static boolean
89 cell_draw_range_elements(struct pipe_context *pipe,
90 struct pipe_buffer *indexBuffer,
91 unsigned indexSize,
92 unsigned min_index,
93 unsigned max_index,
94 unsigned mode, unsigned start, unsigned count)
95 {
96 struct cell_context *sp = cell_context(pipe);
97 struct draw_context *draw = sp->draw;
98 unsigned i;
99
100 if (sp->dirty)
101 cell_update_derived( sp );
102
103 #if 0
104 cell_map_surfaces(sp);
105 #endif
106 cell_map_constant_buffers(sp);
107
108 /*
109 * Map vertex buffers
110 */
111 for (i = 0; i < sp->num_vertex_buffers; i++) {
112 void *buf = pipe_buffer_map(pipe->screen,
113 sp->vertex_buffer[i].buffer,
114 PIPE_BUFFER_USAGE_CPU_READ);
115 cell_flush_buffer_range(sp, buf, sp->vertex_buffer[i].buffer->size);
116 draw_set_mapped_vertex_buffer(draw, i, buf);
117 }
118 /* Map index buffer, if present */
119 if (indexBuffer) {
120 void *mapped_indexes = pipe_buffer_map(pipe->screen,
121 indexBuffer,
122 PIPE_BUFFER_USAGE_CPU_READ);
123 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
124 }
125 else {
126 /* no index/element buffer */
127 draw_set_mapped_element_buffer(draw, 0, NULL);
128 }
129
130
131 /* draw! */
132 draw_arrays(draw, mode, start, count);
133
134 /*
135 * unmap vertex/index buffers - will cause draw module to flush
136 */
137 for (i = 0; i < sp->num_vertex_buffers; i++) {
138 draw_set_mapped_vertex_buffer(draw, i, NULL);
139 pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
140 }
141 if (indexBuffer) {
142 draw_set_mapped_element_buffer(draw, 0, NULL);
143 pipe_buffer_unmap(pipe->screen, indexBuffer);
144 }
145
146 /* Note: leave drawing surfaces mapped */
147 cell_unmap_constant_buffers(sp);
148
149 return TRUE;
150 }
151
152
153 static boolean
154 cell_draw_elements(struct pipe_context *pipe,
155 struct pipe_buffer *indexBuffer,
156 unsigned indexSize,
157 unsigned mode, unsigned start, unsigned count)
158 {
159 return cell_draw_range_elements( pipe, indexBuffer,
160 indexSize,
161 0, 0xffffffff,
162 mode, start, count );
163 }
164
165
166 static boolean
167 cell_draw_arrays(struct pipe_context *pipe, unsigned mode,
168 unsigned start, unsigned count)
169 {
170 return cell_draw_elements(pipe, NULL, 0, mode, start, count);
171 }
172
173
174 static void
175 cell_set_edgeflags(struct pipe_context *pipe, const unsigned *edgeflags)
176 {
177 struct cell_context *cell = cell_context(pipe);
178 draw_set_edgeflags(cell->draw, edgeflags);
179 }
180
181
182
183 void
184 cell_init_draw_functions(struct cell_context *cell)
185 {
186 cell->pipe.draw_arrays = cell_draw_arrays;
187 cell->pipe.draw_elements = cell_draw_elements;
188 cell->pipe.draw_range_elements = cell_draw_range_elements;
189 cell->pipe.set_edgeflags = cell_set_edgeflags;
190 }
191