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_shader.h"
44 #include "svga_swtnl_private.h"
47 static const struct vertex_info
*
48 svga_vbuf_render_get_vertex_info( struct vbuf_render
*render
)
50 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
51 struct svga_context
*svga
= svga_render
->svga
;
53 svga_swtnl_update_vdecl(svga
);
55 return &svga_render
->vertex_info
;
60 svga_vbuf_render_allocate_vertices( struct vbuf_render
*render
,
64 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
65 struct svga_context
*svga
= svga_render
->svga
;
66 struct pipe_screen
*screen
= svga
->pipe
.screen
;
67 size_t size
= (size_t)nr_vertices
* (size_t)vertex_size
;
68 boolean new_vbuf
= FALSE
;
69 boolean new_ibuf
= FALSE
;
71 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
72 SVGA_STATS_TIME_VBUFRENDERALLOCVERT
);
74 if (svga_render
->vertex_size
!= vertex_size
)
75 svga
->swtnl
.new_vdecl
= TRUE
;
76 svga_render
->vertex_size
= (size_t)vertex_size
;
78 if (svga
->swtnl
.new_vbuf
)
79 new_ibuf
= new_vbuf
= TRUE
;
80 svga
->swtnl
.new_vbuf
= FALSE
;
82 if (svga_render
->vbuf_size
< svga_render
->vbuf_offset
+ svga_render
->vbuf_used
+ size
)
86 pipe_resource_reference(&svga_render
->vbuf
, NULL
);
88 pipe_resource_reference(&svga_render
->ibuf
, NULL
);
90 if (!svga_render
->vbuf
) {
91 svga_render
->vbuf_size
= MAX2(size
, svga_render
->vbuf_alloc_size
);
92 svga_render
->vbuf
= pipe_buffer_create(screen
,
93 PIPE_BIND_VERTEX_BUFFER
,
95 svga_render
->vbuf_size
);
96 if(!svga_render
->vbuf
) {
97 svga_context_flush(svga
, NULL
);
98 assert(!svga_render
->vbuf
);
99 svga_render
->vbuf
= pipe_buffer_create(screen
,
100 PIPE_BIND_VERTEX_BUFFER
,
102 svga_render
->vbuf_size
);
103 /* The buffer allocation may fail if we run out of memory.
104 * The draw module's vbuf code should handle that without crashing.
108 svga
->swtnl
.new_vdecl
= TRUE
;
109 svga_render
->vbuf_offset
= 0;
111 svga_render
->vbuf_offset
+= svga_render
->vbuf_used
;
114 svga_render
->vbuf_used
= 0;
116 if (svga
->swtnl
.new_vdecl
)
117 svga_render
->vdecl_offset
= svga_render
->vbuf_offset
;
119 SVGA_STATS_TIME_POP(svga_sws(svga
));
125 svga_vbuf_render_map_vertices( struct vbuf_render
*render
)
127 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
128 struct svga_context
*svga
= svga_render
->svga
;
129 void * retPtr
= NULL
;
131 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
132 SVGA_STATS_TIME_VBUFRENDERMAPVERT
);
134 if (svga_render
->vbuf
) {
135 char *ptr
= (char*)pipe_buffer_map(&svga
->pipe
,
137 PIPE_TRANSFER_WRITE
|
138 PIPE_TRANSFER_FLUSH_EXPLICIT
|
139 PIPE_TRANSFER_DISCARD_RANGE
|
140 PIPE_TRANSFER_UNSYNCHRONIZED
,
141 &svga_render
->vbuf_transfer
);
143 svga_render
->vbuf_ptr
= ptr
;
144 retPtr
= ptr
+ svga_render
->vbuf_offset
;
147 svga_render
->vbuf_ptr
= NULL
;
148 svga_render
->vbuf_transfer
= NULL
;
153 /* we probably ran out of memory when allocating the vertex buffer */
157 SVGA_STATS_TIME_POP(svga_sws(svga
));
162 svga_vbuf_render_unmap_vertices( struct vbuf_render
*render
,
166 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
167 struct svga_context
*svga
= svga_render
->svga
;
168 unsigned offset
, length
;
169 size_t used
= svga_render
->vertex_size
* ((size_t)max_index
+ 1);
171 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
172 SVGA_STATS_TIME_VBUFRENDERUNMAPVERT
);
174 offset
= svga_render
->vbuf_offset
+ svga_render
->vertex_size
* min_index
;
175 length
= svga_render
->vertex_size
* (max_index
+ 1 - min_index
);
178 /* dump vertex data */
179 const float *f
= (const float *) ((char *) svga_render
->vbuf_ptr
+
180 svga_render
->vbuf_offset
);
182 debug_printf("swtnl vertex data:\n");
183 for (i
= 0; i
< length
/ 4; i
+= 4) {
184 debug_printf("%u: %f %f %f %f\n", i
, f
[i
], f
[i
+1], f
[i
+2], f
[i
+3]);
188 pipe_buffer_flush_mapped_range(&svga
->pipe
,
189 svga_render
->vbuf_transfer
,
191 pipe_buffer_unmap(&svga
->pipe
, svga_render
->vbuf_transfer
);
192 svga_render
->min_index
= min_index
;
193 svga_render
->max_index
= max_index
;
194 svga_render
->vbuf_used
= MAX2(svga_render
->vbuf_used
, used
);
196 SVGA_STATS_TIME_POP(svga_sws(svga
));
200 svga_vbuf_render_set_primitive( struct vbuf_render
*render
,
201 enum pipe_prim_type prim
)
203 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
204 svga_render
->prim
= prim
;
208 svga_vbuf_submit_state( struct svga_vbuf_render
*svga_render
)
210 struct svga_context
*svga
= svga_render
->svga
;
211 SVGA3dVertexDecl vdecl
[PIPE_MAX_ATTRIBS
];
214 static const unsigned zero
[PIPE_MAX_ATTRIBS
] = {0};
216 /* if the vdecl or vbuf hasn't changed do nothing */
217 if (!svga
->swtnl
.new_vdecl
)
220 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
221 SVGA_STATS_TIME_VBUFSUBMITSTATE
);
223 memcpy(vdecl
, svga_render
->vdecl
, sizeof(vdecl
));
225 /* flush the hw state */
226 ret
= svga_hwtnl_flush(svga
->hwtnl
);
227 if (ret
!= PIPE_OK
) {
228 svga_context_flush(svga
, NULL
);
229 ret
= svga_hwtnl_flush(svga
->hwtnl
);
230 /* if we hit this path we might become synced with hw */
231 svga
->swtnl
.new_vbuf
= TRUE
;
232 assert(ret
== PIPE_OK
);
235 for (i
= 0; i
< svga_render
->vdecl_count
; i
++) {
236 vdecl
[i
].array
.offset
+= svga_render
->vdecl_offset
;
239 svga_hwtnl_vertex_decls(svga
->hwtnl
,
240 svga_render
->vdecl_count
,
243 svga_render
->layout_id
);
245 /* Specify the vertex buffer (there's only ever one) */
247 struct pipe_vertex_buffer vb
;
248 vb
.buffer
= svga_render
->vbuf
;
249 vb
.buffer_offset
= svga_render
->vdecl_offset
;
250 vb
.stride
= vdecl
[0].array
.stride
;
251 vb
.user_buffer
= NULL
;
252 svga_hwtnl_vertex_buffers(svga
->hwtnl
, 1, &vb
);
255 /* We have already taken care of flatshading, so let the hwtnl
256 * module use whatever is most convenient:
258 if (svga
->state
.sw
.need_pipeline
) {
259 svga_hwtnl_set_flatshade(svga
->hwtnl
, FALSE
, FALSE
);
260 svga_hwtnl_set_fillmode(svga
->hwtnl
, PIPE_POLYGON_MODE_FILL
);
263 svga_hwtnl_set_flatshade( svga
->hwtnl
,
264 svga
->curr
.rast
->templ
.flatshade
||
265 svga
->state
.hw_draw
.fs
->uses_flat_interp
,
266 svga
->curr
.rast
->templ
.flatshade_first
);
268 svga_hwtnl_set_fillmode(svga
->hwtnl
, svga
->curr
.rast
->hw_fillmode
);
271 svga
->swtnl
.new_vdecl
= FALSE
;
272 SVGA_STATS_TIME_POP(svga_sws(svga
));
276 svga_vbuf_render_draw_arrays( struct vbuf_render
*render
,
277 unsigned start
, uint nr
)
279 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
280 struct svga_context
*svga
= svga_render
->svga
;
281 unsigned bias
= (svga_render
->vbuf_offset
- svga_render
->vdecl_offset
) / svga_render
->vertex_size
;
282 enum pipe_error ret
= PIPE_OK
;
283 /* instancing will already have been resolved at this point by 'draw' */
284 const unsigned start_instance
= 0;
285 const unsigned instance_count
= 1;
287 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_VBUFDRAWARRAYS
);
289 /* off to hardware */
290 svga_vbuf_submit_state(svga_render
);
292 /* Need to call update_state() again as the draw module may have
293 * altered some of our state behind our backs. Testcase:
296 svga_update_state_retry( svga
, SVGA_STATE_HW_DRAW
);
298 ret
= svga_hwtnl_draw_arrays(svga
->hwtnl
, svga_render
->prim
, start
+ bias
, nr
,
299 start_instance
, instance_count
);
300 if (ret
!= PIPE_OK
) {
301 svga_context_flush(svga
, NULL
);
302 ret
= svga_hwtnl_draw_arrays(svga
->hwtnl
, svga_render
->prim
,
304 start_instance
, instance_count
);
305 svga
->swtnl
.new_vbuf
= TRUE
;
306 assert(ret
== PIPE_OK
);
308 SVGA_STATS_TIME_POP(svga_sws(svga
));
313 svga_vbuf_render_draw_elements( struct vbuf_render
*render
,
314 const ushort
*indices
,
317 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
318 struct svga_context
*svga
= svga_render
->svga
;
319 struct pipe_screen
*screen
= svga
->pipe
.screen
;
320 int bias
= (svga_render
->vbuf_offset
- svga_render
->vdecl_offset
) / svga_render
->vertex_size
;
322 size_t size
= 2 * nr_indices
;
323 /* instancing will already have been resolved at this point by 'draw' */
324 const unsigned start_instance
= 0;
325 const unsigned instance_count
= 1;
327 assert(( svga_render
->vbuf_offset
- svga_render
->vdecl_offset
) % svga_render
->vertex_size
== 0);
329 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_VBUFDRAWELEMENTS
);
331 if (svga_render
->ibuf_size
< svga_render
->ibuf_offset
+ size
)
332 pipe_resource_reference(&svga_render
->ibuf
, NULL
);
334 if (!svga_render
->ibuf
) {
335 svga_render
->ibuf_size
= MAX2(size
, svga_render
->ibuf_alloc_size
);
336 svga_render
->ibuf
= pipe_buffer_create(screen
,
337 PIPE_BIND_INDEX_BUFFER
,
339 svga_render
->ibuf_size
);
340 svga_render
->ibuf_offset
= 0;
343 pipe_buffer_write_nooverlap(&svga
->pipe
, svga_render
->ibuf
,
344 svga_render
->ibuf_offset
, 2 * nr_indices
, indices
);
346 /* off to hardware */
347 svga_vbuf_submit_state(svga_render
);
349 /* Need to call update_state() again as the draw module may have
350 * altered some of our state behind our backs. Testcase:
353 svga_update_state_retry( svga
, SVGA_STATE_HW_DRAW
);
355 ret
= svga_hwtnl_draw_range_elements(svga
->hwtnl
,
359 svga_render
->min_index
,
360 svga_render
->max_index
,
362 svga_render
->ibuf_offset
/ 2, nr_indices
,
363 start_instance
, instance_count
);
365 svga_context_flush(svga
, NULL
);
366 ret
= svga_hwtnl_draw_range_elements(svga
->hwtnl
,
370 svga_render
->min_index
,
371 svga_render
->max_index
,
373 svga_render
->ibuf_offset
/ 2,
375 start_instance
, instance_count
);
376 svga
->swtnl
.new_vbuf
= TRUE
;
377 assert(ret
== PIPE_OK
);
379 svga_render
->ibuf_offset
+= size
;
381 SVGA_STATS_TIME_POP(svga_sws(svga
));
386 svga_vbuf_render_release_vertices( struct vbuf_render
*render
)
393 svga_vbuf_render_destroy( struct vbuf_render
*render
)
395 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
397 pipe_resource_reference(&svga_render
->vbuf
, NULL
);
398 pipe_resource_reference(&svga_render
->ibuf
, NULL
);
404 * Create a new primitive render.
407 svga_vbuf_render_create( struct svga_context
*svga
)
409 struct svga_vbuf_render
*svga_render
= CALLOC_STRUCT(svga_vbuf_render
);
411 svga_render
->svga
= svga
;
412 svga_render
->ibuf_size
= 0;
413 svga_render
->vbuf_size
= 0;
414 svga_render
->ibuf_alloc_size
= 4*1024;
415 svga_render
->vbuf_alloc_size
= 64*1024;
416 svga_render
->layout_id
= SVGA3D_INVALID_ID
;
417 svga_render
->base
.max_vertex_buffer_bytes
= 64*1024/10;
418 svga_render
->base
.max_indices
= 65536;
419 svga_render
->base
.get_vertex_info
= svga_vbuf_render_get_vertex_info
;
420 svga_render
->base
.allocate_vertices
= svga_vbuf_render_allocate_vertices
;
421 svga_render
->base
.map_vertices
= svga_vbuf_render_map_vertices
;
422 svga_render
->base
.unmap_vertices
= svga_vbuf_render_unmap_vertices
;
423 svga_render
->base
.set_primitive
= svga_vbuf_render_set_primitive
;
424 svga_render
->base
.draw_elements
= svga_vbuf_render_draw_elements
;
425 svga_render
->base
.draw_arrays
= svga_vbuf_render_draw_arrays
;
426 svga_render
->base
.release_vertices
= svga_vbuf_render_release_vertices
;
427 svga_render
->base
.destroy
= svga_vbuf_render_destroy
;
429 return &svga_render
->base
;