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