svga: remove unneeded memset() call in draw_vgpu10()
[mesa.git] / src / gallium / drivers / svga / svga_draw.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "pipe/p_compiler.h"
27 #include "util/u_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_helpers.h"
30 #include "util/u_memory.h"
31 #include "util/u_math.h"
32
33 #include "svga_context.h"
34 #include "svga_draw.h"
35 #include "svga_draw_private.h"
36 #include "svga_debug.h"
37 #include "svga_screen.h"
38 #include "svga_resource.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_resource_texture.h"
41 #include "svga_shader.h"
42 #include "svga_surface.h"
43 #include "svga_winsys.h"
44 #include "svga_cmd.h"
45
46
47 struct svga_hwtnl *
48 svga_hwtnl_create(struct svga_context *svga)
49 {
50 struct svga_hwtnl *hwtnl = CALLOC_STRUCT(svga_hwtnl);
51 if (!hwtnl)
52 goto fail;
53
54 hwtnl->svga = svga;
55
56 hwtnl->cmd.swc = svga->swc;
57
58 return hwtnl;
59
60 fail:
61 return NULL;
62 }
63
64
65 void
66 svga_hwtnl_destroy(struct svga_hwtnl *hwtnl)
67 {
68 unsigned i, j;
69
70 for (i = 0; i < PIPE_PRIM_MAX; i++) {
71 for (j = 0; j < IDX_CACHE_MAX; j++) {
72 pipe_resource_reference(&hwtnl->index_cache[i][j].buffer, NULL);
73 }
74 }
75
76 for (i = 0; i < hwtnl->cmd.vbuf_count; i++)
77 pipe_resource_reference(&hwtnl->cmd.vbufs[i].buffer, NULL);
78
79 for (i = 0; i < hwtnl->cmd.prim_count; i++)
80 pipe_resource_reference(&hwtnl->cmd.prim_ib[i], NULL);
81
82 FREE(hwtnl);
83 }
84
85
86 void
87 svga_hwtnl_set_flatshade(struct svga_hwtnl *hwtnl,
88 boolean flatshade, boolean flatshade_first)
89 {
90 struct svga_screen *svgascreen = svga_screen(hwtnl->svga->pipe.screen);
91
92 /* User-specified PV */
93 hwtnl->api_pv = (flatshade && !flatshade_first) ? PV_LAST : PV_FIRST;
94
95 /* Device supported PV */
96 if (svgascreen->haveProvokingVertex) {
97 /* use the mode specified by the user */
98 hwtnl->hw_pv = hwtnl->api_pv;
99 }
100 else {
101 /* the device only support first provoking vertex */
102 hwtnl->hw_pv = PV_FIRST;
103 }
104 }
105
106
107 void
108 svga_hwtnl_set_fillmode(struct svga_hwtnl *hwtnl, unsigned mode)
109 {
110 hwtnl->api_fillmode = mode;
111 }
112
113
114 void
115 svga_hwtnl_vertex_decls(struct svga_hwtnl *hwtnl,
116 unsigned count,
117 const SVGA3dVertexDecl * decls,
118 const unsigned *buffer_indexes,
119 SVGA3dElementLayoutId layout_id)
120 {
121 assert(hwtnl->cmd.prim_count == 0);
122 hwtnl->cmd.vdecl_count = count;
123 hwtnl->cmd.vdecl_layout_id = layout_id;
124 memcpy(hwtnl->cmd.vdecl, decls, count * sizeof(*decls));
125 memcpy(hwtnl->cmd.vdecl_buffer_index, buffer_indexes,
126 count * sizeof(unsigned));
127 }
128
129
130 /**
131 * Specify vertex buffers for hardware drawing.
132 */
133 void
134 svga_hwtnl_vertex_buffers(struct svga_hwtnl *hwtnl,
135 unsigned count, struct pipe_vertex_buffer *buffers)
136 {
137 util_set_vertex_buffers_count(hwtnl->cmd.vbufs,
138 &hwtnl->cmd.vbuf_count, buffers, 0, count);
139 }
140
141
142 /**
143 * Determine whether the specified buffer is referred in the primitive queue,
144 * for which no commands have been written yet.
145 */
146 boolean
147 svga_hwtnl_is_buffer_referred(struct svga_hwtnl *hwtnl,
148 struct pipe_resource *buffer)
149 {
150 unsigned i;
151
152 if (svga_buffer_is_user_buffer(buffer)) {
153 return FALSE;
154 }
155
156 if (!hwtnl->cmd.prim_count) {
157 return FALSE;
158 }
159
160 for (i = 0; i < hwtnl->cmd.vbuf_count; ++i) {
161 if (hwtnl->cmd.vbufs[i].buffer == buffer) {
162 return TRUE;
163 }
164 }
165
166 for (i = 0; i < hwtnl->cmd.prim_count; ++i) {
167 if (hwtnl->cmd.prim_ib[i] == buffer) {
168 return TRUE;
169 }
170 }
171
172 return FALSE;
173 }
174
175
176 static enum pipe_error
177 draw_vgpu9(struct svga_hwtnl *hwtnl)
178 {
179 struct svga_winsys_context *swc = hwtnl->cmd.swc;
180 struct svga_context *svga = hwtnl->svga;
181 enum pipe_error ret;
182 struct svga_winsys_surface *vb_handle[SVGA3D_INPUTREG_MAX];
183 struct svga_winsys_surface *ib_handle[QSZ];
184 struct svga_winsys_surface *handle;
185 SVGA3dVertexDecl *vdecl;
186 SVGA3dPrimitiveRange *prim;
187 unsigned i;
188
189 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
190 unsigned j = hwtnl->cmd.vdecl_buffer_index[i];
191 handle = svga_buffer_handle(svga, hwtnl->cmd.vbufs[j].buffer);
192 if (!handle)
193 return PIPE_ERROR_OUT_OF_MEMORY;
194
195 vb_handle[i] = handle;
196 }
197
198 for (i = 0; i < hwtnl->cmd.prim_count; i++) {
199 if (hwtnl->cmd.prim_ib[i]) {
200 handle = svga_buffer_handle(svga, hwtnl->cmd.prim_ib[i]);
201 if (!handle)
202 return PIPE_ERROR_OUT_OF_MEMORY;
203 }
204 else
205 handle = NULL;
206
207 ib_handle[i] = handle;
208 }
209
210 if (svga->rebind.flags.rendertargets) {
211 ret = svga_reemit_framebuffer_bindings(svga);
212 if (ret != PIPE_OK) {
213 return ret;
214 }
215 }
216
217 if (svga->rebind.flags.texture_samplers) {
218 ret = svga_reemit_tss_bindings(svga);
219 if (ret != PIPE_OK) {
220 return ret;
221 }
222 }
223
224 if (svga->rebind.flags.vs) {
225 ret = svga_reemit_vs_bindings(svga);
226 if (ret != PIPE_OK) {
227 return ret;
228 }
229 }
230
231 if (svga->rebind.flags.fs) {
232 ret = svga_reemit_fs_bindings(svga);
233 if (ret != PIPE_OK) {
234 return ret;
235 }
236 }
237
238 SVGA_DBG(DEBUG_DMA, "draw to sid %p, %d prims\n",
239 svga->curr.framebuffer.cbufs[0] ?
240 svga_surface(svga->curr.framebuffer.cbufs[0])->handle : NULL,
241 hwtnl->cmd.prim_count);
242
243 ret = SVGA3D_BeginDrawPrimitives(swc,
244 &vdecl,
245 hwtnl->cmd.vdecl_count,
246 &prim, hwtnl->cmd.prim_count);
247 if (ret != PIPE_OK)
248 return ret;
249
250 memcpy(vdecl,
251 hwtnl->cmd.vdecl,
252 hwtnl->cmd.vdecl_count * sizeof hwtnl->cmd.vdecl[0]);
253
254 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
255 /* check for 4-byte alignment */
256 assert(vdecl[i].array.offset % 4 == 0);
257 assert(vdecl[i].array.stride % 4 == 0);
258
259 /* Given rangeHint is considered to be relative to indexBias, and
260 * indexBias varies per primitive, we cannot accurately supply an
261 * rangeHint when emitting more than one primitive per draw command.
262 */
263 if (hwtnl->cmd.prim_count == 1) {
264 vdecl[i].rangeHint.first = hwtnl->cmd.min_index[0];
265 vdecl[i].rangeHint.last = hwtnl->cmd.max_index[0] + 1;
266 }
267 else {
268 vdecl[i].rangeHint.first = 0;
269 vdecl[i].rangeHint.last = 0;
270 }
271
272 swc->surface_relocation(swc,
273 &vdecl[i].array.surfaceId,
274 NULL, vb_handle[i], SVGA_RELOC_READ);
275 }
276
277 memcpy(prim,
278 hwtnl->cmd.prim, hwtnl->cmd.prim_count * sizeof hwtnl->cmd.prim[0]);
279
280 for (i = 0; i < hwtnl->cmd.prim_count; i++) {
281 swc->surface_relocation(swc,
282 &prim[i].indexArray.surfaceId,
283 NULL, ib_handle[i], SVGA_RELOC_READ);
284 pipe_resource_reference(&hwtnl->cmd.prim_ib[i], NULL);
285 }
286
287 SVGA_FIFOCommitAll(swc);
288
289 hwtnl->cmd.prim_count = 0;
290
291 return PIPE_OK;
292 }
293
294
295 static SVGA3dSurfaceFormat
296 xlate_index_format(unsigned indexWidth)
297 {
298 if (indexWidth == 2) {
299 return SVGA3D_R16_UINT;
300 }
301 else if (indexWidth == 4) {
302 return SVGA3D_R32_UINT;
303 }
304 else {
305 assert(!"Bad indexWidth");
306 return SVGA3D_R32_UINT;
307 }
308 }
309
310
311 static enum pipe_error
312 validate_sampler_resources(struct svga_context *svga)
313 {
314 unsigned shader;
315
316 assert(svga_have_vgpu10(svga));
317
318 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
319 unsigned count = svga->curr.num_sampler_views[shader];
320 unsigned i;
321 struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
322 enum pipe_error ret;
323
324 /*
325 * Reference bound sampler resources to ensure pending updates are
326 * noticed by the device.
327 */
328 for (i = 0; i < count; i++) {
329 struct svga_pipe_sampler_view *sv =
330 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
331
332 if (sv) {
333 if (sv->base.texture->target == PIPE_BUFFER) {
334 surfaces[i] = svga_buffer_handle(svga, sv->base.texture);
335 }
336 else {
337 surfaces[i] = svga_texture(sv->base.texture)->handle;
338 }
339 }
340 else {
341 surfaces[i] = NULL;
342 }
343 }
344
345 if (shader == PIPE_SHADER_FRAGMENT &&
346 svga->curr.rast->templ.poly_stipple_enable) {
347 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
348 struct svga_pipe_sampler_view *sv =
349 svga->polygon_stipple.sampler_view;
350
351 assert(sv);
352 surfaces[unit] = svga_texture(sv->base.texture)->handle;
353 count = MAX2(count, unit+1);
354 }
355
356 /* rebind the shader resources if needed */
357 if (svga->rebind.flags.texture_samplers) {
358 for (i = 0; i < count; i++) {
359 if (surfaces[i]) {
360 ret = svga->swc->resource_rebind(svga->swc,
361 surfaces[i],
362 NULL,
363 SVGA_RELOC_READ);
364 if (ret != PIPE_OK)
365 return ret;
366 }
367 }
368 }
369 }
370 svga->rebind.flags.texture_samplers = FALSE;
371
372 return PIPE_OK;
373 }
374
375
376 static enum pipe_error
377 validate_constant_buffers(struct svga_context *svga)
378 {
379 unsigned shader;
380
381 assert(svga_have_vgpu10(svga));
382
383 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
384 enum pipe_error ret;
385 struct svga_buffer *buffer;
386 struct svga_winsys_surface *handle;
387 unsigned enabled_constbufs;
388
389 /* Rebind the default constant buffer if needed */
390 if (svga->rebind.flags.constbufs) {
391 buffer = svga_buffer(svga->state.hw_draw.constbuf[shader]);
392 if (buffer) {
393 ret = svga->swc->resource_rebind(svga->swc,
394 buffer->handle,
395 NULL,
396 SVGA_RELOC_READ);
397 if (ret != PIPE_OK)
398 return ret;
399 }
400 }
401
402 /*
403 * Reference other bound constant buffers to ensure pending updates are
404 * noticed by the device.
405 */
406 enabled_constbufs = svga->state.hw_draw.enabled_constbufs[shader] & ~1u;
407 while (enabled_constbufs) {
408 unsigned i = u_bit_scan(&enabled_constbufs);
409 buffer = svga_buffer(svga->curr.constbufs[shader][i].buffer);
410 if (buffer) {
411 handle = svga_buffer_handle(svga, &buffer->b.b);
412
413 if (svga->rebind.flags.constbufs) {
414 ret = svga->swc->resource_rebind(svga->swc,
415 handle,
416 NULL,
417 SVGA_RELOC_READ);
418 if (ret != PIPE_OK)
419 return ret;
420 }
421 }
422 }
423 }
424 svga->rebind.flags.constbufs = FALSE;
425
426 return PIPE_OK;
427 }
428
429
430 static enum pipe_error
431 draw_vgpu10(struct svga_hwtnl *hwtnl,
432 const SVGA3dPrimitiveRange *range,
433 unsigned vcount,
434 unsigned min_index,
435 unsigned max_index, struct pipe_resource *ib,
436 unsigned start_instance, unsigned instance_count)
437 {
438 struct svga_context *svga = hwtnl->svga;
439 struct pipe_resource *vbuffers[SVGA3D_INPUTREG_MAX];
440 struct svga_winsys_surface *vbuffer_handles[SVGA3D_INPUTREG_MAX];
441 struct svga_winsys_surface *ib_handle;
442 const unsigned vbuf_count = hwtnl->cmd.vbuf_count;
443 int last_vbuf = -1;
444 enum pipe_error ret;
445 unsigned i;
446
447 assert(svga_have_vgpu10(svga));
448 assert(hwtnl->cmd.prim_count == 0);
449
450 /* We need to reemit all the current resource bindings along with the Draw
451 * command to be sure that the referenced resources are available for the
452 * Draw command, just in case the surfaces associated with the resources
453 * are paged out.
454 */
455 if (svga->rebind.val) {
456 ret = svga_rebind_framebuffer_bindings(svga);
457 if (ret != PIPE_OK)
458 return ret;
459
460 ret = svga_rebind_shaders(svga);
461 if (ret != PIPE_OK)
462 return ret;
463
464 /* Rebind stream output targets */
465 ret = svga_rebind_stream_output_targets(svga);
466 if (ret != PIPE_OK)
467 return ret;
468
469 /* No need to explicitly rebind index buffer and vertex buffers here.
470 * Even if the same index buffer or vertex buffers are referenced for this
471 * draw and we skip emitting the redundant set command, we will still
472 * reference the associated resources.
473 */
474 }
475
476 ret = validate_sampler_resources(svga);
477 if (ret != PIPE_OK)
478 return ret;
479
480 ret = validate_constant_buffers(svga);
481 if (ret != PIPE_OK)
482 return ret;
483
484 /* Get handle for each referenced vertex buffer */
485 for (i = 0; i < vbuf_count; i++) {
486 struct svga_buffer *sbuf = svga_buffer(hwtnl->cmd.vbufs[i].buffer);
487
488 if (sbuf) {
489 assert(sbuf->key.flags & SVGA3D_SURFACE_BIND_VERTEX_BUFFER);
490 vbuffer_handles[i] = svga_buffer_handle(svga, &sbuf->b.b);
491 if (vbuffer_handles[i] == NULL)
492 return PIPE_ERROR_OUT_OF_MEMORY;
493 vbuffers[i] = &sbuf->b.b;
494 last_vbuf = i;
495 }
496 else {
497 vbuffers[i] = NULL;
498 vbuffer_handles[i] = NULL;
499 }
500 }
501
502 for (; i < svga->state.hw_draw.num_vbuffers; i++) {
503 vbuffers[i] = NULL;
504 vbuffer_handles[i] = NULL;
505 }
506
507 /* Get handle for the index buffer */
508 if (ib) {
509 struct svga_buffer *sbuf = svga_buffer(ib);
510
511 assert(sbuf->key.flags & SVGA3D_SURFACE_BIND_INDEX_BUFFER);
512 (void) sbuf; /* silence unused var warning */
513
514 ib_handle = svga_buffer_handle(svga, ib);
515 if (!ib_handle)
516 return PIPE_ERROR_OUT_OF_MEMORY;
517 }
518 else {
519 ib_handle = NULL;
520 }
521
522 /* setup vertex attribute input layout */
523 if (svga->state.hw_draw.layout_id != hwtnl->cmd.vdecl_layout_id) {
524 ret = SVGA3D_vgpu10_SetInputLayout(svga->swc,
525 hwtnl->cmd.vdecl_layout_id);
526 if (ret != PIPE_OK)
527 return ret;
528
529 svga->state.hw_draw.layout_id = hwtnl->cmd.vdecl_layout_id;
530 }
531
532 /* setup vertex buffers */
533 {
534 SVGA3dVertexBuffer vbuffer_attrs[PIPE_MAX_ATTRIBS];
535
536 for (i = 0; i < vbuf_count; i++) {
537 vbuffer_attrs[i].stride = hwtnl->cmd.vbufs[i].stride;
538 vbuffer_attrs[i].offset = hwtnl->cmd.vbufs[i].buffer_offset;
539 vbuffer_attrs[i].sid = 0;
540 }
541
542 /* If we haven't yet emitted a drawing command or if any
543 * vertex buffer state is changing, issue that state now.
544 */
545 if (((hwtnl->cmd.swc->hints & SVGA_HINT_FLAG_CAN_PRE_FLUSH) == 0) ||
546 vbuf_count != svga->state.hw_draw.num_vbuffers ||
547 memcmp(vbuffer_attrs, svga->state.hw_draw.vbuffer_attrs,
548 vbuf_count * sizeof(vbuffer_attrs[0])) ||
549 memcmp(vbuffers, svga->state.hw_draw.vbuffers,
550 vbuf_count * sizeof(vbuffers[0]))) {
551
552 unsigned num_vbuffers;
553
554 /* get the max of the current bound vertex buffers count and
555 * the to-be-bound vertex buffers count, so as to unbind
556 * the unused vertex buffers.
557 */
558 num_vbuffers = MAX2(vbuf_count, svga->state.hw_draw.num_vbuffers);
559
560 if (num_vbuffers > 0) {
561
562 ret = SVGA3D_vgpu10_SetVertexBuffers(svga->swc, num_vbuffers,
563 0, /* startBuffer */
564 vbuffer_attrs,
565 vbuffer_handles);
566 if (ret != PIPE_OK)
567 return ret;
568
569 /* save the number of vertex buffers sent to the device, not
570 * including trailing unbound vertex buffers.
571 */
572 svga->state.hw_draw.num_vbuffers = last_vbuf + 1;
573 memcpy(svga->state.hw_draw.vbuffer_attrs, vbuffer_attrs,
574 num_vbuffers * sizeof(vbuffer_attrs[0]));
575 for (i = 0; i < num_vbuffers; i++) {
576 pipe_resource_reference(&svga->state.hw_draw.vbuffers[i],
577 vbuffers[i]);
578 }
579 }
580 }
581 else {
582 /* Even though we can avoid emitting the redundant SetVertexBuffers
583 * command, we still need to reference the vertex buffers surfaces.
584 */
585 for (i = 0; i < vbuf_count; i++) {
586 if (vbuffer_handles[i]) {
587 ret = svga->swc->resource_rebind(svga->swc, vbuffer_handles[i],
588 NULL, SVGA_RELOC_READ);
589 if (ret != PIPE_OK)
590 return ret;
591 }
592 }
593 }
594 }
595
596 /* Set primitive type (line, tri, etc) */
597 if (svga->state.hw_draw.topology != range->primType) {
598 ret = SVGA3D_vgpu10_SetTopology(svga->swc, range->primType);
599 if (ret != PIPE_OK)
600 return ret;
601
602 svga->state.hw_draw.topology = range->primType;
603 }
604
605 if (ib_handle) {
606 /* indexed drawing */
607 SVGA3dSurfaceFormat indexFormat = xlate_index_format(range->indexWidth);
608
609 /* setup index buffer */
610 if (ib != svga->state.hw_draw.ib ||
611 indexFormat != svga->state.hw_draw.ib_format ||
612 range->indexArray.offset != svga->state.hw_draw.ib_offset) {
613
614 assert(indexFormat != SVGA3D_FORMAT_INVALID);
615 ret = SVGA3D_vgpu10_SetIndexBuffer(svga->swc, ib_handle,
616 indexFormat,
617 range->indexArray.offset);
618 if (ret != PIPE_OK)
619 return ret;
620
621 pipe_resource_reference(&svga->state.hw_draw.ib, ib);
622 svga->state.hw_draw.ib_format = indexFormat;
623 svga->state.hw_draw.ib_offset = range->indexArray.offset;
624 }
625 else {
626 /* Even though we can avoid emitting the redundant SetIndexBuffer
627 * command, we still need to reference the index buffer surface.
628 */
629 ret = svga->swc->resource_rebind(svga->swc, ib_handle,
630 NULL, SVGA_RELOC_READ);
631 if (ret != PIPE_OK)
632 return ret;
633 }
634
635 if (instance_count > 1) {
636 ret = SVGA3D_vgpu10_DrawIndexedInstanced(svga->swc,
637 vcount,
638 instance_count,
639 0, /* startIndexLocation */
640 range->indexBias,
641 start_instance);
642 if (ret != PIPE_OK)
643 return ret;
644 }
645 else {
646 /* non-instanced drawing */
647 ret = SVGA3D_vgpu10_DrawIndexed(svga->swc,
648 vcount,
649 0, /* startIndexLocation */
650 range->indexBias);
651 if (ret != PIPE_OK)
652 return ret;
653 }
654 }
655 else {
656 /* non-indexed drawing */
657 if (svga->state.hw_draw.ib_format != SVGA3D_FORMAT_INVALID ||
658 svga->state.hw_draw.ib != NULL) {
659 /* Unbind previously bound index buffer */
660 ret = SVGA3D_vgpu10_SetIndexBuffer(svga->swc, NULL,
661 SVGA3D_FORMAT_INVALID, 0);
662 if (ret != PIPE_OK)
663 return ret;
664 pipe_resource_reference(&svga->state.hw_draw.ib, NULL);
665 svga->state.hw_draw.ib_format = SVGA3D_FORMAT_INVALID;
666 }
667
668 assert(svga->state.hw_draw.ib == NULL);
669
670 if (instance_count > 1) {
671 ret = SVGA3D_vgpu10_DrawInstanced(svga->swc,
672 vcount,
673 instance_count,
674 range->indexBias,
675 start_instance);
676 if (ret != PIPE_OK)
677 return ret;
678 }
679 else {
680 /* non-instanced */
681 ret = SVGA3D_vgpu10_Draw(svga->swc,
682 vcount,
683 range->indexBias);
684 if (ret != PIPE_OK)
685 return ret;
686 }
687 }
688
689 hwtnl->cmd.prim_count = 0;
690
691 return PIPE_OK;
692 }
693
694
695
696 /**
697 * Emit any pending drawing commands to the command buffer.
698 * When we receive VGPU9 drawing commands we accumulate them and don't
699 * immediately emit them into the command buffer.
700 * This function needs to be called before we change state that could
701 * effect those pending draws.
702 */
703 enum pipe_error
704 svga_hwtnl_flush(struct svga_hwtnl *hwtnl)
705 {
706 if (!svga_have_vgpu10(hwtnl->svga) && hwtnl->cmd.prim_count) {
707 /* we only queue up primitive for VGPU9 */
708 return draw_vgpu9(hwtnl);
709 }
710 return PIPE_OK;
711 }
712
713
714 void
715 svga_hwtnl_set_index_bias(struct svga_hwtnl *hwtnl, int index_bias)
716 {
717 hwtnl->index_bias = index_bias;
718 }
719
720
721
722 /***********************************************************************
723 * Internal functions:
724 */
725
726 /**
727 * For debugging only.
728 */
729 static void
730 check_draw_params(struct svga_hwtnl *hwtnl,
731 const SVGA3dPrimitiveRange *range,
732 unsigned min_index, unsigned max_index,
733 struct pipe_resource *ib)
734 {
735 unsigned i;
736
737 assert(!svga_have_vgpu10(hwtnl->svga));
738
739 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
740 unsigned j = hwtnl->cmd.vdecl_buffer_index[i];
741 const struct pipe_vertex_buffer *vb = &hwtnl->cmd.vbufs[j];
742 unsigned size = vb->buffer ? vb->buffer->width0 : 0;
743 unsigned offset = hwtnl->cmd.vdecl[i].array.offset;
744 unsigned stride = hwtnl->cmd.vdecl[i].array.stride;
745 int index_bias = (int) range->indexBias + hwtnl->index_bias;
746 unsigned width;
747
748 if (size == 0)
749 continue;
750
751 assert(vb);
752 assert(size);
753 assert(offset < size);
754 assert(min_index <= max_index);
755 (void) width;
756 (void) stride;
757 (void) offset;
758 (void) size;
759
760 switch (hwtnl->cmd.vdecl[i].identity.type) {
761 case SVGA3D_DECLTYPE_FLOAT1:
762 width = 4;
763 break;
764 case SVGA3D_DECLTYPE_FLOAT2:
765 width = 4 * 2;
766 break;
767 case SVGA3D_DECLTYPE_FLOAT3:
768 width = 4 * 3;
769 break;
770 case SVGA3D_DECLTYPE_FLOAT4:
771 width = 4 * 4;
772 break;
773 case SVGA3D_DECLTYPE_D3DCOLOR:
774 width = 4;
775 break;
776 case SVGA3D_DECLTYPE_UBYTE4:
777 width = 1 * 4;
778 break;
779 case SVGA3D_DECLTYPE_SHORT2:
780 width = 2 * 2;
781 break;
782 case SVGA3D_DECLTYPE_SHORT4:
783 width = 2 * 4;
784 break;
785 case SVGA3D_DECLTYPE_UBYTE4N:
786 width = 1 * 4;
787 break;
788 case SVGA3D_DECLTYPE_SHORT2N:
789 width = 2 * 2;
790 break;
791 case SVGA3D_DECLTYPE_SHORT4N:
792 width = 2 * 4;
793 break;
794 case SVGA3D_DECLTYPE_USHORT2N:
795 width = 2 * 2;
796 break;
797 case SVGA3D_DECLTYPE_USHORT4N:
798 width = 2 * 4;
799 break;
800 case SVGA3D_DECLTYPE_UDEC3:
801 width = 4;
802 break;
803 case SVGA3D_DECLTYPE_DEC3N:
804 width = 4;
805 break;
806 case SVGA3D_DECLTYPE_FLOAT16_2:
807 width = 2 * 2;
808 break;
809 case SVGA3D_DECLTYPE_FLOAT16_4:
810 width = 2 * 4;
811 break;
812 default:
813 assert(0);
814 width = 0;
815 break;
816 }
817
818 if (index_bias >= 0) {
819 assert(offset + index_bias * stride + width <= size);
820 }
821
822 /*
823 * min_index/max_index are merely conservative guesses, so we can't
824 * make buffer overflow detection based on their values.
825 */
826 }
827
828 assert(range->indexWidth == range->indexArray.stride);
829
830 if (ib) {
831 MAYBE_UNUSED unsigned size = ib->width0;
832 MAYBE_UNUSED unsigned offset = range->indexArray.offset;
833 MAYBE_UNUSED unsigned stride = range->indexArray.stride;
834 MAYBE_UNUSED unsigned count;
835
836 assert(size);
837 assert(offset < size);
838 assert(stride);
839
840 switch (range->primType) {
841 case SVGA3D_PRIMITIVE_POINTLIST:
842 count = range->primitiveCount;
843 break;
844 case SVGA3D_PRIMITIVE_LINELIST:
845 count = range->primitiveCount * 2;
846 break;
847 case SVGA3D_PRIMITIVE_LINESTRIP:
848 count = range->primitiveCount + 1;
849 break;
850 case SVGA3D_PRIMITIVE_TRIANGLELIST:
851 count = range->primitiveCount * 3;
852 break;
853 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
854 count = range->primitiveCount + 2;
855 break;
856 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
857 count = range->primitiveCount + 2;
858 break;
859 default:
860 assert(0);
861 count = 0;
862 break;
863 }
864
865 assert(offset + count * stride <= size);
866 }
867 }
868
869
870 /**
871 * All drawing filters down into this function, either directly
872 * on the hardware path or after doing software vertex processing.
873 */
874 enum pipe_error
875 svga_hwtnl_prim(struct svga_hwtnl *hwtnl,
876 const SVGA3dPrimitiveRange * range,
877 unsigned vcount,
878 unsigned min_index,
879 unsigned max_index, struct pipe_resource *ib,
880 unsigned start_instance, unsigned instance_count)
881 {
882 enum pipe_error ret = PIPE_OK;
883
884 if (svga_have_vgpu10(hwtnl->svga)) {
885 /* draw immediately */
886 ret = draw_vgpu10(hwtnl, range, vcount, min_index, max_index, ib,
887 start_instance, instance_count);
888 if (ret != PIPE_OK) {
889 svga_context_flush(hwtnl->svga, NULL);
890 ret = draw_vgpu10(hwtnl, range, vcount, min_index, max_index, ib,
891 start_instance, instance_count);
892 assert(ret == PIPE_OK);
893 }
894 }
895 else {
896 /* batch up drawing commands */
897 #ifdef DEBUG
898 check_draw_params(hwtnl, range, min_index, max_index, ib);
899 assert(start_instance == 0);
900 assert(instance_count <= 1);
901 #else
902 (void) check_draw_params;
903 #endif
904
905 if (hwtnl->cmd.prim_count + 1 >= QSZ) {
906 ret = svga_hwtnl_flush(hwtnl);
907 if (ret != PIPE_OK)
908 return ret;
909 }
910
911 /* min/max indices are relative to bias */
912 hwtnl->cmd.min_index[hwtnl->cmd.prim_count] = min_index;
913 hwtnl->cmd.max_index[hwtnl->cmd.prim_count] = max_index;
914
915 hwtnl->cmd.prim[hwtnl->cmd.prim_count] = *range;
916 hwtnl->cmd.prim[hwtnl->cmd.prim_count].indexBias += hwtnl->index_bias;
917
918 pipe_resource_reference(&hwtnl->cmd.prim_ib[hwtnl->cmd.prim_count], ib);
919 hwtnl->cmd.prim_count++;
920 }
921
922 return ret;
923 }