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