Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_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 #include "util/u_prim.h"
39
40 #include "lp_context.h"
41 #include "lp_state.h"
42
43 #include "draw/draw_context.h"
44
45
46
47 static void
48 llvmpipe_map_constant_buffers(struct llvmpipe_context *lp)
49 {
50 struct pipe_screen *screen = lp->pipe.screen;
51 uint i, size;
52
53 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
54 if (lp->constants[i].buffer && lp->constants[i].buffer->size)
55 lp->mapped_constants[i] = screen->buffer_map(screen, lp->constants[i].buffer,
56 PIPE_BUFFER_USAGE_CPU_READ);
57 }
58
59 if (lp->constants[PIPE_SHADER_VERTEX].buffer)
60 size = lp->constants[PIPE_SHADER_VERTEX].buffer->size;
61 else
62 size = 0;
63
64 lp->jit_context.constants = lp->mapped_constants[PIPE_SHADER_FRAGMENT];
65
66 draw_set_mapped_constant_buffer(lp->draw,
67 lp->mapped_constants[PIPE_SHADER_VERTEX],
68 size);
69 }
70
71
72 static void
73 llvmpipe_unmap_constant_buffers(struct llvmpipe_context *lp)
74 {
75 struct pipe_screen *screen = lp->pipe.screen;
76 uint i;
77
78 /* really need to flush all prims since the vert/frag shaders const buffers
79 * are going away now.
80 */
81 draw_flush(lp->draw);
82
83 draw_set_mapped_constant_buffer(lp->draw, NULL, 0);
84
85 lp->jit_context.constants = NULL;
86
87 for (i = 0; i < 2; i++) {
88 if (lp->constants[i].buffer && lp->constants[i].buffer->size)
89 screen->buffer_unmap(screen, lp->constants[i].buffer);
90 lp->mapped_constants[i] = NULL;
91 }
92 }
93
94
95 boolean
96 llvmpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
97 unsigned start, unsigned count)
98 {
99 return llvmpipe_draw_elements(pipe, NULL, 0, mode, start, count);
100 }
101
102
103 /**
104 * Draw vertex arrays, with optional indexing.
105 * Basically, map the vertex buffers (and drawing surfaces), then hand off
106 * the drawing to the 'draw' module.
107 */
108 boolean
109 llvmpipe_draw_range_elements(struct pipe_context *pipe,
110 struct pipe_buffer *indexBuffer,
111 unsigned indexSize,
112 unsigned min_index,
113 unsigned max_index,
114 unsigned mode, unsigned start, unsigned count)
115 {
116 struct llvmpipe_context *lp = llvmpipe_context(pipe);
117 struct draw_context *draw = lp->draw;
118 unsigned i;
119
120 lp->reduced_api_prim = u_reduced_prim(mode);
121
122 if (lp->dirty)
123 llvmpipe_update_derived( lp );
124
125 llvmpipe_map_transfers(lp);
126 llvmpipe_map_constant_buffers(lp);
127
128 /*
129 * Map vertex buffers
130 */
131 for (i = 0; i < lp->num_vertex_buffers; i++) {
132 void *buf
133 = pipe_buffer_map(pipe->screen,
134 lp->vertex_buffer[i].buffer,
135 PIPE_BUFFER_USAGE_CPU_READ);
136 draw_set_mapped_vertex_buffer(draw, i, buf);
137 }
138
139 /* Map index buffer, if present */
140 if (indexBuffer) {
141 void *mapped_indexes
142 = pipe_buffer_map(pipe->screen, indexBuffer,
143 PIPE_BUFFER_USAGE_CPU_READ);
144 draw_set_mapped_element_buffer_range(draw, indexSize,
145 min_index,
146 max_index,
147 mapped_indexes);
148 }
149 else {
150 /* no index/element buffer */
151 draw_set_mapped_element_buffer_range(draw, 0, start,
152 start + count - 1, NULL);
153 }
154
155 /* draw! */
156 draw_arrays(draw, mode, start, count);
157
158 /*
159 * unmap vertex/index buffers - will cause draw module to flush
160 */
161 for (i = 0; i < lp->num_vertex_buffers; i++) {
162 draw_set_mapped_vertex_buffer(draw, i, NULL);
163 pipe_buffer_unmap(pipe->screen, lp->vertex_buffer[i].buffer);
164 }
165 if (indexBuffer) {
166 draw_set_mapped_element_buffer(draw, 0, NULL);
167 pipe_buffer_unmap(pipe->screen, indexBuffer);
168 }
169
170
171 /* Note: leave drawing surfaces mapped */
172 llvmpipe_unmap_constant_buffers(lp);
173
174 lp->dirty_render_cache = TRUE;
175
176 return TRUE;
177 }
178
179
180 boolean
181 llvmpipe_draw_elements(struct pipe_context *pipe,
182 struct pipe_buffer *indexBuffer,
183 unsigned indexSize,
184 unsigned mode, unsigned start, unsigned count)
185 {
186 return llvmpipe_draw_range_elements( pipe, indexBuffer,
187 indexSize,
188 0, 0xffffffff,
189 mode, start, count );
190 }
191
192
193 void
194 llvmpipe_set_edgeflags(struct pipe_context *pipe, const unsigned *edgeflags)
195 {
196 struct llvmpipe_context *lp = llvmpipe_context(pipe);
197 draw_set_edgeflags(lp->draw, edgeflags);
198 }