summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/draw/draw_vs_varient.c
blob: 7ee567d4789823fb1a0a645e8aa87cb997e85d0e (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
/**************************************************************************
 * 
 * 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.
 * 
 **************************************************************************/

 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */

#include "util/u_memory.h"
#include "util/u_math.h"
#include "draw/draw_context.h"
#include "draw/draw_private.h"
#include "draw/draw_vbuf.h"
#include "draw/draw_vertex.h"
#include "draw/draw_vs.h"
#include "translate/translate.h"
#include "translate/translate_cache.h"

/* A first pass at incorporating vertex fetch/emit functionality into 
 */
struct draw_vs_varient_generic {
   struct draw_vs_varient base;

   struct draw_vertex_shader *shader;
   struct draw_context *draw;
   
   /* Basic plan is to run these two translate functions before/after
    * the vertex shader's existing run_linear() routine to simulate
    * the inclusion of this functionality into the shader...  
    * 
    * Next will look at actually including it.
    */
   struct translate *fetch;
   struct translate *emit;

   unsigned temp_vertex_stride;
};





static void vsvg_set_buffer( struct draw_vs_varient *varient,
                             unsigned buffer,
                             const void *ptr,
                             unsigned stride )
{
   struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;

   vsvg->fetch->set_buffer(vsvg->fetch, 
                           buffer, 
                           ptr, 
                           stride);
}


/* Mainly for debug at this stage:
 */
static void do_rhw_viewport( struct draw_vs_varient_generic *vsvg,
                             unsigned count,
                             void *output_buffer )
{
   char *ptr = (char *)output_buffer;
   const float *scale = vsvg->base.vs->draw->viewport.scale;
   const float *trans = vsvg->base.vs->draw->viewport.translate;
   unsigned stride = vsvg->temp_vertex_stride;
   unsigned j;

   ptr += vsvg->base.vs->position_output * 4 * sizeof(float);

   for (j = 0; j < count; j++, ptr += stride) {
      float *data = (float *)ptr;
      float w = 1.0f / data[3];

      data[0] = data[0] * w * scale[0] + trans[0];
      data[1] = data[1] * w * scale[1] + trans[1];
      data[2] = data[2] * w * scale[2] + trans[2];
      data[3] = w;
   }
}

static void do_viewport( struct draw_vs_varient_generic *vsvg,
                         unsigned count,
                         void *output_buffer )
{
   char *ptr = (char *)output_buffer;
   const float *scale = vsvg->base.vs->draw->viewport.scale;
   const float *trans = vsvg->base.vs->draw->viewport.translate;
   unsigned stride = vsvg->temp_vertex_stride;
   unsigned j;

   ptr += vsvg->base.vs->position_output * 4 * sizeof(float);

   for (j = 0; j < count; j++, ptr += stride) {
      float *data = (float *)ptr;

      data[0] = data[0] * scale[0] + trans[0];
      data[1] = data[1] * scale[1] + trans[1];
      data[2] = data[2] * scale[2] + trans[2];
   }
}
                         

static void PIPE_CDECL vsvg_run_elts( struct draw_vs_varient *varient,
                                      const unsigned *elts,
                                      unsigned count,
                                      void *output_buffer)
{
   struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
   unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
   void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
   
   if (0) debug_printf("%s %d \n", __FUNCTION__,  count);
			
   /* Want to do this in small batches for cache locality?
    */
   
   vsvg->fetch->run_elts( vsvg->fetch, 
                          elts,
                          count,
                          temp_buffer );

   vsvg->base.vs->run_linear( vsvg->base.vs, 
                              temp_buffer,
                              temp_buffer,
                              (const float (*)[4])vsvg->base.vs->draw->pt.user.constants,
                              count,
                              temp_vertex_stride, 
                              temp_vertex_stride);


   if (vsvg->base.key.clip) {
      /* not really handling clipping, just do the rhw so we can
       * see the results...
       */
      do_rhw_viewport( vsvg,
                       count,
                       temp_buffer );
   }
   else if (vsvg->base.key.viewport) {
      do_viewport( vsvg,
                   count,
                   temp_buffer );
   }


   vsvg->emit->set_buffer( vsvg->emit,
                           0, 
                           temp_buffer,
                           temp_vertex_stride );

   vsvg->emit->set_buffer( vsvg->emit, 
                           1,
                           &vsvg->draw->rasterizer->point_size,
                           0);

   vsvg->emit->run( vsvg->emit,
                    0, count,
                    output_buffer );

   FREE(temp_buffer);
}


static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
                                        unsigned start,
                                        unsigned count,
                                        void *output_buffer )
{
   struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
   unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
   void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
	
   if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__, start, count,
                       vsvg->base.key.output_stride,
                       temp_vertex_stride);

   vsvg->fetch->run( vsvg->fetch, 
                     start,
                     count,
                     temp_buffer );

   vsvg->base.vs->run_linear( vsvg->base.vs, 
                              temp_buffer,
                              temp_buffer,
                              (const float (*)[4])vsvg->base.vs->draw->pt.user.constants,
                              count,
                              temp_vertex_stride, 
                              temp_vertex_stride);

   if (vsvg->base.key.clip) {
      /* not really handling clipping, just do the rhw so we can
       * see the results...
       */
      do_rhw_viewport( vsvg,
                       count,
                       temp_buffer );
   }
   else if (vsvg->base.key.viewport) {
      do_viewport( vsvg,
                   count,
                   temp_buffer );
   }

   vsvg->emit->set_buffer( vsvg->emit,
                           0, 
                           temp_buffer,
                           temp_vertex_stride );
   
   vsvg->emit->set_buffer( vsvg->emit, 
                           1,
                           &vsvg->draw->rasterizer->point_size,
                           0);
   
   vsvg->emit->run( vsvg->emit,
                    0, count,
                    output_buffer );

   FREE(temp_buffer);
}





static void vsvg_destroy( struct draw_vs_varient *varient )
{
   FREE(varient);
}


struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
                                                 const struct draw_vs_varient_key *key )
{
   unsigned i;
   struct translate_key fetch, emit;

   struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
   if (vsvg == NULL)
      return NULL;

   vsvg->base.key = *key;
   vsvg->base.vs = vs;
   vsvg->base.set_buffer    = vsvg_set_buffer;
   vsvg->base.run_elts      = vsvg_run_elts;
   vsvg->base.run_linear    = vsvg_run_linear;
   vsvg->base.destroy       = vsvg_destroy;

   vsvg->draw = vs->draw;

   vsvg->temp_vertex_stride = MAX2(key->nr_inputs,
                                   vsvg->base.vs->info.num_outputs) * 4 * sizeof(float);

   /* Build free-standing fetch and emit functions:
    */
   fetch.nr_elements = key->nr_inputs;
   fetch.output_stride = vsvg->temp_vertex_stride;
   for (i = 0; i < key->nr_inputs; i++) {
      fetch.element[i].input_format = key->element[i].in.format;
      fetch.element[i].input_buffer = key->element[i].in.buffer;
      fetch.element[i].input_offset = key->element[i].in.offset;
      fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
      fetch.element[i].output_offset = i * 4 * sizeof(float);
      assert(fetch.element[i].output_offset < fetch.output_stride);
   }


   emit.nr_elements = key->nr_outputs;
   emit.output_stride = key->output_stride;
   for (i = 0; i < key->nr_outputs; i++) {
      if (key->element[i].out.format != EMIT_1F_PSIZE)
      {      
         emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
         emit.element[i].input_buffer = 0;
         emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
         emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
         emit.element[i].output_offset = key->element[i].out.offset;
         assert(emit.element[i].input_offset <= fetch.output_stride);
      }
      else {
         emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
         emit.element[i].input_buffer = 1;
         emit.element[i].input_offset = 0;
         emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
         emit.element[i].output_offset = key->element[i].out.offset;
      }
   }

   vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
   vsvg->emit = draw_vs_get_emit( vs->draw, &emit );

   return &vsvg->base;
}