radeonsi: emit TA_BC_BASE_ADDR_HI for border color on CIK
[mesa.git] / src / gallium / drivers / svga / svga_draw_elements.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 "util/u_inlines.h"
27 #include "util/u_upload_mgr.h"
28 #include "indices/u_indices.h"
29
30 #include "svga_cmd.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"
37
38
39 static enum pipe_error
40 translate_indices( struct svga_hwtnl *hwtnl,
41 struct pipe_resource *src,
42 unsigned offset,
43 unsigned nr,
44 unsigned index_size,
45 u_translate_func translate,
46 struct pipe_resource **out_buf )
47 {
48 struct pipe_context *pipe = &hwtnl->svga->pipe;
49 struct pipe_transfer *src_transfer = NULL;
50 struct pipe_transfer *dst_transfer = NULL;
51 unsigned size = index_size * nr;
52 const void *src_map = NULL;
53 struct pipe_resource *dst = NULL;
54 void *dst_map = NULL;
55
56 dst = pipe_buffer_create( pipe->screen,
57 PIPE_BIND_INDEX_BUFFER,
58 PIPE_USAGE_STATIC,
59 size );
60 if (dst == NULL)
61 goto fail;
62
63 src_map = pipe_buffer_map( pipe, src, PIPE_TRANSFER_READ, &src_transfer );
64 if (src_map == NULL)
65 goto fail;
66
67 dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer );
68 if (dst_map == NULL)
69 goto fail;
70
71 translate( (const char *)src_map + offset,
72 nr,
73 dst_map );
74
75 pipe_buffer_unmap( pipe, src_transfer );
76 pipe_buffer_unmap( pipe, dst_transfer );
77
78 *out_buf = dst;
79 return PIPE_OK;
80
81 fail:
82 if (src_map)
83 pipe_buffer_unmap( pipe, src_transfer );
84
85 if (dst_map)
86 pipe_buffer_unmap( pipe, dst_transfer );
87
88 if (dst)
89 pipe->screen->resource_destroy( pipe->screen, dst );
90
91 return PIPE_ERROR_OUT_OF_MEMORY;
92 }
93
94
95
96
97
98 enum pipe_error
99 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
100 struct pipe_resource *index_buffer,
101 unsigned index_size,
102 int index_bias,
103 unsigned min_index,
104 unsigned max_index,
105 unsigned prim,
106 unsigned start,
107 unsigned count )
108 {
109 struct pipe_resource *upload_buffer = NULL;
110 SVGA3dPrimitiveRange range;
111 unsigned hw_prim;
112 unsigned hw_count;
113 unsigned index_offset = start * index_size;
114 enum pipe_error ret = PIPE_OK;
115
116 hw_prim = svga_translate_prim(prim, count, &hw_count);
117 if (hw_count == 0)
118 goto done;
119
120 if (index_buffer &&
121 svga_buffer_is_user_buffer(index_buffer))
122 {
123 assert( index_buffer->width0 >= index_offset + count * index_size );
124
125 ret = u_upload_buffer( hwtnl->upload_ib,
126 0,
127 index_offset,
128 count * index_size,
129 index_buffer,
130 &index_offset,
131 &upload_buffer);
132 if (ret != PIPE_OK)
133 goto done;
134
135 /* Don't need to worry about refcounting index_buffer as this is
136 * just a stack variable without a counted reference of its own.
137 * The caller holds the reference.
138 */
139 index_buffer = upload_buffer;
140 }
141
142 range.primType = hw_prim;
143 range.primitiveCount = hw_count;
144 range.indexArray.offset = index_offset;
145 range.indexArray.stride = index_size;
146 range.indexWidth = index_size;
147 range.indexBias = index_bias;
148
149 ret = svga_hwtnl_prim( hwtnl, &range, min_index, max_index, index_buffer );
150 if (ret != PIPE_OK)
151 goto done;
152
153 done:
154 if (upload_buffer)
155 pipe_resource_reference( &upload_buffer, NULL );
156
157 return ret;
158 }
159
160
161
162
163 enum pipe_error
164 svga_hwtnl_draw_range_elements( struct svga_hwtnl *hwtnl,
165 struct pipe_resource *index_buffer,
166 unsigned index_size,
167 int index_bias,
168 unsigned min_index,
169 unsigned max_index,
170 unsigned prim, unsigned start, unsigned count)
171 {
172 unsigned gen_prim, gen_size, gen_nr, gen_type;
173 u_translate_func gen_func;
174 enum pipe_error ret = PIPE_OK;
175
176 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
177 prim >= PIPE_PRIM_TRIANGLES)
178 {
179 gen_type = u_unfilled_translator( prim,
180 index_size,
181 count,
182 hwtnl->api_fillmode,
183 &gen_prim,
184 &gen_size,
185 &gen_nr,
186 &gen_func );
187 }
188 else
189 {
190 gen_type = u_index_translator( svga_hw_prims,
191 prim,
192 index_size,
193 count,
194 hwtnl->api_pv,
195 hwtnl->hw_pv,
196 &gen_prim,
197 &gen_size,
198 &gen_nr,
199 &gen_func );
200 }
201
202 if (gen_type == U_TRANSLATE_MEMCPY) {
203 /* No need for translation, just pass through to hardware:
204 */
205 return svga_hwtnl_simple_draw_range_elements( hwtnl, index_buffer,
206 index_size,
207 index_bias,
208 min_index,
209 max_index,
210 gen_prim, start, count );
211 }
212 else {
213 struct pipe_resource *gen_buf = NULL;
214
215 /* Need to allocate a new index buffer and run the translate
216 * func to populate it. Could potentially cache this translated
217 * index buffer with the original to avoid future
218 * re-translations. Not much point if we're just accelerating
219 * GL though, as index buffers are typically used only once
220 * there.
221 */
222 ret = translate_indices( hwtnl,
223 index_buffer,
224 start * index_size,
225 gen_nr,
226 gen_size,
227 gen_func,
228 &gen_buf );
229 if (ret != PIPE_OK)
230 goto done;
231
232 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
233 gen_buf,
234 gen_size,
235 index_bias,
236 min_index,
237 max_index,
238 gen_prim,
239 0,
240 gen_nr );
241 if (ret != PIPE_OK)
242 goto done;
243
244 done:
245 if (gen_buf)
246 pipe_resource_reference( &gen_buf, NULL );
247
248 return ret;
249 }
250 }