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
83 < svga_render
->vbuf_offset
+ svga_render
->vbuf_used
+ size
)
87 pipe_resource_reference(&svga_render
->vbuf
, NULL
);
89 pipe_resource_reference(&svga_render
->ibuf
, NULL
);
91 if (!svga_render
->vbuf
) {
92 svga_render
->vbuf_size
= MAX2(size
, svga_render
->vbuf_alloc_size
);
93 svga_render
->vbuf
= pipe_buffer_create(screen
,
94 PIPE_BIND_VERTEX_BUFFER
,
96 svga_render
->vbuf_size
);
97 if (!svga_render
->vbuf
) {
98 svga_context_flush(svga
, NULL
);
99 assert(!svga_render
->vbuf
);
100 svga_render
->vbuf
= pipe_buffer_create(screen
,
101 PIPE_BIND_VERTEX_BUFFER
,
103 svga_render
->vbuf_size
);
104 /* The buffer allocation may fail if we run out of memory.
105 * The draw module's vbuf code should handle that without crashing.
109 svga
->swtnl
.new_vdecl
= TRUE
;
110 svga_render
->vbuf_offset
= 0;
112 svga_render
->vbuf_offset
+= svga_render
->vbuf_used
;
115 svga_render
->vbuf_used
= 0;
117 if (svga
->swtnl
.new_vdecl
)
118 svga_render
->vdecl_offset
= svga_render
->vbuf_offset
;
120 SVGA_STATS_TIME_POP(svga_sws(svga
));
127 svga_vbuf_render_map_vertices(struct vbuf_render
*render
)
129 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
130 struct svga_context
*svga
= svga_render
->svga
;
131 void * retPtr
= NULL
;
133 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
134 SVGA_STATS_TIME_VBUFRENDERMAPVERT
);
136 if (svga_render
->vbuf
) {
137 char *ptr
= (char*)pipe_buffer_map(&svga
->pipe
,
139 PIPE_TRANSFER_WRITE
|
140 PIPE_TRANSFER_FLUSH_EXPLICIT
|
141 PIPE_TRANSFER_DISCARD_RANGE
|
142 PIPE_TRANSFER_UNSYNCHRONIZED
,
143 &svga_render
->vbuf_transfer
);
145 svga_render
->vbuf_ptr
= ptr
;
146 retPtr
= ptr
+ svga_render
->vbuf_offset
;
149 svga_render
->vbuf_ptr
= NULL
;
150 svga_render
->vbuf_transfer
= NULL
;
155 /* we probably ran out of memory when allocating the vertex buffer */
159 SVGA_STATS_TIME_POP(svga_sws(svga
));
165 svga_vbuf_render_unmap_vertices(struct vbuf_render
*render
,
169 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
170 struct svga_context
*svga
= svga_render
->svga
;
171 unsigned offset
, length
;
172 size_t used
= svga_render
->vertex_size
* ((size_t)max_index
+ 1);
174 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
175 SVGA_STATS_TIME_VBUFRENDERUNMAPVERT
);
177 offset
= svga_render
->vbuf_offset
+ svga_render
->vertex_size
* min_index
;
178 length
= svga_render
->vertex_size
* (max_index
+ 1 - min_index
);
181 /* dump vertex data */
182 const float *f
= (const float *) ((char *) svga_render
->vbuf_ptr
+
183 svga_render
->vbuf_offset
);
185 debug_printf("swtnl vertex data:\n");
186 for (i
= 0; i
< length
/ 4; i
+= 4) {
187 debug_printf("%u: %f %f %f %f\n", i
, f
[i
], f
[i
+1], f
[i
+2], f
[i
+3]);
191 pipe_buffer_flush_mapped_range(&svga
->pipe
,
192 svga_render
->vbuf_transfer
,
194 pipe_buffer_unmap(&svga
->pipe
, svga_render
->vbuf_transfer
);
195 svga_render
->min_index
= min_index
;
196 svga_render
->max_index
= max_index
;
197 svga_render
->vbuf_used
= MAX2(svga_render
->vbuf_used
, used
);
199 SVGA_STATS_TIME_POP(svga_sws(svga
));
204 svga_vbuf_render_set_primitive(struct vbuf_render
*render
,
205 enum pipe_prim_type prim
)
207 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
208 svga_render
->prim
= prim
;
213 svga_vbuf_submit_state(struct svga_vbuf_render
*svga_render
)
215 struct svga_context
*svga
= svga_render
->svga
;
216 SVGA3dVertexDecl vdecl
[PIPE_MAX_ATTRIBS
];
219 static const unsigned zero
[PIPE_MAX_ATTRIBS
] = {0};
221 /* if the vdecl or vbuf hasn't changed do nothing */
222 if (!svga
->swtnl
.new_vdecl
)
225 SVGA_STATS_TIME_PUSH(svga_sws(svga
),
226 SVGA_STATS_TIME_VBUFSUBMITSTATE
);
228 memcpy(vdecl
, svga_render
->vdecl
, sizeof(vdecl
));
230 /* flush the hw state */
231 ret
= svga_hwtnl_flush(svga
->hwtnl
);
232 if (ret
!= PIPE_OK
) {
233 svga_context_flush(svga
, NULL
);
234 ret
= svga_hwtnl_flush(svga
->hwtnl
);
235 /* if we hit this path we might become synced with hw */
236 svga
->swtnl
.new_vbuf
= TRUE
;
237 assert(ret
== PIPE_OK
);
240 for (i
= 0; i
< svga_render
->vdecl_count
; i
++) {
241 vdecl
[i
].array
.offset
+= svga_render
->vdecl_offset
;
244 svga_hwtnl_vertex_decls(svga
->hwtnl
,
245 svga_render
->vdecl_count
,
248 svga_render
->layout_id
);
250 /* Specify the vertex buffer (there's only ever one) */
252 struct pipe_vertex_buffer vb
;
253 vb
.is_user_buffer
= false;
254 vb
.buffer
.resource
= svga_render
->vbuf
;
255 vb
.buffer_offset
= svga_render
->vdecl_offset
;
256 vb
.stride
= vdecl
[0].array
.stride
;
257 svga_hwtnl_vertex_buffers(svga
->hwtnl
, 1, &vb
);
260 /* We have already taken care of flatshading, so let the hwtnl
261 * module use whatever is most convenient:
263 if (svga
->state
.sw
.need_pipeline
) {
264 svga_hwtnl_set_flatshade(svga
->hwtnl
, FALSE
, FALSE
);
265 svga_hwtnl_set_fillmode(svga
->hwtnl
, PIPE_POLYGON_MODE_FILL
);
268 svga_hwtnl_set_flatshade(svga
->hwtnl
,
269 svga
->curr
.rast
->templ
.flatshade
||
270 svga
->state
.hw_draw
.fs
->uses_flat_interp
,
271 svga
->curr
.rast
->templ
.flatshade_first
);
273 svga_hwtnl_set_fillmode(svga
->hwtnl
, svga
->curr
.rast
->hw_fillmode
);
276 svga
->swtnl
.new_vdecl
= FALSE
;
277 SVGA_STATS_TIME_POP(svga_sws(svga
));
282 svga_vbuf_render_draw_arrays(struct vbuf_render
*render
,
283 unsigned start
, uint nr
)
285 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
286 struct svga_context
*svga
= svga_render
->svga
;
287 unsigned bias
= (svga_render
->vbuf_offset
- svga_render
->vdecl_offset
)
288 / svga_render
->vertex_size
;
289 enum pipe_error ret
= PIPE_OK
;
290 /* instancing will already have been resolved at this point by 'draw' */
291 const unsigned start_instance
= 0;
292 const unsigned instance_count
= 1;
294 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_VBUFDRAWARRAYS
);
296 /* off to hardware */
297 svga_vbuf_submit_state(svga_render
);
299 /* Need to call update_state() again as the draw module may have
300 * altered some of our state behind our backs. Testcase:
303 svga_update_state_retry(svga
, SVGA_STATE_HW_DRAW
);
305 ret
= svga_hwtnl_draw_arrays(svga
->hwtnl
, svga_render
->prim
, start
+ bias
,
306 nr
, start_instance
, instance_count
);
307 if (ret
!= PIPE_OK
) {
308 svga_context_flush(svga
, NULL
);
309 ret
= svga_hwtnl_draw_arrays(svga
->hwtnl
, svga_render
->prim
,
311 start_instance
, instance_count
);
312 svga
->swtnl
.new_vbuf
= TRUE
;
313 assert(ret
== PIPE_OK
);
315 SVGA_STATS_TIME_POP(svga_sws(svga
));
320 svga_vbuf_render_draw_elements(struct vbuf_render
*render
,
321 const ushort
*indices
,
324 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
325 struct svga_context
*svga
= svga_render
->svga
;
326 int bias
= (svga_render
->vbuf_offset
- svga_render
->vdecl_offset
)
327 / svga_render
->vertex_size
;
329 /* instancing will already have been resolved at this point by 'draw' */
330 const struct pipe_draw_info info
= {
332 .mode
= svga_render
->prim
,
333 .has_user_indices
= 1,
334 .index
.user
= indices
,
338 .min_index
= svga_render
->min_index
,
339 .max_index
= svga_render
->max_index
,
344 assert((svga_render
->vbuf_offset
- svga_render
->vdecl_offset
)
345 % svga_render
->vertex_size
== 0);
347 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_VBUFDRAWELEMENTS
);
349 /* off to hardware */
350 svga_vbuf_submit_state(svga_render
);
352 /* Need to call update_state() again as the draw module may have
353 * altered some of our state behind our backs. Testcase:
356 svga_update_state_retry(svga
, SVGA_STATE_HW_DRAW
);
357 ret
= svga_hwtnl_draw_range_elements(svga
->hwtnl
, &info
, nr_indices
);
358 if (ret
!= PIPE_OK
) {
359 svga_context_flush(svga
, NULL
);
360 ret
= svga_hwtnl_draw_range_elements(svga
->hwtnl
, &info
, nr_indices
);
361 svga
->swtnl
.new_vbuf
= TRUE
;
362 assert(ret
== PIPE_OK
);
364 SVGA_STATS_TIME_POP(svga_sws(svga
));
369 svga_vbuf_render_release_vertices(struct vbuf_render
*render
)
376 svga_vbuf_render_destroy(struct vbuf_render
*render
)
378 struct svga_vbuf_render
*svga_render
= svga_vbuf_render(render
);
380 pipe_resource_reference(&svga_render
->vbuf
, NULL
);
381 pipe_resource_reference(&svga_render
->ibuf
, NULL
);
387 * Create a new primitive render.
390 svga_vbuf_render_create(struct svga_context
*svga
)
392 struct svga_vbuf_render
*svga_render
= CALLOC_STRUCT(svga_vbuf_render
);
394 svga_render
->svga
= svga
;
395 svga_render
->ibuf_size
= 0;
396 svga_render
->vbuf_size
= 0;
397 svga_render
->ibuf_alloc_size
= 4*1024;
398 svga_render
->vbuf_alloc_size
= 64*1024;
399 svga_render
->layout_id
= SVGA3D_INVALID_ID
;
400 svga_render
->base
.max_vertex_buffer_bytes
= 64*1024/10;
401 svga_render
->base
.max_indices
= 65536;
402 svga_render
->base
.get_vertex_info
= svga_vbuf_render_get_vertex_info
;
403 svga_render
->base
.allocate_vertices
= svga_vbuf_render_allocate_vertices
;
404 svga_render
->base
.map_vertices
= svga_vbuf_render_map_vertices
;
405 svga_render
->base
.unmap_vertices
= svga_vbuf_render_unmap_vertices
;
406 svga_render
->base
.set_primitive
= svga_vbuf_render_set_primitive
;
407 svga_render
->base
.draw_elements
= svga_vbuf_render_draw_elements
;
408 svga_render
->base
.draw_arrays
= svga_vbuf_render_draw_arrays
;
409 svga_render
->base
.release_vertices
= svga_vbuf_render_release_vertices
;
410 svga_render
->base
.destroy
= svga_vbuf_render_destroy
;
412 return &svga_render
->base
;