Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4358 Serge 1
/**************************************************************************
2
 *
3
 * Copyright 2010, VMware Inc.
4
 * All Rights Reserved.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sub license, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice (including the
15
 * next paragraph) shall be included in all copies or substantial portions
16
 * of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 *
26
 **************************************************************************/
27
 
28
/*
29
 * Binning code for points
30
 */
31
 
32
#include "util/u_math.h"
33
#include "util/u_memory.h"
34
#include "lp_setup_context.h"
35
#include "lp_perf.h"
36
#include "lp_rast.h"
37
#include "lp_state_fs.h"
38
#include "lp_state_setup.h"
39
#include "lp_context.h"
40
#include "tgsi/tgsi_scan.h"
41
 
42
#define NUM_CHANNELS 4
43
 
44
struct point_info {
45
   /* x,y deltas */
46
   int dy01, dy12;
47
   int dx01, dx12;
48
 
49
   const float (*v0)[4];
50
 
51
   float (*a0)[4];
52
   float (*dadx)[4];
53
   float (*dady)[4];
54
};
55
 
56
 
57
/**
58
 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
59
 */
60
static void
61
constant_coef(struct lp_setup_context *setup,
62
              struct point_info *info,
63
              unsigned slot,
64
              const float value,
65
              unsigned i)
66
{
67
   info->a0[slot][i] = value;
68
   info->dadx[slot][i] = 0.0f;
69
   info->dady[slot][i] = 0.0f;
70
}
71
 
72
 
73
static void
74
point_persp_coeff(struct lp_setup_context *setup,
75
                  const struct point_info *info,
76
                  unsigned slot,
77
                  unsigned i)
78
{
79
   /*
80
    * Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A
81
    * better stratergy would be to take the primitive in consideration when
82
    * generating the fragment shader key, and therefore avoid the per-fragment
83
    * perspective divide.
84
    */
85
 
86
   float w0 = info->v0[0][3];
87
 
88
   assert(i < 4);
89
 
90
   info->a0[slot][i] = info->v0[slot][i]*w0;
91
   info->dadx[slot][i] = 0.0f;
92
   info->dady[slot][i] = 0.0f;
93
}
94
 
95
 
96
/**
97
 * Setup automatic texcoord coefficients (for sprite rendering).
98
 * \param slot  the vertex attribute slot to setup
99
 * \param i  the attribute channel in [0,3]
100
 * \param sprite_coord_origin  one of PIPE_SPRITE_COORD_x
101
 * \param perspective  does the shader expects pre-multiplied w, i.e.,
102
 *    LP_INTERP_PERSPECTIVE is specified in the shader key
103
 */
104
static void
105
texcoord_coef(struct lp_setup_context *setup,
106
              const struct point_info *info,
107
              unsigned slot,
108
              unsigned i,
109
              unsigned sprite_coord_origin,
110
              boolean perspective)
111
{
112
   float w0 = info->v0[0][3];
113
 
114
   assert(i < 4);
115
 
116
   if (i == 0) {
117
      float dadx = FIXED_ONE / (float)info->dx12;
118
      float dady =  0.0f;
119
      float x0 = info->v0[0][0] - setup->pixel_offset;
120
      float y0 = info->v0[0][1] - setup->pixel_offset;
121
 
122
      info->dadx[slot][0] = dadx;
123
      info->dady[slot][0] = dady;
124
      info->a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);
125
 
126
      if (perspective) {
127
         info->dadx[slot][0] *= w0;
128
         info->dady[slot][0] *= w0;
129
         info->a0[slot][0] *= w0;
130
      }
131
   }
132
   else if (i == 1) {
133
      float dadx = 0.0f;
134
      float dady = FIXED_ONE / (float)info->dx12;
135
      float x0 = info->v0[0][0] - setup->pixel_offset;
136
      float y0 = info->v0[0][1] - setup->pixel_offset;
137
 
138
      if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {
139
         dady = -dady;
140
      }
141
 
142
      info->dadx[slot][1] = dadx;
143
      info->dady[slot][1] = dady;
144
      info->a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);
145
 
146
      if (perspective) {
147
         info->dadx[slot][1] *= w0;
148
         info->dady[slot][1] *= w0;
149
         info->a0[slot][1] *= w0;
150
      }
151
   }
152
   else if (i == 2) {
153
      info->a0[slot][2] = 0.0f;
154
      info->dadx[slot][2] = 0.0f;
155
      info->dady[slot][2] = 0.0f;
156
   }
157
   else {
158
      info->a0[slot][3] = perspective ? w0 : 1.0f;
159
      info->dadx[slot][3] = 0.0f;
160
      info->dady[slot][3] = 0.0f;
161
   }
162
}
163
 
164
 
165
/**
166
 * Special coefficient setup for gl_FragCoord.
167
 * X and Y are trivial
168
 * Z and W are copied from position_coef which should have already been computed.
169
 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
170
 */
171
static void
172
setup_point_fragcoord_coef(struct lp_setup_context *setup,
173
                           struct point_info *info,
174
                           unsigned slot,
175
                           unsigned usage_mask)
176
{
177
   /*X*/
178
   if (usage_mask & TGSI_WRITEMASK_X) {
179
      info->a0[slot][0] = 0.0;
180
      info->dadx[slot][0] = 1.0;
181
      info->dady[slot][0] = 0.0;
182
   }
183
 
184
   /*Y*/
185
   if (usage_mask & TGSI_WRITEMASK_Y) {
186
      info->a0[slot][1] = 0.0;
187
      info->dadx[slot][1] = 0.0;
188
      info->dady[slot][1] = 1.0;
189
   }
190
 
191
   /*Z*/
192
   if (usage_mask & TGSI_WRITEMASK_Z) {
193
      constant_coef(setup, info, slot, info->v0[0][2], 2);
194
   }
195
 
196
   /*W*/
197
   if (usage_mask & TGSI_WRITEMASK_W) {
198
      constant_coef(setup, info, slot, info->v0[0][3], 3);
199
   }
200
}
201
 
202
 
203
/**
204
 * Compute the point->coef[] array dadx, dady, a0 values.
205
 */
206
static void
207
setup_point_coefficients( struct lp_setup_context *setup,
208
                          struct point_info *info)
209
{
210
   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
211
   const struct lp_fragment_shader *shader = setup->fs.current.variant->shader;
212
   unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
213
   unsigned slot;
214
 
215
   /* setup interpolation for all the remaining attributes:
216
    */
217
   for (slot = 0; slot < key->num_inputs; slot++) {
218
      unsigned vert_attr = key->inputs[slot].src_index;
219
      unsigned usage_mask = key->inputs[slot].usage_mask;
220
      enum lp_interp interp = key->inputs[slot].interp;
221
      boolean perspective = !!(interp == LP_INTERP_PERSPECTIVE);
222
      unsigned i;
223
 
224
      if (perspective & usage_mask) {
225
         fragcoord_usage_mask |= TGSI_WRITEMASK_W;
226
      }
227
 
228
      switch (interp) {
229
      case LP_INTERP_POSITION:
230
         /*
231
          * The generated pixel interpolators will pick up the coeffs from
232
          * slot 0, so all need to ensure that the usage mask is covers all
233
          * usages.
234
          */
235
         fragcoord_usage_mask |= usage_mask;
236
         break;
237
 
238
      case LP_INTERP_LINEAR:
239
         /* Sprite tex coords may use linear interpolation someday */
240
         /* fall-through */
241
      case LP_INTERP_PERSPECTIVE:
242
         /* check if the sprite coord flag is set for this attribute.
243
          * If so, set it up so it up so x and y vary from 0 to 1.
244
          */
245
         if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_GENERIC) {
246
            unsigned semantic_index = shader->info.base.input_semantic_index[slot];
247
            /* Note that sprite_coord enable is a bitfield of
248
             * PIPE_MAX_SHADER_OUTPUTS bits.
249
             */
250
            if (semantic_index < PIPE_MAX_SHADER_OUTPUTS &&
251
                (setup->sprite_coord_enable & (1 << semantic_index))) {
252
               for (i = 0; i < NUM_CHANNELS; i++) {
253
                  if (usage_mask & (1 << i)) {
254
                     texcoord_coef(setup, info, slot + 1, i,
255
                                   setup->sprite_coord_origin,
256
                                   perspective);
257
                  }
258
               }
259
               break;
260
            }
261
         }
262
         /* fall-through */
263
      case LP_INTERP_CONSTANT:
264
         for (i = 0; i < NUM_CHANNELS; i++) {
265
            if (usage_mask & (1 << i)) {
266
               if (perspective) {
267
                  point_persp_coeff(setup, info, slot+1, i);
268
               }
269
               else {
270
                  constant_coef(setup, info, slot+1, info->v0[vert_attr][i], i);
271
               }
272
            }
273
         }
274
         break;
275
 
276
      case LP_INTERP_FACING:
277
         for (i = 0; i < NUM_CHANNELS; i++)
278
            if (usage_mask & (1 << i))
279
               constant_coef(setup, info, slot+1, 1.0, i);
280
         break;
281
 
282
      default:
283
         assert(0);
284
         break;
285
      }
286
   }
287
 
288
   /* The internal position input is in slot zero:
289
    */
290
   setup_point_fragcoord_coef(setup, info, 0,
291
                              fragcoord_usage_mask);
292
}
293
 
294
 
295
static INLINE int
296
subpixel_snap(float a)
297
{
298
   return util_iround(FIXED_ONE * a);
299
}
300
 
301
 
302
static boolean
303
try_setup_point( struct lp_setup_context *setup,
304
                 const float (*v0)[4] )
305
{
306
   struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
307
   /* x/y positions in fixed point */
308
   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
309
   const int sizeAttr = setup->psize;
310
   const float size
311
      = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
312
      : setup->point_size;
313
 
314
   /* Point size as fixed point integer, remove rounding errors
315
    * and gives minimum width for very small points
316
    */
317
   int fixed_width = MAX2(FIXED_ONE,
318
                          (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
319
 
320
   const int x0 = subpixel_snap(v0[0][0] - setup->pixel_offset) - fixed_width/2;
321
   const int y0 = subpixel_snap(v0[0][1] - setup->pixel_offset) - fixed_width/2;
322
 
323
   struct lp_scene *scene = setup->scene;
324
   struct lp_rast_triangle *point;
325
   unsigned bytes;
326
   struct u_rect bbox;
327
   unsigned nr_planes = 4;
328
   struct point_info info;
329
   unsigned scissor_index = 0;
330
   unsigned layer = 0;
331
 
332
   if (setup->viewport_index_slot > 0) {
333
      unsigned *udata = (unsigned*)v0[setup->viewport_index_slot];
334
      scissor_index = lp_clamp_scissor_idx(*udata);
335
   }
336
   if (setup->layer_slot > 0) {
337
      layer = *(unsigned*)v0[setup->layer_slot];
338
      layer = MIN2(layer, scene->fb_max_layer);
339
   }
340
 
341
   /* Bounding rectangle (in pixels) */
342
   {
343
      /* Yes this is necessary to accurately calculate bounding boxes
344
       * with the two fill-conventions we support.  GL (normally) ends
345
       * up needing a bottom-left fill convention, which requires
346
       * slightly different rounding.
347
       */
348
      int adj = (setup->pixel_offset != 0) ? 1 : 0;
349
 
350
      bbox.x0 = (x0 + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
351
      bbox.x1 = (x0 + fixed_width + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
352
      bbox.y0 = (y0 + (FIXED_ONE-1)) >> FIXED_ORDER;
353
      bbox.y1 = (y0 + fixed_width + (FIXED_ONE-1)) >> FIXED_ORDER;
354
 
355
      /* Inclusive coordinates:
356
       */
357
      bbox.x1--;
358
      bbox.y1--;
359
   }
360
 
361
   if (!u_rect_test_intersection(&setup->draw_regions[scissor_index], &bbox)) {
362
      if (0) debug_printf("offscreen\n");
363
      LP_COUNT(nr_culled_tris);
364
      return TRUE;
365
   }
366
 
367
   u_rect_find_intersection(&setup->draw_regions[scissor_index], &bbox);
368
 
369
   point = lp_setup_alloc_triangle(scene,
370
                                   key->num_inputs,
371
                                   nr_planes,
372
                                   &bytes);
373
   if (!point)
374
      return FALSE;
375
 
376
#ifdef DEBUG
377
   point->v[0][0] = v0[0][0];
378
   point->v[0][1] = v0[0][1];
379
#endif
380
 
381
   LP_COUNT(nr_tris);
382
 
383
   if (lp_context->active_statistics_queries) {
384
      lp_context->pipeline_statistics.c_primitives++;
385
   }
386
 
387
   info.v0 = v0;
388
   info.dx01 = 0;
389
   info.dx12 = fixed_width;
390
   info.dy01 = fixed_width;
391
   info.dy12 = 0;
392
   info.a0 = GET_A0(&point->inputs);
393
   info.dadx = GET_DADX(&point->inputs);
394
   info.dady = GET_DADY(&point->inputs);
395
 
396
   /* Setup parameter interpolants:
397
    */
398
   setup_point_coefficients(setup, &info);
399
 
400
   point->inputs.frontfacing = TRUE;
401
   point->inputs.disable = FALSE;
402
   point->inputs.opaque = FALSE;
403
   point->inputs.layer = layer;
404
 
405
   {
406
      struct lp_rast_plane *plane = GET_PLANES(point);
407
 
408
      plane[0].dcdx = -1;
409
      plane[0].dcdy = 0;
410
      plane[0].c = 1-bbox.x0;
411
      plane[0].eo = 1;
412
 
413
      plane[1].dcdx = 1;
414
      plane[1].dcdy = 0;
415
      plane[1].c = bbox.x1+1;
416
      plane[1].eo = 0;
417
 
418
      plane[2].dcdx = 0;
419
      plane[2].dcdy = 1;
420
      plane[2].c = 1-bbox.y0;
421
      plane[2].eo = 1;
422
 
423
      plane[3].dcdx = 0;
424
      plane[3].dcdy = -1;
425
      plane[3].c = bbox.y1+1;
426
      plane[3].eo = 0;
427
   }
428
 
429
   return lp_setup_bin_triangle(setup, point, &bbox, nr_planes, scissor_index);
430
}
431
 
432
 
433
static void
434
lp_setup_point(struct lp_setup_context *setup,
435
               const float (*v0)[4])
436
{
437
   if (!try_setup_point( setup, v0 ))
438
   {
439
      if (!lp_setup_flush_and_restart(setup))
440
         return;
441
 
442
      if (!try_setup_point( setup, v0 ))
443
         return;
444
   }
445
}
446
 
447
 
448
void
449
lp_setup_choose_point( struct lp_setup_context *setup )
450
{
451
   setup->point = lp_setup_point;
452
}
453