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 "util/u_inlines.h"
27 #include "util/u_prim.h"
28 #include "indices/u_indices.h"
31 #include "svga_draw.h"
32 #include "svga_draw_private.h"
33 #include "svga_resource_buffer.h"
34 #include "svga_winsys.h"
35 #include "svga_context.h"
36 #include "svga_hw_reg.h"
39 static enum pipe_error
40 translate_indices(struct svga_hwtnl
*hwtnl
, struct pipe_resource
*src
,
41 unsigned offset
, unsigned prim
, unsigned nr
,
43 u_translate_func translate
, struct pipe_resource
**out_buf
)
45 struct pipe_context
*pipe
= &hwtnl
->svga
->pipe
;
46 struct pipe_transfer
*src_transfer
= NULL
;
47 struct pipe_transfer
*dst_transfer
= NULL
;
49 const void *src_map
= NULL
;
50 struct pipe_resource
*dst
= NULL
;
53 /* Need to trim vertex count to make sure we don't write too much data
54 * to the dst buffer in the translate() call.
56 u_trim_pipe_prim(prim
, &nr
);
58 size
= index_size
* nr
;
60 dst
= pipe_buffer_create(pipe
->screen
,
61 PIPE_BIND_INDEX_BUFFER
, PIPE_USAGE_DEFAULT
, size
);
65 src_map
= pipe_buffer_map(pipe
, src
, PIPE_TRANSFER_READ
, &src_transfer
);
69 dst_map
= pipe_buffer_map(pipe
, dst
, PIPE_TRANSFER_WRITE
, &dst_transfer
);
73 translate((const char *) src_map
+ offset
, 0, nr
, dst_map
);
75 pipe_buffer_unmap(pipe
, src_transfer
);
76 pipe_buffer_unmap(pipe
, dst_transfer
);
83 pipe_buffer_unmap(pipe
, src_transfer
);
86 pipe_buffer_unmap(pipe
, dst_transfer
);
89 pipe
->screen
->resource_destroy(pipe
->screen
, dst
);
91 return PIPE_ERROR_OUT_OF_MEMORY
;
96 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl
*hwtnl
,
97 struct pipe_resource
*index_buffer
,
98 unsigned index_size
, int index_bias
,
99 unsigned min_index
, unsigned max_index
,
100 unsigned prim
, unsigned start
,
103 SVGA3dPrimitiveRange range
;
106 unsigned index_offset
= start
* index_size
;
108 hw_prim
= svga_translate_prim(prim
, count
, &hw_count
);
110 return PIPE_OK
; /* nothing to draw */
112 /* We should never see user-space buffers in the driver. The vbuf
113 * module should have converted them into real buffers.
116 assert(!svga_buffer_is_user_buffer(index_buffer
));
118 range
.primType
= hw_prim
;
119 range
.primitiveCount
= hw_count
;
120 range
.indexArray
.offset
= index_offset
;
121 range
.indexArray
.stride
= index_size
;
122 range
.indexWidth
= index_size
;
123 range
.indexBias
= index_bias
;
125 return svga_hwtnl_prim(hwtnl
, &range
, min_index
, max_index
, index_buffer
);
130 svga_hwtnl_draw_range_elements(struct svga_hwtnl
*hwtnl
,
131 struct pipe_resource
*index_buffer
,
132 unsigned index_size
, int index_bias
,
133 unsigned min_index
, unsigned max_index
,
134 unsigned prim
, unsigned start
, unsigned count
)
136 unsigned gen_prim
, gen_size
, gen_nr
, gen_type
;
137 u_translate_func gen_func
;
138 enum pipe_error ret
= PIPE_OK
;
140 if (hwtnl
->api_fillmode
!= PIPE_POLYGON_MODE_FILL
&&
141 prim
>= PIPE_PRIM_TRIANGLES
) {
142 gen_type
= u_unfilled_translator(prim
,
147 &gen_size
, &gen_nr
, &gen_func
);
150 gen_type
= u_index_translator(svga_hw_prims
,
156 &gen_prim
, &gen_size
, &gen_nr
, &gen_func
);
159 if (gen_type
== U_TRANSLATE_MEMCPY
) {
160 /* No need for translation, just pass through to hardware:
162 return svga_hwtnl_simple_draw_range_elements(hwtnl
, index_buffer
,
167 gen_prim
, start
, count
);
170 struct pipe_resource
*gen_buf
= NULL
;
172 /* Need to allocate a new index buffer and run the translate
173 * func to populate it. Could potentially cache this translated
174 * index buffer with the original to avoid future
175 * re-translations. Not much point if we're just accelerating
176 * GL though, as index buffers are typically used only once
179 ret
= translate_indices(hwtnl
,
182 gen_prim
, gen_nr
, gen_size
, gen_func
, &gen_buf
);
186 ret
= svga_hwtnl_simple_draw_range_elements(hwtnl
,
192 gen_prim
, 0, gen_nr
);
198 pipe_resource_reference(&gen_buf
, NULL
);