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 */
61static enum pipe_error
62translate_indices(struct svga_hwtnl *hwtnl,
63                  const struct pipe_draw_info *info,
64                  const struct pipe_draw_start_count_bias *draw,
65                  enum pipe_prim_type gen_prim,
66                  unsigned orig_nr, unsigned gen_nr,
67                  unsigned gen_size,
68                  u_translate_func translate,
69                  struct pipe_resource **out_buf,
70                  unsigned *out_offset)
71{
72   struct pipe_context *pipe = &hwtnl->svga->pipe;
73   struct svga_screen *screen = svga_screen(pipe->screen);
74   struct svga_buffer *src_sbuf = NULL;
75   struct pipe_transfer *src_transfer = NULL;
76   struct pipe_transfer *dst_transfer = NULL;
77   const unsigned size = gen_size * gen_nr;
78   const unsigned offset = draw->start * info->index_size;
79   const void *src_map = NULL;
80   struct pipe_resource *dst = NULL;
81   void *dst_map = NULL;
82
83   assert(gen_size == 2 || gen_size == 4);
84   if (!info->has_user_indices)
85      src_sbuf = svga_buffer(info->index.resource);
86
87   /* If the draw_info provides us with a buffer rather than a
88    * user pointer, Check to see if we've already translated that buffer
89    */
90   if (src_sbuf && !screen->debug.no_cache_index_buffers) {
91      /* Check if we already have a translated index buffer */
92      if (src_sbuf->translated_indices.buffer &&
93          src_sbuf->translated_indices.orig_prim == info->mode &&
94          src_sbuf->translated_indices.new_prim == gen_prim &&
95          src_sbuf->translated_indices.offset == offset &&
96          src_sbuf->translated_indices.count == orig_nr &&
97          src_sbuf->translated_indices.index_size == gen_size) {
98         pipe_resource_reference(out_buf, src_sbuf->translated_indices.buffer);
99         return PIPE_OK;
100      }
101   }
102
103   /* Need to trim vertex count to make sure we don't write too much data
104    * to the dst buffer in the translate() call.
105    */
106   u_trim_pipe_prim(gen_prim, &gen_nr);
107
108   if (src_sbuf) {
109      /* If we have a source buffer, create a destination buffer in the
110       * hope that we can reuse the translated data later. If not,
111       * we'd probably be better off using the upload buffer.
112       */
113      dst = pipe_buffer_create(pipe->screen,
114                               PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_IMMUTABLE,
115                               size);
116      if (!dst)
117         goto fail;
118
119      dst_map = pipe_buffer_map(pipe, dst, PIPE_MAP_WRITE, &dst_transfer);
120      if (!dst_map)
121         goto fail;
122
123      *out_offset = 0;
124      src_map = pipe_buffer_map(pipe, info->index.resource,
125                                PIPE_MAP_READ |
126                                PIPE_MAP_UNSYNCHRONIZED,
127                                &src_transfer);
128      if (!src_map)
129         goto fail;
130   } else {
131      /* Allocate upload buffer space. Align to the index size. */
132      u_upload_alloc(pipe->stream_uploader, 0, size, gen_size,
133                     out_offset, &dst, &dst_map);
134      if (!dst)
135         goto fail;
136
137      src_map = info->index.user;
138   }
139
140   translate((const char *) src_map + offset, 0, 0, gen_nr, 0, dst_map);
141
142   if (src_transfer)
143      pipe_buffer_unmap(pipe, src_transfer);
144
145   if (dst_transfer)
146      pipe_buffer_unmap(pipe, dst_transfer);
147   else
148      u_upload_unmap(pipe->stream_uploader);
149
150   *out_buf = dst;
151
152   if (src_sbuf && !screen->debug.no_cache_index_buffers) {
153      /* Save the new, translated index buffer in the hope we can use it
154       * again in the future.
155       */
156      pipe_resource_reference(&src_sbuf->translated_indices.buffer, dst);
157      src_sbuf->translated_indices.orig_prim = info->mode;
158      src_sbuf->translated_indices.new_prim = gen_prim;
159      src_sbuf->translated_indices.offset = offset;
160      src_sbuf->translated_indices.count = orig_nr;
161      src_sbuf->translated_indices.index_size = gen_size;
162   }
163
164   return PIPE_OK;
165
166 fail:
167   if (src_transfer)
168      pipe_buffer_unmap(pipe, src_transfer);
169
170   if (dst_transfer)
171      pipe_buffer_unmap(pipe, dst_transfer);
172   else if (dst_map)
173      u_upload_unmap(pipe->stream_uploader);
174
175   if (dst)
176      pipe_resource_reference(&dst, NULL);
177
178   return PIPE_ERROR_OUT_OF_MEMORY;
179}
180
181
182enum pipe_error
183svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
184                                      struct pipe_resource *index_buffer,
185                                      unsigned index_size, int index_bias,
186                                      unsigned min_index, unsigned max_index,
187                                      enum pipe_prim_type prim, unsigned start,
188                                      unsigned count,
189                                      unsigned start_instance,
190                                      unsigned instance_count,
191                                      ubyte vertices_per_patch)
192{
193   SVGA3dPrimitiveRange range;
194   unsigned hw_prim;
195   unsigned hw_count;
196   unsigned index_offset = start * index_size;
197
198   hw_prim = svga_translate_prim(prim, count, &hw_count, vertices_per_patch);
199   if (hw_count == 0)
200      return PIPE_OK; /* nothing to draw */
201
202   range.primType = hw_prim;
203   range.primitiveCount = hw_count;
204   range.indexArray.offset = index_offset;
205   range.indexArray.stride = index_size;
206   range.indexWidth = index_size;
207   range.indexBias = index_bias;
208
209   return svga_hwtnl_prim(hwtnl, &range, count,
210                          min_index, max_index, index_buffer,
211                          start_instance, instance_count,
212                          NULL, NULL);
213}
214
215
216enum pipe_error
217svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,
218                               const struct pipe_draw_info *info,
219                               const struct pipe_draw_start_count_bias *draw,
220                               unsigned count)
221{
222   struct pipe_context *pipe = &hwtnl->svga->pipe;
223   enum pipe_prim_type gen_prim;
224   unsigned gen_size, gen_nr;
225   enum indices_mode gen_type;
226   u_translate_func gen_func;
227   enum pipe_error ret = PIPE_OK;
228
229   SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga),
230                        SVGA_STATS_TIME_HWTNLDRAWELEMENTS);
231
232   if (svga_need_unfilled_fallback(hwtnl, info->mode)) {
233      gen_type = u_unfilled_translator(info->mode,
234                                       info->index_size,
235                                       count,
236                                       hwtnl->api_fillmode,
237                                       &gen_prim,
238                                       &gen_size, &gen_nr, &gen_func);
239   }
240   else {
241      unsigned hw_pv;
242
243      /* There is no geometry ordering with PATCH, so no need to
244       * consider provoking vertex mode for the translation.
245       * So use the same api_pv as the hw_pv.
246       */
247      hw_pv = info->mode == PIPE_PRIM_PATCHES ? hwtnl->api_pv :
248                                                hwtnl->hw_pv;
249      gen_type = u_index_translator(svga_hw_prims,
250                                    info->mode,
251                                    info->index_size,
252                                    count,
253                                    hwtnl->api_pv,
254                                    hw_pv,
255                                    PR_DISABLE,
256                                    &gen_prim, &gen_size, &gen_nr, &gen_func);
257   }
258
259   if ((gen_type == U_TRANSLATE_MEMCPY) && (info->index_size == gen_size)) {
260      /* No need for translation, just pass through to hardware:
261       */
262      unsigned start_offset = draw->start * info->index_size;
263      struct pipe_resource *index_buffer = NULL;
264      unsigned index_offset;
265
266      if (info->has_user_indices) {
267         u_upload_data(pipe->stream_uploader, 0, count * info->index_size,
268                       info->index_size, (char *) info->index.user + start_offset,
269                       &index_offset, &index_buffer);
270         u_upload_unmap(pipe->stream_uploader);
271         index_offset /= info->index_size;
272      } else {
273         pipe_resource_reference(&index_buffer, info->index.resource);
274         index_offset = draw->start;
275      }
276
277      assert(index_buffer != NULL);
278
279      ret = svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,
280                                                  info->index_size,
281                                                  draw->index_bias,
282                                                  info->index_bounds_valid ? info->min_index : 0,
283                                                  info->index_bounds_valid ? info->max_index : ~0,
284                                                  gen_prim, index_offset, count,
285                                                  info->start_instance,
286                                                  info->instance_count,
287                                                  hwtnl->svga->patch_vertices);
288      pipe_resource_reference(&index_buffer, NULL);
289   }
290   else {
291      struct pipe_resource *gen_buf = NULL;
292      unsigned gen_offset = 0;
293
294      /* Need to allocate a new index buffer and run the translate
295       * func to populate it.  Could potentially cache this translated
296       * index buffer with the original to avoid future
297       * re-translations.  Not much point if we're just accelerating
298       * GL though, as index buffers are typically used only once
299       * there.
300       */
301      ret = translate_indices(hwtnl, info, draw, gen_prim,
302                              count, gen_nr, gen_size,
303                              gen_func, &gen_buf, &gen_offset);
304      if (ret == PIPE_OK) {
305         gen_offset /= gen_size;
306         ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
307                                                     gen_buf,
308                                                     gen_size,
309                                                     draw->index_bias,
310                                                     info->index_bounds_valid ? info->min_index : 0,
311                                                     info->index_bounds_valid ? info->max_index : ~0,
312                                                     gen_prim, gen_offset,
313                                                     gen_nr,
314                                                     info->start_instance,
315                                                     info->instance_count,
316                                                     hwtnl->svga->patch_vertices);
317      }
318
319      if (gen_buf) {
320         pipe_resource_reference(&gen_buf, NULL);
321      }
322   }
323
324   SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));
325   return ret;
326}
327