1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
26 #include "draw/draw_vbuf.h"
27 #include "draw/draw_context.h"
28 #include "draw/draw_vertex.h"
30 #include "util/u_debug.h"
31 #include "util/u_inlines.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
35 #include "svga_context.h"
36 #include "svga_state.h"
37 #include "svga_swtnl.h"
39 #include "svga_types.h"
41 #include "svga3d_reg.h"
42 #include "svga_draw.h"
43 #include "svga_swtnl_private.h"
46 static const struct vertex_info *
47 svga_vbuf_render_get_vertex_info( struct vbuf_render *render )
49 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
50 struct svga_context *svga = svga_render->svga;
52 svga_swtnl_update_vdecl(svga);
54 return &svga_render->vertex_info;
59 svga_vbuf_render_allocate_vertices( struct vbuf_render *render,
63 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
64 struct svga_context *svga = svga_render->svga;
65 struct pipe_screen *screen = svga->pipe.screen;
66 size_t size = (size_t)nr_vertices * (size_t)vertex_size;
67 boolean new_vbuf = FALSE;
68 boolean new_ibuf = FALSE;
70 if (svga_render->vertex_size != vertex_size)
71 svga->swtnl.new_vdecl = TRUE;
72 svga_render->vertex_size = (size_t)vertex_size;
74 if (svga->swtnl.new_vbuf)
75 new_ibuf = new_vbuf = TRUE;
76 svga->swtnl.new_vbuf = FALSE;
78 if (svga_render->vbuf_size < svga_render->vbuf_offset + svga_render->vbuf_used + size)
82 pipe_buffer_reference(&svga_render->vbuf, NULL);
84 pipe_buffer_reference(&svga_render->ibuf, NULL);
86 if (!svga_render->vbuf) {
87 svga_render->vbuf_size = MAX2(size, svga_render->vbuf_alloc_size);
88 svga_render->vbuf = pipe_buffer_create(screen,
90 PIPE_BUFFER_USAGE_VERTEX,
91 svga_render->vbuf_size);
92 if(!svga_render->vbuf) {
93 svga_context_flush(svga, NULL);
94 svga_render->vbuf = pipe_buffer_create(screen,
96 PIPE_BUFFER_USAGE_VERTEX,
97 svga_render->vbuf_size);
98 assert(svga_render->vbuf);
101 svga->swtnl.new_vdecl = TRUE;
102 svga_render->vbuf_offset = 0;
104 svga_render->vbuf_offset += svga_render->vbuf_used;
107 svga_render->vbuf_used = 0;
109 if (svga->swtnl.new_vdecl)
110 svga_render->vdecl_offset = svga_render->vbuf_offset;
116 svga_vbuf_render_map_vertices( struct vbuf_render *render )
118 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
119 struct svga_context *svga = svga_render->svga;
120 struct pipe_screen *screen = svga->pipe.screen;
122 char *ptr = (char*)pipe_buffer_map(screen,
124 PIPE_BUFFER_USAGE_CPU_WRITE |
125 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
126 PIPE_BUFFER_USAGE_DISCARD |
127 PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
128 return ptr + svga_render->vbuf_offset;
132 svga_vbuf_render_unmap_vertices( struct vbuf_render *render,
136 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
137 struct svga_context *svga = svga_render->svga;
138 struct pipe_screen *screen = svga->pipe.screen;
139 unsigned offset, length;
140 size_t used = svga_render->vertex_size * ((size_t)max_index + 1);
142 offset = svga_render->vbuf_offset + svga_render->vertex_size * min_index;
143 length = svga_render->vertex_size * (max_index + 1 - min_index);
144 pipe_buffer_flush_mapped_range(screen, svga_render->vbuf, offset, length);
145 pipe_buffer_unmap(screen, svga_render->vbuf);
146 svga_render->min_index = min_index;
147 svga_render->max_index = max_index;
148 svga_render->vbuf_used = MAX2(svga_render->vbuf_used, used);
152 svga_vbuf_render_set_primitive( struct vbuf_render *render,
155 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
156 svga_render->prim = prim;
162 svga_vbuf_sumbit_state( struct svga_vbuf_render *svga_render )
164 struct svga_context *svga = svga_render->svga;
165 SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
169 /* if the vdecl or vbuf hasn't changed do nothing */
170 if (!svga->swtnl.new_vdecl)
173 memcpy(vdecl, svga_render->vdecl, sizeof(vdecl));
175 /* flush the hw state */
176 ret = svga_hwtnl_flush(svga->hwtnl);
178 svga_context_flush(svga, NULL);
179 ret = svga_hwtnl_flush(svga->hwtnl);
180 /* if we hit this path we might become synced with hw */
181 svga->swtnl.new_vbuf = TRUE;
185 svga_hwtnl_reset_vdecl(svga->hwtnl, svga_render->vdecl_count);
187 for (i = 0; i < svga_render->vdecl_count; i++) {
188 vdecl[i].array.offset += svga_render->vdecl_offset;
190 svga_hwtnl_vdecl( svga->hwtnl,
196 /* We have already taken care of flatshading, so let the hwtnl
197 * module use whatever is most convenient:
199 if (svga->state.sw.need_pipeline) {
200 svga_hwtnl_set_flatshade(svga->hwtnl, FALSE, FALSE);
201 svga_hwtnl_set_unfilled(svga->hwtnl, PIPE_POLYGON_MODE_FILL);
204 svga_hwtnl_set_flatshade( svga->hwtnl,
205 svga->curr.rast->templ.flatshade,
206 svga->curr.rast->templ.flatshade_first );
208 svga_hwtnl_set_unfilled( svga->hwtnl,
209 svga->curr.rast->hw_unfilled );
212 svga->swtnl.new_vdecl = FALSE;
216 svga_vbuf_render_draw_arrays( struct vbuf_render *render,
220 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
221 struct svga_context *svga = svga_render->svga;
222 unsigned bias = (svga_render->vbuf_offset - svga_render->vdecl_offset) / svga_render->vertex_size;
223 enum pipe_error ret = 0;
225 svga_vbuf_sumbit_state(svga_render);
227 /* Need to call update_state() again as the draw module may have
228 * altered some of our state behind our backs. Testcase:
231 svga_update_state_retry( svga, SVGA_STATE_HW_DRAW );
233 ret = svga_hwtnl_draw_arrays(svga->hwtnl, svga_render->prim, start + bias, nr);
234 if (ret != PIPE_OK) {
235 svga_context_flush(svga, NULL);
236 ret = svga_hwtnl_draw_arrays(svga->hwtnl, svga_render->prim, start + bias, nr);
237 svga->swtnl.new_vbuf = TRUE;
238 assert(ret == PIPE_OK);
244 svga_vbuf_render_draw( struct vbuf_render *render,
245 const ushort *indices,
248 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
249 struct svga_context *svga = svga_render->svga;
250 struct pipe_screen *screen = svga->pipe.screen;
251 unsigned bias = (svga_render->vbuf_offset - svga_render->vdecl_offset) / svga_render->vertex_size;
253 size_t size = 2 * nr_indices;
255 assert(( svga_render->vbuf_offset - svga_render->vdecl_offset) % svga_render->vertex_size == 0);
257 if (svga_render->ibuf_size < svga_render->ibuf_offset + size)
258 pipe_buffer_reference(&svga_render->ibuf, NULL);
260 if (!svga_render->ibuf) {
261 svga_render->ibuf_size = MAX2(size, svga_render->ibuf_alloc_size);
262 svga_render->ibuf = pipe_buffer_create(screen,
264 PIPE_BUFFER_USAGE_VERTEX,
265 svga_render->ibuf_size);
266 svga_render->ibuf_offset = 0;
269 pipe_buffer_write_nooverlap(screen, svga_render->ibuf,
270 svga_render->ibuf_offset, 2 * nr_indices, indices);
273 /* off to hardware */
274 svga_vbuf_sumbit_state(svga_render);
276 /* Need to call update_state() again as the draw module may have
277 * altered some of our state behind our backs. Testcase:
280 svga_update_state_retry( svga, SVGA_STATE_HW_DRAW );
282 ret = svga_hwtnl_draw_range_elements(svga->hwtnl,
285 svga_render->min_index,
286 svga_render->max_index,
288 svga_render->ibuf_offset / 2, nr_indices, bias);
290 svga_context_flush(svga, NULL);
291 ret = svga_hwtnl_draw_range_elements(svga->hwtnl,
294 svga_render->min_index,
295 svga_render->max_index,
297 svga_render->ibuf_offset / 2, nr_indices, bias);
298 svga->swtnl.new_vbuf = TRUE;
299 assert(ret == PIPE_OK);
302 svga_render->ibuf_offset += size;
307 svga_vbuf_render_release_vertices( struct vbuf_render *render )
314 svga_vbuf_render_destroy( struct vbuf_render *render )
316 struct svga_vbuf_render *svga_render = svga_vbuf_render(render);
318 pipe_buffer_reference(&svga_render->vbuf, NULL);
319 pipe_buffer_reference(&svga_render->ibuf, NULL);
325 * Create a new primitive render.
328 svga_vbuf_render_create( struct svga_context *svga )
330 struct svga_vbuf_render *svga_render = CALLOC_STRUCT(svga_vbuf_render);
332 svga_render->svga = svga;
333 svga_render->ibuf_size = 0;
334 svga_render->vbuf_size = 0;
335 svga_render->ibuf_alloc_size = 4*1024;
336 svga_render->vbuf_alloc_size = 64*1024;
337 svga_render->base.max_vertex_buffer_bytes = 64*1024/10;
338 svga_render->base.max_indices = 65536;
339 svga_render->base.get_vertex_info = svga_vbuf_render_get_vertex_info;
340 svga_render->base.allocate_vertices = svga_vbuf_render_allocate_vertices;
341 svga_render->base.map_vertices = svga_vbuf_render_map_vertices;
342 svga_render->base.unmap_vertices = svga_vbuf_render_unmap_vertices;
343 svga_render->base.set_primitive = svga_vbuf_render_set_primitive;
344 svga_render->base.draw = svga_vbuf_render_draw;
345 svga_render->base.draw_arrays = svga_vbuf_render_draw_arrays;
346 svga_render->base.release_vertices = svga_vbuf_render_release_vertices;
347 svga_render->base.destroy = svga_vbuf_render_destroy;
349 return &svga_render->base;