gallium: add PIPE_CAP_FRAMEBUFFER_MSAA_CONSTRAINTS
[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_prim.h"
28 #include "util/u_upload_mgr.h"
29 #include "indices/u_indices.h"
30
31 #include "svga_cmd.h"
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_resource_buffer.h"
35 #include "svga_winsys.h"
36 #include "svga_context.h"
37 #include "svga_hw_reg.h"
38
39
40 /**
41 * Return a new index buffer which contains a translation of the original
42 * index buffer. An example of a translation is converting from QUAD
43 * primitives to TRIANGLE primitives. Each set of four indexes for a quad
44 * will be converted to six indices for two triangles.
45 *
46 * Before generating the new index buffer we'll check if the incoming
47 * buffer already has a translated buffer that can be re-used.
48 * This benefits demos like Cinebench R15 which has many
49 * glDrawElements(GL_QUADS) commands (we can't draw quads natively).
50 *
51 * \param offset offset in bytes to first index to translate in src buffer
52 * \param orig_prim original primitive type (like PIPE_PRIM_QUADS)
53 * \param gen_prim new/generated primitive type (like PIPE_PRIM_TRIANGLES)
54 * \param orig_nr number of indexes to translate in source buffer
55 * \param gen_nr number of indexes to write into new/dest buffer
56 * \param index_size bytes per index (2 or 4)
57 * \param translate the translation function from the u_translate module
58 * \param out_buf returns the new/translated index buffer
59 * \return error code to indicate success failure
60 */
61 static enum pipe_error
62 translate_indices(struct svga_hwtnl *hwtnl, struct pipe_resource *src,
63 unsigned offset,
64 enum pipe_prim_type orig_prim, enum pipe_prim_type gen_prim,
65 unsigned orig_nr, unsigned gen_nr,
66 unsigned index_size,
67 u_translate_func translate, struct pipe_resource **out_buf)
68 {
69 struct pipe_context *pipe = &hwtnl->svga->pipe;
70 struct svga_screen *screen = svga_screen(pipe->screen);
71 struct svga_buffer *src_sbuf = svga_buffer(src);
72 struct pipe_transfer *src_transfer = NULL;
73 struct pipe_transfer *dst_transfer = NULL;
74 unsigned size = index_size * gen_nr;
75 const void *src_map = NULL;
76 struct pipe_resource *dst = NULL;
77 void *dst_map = NULL;
78
79 assert(index_size == 2 || index_size == 4);
80
81 if (!screen->debug.no_cache_index_buffers) {
82 /* Check if we already have a translated index buffer */
83 if (src_sbuf->translated_indices.buffer &&
84 src_sbuf->translated_indices.orig_prim == orig_prim &&
85 src_sbuf->translated_indices.new_prim == gen_prim &&
86 src_sbuf->translated_indices.offset == offset &&
87 src_sbuf->translated_indices.count == orig_nr &&
88 src_sbuf->translated_indices.index_size == index_size) {
89 pipe_resource_reference(out_buf, src_sbuf->translated_indices.buffer);
90 return PIPE_OK;
91 }
92 }
93
94 /* Need to trim vertex count to make sure we don't write too much data
95 * to the dst buffer in the translate() call.
96 */
97 u_trim_pipe_prim(gen_prim, &gen_nr);
98
99 size = index_size * gen_nr;
100
101 dst = pipe_buffer_create(pipe->screen,
102 PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_DEFAULT, size);
103 if (!dst)
104 goto fail;
105
106 src_map = pipe_buffer_map(pipe, src, PIPE_TRANSFER_READ, &src_transfer);
107 if (!src_map)
108 goto fail;
109
110 dst_map = pipe_buffer_map(pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer);
111 if (!dst_map)
112 goto fail;
113
114 translate((const char *) src_map + offset, 0, 0, gen_nr, 0, dst_map);
115
116 pipe_buffer_unmap(pipe, src_transfer);
117 pipe_buffer_unmap(pipe, dst_transfer);
118
119 *out_buf = dst;
120
121 if (!screen->debug.no_cache_index_buffers) {
122 /* Save the new, translated index buffer in the hope we can use it
123 * again in the future.
124 */
125 pipe_resource_reference(&src_sbuf->translated_indices.buffer, dst);
126 src_sbuf->translated_indices.orig_prim = orig_prim;
127 src_sbuf->translated_indices.new_prim = gen_prim;
128 src_sbuf->translated_indices.offset = offset;
129 src_sbuf->translated_indices.count = orig_nr;
130 src_sbuf->translated_indices.index_size = index_size;
131 }
132
133 return PIPE_OK;
134
135 fail:
136 if (src_map)
137 pipe_buffer_unmap(pipe, src_transfer);
138
139 if (dst_map)
140 pipe_buffer_unmap(pipe, dst_transfer);
141
142 if (dst)
143 pipe->screen->resource_destroy(pipe->screen, dst);
144
145 return PIPE_ERROR_OUT_OF_MEMORY;
146 }
147
148
149 enum pipe_error
150 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
151 struct pipe_resource *index_buffer,
152 unsigned index_size, int index_bias,
153 unsigned min_index, unsigned max_index,
154 enum pipe_prim_type prim, unsigned start,
155 unsigned count,
156 unsigned start_instance,
157 unsigned instance_count)
158 {
159 SVGA3dPrimitiveRange range;
160 unsigned hw_prim;
161 unsigned hw_count;
162 unsigned index_offset = start * index_size;
163
164 hw_prim = svga_translate_prim(prim, count, &hw_count);
165 if (hw_count == 0)
166 return PIPE_OK; /* nothing to draw */
167
168 range.primType = hw_prim;
169 range.primitiveCount = hw_count;
170 range.indexArray.offset = index_offset;
171 range.indexArray.stride = index_size;
172 range.indexWidth = index_size;
173 range.indexBias = index_bias;
174
175 return svga_hwtnl_prim(hwtnl, &range, count,
176 min_index, max_index, index_buffer,
177 start_instance, instance_count);
178 }
179
180
181 enum pipe_error
182 svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,
183 struct pipe_resource *index_buffer,
184 unsigned index_size, int index_bias,
185 unsigned min_index, unsigned max_index,
186 enum pipe_prim_type prim, unsigned start, unsigned count,
187 unsigned start_instance, unsigned instance_count)
188 {
189 enum pipe_prim_type gen_prim;
190 unsigned gen_size, gen_nr;
191 enum indices_mode gen_type;
192 u_translate_func gen_func;
193 enum pipe_error ret = PIPE_OK;
194
195 SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga),
196 SVGA_STATS_TIME_HWTNLDRAWELEMENTS);
197
198 if (svga_need_unfilled_fallback(hwtnl, prim)) {
199 gen_type = u_unfilled_translator(prim,
200 index_size,
201 count,
202 hwtnl->api_fillmode,
203 &gen_prim,
204 &gen_size, &gen_nr, &gen_func);
205 }
206 else {
207 gen_type = u_index_translator(svga_hw_prims,
208 prim,
209 index_size,
210 count,
211 hwtnl->api_pv,
212 hwtnl->hw_pv,
213 PR_DISABLE,
214 &gen_prim, &gen_size, &gen_nr, &gen_func);
215 }
216
217 if (gen_type == U_TRANSLATE_MEMCPY) {
218 /* No need for translation, just pass through to hardware:
219 */
220 ret = svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,
221 index_size,
222 index_bias,
223 min_index,
224 max_index,
225 gen_prim, start, count,
226 start_instance,
227 instance_count);
228 }
229 else {
230 struct pipe_resource *gen_buf = NULL;
231
232 /* Need to allocate a new index buffer and run the translate
233 * func to populate it. Could potentially cache this translated
234 * index buffer with the original to avoid future
235 * re-translations. Not much point if we're just accelerating
236 * GL though, as index buffers are typically used only once
237 * there.
238 */
239 ret = translate_indices(hwtnl,
240 index_buffer,
241 start * index_size,
242 prim, gen_prim,
243 count, gen_nr, gen_size,
244 gen_func, &gen_buf);
245 if (ret == PIPE_OK) {
246 ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
247 gen_buf,
248 gen_size,
249 index_bias,
250 min_index,
251 max_index,
252 gen_prim, 0, gen_nr,
253 start_instance,
254 instance_count);
255 }
256
257 if (gen_buf) {
258 pipe_resource_reference(&gen_buf, NULL);
259 }
260 }
261
262 SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));
263 return ret;
264 }