summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/svga/svga_draw.c
blob: 2c873a0f7ac276fc65ccb80c0c12df6ef20fecb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**********************************************************
 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/

#include "pipe/p_compiler.h"
#include "util/u_inlines.h"
#include "pipe/p_defines.h"
#include "util/u_memory.h"
#include "util/u_math.h"
#include "util/u_upload_mgr.h"

#include "svga_context.h"
#include "svga_draw.h"
#include "svga_draw_private.h"
#include "svga_debug.h"
#include "svga_screen.h"
#include "svga_resource_buffer.h"
#include "svga_resource_texture.h"
#include "svga_surface.h"
#include "svga_winsys.h"
#include "svga_cmd.h"


struct svga_hwtnl *svga_hwtnl_create( struct svga_context *svga,
                                      struct u_upload_mgr *upload_ib,
                                      struct svga_winsys_context *swc )
{
   struct svga_hwtnl *hwtnl = CALLOC_STRUCT(svga_hwtnl);
   if (hwtnl == NULL)
      goto fail;

   hwtnl->svga = svga;
   hwtnl->upload_ib = upload_ib;
   
   hwtnl->cmd.swc = swc;

   return hwtnl;

fail:
   return NULL;
}

void svga_hwtnl_destroy( struct svga_hwtnl *hwtnl )
{
   int i, j;

   for (i = 0; i < PIPE_PRIM_MAX; i++) {
      for (j = 0; j < IDX_CACHE_MAX; j++) {
         pipe_resource_reference( &hwtnl->index_cache[i][j].buffer,
                                NULL );
      }
   }

   for (i = 0; i < hwtnl->cmd.vdecl_count; i++)
      pipe_resource_reference(&hwtnl->cmd.vdecl_vb[i], NULL);

   for (i = 0; i < hwtnl->cmd.prim_count; i++)
      pipe_resource_reference(&hwtnl->cmd.prim_ib[i], NULL);
      

   FREE(hwtnl);
}


void svga_hwtnl_set_flatshade( struct svga_hwtnl *hwtnl,
                               boolean flatshade,
                               boolean flatshade_first )
{
   hwtnl->hw_pv = PV_FIRST;
   hwtnl->api_pv = (flatshade && !flatshade_first) ? PV_LAST : PV_FIRST;
}                               

void svga_hwtnl_set_unfilled( struct svga_hwtnl *hwtnl,
                              unsigned mode )
{
   hwtnl->api_fillmode = mode;
}                               

void svga_hwtnl_reset_vdecl( struct svga_hwtnl *hwtnl,
                             unsigned count )
{
   unsigned i;

   assert(hwtnl->cmd.prim_count == 0);

   for (i = count; i < hwtnl->cmd.vdecl_count; i++) {
      pipe_resource_reference(&hwtnl->cmd.vdecl_vb[i],
                            NULL);
   }

   hwtnl->cmd.vdecl_count = count;
}


void svga_hwtnl_vdecl( struct svga_hwtnl *hwtnl,
		       unsigned i,
		       const SVGA3dVertexDecl *decl,
		       struct pipe_resource *vb)
{
   assert(hwtnl->cmd.prim_count == 0);

   assert( i < hwtnl->cmd.vdecl_count );

   hwtnl->cmd.vdecl[i] = *decl;

   pipe_resource_reference(&hwtnl->cmd.vdecl_vb[i], vb);   
}



enum pipe_error
svga_hwtnl_flush( struct svga_hwtnl *hwtnl )
{
   struct svga_winsys_context *swc = hwtnl->cmd.swc;
   struct svga_context *svga = hwtnl->svga;
   enum pipe_error ret;

   if (hwtnl->cmd.prim_count) {
      struct svga_winsys_surface *vb_handle[SVGA3D_INPUTREG_MAX];
      struct svga_winsys_surface *ib_handle[QSZ];
      struct svga_winsys_surface *handle;
      SVGA3dVertexDecl *vdecl;
      SVGA3dPrimitiveRange *prim;
      unsigned i;

      /* Unmap upload manager vertex buffers */
      u_upload_flush(svga->upload_vb);

      for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
         handle = svga_buffer_handle(svga, hwtnl->cmd.vdecl_vb[i]);
         if (handle == NULL)
            return PIPE_ERROR_OUT_OF_MEMORY;

         vb_handle[i] = handle;
      }

      /* Unmap upload manager index buffers */
      u_upload_flush(svga->upload_ib);

      for (i = 0; i < hwtnl->cmd.prim_count; i++) {
         if (hwtnl->cmd.prim_ib[i]) {
            handle = svga_buffer_handle(svga, hwtnl->cmd.prim_ib[i]);
            if (handle == NULL)
               return PIPE_ERROR_OUT_OF_MEMORY;
         }
         else
            handle = NULL;

         ib_handle[i] = handle;
      }

      SVGA_DBG(DEBUG_DMA, "draw to sid %p, %d prims\n",
               svga->curr.framebuffer.cbufs[0] ?
               svga_surface(svga->curr.framebuffer.cbufs[0])->handle : NULL,
               hwtnl->cmd.prim_count);

      ret = SVGA3D_BeginDrawPrimitives(swc, 
                                       &vdecl, 
                                       hwtnl->cmd.vdecl_count, 
                                       &prim, 
                                       hwtnl->cmd.prim_count);
      if (ret != PIPE_OK) 
         return ret;

      
      memcpy( vdecl,
              hwtnl->cmd.vdecl,
              hwtnl->cmd.vdecl_count * sizeof hwtnl->cmd.vdecl[0]);

      for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
         /* Given rangeHint is considered to be relative to indexBias, and 
          * indexBias varies per primitive, we cannot accurately supply an 
          * rangeHint when emitting more than one primitive per draw command.
          */
         if (hwtnl->cmd.prim_count == 1) {
            vdecl[i].rangeHint.first = hwtnl->cmd.min_index[0];
            vdecl[i].rangeHint.last = hwtnl->cmd.max_index[0] + 1;
         }
         else {
            vdecl[i].rangeHint.first = 0;
            vdecl[i].rangeHint.last = 0;
         }

         swc->surface_relocation(swc,
                                 &vdecl[i].array.surfaceId,
                                 vb_handle[i],
                                 SVGA_RELOC_READ);
      }

      memcpy( prim,
              hwtnl->cmd.prim,
              hwtnl->cmd.prim_count * sizeof hwtnl->cmd.prim[0]);

      for (i = 0; i < hwtnl->cmd.prim_count; i++) {
         swc->surface_relocation(swc,
                                 &prim[i].indexArray.surfaceId,
                                 ib_handle[i],
                                 SVGA_RELOC_READ);
         pipe_resource_reference(&hwtnl->cmd.prim_ib[i], NULL);
      }
      
      SVGA_FIFOCommitAll( swc );
      hwtnl->cmd.prim_count = 0;
   }

   return PIPE_OK;
}





/***********************************************************************
 * Internal functions:
 */

enum pipe_error svga_hwtnl_prim( struct svga_hwtnl *hwtnl,
                                 const SVGA3dPrimitiveRange *range,
                                 unsigned min_index,
                                 unsigned max_index,
                                 struct pipe_resource *ib )
{
   int ret = PIPE_OK;

#ifdef DEBUG
   {
      unsigned i;
      for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
         struct pipe_resource *vb = hwtnl->cmd.vdecl_vb[i];
         unsigned size = vb ? vb->width0 : 0;
         unsigned offset = hwtnl->cmd.vdecl[i].array.offset;
         unsigned stride = hwtnl->cmd.vdecl[i].array.stride;
         unsigned index_bias = range->indexBias;
         unsigned width;

         assert(vb);
         assert(size);
         assert(offset < size);
         assert(index_bias >= 0);
         assert(min_index <= max_index);
         assert(offset + index_bias*stride < size);
         if (min_index != ~0) {
            assert(offset + (index_bias + min_index) * stride < size);
         }

         switch (hwtnl->cmd.vdecl[i].identity.type) {
         case SVGA3D_DECLTYPE_FLOAT1:
            width = 4;
            break;
         case SVGA3D_DECLTYPE_FLOAT2:
            width = 4*2;
            break;
         case SVGA3D_DECLTYPE_FLOAT3:
            width = 4*3;
            break;
         case SVGA3D_DECLTYPE_FLOAT4:
            width = 4*4;
            break;
         case SVGA3D_DECLTYPE_D3DCOLOR:
            width = 4;
            break;
         case SVGA3D_DECLTYPE_UBYTE4:
            width = 1*4;
            break;
         case SVGA3D_DECLTYPE_SHORT2:
            width = 2*2;
            break;
         case SVGA3D_DECLTYPE_SHORT4:
            width = 2*4;
            break;
         case SVGA3D_DECLTYPE_UBYTE4N:
            width = 1*4;
            break;
         case SVGA3D_DECLTYPE_SHORT2N:
            width = 2*2;
            break;
         case SVGA3D_DECLTYPE_SHORT4N:
            width = 2*4;
            break;
         case SVGA3D_DECLTYPE_USHORT2N:
            width = 2*2;
            break;
         case SVGA3D_DECLTYPE_USHORT4N:
            width = 2*4;
            break;
         case SVGA3D_DECLTYPE_UDEC3:
            width = 4;
            break;
         case SVGA3D_DECLTYPE_DEC3N:
            width = 4;
            break;
         case SVGA3D_DECLTYPE_FLOAT16_2:
            width = 2*2;
            break;
         case SVGA3D_DECLTYPE_FLOAT16_4:
            width = 2*4;
            break;
         default:
            assert(0);
            width = 0;
            break;
         }

         if (max_index != ~0) {
            assert(offset + (index_bias + max_index) * stride + width <= size);
         }
      }

      assert(range->indexWidth == range->indexArray.stride);

      if(ib) {
         unsigned size = ib->width0;
         unsigned offset = range->indexArray.offset;
         unsigned stride = range->indexArray.stride;
         unsigned count;

         assert(size);
         assert(offset < size);
         assert(stride);

         switch (range->primType) {
         case SVGA3D_PRIMITIVE_POINTLIST:
            count = range->primitiveCount;
            break;
         case SVGA3D_PRIMITIVE_LINELIST:
            count = range->primitiveCount * 2;
            break;
         case SVGA3D_PRIMITIVE_LINESTRIP:
            count = range->primitiveCount + 1;
            break;
         case SVGA3D_PRIMITIVE_TRIANGLELIST:
            count = range->primitiveCount * 3;
            break;
         case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
            count = range->primitiveCount + 2;
            break;
         case SVGA3D_PRIMITIVE_TRIANGLEFAN:
            count = range->primitiveCount + 2;
            break;
         default:
            assert(0);
            count = 0;
            break;
         }

         assert(offset + count*stride <= size);
      }
   }
#endif

   if (hwtnl->cmd.prim_count+1 >= QSZ) {
      ret = svga_hwtnl_flush( hwtnl );
      if (ret != PIPE_OK)
         return ret;
   }
   
   /* min/max indices are relative to bias */
   hwtnl->cmd.min_index[hwtnl->cmd.prim_count] = min_index;
   hwtnl->cmd.max_index[hwtnl->cmd.prim_count] = max_index;

   hwtnl->cmd.prim[hwtnl->cmd.prim_count] = *range;

   pipe_resource_reference(&hwtnl->cmd.prim_ib[hwtnl->cmd.prim_count], ib);
   hwtnl->cmd.prim_count++;

   return ret;
}