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 "svga_cmd.h"
27
28#include "util/u_inlines.h"
29#include "indices/u_indices.h"
30
31#include "svga_hw_reg.h"
32#include "svga_draw.h"
33#include "svga_draw_private.h"
34#include "svga_context.h"
35
36
37#define DBG 0
38
39
40
41
42static enum pipe_error generate_indices( struct svga_hwtnl *hwtnl,
43                                         unsigned nr,
44                                         unsigned index_size,
45                                         u_generate_func generate,
46                                         struct pipe_resource **out_buf )
47{
48   struct pipe_context *pipe = &hwtnl->svga->pipe;
49   struct pipe_transfer *transfer;
50   unsigned size = index_size * nr;
51   struct pipe_resource *dst = NULL;
52   void *dst_map = NULL;
53
54   dst = pipe_buffer_create( pipe->screen,
55			     PIPE_BIND_INDEX_BUFFER,
56			     PIPE_USAGE_STATIC,
57			     size );
58   if (dst == NULL)
59      goto fail;
60
61   dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE,
62			      &transfer);
63   if (dst_map == NULL)
64      goto fail;
65
66   generate( nr,
67             dst_map );
68
69   pipe_buffer_unmap( pipe, transfer );
70
71   *out_buf = dst;
72   return PIPE_OK;
73
74fail:
75   if (dst_map)
76      pipe_buffer_unmap( pipe, transfer );
77
78   if (dst)
79      pipe->screen->resource_destroy( pipe->screen, dst );
80
81   return PIPE_ERROR_OUT_OF_MEMORY;
82}
83
84static boolean compare( unsigned cached_nr,
85                        unsigned nr,
86                        unsigned type )
87{
88   if (type == U_GENERATE_REUSABLE)
89      return cached_nr >= nr;
90   else
91      return cached_nr == nr;
92}
93
94static enum pipe_error retrieve_or_generate_indices( struct svga_hwtnl *hwtnl,
95                                                     unsigned prim,
96                                                     unsigned gen_type,
97                                                     unsigned gen_nr,
98                                                     unsigned gen_size,
99                                                     u_generate_func generate,
100                                                     struct pipe_resource **out_buf )
101{
102   enum pipe_error ret = PIPE_OK;
103   int i;
104
105   for (i = 0; i < IDX_CACHE_MAX; i++) {
106      if (hwtnl->index_cache[prim][i].buffer != NULL &&
107          hwtnl->index_cache[prim][i].generate == generate)
108      {
109         if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type))
110         {
111            pipe_resource_reference( out_buf,
112                                   hwtnl->index_cache[prim][i].buffer );
113
114            if (DBG)
115               debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);
116
117            return PIPE_OK;
118         }
119         else if (gen_type == U_GENERATE_REUSABLE)
120         {
121            pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
122                                   NULL );
123
124            if (DBG)
125               debug_printf("%s discard %d/%d\n", __FUNCTION__,
126                            i, hwtnl->index_cache[prim][i].gen_nr);
127
128            break;
129         }
130      }
131   }
132
133   if (i == IDX_CACHE_MAX)
134   {
135      unsigned smallest = 0;
136      unsigned smallest_size = ~0;
137
138      for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {
139         if (hwtnl->index_cache[prim][i].buffer == NULL)
140         {
141            smallest = i;
142            smallest_size = 0;
143         }
144         else if (hwtnl->index_cache[prim][i].gen_nr < smallest)
145         {
146            smallest = i;
147            smallest_size = hwtnl->index_cache[prim][i].gen_nr;
148         }
149      }
150
151      assert (smallest != IDX_CACHE_MAX);
152
153      pipe_resource_reference( &hwtnl->index_cache[prim][smallest].buffer,
154                             NULL );
155
156      if (DBG)
157         debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,
158                      smallest, smallest_size);
159
160      i = smallest;
161   }
162
163
164   ret = generate_indices( hwtnl,
165                           gen_nr,
166                           gen_size,
167                           generate,
168                           out_buf );
169   if (ret != PIPE_OK)
170      return ret;
171
172
173   hwtnl->index_cache[prim][i].generate = generate;
174   hwtnl->index_cache[prim][i].gen_nr = gen_nr;
175   pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
176                          *out_buf );
177
178   if (DBG)
179      debug_printf("%s cache %d/%d\n", __FUNCTION__,
180                   i, hwtnl->index_cache[prim][i].gen_nr);
181
182   return PIPE_OK;
183}
184
185
186
187static enum pipe_error
188simple_draw_arrays( struct svga_hwtnl *hwtnl,
189                    unsigned prim, unsigned start, unsigned count )
190{
191   SVGA3dPrimitiveRange range;
192   unsigned hw_prim;
193   unsigned hw_count;
194
195   hw_prim = svga_translate_prim(prim, count, &hw_count);
196   if (hw_count == 0)
197      return PIPE_ERROR_BAD_INPUT;
198
199   range.primType = hw_prim;
200   range.primitiveCount = hw_count;
201   range.indexArray.surfaceId = SVGA3D_INVALID_ID;
202   range.indexArray.offset = 0;
203   range.indexArray.stride = 0;
204   range.indexWidth = 0;
205   range.indexBias = start;
206
207   /* Min/max index should be calculated prior to applying bias, so we
208    * end up with min_index = 0, max_index = count - 1 and everybody
209    * looking at those numbers knows to adjust them by
210    * range.indexBias.
211    */
212   return svga_hwtnl_prim( hwtnl, &range, 0, count - 1, NULL );
213}
214
215
216
217
218
219
220
221
222
223
224enum pipe_error
225svga_hwtnl_draw_arrays( struct svga_hwtnl *hwtnl,
226                        unsigned prim,
227                        unsigned start,
228                        unsigned count)
229{
230   unsigned gen_prim, gen_size, gen_nr, gen_type;
231   u_generate_func gen_func;
232   enum pipe_error ret = PIPE_OK;
233
234   if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
235       prim >= PIPE_PRIM_TRIANGLES)
236   {
237      gen_type = u_unfilled_generator( prim,
238                                       start,
239                                       count,
240                                       hwtnl->api_fillmode,
241                                       &gen_prim,
242                                       &gen_size,
243                                       &gen_nr,
244                                       &gen_func );
245   }
246   else {
247      gen_type = u_index_generator( svga_hw_prims,
248                                    prim,
249                                    start,
250                                    count,
251                                    hwtnl->api_pv,
252                                    hwtnl->hw_pv,
253                                    &gen_prim,
254                                    &gen_size,
255                                    &gen_nr,
256                                    &gen_func );
257   }
258
259   if (gen_type == U_GENERATE_LINEAR) {
260      return simple_draw_arrays( hwtnl, gen_prim, start, count );
261   }
262   else {
263      struct pipe_resource *gen_buf = NULL;
264
265      /* Need to draw as indexed primitive.
266       * Potentially need to run the gen func to build an index buffer.
267       */
268      ret = retrieve_or_generate_indices( hwtnl,
269                                          prim,
270                                          gen_type,
271                                          gen_nr,
272                                          gen_size,
273                                          gen_func,
274                                          &gen_buf );
275      if (ret != PIPE_OK)
276         goto done;
277
278      ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
279                                                   gen_buf,
280                                                   gen_size,
281                                                   start,
282                                                   0,
283                                                   count - 1,
284                                                   gen_prim,
285                                                   0,
286                                                   gen_nr );
287
288      if (ret != PIPE_OK)
289         goto done;
290
291   done:
292      if (gen_buf)
293         pipe_resource_reference( &gen_buf, NULL );
294
295      return ret;
296   }
297}
298
299