aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/draw/draw_vs_aos_io.c
blob: a6eb37d12806daa9522f7c32028cf6e9c2c25339 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 "util/u_memory.h"
#include "pipe/p_shader_tokens.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
#include "tgsi/tgsi_exec.h"
#include "draw_vs.h"
#include "draw_vs_aos.h"
#include "draw_vertex.h"

#include "rtasm/rtasm_x86sse.h"

#ifdef PIPE_ARCH_X86

/* Note - don't yet have to worry about interacting with the code in
 * draw_vs_aos.c as there is no intermingling of generated code...
 * That may have to change, we'll see.
 */
static void emit_load_R32G32B32A32( struct aos_compilation *cp, 			   
				    struct x86_reg data,
				    struct x86_reg src_ptr )
{
   sse_movups(cp->func, data, src_ptr);
}

static void emit_load_R32G32B32( struct aos_compilation *cp, 			   
				 struct x86_reg data,
				 struct x86_reg src_ptr )
{
#if 1
   sse_movss(cp->func, data, x86_make_disp(src_ptr, 8));
   /* data = z ? ? ? */
   sse_shufps(cp->func, data, aos_get_internal_xmm( cp, IMM_IDENTITY ), SHUF(X,Y,Z,W) );
   /* data = z ? 0 1 */
   sse_shufps(cp->func, data, data, SHUF(Y,Z,X,W) );
   /* data = ? 0 z 1 */
   sse_movlps(cp->func, data, src_ptr);
   /* data = x y z 1 */
#else
   sse_movups(cp->func, data, src_ptr);
   /* data = x y z ? */
   sse2_pshufd(cp->func, data, data, SHUF(W,X,Y,Z) );
   /* data = ? x y z */
   sse_movss(cp->func, data, aos_get_internal_xmm( cp, IMM_ONES ) );
   /* data = 1 x y z */
   sse2_pshufd(cp->func, data, data, SHUF(Y,Z,W,X) );
   /* data = x y z 1 */
#endif
}

static void emit_load_R32G32( struct aos_compilation *cp, 
			   struct x86_reg data,
			   struct x86_reg src_ptr )
{
   sse_movups(cp->func, data, aos_get_internal_xmm( cp, IMM_IDENTITY ) );
   sse_movlps(cp->func, data, src_ptr);
}


static void emit_load_R32( struct aos_compilation *cp, 
			   struct x86_reg data,
			   struct x86_reg src_ptr )
{
   sse_movss(cp->func, data, src_ptr);
   sse_orps(cp->func, data, aos_get_internal_xmm( cp, IMM_IDENTITY ) );
}


static void emit_load_R8G8B8A8_UNORM( struct aos_compilation *cp,
				       struct x86_reg data,
				       struct x86_reg src_ptr )
{
   sse_movss(cp->func, data, src_ptr);
   sse2_punpcklbw(cp->func, data, aos_get_internal_xmm( cp, IMM_IDENTITY ));
   sse2_punpcklbw(cp->func, data, aos_get_internal_xmm( cp, IMM_IDENTITY ));
   sse2_cvtdq2ps(cp->func, data, data);
   sse_mulps(cp->func, data, aos_get_internal(cp, IMM_INV_255));
}



/* Extended swizzles?  Maybe later.
 */  
static void emit_swizzle( struct aos_compilation *cp,
			  struct x86_reg dest,
			  struct x86_reg src,
			  ubyte shuffle )
{
   sse_shufps(cp->func, dest, src, shuffle);
}



static boolean get_buffer_ptr( struct aos_compilation *cp,
                               boolean linear,
                               unsigned buf_idx,
                               struct x86_reg elt,
                               struct x86_reg ptr)
{
   struct x86_reg buf = x86_make_disp(aos_get_x86( cp, 0, X86_BUFFERS ), 
                                      buf_idx * sizeof(struct aos_buffer));

   struct x86_reg buf_stride = x86_make_disp(buf, 
                                             Offset(struct aos_buffer, stride));
   if (linear) {
      struct x86_reg buf_ptr = x86_make_disp(buf, 
                                             Offset(struct aos_buffer, ptr));


      /* Calculate pointer to current attrib:
       */
      x86_mov(cp->func, ptr, buf_ptr);
      x86_mov(cp->func, elt, buf_stride);
      x86_add(cp->func, elt, ptr);
      if (buf_idx == 0) sse_prefetchnta(cp->func, x86_make_disp(elt, 192));
      x86_mov(cp->func, buf_ptr, elt);
   }
   else {
      struct x86_reg buf_base_ptr = x86_make_disp(buf, 
                                                  Offset(struct aos_buffer, base_ptr));


      /* Calculate pointer to current attrib:
       */
      x86_mov(cp->func, ptr, buf_stride);
      x86_imul(cp->func, ptr, elt);
      x86_add(cp->func, ptr, buf_base_ptr);
   }

   cp->insn_counter++;

   return TRUE;
}


static boolean load_input( struct aos_compilation *cp,
                           unsigned idx,
                           struct x86_reg bufptr )
{
   unsigned format = cp->vaos->base.key.element[idx].in.format;
   unsigned offset = cp->vaos->base.key.element[idx].in.offset;
   struct x86_reg dataXMM = aos_get_xmm_reg(cp);

   /* Figure out source pointer address:
    */
   struct x86_reg src = x86_make_disp(bufptr, offset);

   aos_adopt_xmm_reg( cp,
                      dataXMM,
                      TGSI_FILE_INPUT,
                      idx,
                      TRUE );

   switch (format) {
   case PIPE_FORMAT_R32_FLOAT:
      emit_load_R32(cp, dataXMM, src);
      break;
   case PIPE_FORMAT_R32G32_FLOAT:
      emit_load_R32G32(cp, dataXMM, src);
      break;
   case PIPE_FORMAT_R32G32B32_FLOAT:
      emit_load_R32G32B32(cp, dataXMM, src);
      break;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      emit_load_R32G32B32A32(cp, dataXMM, src);
      break;
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      emit_load_R8G8B8A8_UNORM(cp, dataXMM, src);
      emit_swizzle(cp, dataXMM, dataXMM, SHUF(Z,Y,X,W));
      break;
   case PIPE_FORMAT_R8G8B8A8_UNORM:
      emit_load_R8G8B8A8_UNORM(cp, dataXMM, src);
      break;
   default:
      AOS_ERROR(cp, "unhandled input format");
      return FALSE;
   }

   return TRUE;
}

static boolean load_inputs( struct aos_compilation *cp,
                            unsigned buffer,
                            struct x86_reg ptr )
{
   unsigned i;

   for (i = 0; i < cp->vaos->base.key.nr_inputs; i++) {
      if (cp->vaos->base.key.element[i].in.buffer == buffer) {

         if (!load_input( cp, i, ptr ))
            return FALSE;

         cp->insn_counter++;
      }
   }
   
   return TRUE;
}

boolean aos_init_inputs( struct aos_compilation *cp, boolean linear )
{
   unsigned i;
   for (i = 0; i < cp->vaos->nr_vb; i++) {
      struct x86_reg buf = x86_make_disp(aos_get_x86( cp, 0, X86_BUFFERS ), 
                                         i * sizeof(struct aos_buffer));

      struct x86_reg buf_base_ptr = x86_make_disp(buf, 
                                                  Offset(struct aos_buffer, base_ptr));

      if (cp->vaos->base.key.const_vbuffers & (1<<i)) {
         struct x86_reg ptr = cp->tmp_EAX;

         x86_mov(cp->func, ptr, buf_base_ptr);

         /* Load all inputs for this constant vertex buffer
          */
         load_inputs( cp, i, x86_deref(ptr) );
         
         /* Then just force them out to aos_machine.input[]
          */
         aos_spill_all( cp );

      }
      else if (linear) {

         struct x86_reg elt = cp->idx_EBX;
         struct x86_reg ptr = cp->tmp_EAX;

         struct x86_reg buf_stride = x86_make_disp(buf, 
                                                   Offset(struct aos_buffer, stride));

         struct x86_reg buf_ptr = x86_make_disp(buf, 
                                                Offset(struct aos_buffer, ptr));


         /* Calculate pointer to current attrib:
          */
         x86_mov(cp->func, ptr, buf_stride);
         x86_imul(cp->func, ptr, elt);
         x86_add(cp->func, ptr, buf_base_ptr);


         /* In the linear case, keep the buffer pointer instead of the
          * index number.
          */
         if (cp->vaos->nr_vb == 1) 
            x86_mov( cp->func, elt, ptr );
         else
            x86_mov( cp->func, buf_ptr, ptr );

         cp->insn_counter++;
      }
   }

   return TRUE;
}

boolean aos_fetch_inputs( struct aos_compilation *cp, boolean linear )
{
   unsigned j;

   for (j = 0; j < cp->vaos->nr_vb; j++) {
      if (cp->vaos->base.key.const_vbuffers & (1<<j)) {
         /* just retreive pre-transformed input */
      }
      else if (linear && cp->vaos->nr_vb == 1) {
         load_inputs( cp, 0, cp->idx_EBX );
      }
      else {
         struct x86_reg elt = linear ? cp->idx_EBX : x86_deref(cp->idx_EBX);
         struct x86_reg ptr = cp->tmp_EAX;

         if (!get_buffer_ptr( cp, linear, j, elt, ptr ))
            return FALSE;

         if (!load_inputs( cp, j, ptr ))
            return FALSE;
      }
   }

   return TRUE;
}

boolean aos_incr_inputs( struct aos_compilation *cp, boolean linear )
{
   if (linear && cp->vaos->nr_vb == 1) {
      struct x86_reg stride = x86_make_disp(aos_get_x86( cp, 0, X86_BUFFERS ), 
                                            (0 * sizeof(struct aos_buffer) + 
                                             Offset(struct aos_buffer, stride)));

      x86_add(cp->func, cp->idx_EBX, stride);
      sse_prefetchnta(cp->func, x86_make_disp(cp->idx_EBX, 192));
   }
   else if (linear) {
      /* Nothing to do */
   } 
   else {
      x86_lea(cp->func, cp->idx_EBX, x86_make_disp(cp->idx_EBX, 4));
   }

   return TRUE;
}






static void emit_store_R32G32B32A32( struct aos_compilation *cp, 			   
				     struct x86_reg dst_ptr,
				     struct x86_reg dataXMM )
{
   sse_movups(cp->func, dst_ptr, dataXMM);
}

static void emit_store_R32G32B32( struct aos_compilation *cp, 
				  struct x86_reg dst_ptr,
				  struct x86_reg dataXMM )
{
   sse_movlps(cp->func, dst_ptr, dataXMM);
   sse_shufps(cp->func, dataXMM, dataXMM, SHUF(Z,Z,Z,Z) ); /* NOTE! destructive */
   sse_movss(cp->func, x86_make_disp(dst_ptr,8), dataXMM);
}

static void emit_store_R32G32( struct aos_compilation *cp, 
			       struct x86_reg dst_ptr,
			       struct x86_reg dataXMM )
{
   sse_movlps(cp->func, dst_ptr, dataXMM);
}

static void emit_store_R32( struct aos_compilation *cp, 
			    struct x86_reg dst_ptr,
			    struct x86_reg dataXMM )
{
   sse_movss(cp->func, dst_ptr, dataXMM);
}



static void emit_store_R8G8B8A8_UNORM( struct aos_compilation *cp,
				       struct x86_reg dst_ptr,
				       struct x86_reg dataXMM )
{
   sse_mulps(cp->func, dataXMM, aos_get_internal(cp, IMM_255));
   sse2_cvtps2dq(cp->func, dataXMM, dataXMM);
   sse2_packssdw(cp->func, dataXMM, dataXMM);
   sse2_packuswb(cp->func, dataXMM, dataXMM);
   sse_movss(cp->func, dst_ptr, dataXMM);
}





static boolean emit_output( struct aos_compilation *cp,
                            struct x86_reg ptr,
                            struct x86_reg dataXMM, 
                            unsigned format )
{
   switch (format) {
   case EMIT_1F:
   case EMIT_1F_PSIZE:
      emit_store_R32(cp, ptr, dataXMM);
      break;
   case EMIT_2F:
      emit_store_R32G32(cp, ptr, dataXMM);
      break;
   case EMIT_3F:
      emit_store_R32G32B32(cp, ptr, dataXMM);
      break;
   case EMIT_4F:
      emit_store_R32G32B32A32(cp, ptr, dataXMM);
      break;
   case EMIT_4UB:
      if (1) {
         emit_swizzle(cp, dataXMM, dataXMM, SHUF(Z,Y,X,W));
         emit_store_R8G8B8A8_UNORM(cp, ptr, dataXMM);
      }
      else {
         emit_store_R8G8B8A8_UNORM(cp, ptr, dataXMM);
      }
      break;
   default:
      AOS_ERROR(cp, "unhandled output format");
      return FALSE;
   }

   return TRUE;
}



boolean aos_emit_outputs( struct aos_compilation *cp )
{
   unsigned i;
   
   for (i = 0; i < cp->vaos->base.key.nr_outputs; i++) {
      unsigned format = cp->vaos->base.key.element[i].out.format;
      unsigned offset = cp->vaos->base.key.element[i].out.offset;
      unsigned vs_output = cp->vaos->base.key.element[i].out.vs_output;

      struct x86_reg data;

      if (format == EMIT_1F_PSIZE) {
         data = aos_get_internal_xmm( cp, IMM_PSIZE );
      }
      else {
         data = aos_get_shader_reg( cp, 
                                    TGSI_FILE_OUTPUT,
                                    vs_output );
      }

      if (data.file != file_XMM) {
         struct x86_reg tmp = aos_get_xmm_reg( cp );
         sse_movaps(cp->func, tmp, data);
         data = tmp;
      }
      
      if (!emit_output( cp, 
                        x86_make_disp( cp->outbuf_ECX, offset ),
                        data, 
                        format ))
         return FALSE;

      aos_release_xmm_reg( cp, data.idx );

      cp->insn_counter++;
   }

   return TRUE;
}

#endif