Merge remote branch 'nouveau/gallium-0.1' into nouveau-gallium-0.1
[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
38 #include "cell_context.h"
39 #include "cell_draw_arrays.h"
40 #include "cell_state.h"
41 #include "cell_flush.h"
42
43 #include "draw/draw_context.h"
44
45
46
47 static void
48 cell_map_constant_buffers(struct cell_context *sp)
49 {
50 struct pipe_winsys *ws = sp->pipe.winsys;
51 uint i;
52 for (i = 0; i < 2; i++) {
53 if (sp->constants[i].size) {
54 sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
55 PIPE_BUFFER_USAGE_CPU_READ);
56 cell_flush_buffer_range(sp, sp->mapped_constants[i],
57 sp->constants[i].buffer->size);
58 }
59 }
60
61 draw_set_mapped_constant_buffer(sp->draw,
62 sp->mapped_constants[PIPE_SHADER_VERTEX]);
63 }
64
65 static void
66 cell_unmap_constant_buffers(struct cell_context *sp)
67 {
68 struct pipe_winsys *ws = sp->pipe.winsys;
69 uint i;
70 for (i = 0; i < 2; i++) {
71 if (sp->constants[i].size)
72 ws->buffer_unmap(ws, sp->constants[i].buffer);
73 sp->mapped_constants[i] = NULL;
74 }
75 }
76
77
78 boolean
79 cell_draw_arrays(struct pipe_context *pipe, unsigned mode,
80 unsigned start, unsigned count)
81 {
82 return cell_draw_elements(pipe, NULL, 0, mode, start, count);
83 }
84
85
86
87 /**
88 * Draw vertex arrays, with optional indexing.
89 * Basically, map the vertex buffers (and drawing surfaces), then hand off
90 * the drawing to the 'draw' module.
91 *
92 * XXX should the element buffer be specified/bound with a separate function?
93 */
94 boolean
95 cell_draw_elements(struct pipe_context *pipe,
96 struct pipe_buffer *indexBuffer,
97 unsigned indexSize,
98 unsigned mode, unsigned start, unsigned count)
99 {
100 struct cell_context *sp = cell_context(pipe);
101 struct draw_context *draw = sp->draw;
102 unsigned i;
103
104 /* first, check that the primitive is not malformed. It is the
105 * state tracker's responsibility to do send only correctly formed
106 * primitives down. It currently isn't doing that though...
107 */
108 #if 1
109 count = draw_trim_prim( mode, count );
110 #else
111 if (!draw_validate_prim( mode, count ))
112 assert(0);
113 #endif
114
115 if (sp->dirty)
116 cell_update_derived( sp );
117
118 #if 0
119 cell_map_surfaces(sp);
120 #endif
121 cell_map_constant_buffers(sp);
122
123 /*
124 * Map vertex buffers
125 */
126 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
127 if (sp->vertex_buffer[i].buffer) {
128 void *buf = pipe->winsys->buffer_map(pipe->winsys,
129 sp->vertex_buffer[i].buffer,
130 PIPE_BUFFER_USAGE_CPU_READ);
131 cell_flush_buffer_range(sp, buf, sp->vertex_buffer[i].buffer->size);
132 draw_set_mapped_vertex_buffer(draw, i, buf);
133 }
134 }
135 /* Map index buffer, if present */
136 if (indexBuffer) {
137 void *mapped_indexes = pipe->winsys->buffer_map(pipe->winsys,
138 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 < PIPE_ATTRIB_MAX; i++) {
155 if (sp->vertex_buffer[i].buffer) {
156 draw_set_mapped_vertex_buffer(draw, i, NULL);
157 pipe->winsys->buffer_unmap(pipe->winsys, sp->vertex_buffer[i].buffer);
158 }
159 }
160 if (indexBuffer) {
161 draw_set_mapped_element_buffer(draw, 0, NULL);
162 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
163 }
164
165 /* Note: leave drawing surfaces mapped */
166 cell_unmap_constant_buffers(sp);
167
168 return TRUE;
169 }