xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_draw_elements.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include "indices/u_indices.h"
9 #include "util/u_inlines.h"
10 #include "util/u_prim.h"
11 #include "util/u_upload_mgr.h"
12 
13 #include "svga_cmd.h"
14 #include "svga_draw.h"
15 #include "svga_draw_private.h"
16 #include "svga_resource_buffer.h"
17 #include "svga_winsys.h"
18 #include "svga_context.h"
19 #include "svga_hw_reg.h"
20 
21 
22 /**
23  * Return a new index buffer which contains a translation of the original
24  * index buffer.  An example of a translation is converting from QUAD
25  * primitives to TRIANGLE primitives.  Each set of four indexes for a quad
26  * will be converted to six indices for two triangles.
27  *
28  * Before generating the new index buffer we'll check if the incoming
29  * buffer already has a translated buffer that can be re-used.
30  * This benefits demos like Cinebench R15 which has many
31  * glDrawElements(GL_QUADS) commands (we can't draw quads natively).
32  *
33  * \param offset  offset in bytes to first index to translate in src buffer
34  * \param orig_prim  original primitive type (like MESA_PRIM_QUADS)
35  * \param gen_prim  new/generated primitive type (like MESA_PRIM_TRIANGLES)
36  * \param orig_nr  number of indexes to translate in source buffer
37  * \param gen_nr  number of indexes to write into new/dest buffer
38  * \param index_size  bytes per index (2 or 4)
39  * \param translate  the translation function from the u_translate module
40  * \param out_buf  returns the new/translated index buffer
41  * \return error code to indicate success failure
42  */
43 static enum pipe_error
translate_indices(struct svga_hwtnl * hwtnl,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw,enum mesa_prim gen_prim,unsigned orig_nr,unsigned gen_nr,unsigned gen_size,u_translate_func translate,struct pipe_resource ** out_buf,unsigned * out_offset)44 translate_indices(struct svga_hwtnl *hwtnl,
45                   const struct pipe_draw_info *info,
46                   const struct pipe_draw_start_count_bias *draw,
47                   enum mesa_prim gen_prim,
48                   unsigned orig_nr, unsigned gen_nr,
49                   unsigned gen_size,
50                   u_translate_func translate,
51                   struct pipe_resource **out_buf,
52                   unsigned *out_offset)
53 {
54    struct pipe_context *pipe = &hwtnl->svga->pipe;
55    struct svga_screen *screen = svga_screen(pipe->screen);
56    struct svga_buffer *src_sbuf = NULL;
57    struct pipe_transfer *src_transfer = NULL;
58    struct pipe_transfer *dst_transfer = NULL;
59    const unsigned size = gen_size * gen_nr;
60    const unsigned offset = draw->start * info->index_size;
61    const void *src_map = NULL;
62    struct pipe_resource *dst = NULL;
63    void *dst_map = NULL;
64 
65    assert(gen_size == 2 || gen_size == 4);
66    if (!info->has_user_indices)
67       src_sbuf = svga_buffer(info->index.resource);
68 
69    /* If the draw_info provides us with a buffer rather than a
70     * user pointer, Check to see if we've already translated that buffer
71     */
72    if (src_sbuf && !screen->debug.no_cache_index_buffers) {
73       /* Check if we already have a translated index buffer */
74       if (src_sbuf->translated_indices.buffer &&
75           src_sbuf->translated_indices.orig_prim == info->mode &&
76           src_sbuf->translated_indices.new_prim == gen_prim &&
77           src_sbuf->translated_indices.offset == offset &&
78           src_sbuf->translated_indices.count == orig_nr &&
79           src_sbuf->translated_indices.index_size == gen_size) {
80          pipe_resource_reference(out_buf, src_sbuf->translated_indices.buffer);
81          return PIPE_OK;
82       }
83    }
84 
85    /* Need to trim vertex count to make sure we don't write too much data
86     * to the dst buffer in the translate() call.
87     */
88    u_trim_pipe_prim(gen_prim, &gen_nr);
89 
90    if (src_sbuf) {
91       /* If we have a source buffer, create a destination buffer in the
92        * hope that we can reuse the translated data later. If not,
93        * we'd probably be better off using the upload buffer.
94        */
95       dst = pipe_buffer_create(pipe->screen,
96                                PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_IMMUTABLE,
97                                size);
98       if (!dst)
99          goto fail;
100 
101       dst_map = pipe_buffer_map(pipe, dst, PIPE_MAP_WRITE, &dst_transfer);
102       if (!dst_map)
103          goto fail;
104 
105       *out_offset = 0;
106       src_map = pipe_buffer_map(pipe, info->index.resource,
107                                 PIPE_MAP_READ |
108                                 PIPE_MAP_UNSYNCHRONIZED,
109                                 &src_transfer);
110       if (!src_map)
111          goto fail;
112    } else {
113       /* Allocate upload buffer space. Align to the index size. */
114       u_upload_alloc(pipe->stream_uploader, 0, size, gen_size,
115                      out_offset, &dst, &dst_map);
116       if (!dst)
117          goto fail;
118 
119       src_map = info->index.user;
120    }
121 
122    translate((const char *) src_map + offset, 0, 0, gen_nr, 0, dst_map);
123 
124    if (src_transfer)
125       pipe_buffer_unmap(pipe, src_transfer);
126 
127    if (dst_transfer)
128       pipe_buffer_unmap(pipe, dst_transfer);
129    else
130       u_upload_unmap(pipe->stream_uploader);
131 
132    *out_buf = dst;
133 
134    if (src_sbuf && !screen->debug.no_cache_index_buffers) {
135       /* Save the new, translated index buffer in the hope we can use it
136        * again in the future.
137        */
138       pipe_resource_reference(&src_sbuf->translated_indices.buffer, dst);
139       src_sbuf->translated_indices.orig_prim = info->mode;
140       src_sbuf->translated_indices.new_prim = gen_prim;
141       src_sbuf->translated_indices.offset = offset;
142       src_sbuf->translated_indices.count = orig_nr;
143       src_sbuf->translated_indices.index_size = gen_size;
144    }
145 
146    return PIPE_OK;
147 
148  fail:
149    if (src_transfer)
150       pipe_buffer_unmap(pipe, src_transfer);
151 
152    if (dst_transfer)
153       pipe_buffer_unmap(pipe, dst_transfer);
154    else if (dst_map)
155       u_upload_unmap(pipe->stream_uploader);
156 
157    if (dst)
158       pipe_resource_reference(&dst, NULL);
159 
160    return PIPE_ERROR_OUT_OF_MEMORY;
161 }
162 
163 
164 enum pipe_error
svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl * hwtnl,struct pipe_resource * index_buffer,unsigned index_size,int index_bias,unsigned min_index,unsigned max_index,enum mesa_prim prim,unsigned start,unsigned count,unsigned start_instance,unsigned instance_count,uint8_t vertices_per_patch)165 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
166                                       struct pipe_resource *index_buffer,
167                                       unsigned index_size, int index_bias,
168                                       unsigned min_index, unsigned max_index,
169                                       enum mesa_prim prim, unsigned start,
170                                       unsigned count,
171                                       unsigned start_instance,
172                                       unsigned instance_count,
173                                       uint8_t vertices_per_patch)
174 {
175    SVGA3dPrimitiveRange range;
176    unsigned hw_prim;
177    unsigned hw_count;
178    unsigned index_offset = start * index_size;
179 
180    hw_prim = svga_translate_prim(prim, count, &hw_count, vertices_per_patch);
181    if (hw_count == 0)
182       return PIPE_OK; /* nothing to draw */
183 
184    range.primType = hw_prim;
185    range.primitiveCount = hw_count;
186    range.indexArray.offset = index_offset;
187    range.indexArray.stride = index_size;
188    range.indexWidth = index_size;
189    range.indexBias = index_bias;
190 
191    return svga_hwtnl_prim(hwtnl, &range, count,
192                           min_index, max_index, index_buffer,
193                           start_instance, instance_count,
194                           NULL, NULL);
195 }
196 
197 
198 enum pipe_error
svga_hwtnl_draw_range_elements(struct svga_hwtnl * hwtnl,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw,unsigned count)199 svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,
200                                const struct pipe_draw_info *info,
201                                const struct pipe_draw_start_count_bias *draw,
202                                unsigned count)
203 {
204    struct pipe_context *pipe = &hwtnl->svga->pipe;
205    enum mesa_prim gen_prim;
206    unsigned gen_size, gen_nr;
207    enum indices_mode gen_type;
208    u_translate_func gen_func;
209    enum pipe_error ret = PIPE_OK;
210 
211    SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga),
212                         SVGA_STATS_TIME_HWTNLDRAWELEMENTS);
213 
214    if (svga_need_unfilled_fallback(hwtnl, info->mode)) {
215       gen_type = u_unfilled_translator(info->mode,
216                                        info->index_size,
217                                        count,
218                                        hwtnl->api_fillmode,
219                                        &gen_prim,
220                                        &gen_size, &gen_nr, &gen_func);
221    }
222    else {
223       unsigned hw_pv;
224 
225       /* There is no geometry ordering with PATCH, so no need to
226        * consider provoking vertex mode for the translation.
227        * So use the same api_pv as the hw_pv.
228        */
229       hw_pv = info->mode == MESA_PRIM_PATCHES ? hwtnl->api_pv :
230                                                 hwtnl->hw_pv;
231       gen_type = u_index_translator(svga_hw_prims,
232                                     info->mode,
233                                     info->index_size,
234                                     count,
235                                     hwtnl->api_pv,
236                                     hw_pv,
237                                     PR_DISABLE,
238                                     &gen_prim, &gen_size, &gen_nr, &gen_func);
239    }
240 
241    if ((gen_type == U_TRANSLATE_MEMCPY) && (info->index_size == gen_size)) {
242       /* No need for translation, just pass through to hardware:
243        */
244       unsigned start_offset = draw->start * info->index_size;
245       struct pipe_resource *index_buffer = NULL;
246       unsigned index_offset;
247 
248       if (info->has_user_indices) {
249          u_upload_data(pipe->stream_uploader, 0, count * info->index_size,
250                        info->index_size, (char *) info->index.user + start_offset,
251                        &index_offset, &index_buffer);
252          u_upload_unmap(pipe->stream_uploader);
253          index_offset /= info->index_size;
254       } else {
255          pipe_resource_reference(&index_buffer, info->index.resource);
256          index_offset = draw->start;
257       }
258 
259       assert(index_buffer != NULL);
260 
261       ret = svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,
262                                                   info->index_size,
263                                                   draw->index_bias,
264                                                   info->index_bounds_valid ? info->min_index : 0,
265                                                   info->index_bounds_valid ? info->max_index : ~0,
266                                                   gen_prim, index_offset, count,
267                                                   info->start_instance,
268                                                   info->instance_count,
269                                                   hwtnl->svga->patch_vertices);
270       pipe_resource_reference(&index_buffer, NULL);
271    }
272    else {
273       struct pipe_resource *gen_buf = NULL;
274       unsigned gen_offset = 0;
275 
276       /* Need to allocate a new index buffer and run the translate
277        * func to populate it.  Could potentially cache this translated
278        * index buffer with the original to avoid future
279        * re-translations.  Not much point if we're just accelerating
280        * GL though, as index buffers are typically used only once
281        * there.
282        */
283       ret = translate_indices(hwtnl, info, draw, gen_prim,
284                               count, gen_nr, gen_size,
285                               gen_func, &gen_buf, &gen_offset);
286       if (ret == PIPE_OK) {
287          gen_offset /= gen_size;
288          ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
289                                                      gen_buf,
290                                                      gen_size,
291                                                      draw->index_bias,
292                                                      info->index_bounds_valid ? info->min_index : 0,
293                                                      info->index_bounds_valid ? info->max_index : ~0,
294                                                      gen_prim, gen_offset,
295                                                      gen_nr,
296                                                      info->start_instance,
297                                                      info->instance_count,
298                                                      hwtnl->svga->patch_vertices);
299       }
300 
301       if (gen_buf) {
302          pipe_resource_reference(&gen_buf, NULL);
303       }
304    }
305 
306    SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));
307    return ret;
308 }
309