1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2010, VMware Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/*
29bf215546Sopenharmony_ci * Binning code for points
30bf215546Sopenharmony_ci */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/u_math.h"
33bf215546Sopenharmony_ci#include "util/u_memory.h"
34bf215546Sopenharmony_ci#include "lp_setup_context.h"
35bf215546Sopenharmony_ci#include "lp_perf.h"
36bf215546Sopenharmony_ci#include "lp_rast.h"
37bf215546Sopenharmony_ci#include "lp_state_fs.h"
38bf215546Sopenharmony_ci#include "lp_state_setup.h"
39bf215546Sopenharmony_ci#include "lp_context.h"
40bf215546Sopenharmony_ci#include "tgsi/tgsi_scan.h"
41bf215546Sopenharmony_ci#include "draw/draw_context.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#define NUM_CHANNELS 4
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistruct point_info {
46bf215546Sopenharmony_ci   /* x,y deltas */
47bf215546Sopenharmony_ci   int dy01, dy12;
48bf215546Sopenharmony_ci   int dx01, dx12;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   const float (*v0)[4];
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   float (*a0)[4];
53bf215546Sopenharmony_ci   float (*dadx)[4];
54bf215546Sopenharmony_ci   float (*dady)[4];
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   boolean frontfacing;
57bf215546Sopenharmony_ci};
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci/**
61bf215546Sopenharmony_ci * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
62bf215546Sopenharmony_ci */
63bf215546Sopenharmony_cistatic void
64bf215546Sopenharmony_ciconstant_coef(struct lp_setup_context *setup,
65bf215546Sopenharmony_ci              struct point_info *info,
66bf215546Sopenharmony_ci              unsigned slot,
67bf215546Sopenharmony_ci              const float value,
68bf215546Sopenharmony_ci              unsigned i)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   info->a0[slot][i] = value;
71bf215546Sopenharmony_ci   info->dadx[slot][i] = 0.0f;
72bf215546Sopenharmony_ci   info->dady[slot][i] = 0.0f;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic void
77bf215546Sopenharmony_cipoint_persp_coeff(struct lp_setup_context *setup,
78bf215546Sopenharmony_ci                  const struct point_info *info,
79bf215546Sopenharmony_ci                  unsigned slot,
80bf215546Sopenharmony_ci                  unsigned i)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   /*
83bf215546Sopenharmony_ci    * Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A
84bf215546Sopenharmony_ci    * better strategy would be to take the primitive in consideration when
85bf215546Sopenharmony_ci    * generating the fragment shader key, and therefore avoid the per-fragment
86bf215546Sopenharmony_ci    * perspective divide.
87bf215546Sopenharmony_ci    */
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   float w0 = info->v0[0][3];
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   assert(i < 4);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   info->a0[slot][i] = info->v0[slot][i]*w0;
94bf215546Sopenharmony_ci   info->dadx[slot][i] = 0.0f;
95bf215546Sopenharmony_ci   info->dady[slot][i] = 0.0f;
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci/**
100bf215546Sopenharmony_ci * Setup automatic texcoord coefficients (for sprite rendering).
101bf215546Sopenharmony_ci * \param slot  the vertex attribute slot to setup
102bf215546Sopenharmony_ci * \param i  the attribute channel in [0,3]
103bf215546Sopenharmony_ci * \param sprite_coord_origin  one of PIPE_SPRITE_COORD_x
104bf215546Sopenharmony_ci * \param perspective  does the shader expects pre-multiplied w, i.e.,
105bf215546Sopenharmony_ci *    LP_INTERP_PERSPECTIVE is specified in the shader key
106bf215546Sopenharmony_ci */
107bf215546Sopenharmony_cistatic void
108bf215546Sopenharmony_citexcoord_coef(struct lp_setup_context *setup,
109bf215546Sopenharmony_ci              const struct point_info *info,
110bf215546Sopenharmony_ci              unsigned slot,
111bf215546Sopenharmony_ci              unsigned i,
112bf215546Sopenharmony_ci              unsigned sprite_coord_origin,
113bf215546Sopenharmony_ci              boolean perspective)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci   float w0 = info->v0[0][3];
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   assert(i < 4);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   if (i == 0) {
120bf215546Sopenharmony_ci      float dadx = FIXED_ONE / (float)info->dx12;
121bf215546Sopenharmony_ci      float dady =  0.0f;
122bf215546Sopenharmony_ci      float x0 = info->v0[0][0] - setup->pixel_offset;
123bf215546Sopenharmony_ci      float y0 = info->v0[0][1] - setup->pixel_offset;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci      info->dadx[slot][0] = dadx;
126bf215546Sopenharmony_ci      info->dady[slot][0] = dady;
127bf215546Sopenharmony_ci      info->a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci      if (perspective) {
130bf215546Sopenharmony_ci         info->dadx[slot][0] *= w0;
131bf215546Sopenharmony_ci         info->dady[slot][0] *= w0;
132bf215546Sopenharmony_ci         info->a0[slot][0] *= w0;
133bf215546Sopenharmony_ci      }
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci   else if (i == 1) {
136bf215546Sopenharmony_ci      float dadx = 0.0f;
137bf215546Sopenharmony_ci      float dady = FIXED_ONE / (float)info->dx12;
138bf215546Sopenharmony_ci      float x0 = info->v0[0][0] - setup->pixel_offset;
139bf215546Sopenharmony_ci      float y0 = info->v0[0][1] - setup->pixel_offset;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci      if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {
142bf215546Sopenharmony_ci         dady = -dady;
143bf215546Sopenharmony_ci      }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci      info->dadx[slot][1] = dadx;
146bf215546Sopenharmony_ci      info->dady[slot][1] = dady;
147bf215546Sopenharmony_ci      info->a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci      if (perspective) {
150bf215546Sopenharmony_ci         info->dadx[slot][1] *= w0;
151bf215546Sopenharmony_ci         info->dady[slot][1] *= w0;
152bf215546Sopenharmony_ci         info->a0[slot][1] *= w0;
153bf215546Sopenharmony_ci      }
154bf215546Sopenharmony_ci   }
155bf215546Sopenharmony_ci   else if (i == 2) {
156bf215546Sopenharmony_ci      info->a0[slot][2] = 0.0f;
157bf215546Sopenharmony_ci      info->dadx[slot][2] = 0.0f;
158bf215546Sopenharmony_ci      info->dady[slot][2] = 0.0f;
159bf215546Sopenharmony_ci   }
160bf215546Sopenharmony_ci   else {
161bf215546Sopenharmony_ci      info->a0[slot][3] = perspective ? w0 : 1.0f;
162bf215546Sopenharmony_ci      info->dadx[slot][3] = 0.0f;
163bf215546Sopenharmony_ci      info->dady[slot][3] = 0.0f;
164bf215546Sopenharmony_ci   }
165bf215546Sopenharmony_ci}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci/**
169bf215546Sopenharmony_ci * Special coefficient setup for gl_FragCoord.
170bf215546Sopenharmony_ci * X and Y are trivial
171bf215546Sopenharmony_ci * Z and W are copied from position_coef which should have already been computed.
172bf215546Sopenharmony_ci * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
173bf215546Sopenharmony_ci */
174bf215546Sopenharmony_cistatic void
175bf215546Sopenharmony_cisetup_point_fragcoord_coef(struct lp_setup_context *setup,
176bf215546Sopenharmony_ci                           struct point_info *info,
177bf215546Sopenharmony_ci                           unsigned slot,
178bf215546Sopenharmony_ci                           unsigned usage_mask)
179bf215546Sopenharmony_ci{
180bf215546Sopenharmony_ci   /*X*/
181bf215546Sopenharmony_ci   if (usage_mask & TGSI_WRITEMASK_X) {
182bf215546Sopenharmony_ci      info->a0[slot][0] = 0.0;
183bf215546Sopenharmony_ci      info->dadx[slot][0] = 1.0;
184bf215546Sopenharmony_ci      info->dady[slot][0] = 0.0;
185bf215546Sopenharmony_ci   }
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   /*Y*/
188bf215546Sopenharmony_ci   if (usage_mask & TGSI_WRITEMASK_Y) {
189bf215546Sopenharmony_ci      info->a0[slot][1] = 0.0;
190bf215546Sopenharmony_ci      info->dadx[slot][1] = 0.0;
191bf215546Sopenharmony_ci      info->dady[slot][1] = 1.0;
192bf215546Sopenharmony_ci   }
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci   /*Z*/
195bf215546Sopenharmony_ci   if (usage_mask & TGSI_WRITEMASK_Z) {
196bf215546Sopenharmony_ci      constant_coef(setup, info, slot, info->v0[0][2], 2);
197bf215546Sopenharmony_ci   }
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   /*W*/
200bf215546Sopenharmony_ci   if (usage_mask & TGSI_WRITEMASK_W) {
201bf215546Sopenharmony_ci      constant_coef(setup, info, slot, info->v0[0][3], 3);
202bf215546Sopenharmony_ci   }
203bf215546Sopenharmony_ci}
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci/**
207bf215546Sopenharmony_ci * Compute the point->coef[] array dadx, dady, a0 values.
208bf215546Sopenharmony_ci */
209bf215546Sopenharmony_cistatic void
210bf215546Sopenharmony_cisetup_point_coefficients( struct lp_setup_context *setup,
211bf215546Sopenharmony_ci                          struct point_info *info)
212bf215546Sopenharmony_ci{
213bf215546Sopenharmony_ci   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
214bf215546Sopenharmony_ci   const struct lp_fragment_shader *shader = setup->fs.current.variant->shader;
215bf215546Sopenharmony_ci   unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
216bf215546Sopenharmony_ci   unsigned slot;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   /* setup interpolation for all the remaining attributes:
219bf215546Sopenharmony_ci    */
220bf215546Sopenharmony_ci   for (slot = 0; slot < key->num_inputs; slot++) {
221bf215546Sopenharmony_ci      unsigned vert_attr = key->inputs[slot].src_index;
222bf215546Sopenharmony_ci      unsigned usage_mask = key->inputs[slot].usage_mask;
223bf215546Sopenharmony_ci      enum lp_interp interp = key->inputs[slot].interp;
224bf215546Sopenharmony_ci      boolean perspective = !!(interp == LP_INTERP_PERSPECTIVE);
225bf215546Sopenharmony_ci      unsigned i;
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci      if (perspective && usage_mask) {
228bf215546Sopenharmony_ci         fragcoord_usage_mask |= TGSI_WRITEMASK_W;
229bf215546Sopenharmony_ci      }
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci      switch (interp) {
232bf215546Sopenharmony_ci      case LP_INTERP_POSITION:
233bf215546Sopenharmony_ci         /*
234bf215546Sopenharmony_ci          * The generated pixel interpolators will pick up the coeffs from
235bf215546Sopenharmony_ci          * slot 0, so all need to ensure that the usage mask is covers all
236bf215546Sopenharmony_ci          * usages.
237bf215546Sopenharmony_ci          */
238bf215546Sopenharmony_ci         fragcoord_usage_mask |= usage_mask;
239bf215546Sopenharmony_ci         break;
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci      case LP_INTERP_LINEAR:
242bf215546Sopenharmony_ci         /* Sprite tex coords may use linear interpolation someday */
243bf215546Sopenharmony_ci         FALLTHROUGH;
244bf215546Sopenharmony_ci      case LP_INTERP_PERSPECTIVE: {
245bf215546Sopenharmony_ci         /* check if the sprite coord flag is set for this attribute.
246bf215546Sopenharmony_ci          * If so, set it up so it up so x and y vary from 0 to 1.
247bf215546Sopenharmony_ci          */
248bf215546Sopenharmony_ci         bool do_texcoord_coef = false;
249bf215546Sopenharmony_ci         if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_PCOORD) {
250bf215546Sopenharmony_ci            do_texcoord_coef = true;
251bf215546Sopenharmony_ci         }
252bf215546Sopenharmony_ci         else if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_TEXCOORD) {
253bf215546Sopenharmony_ci            unsigned semantic_index = shader->info.base.input_semantic_index[slot];
254bf215546Sopenharmony_ci            /* Note that sprite_coord enable is a bitfield of
255bf215546Sopenharmony_ci             * PIPE_MAX_SHADER_OUTPUTS bits.
256bf215546Sopenharmony_ci             */
257bf215546Sopenharmony_ci            if (semantic_index < PIPE_MAX_SHADER_OUTPUTS &&
258bf215546Sopenharmony_ci                (setup->sprite_coord_enable & (1u << semantic_index))) {
259bf215546Sopenharmony_ci               do_texcoord_coef = true;
260bf215546Sopenharmony_ci            }
261bf215546Sopenharmony_ci         }
262bf215546Sopenharmony_ci         if (do_texcoord_coef) {
263bf215546Sopenharmony_ci            for (i = 0; i < NUM_CHANNELS; i++) {
264bf215546Sopenharmony_ci               if (usage_mask & (1 << i)) {
265bf215546Sopenharmony_ci                  texcoord_coef(setup, info, slot + 1, i,
266bf215546Sopenharmony_ci                                setup->sprite_coord_origin,
267bf215546Sopenharmony_ci                                perspective);
268bf215546Sopenharmony_ci               }
269bf215546Sopenharmony_ci            }
270bf215546Sopenharmony_ci            break;
271bf215546Sopenharmony_ci         }
272bf215546Sopenharmony_ci      }
273bf215546Sopenharmony_ci         FALLTHROUGH;
274bf215546Sopenharmony_ci      case LP_INTERP_CONSTANT:
275bf215546Sopenharmony_ci         for (i = 0; i < NUM_CHANNELS; i++) {
276bf215546Sopenharmony_ci            if (usage_mask & (1 << i)) {
277bf215546Sopenharmony_ci               if (perspective) {
278bf215546Sopenharmony_ci                  point_persp_coeff(setup, info, slot+1, i);
279bf215546Sopenharmony_ci               }
280bf215546Sopenharmony_ci               else {
281bf215546Sopenharmony_ci                  constant_coef(setup, info, slot+1, info->v0[vert_attr][i], i);
282bf215546Sopenharmony_ci               }
283bf215546Sopenharmony_ci            }
284bf215546Sopenharmony_ci         }
285bf215546Sopenharmony_ci         break;
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci      case LP_INTERP_FACING:
288bf215546Sopenharmony_ci         for (i = 0; i < NUM_CHANNELS; i++)
289bf215546Sopenharmony_ci            if (usage_mask & (1 << i))
290bf215546Sopenharmony_ci               constant_coef(setup, info, slot+1,
291bf215546Sopenharmony_ci                             info->frontfacing ? 1.0f : -1.0f, i);
292bf215546Sopenharmony_ci         break;
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci      default:
295bf215546Sopenharmony_ci         assert(0);
296bf215546Sopenharmony_ci         break;
297bf215546Sopenharmony_ci      }
298bf215546Sopenharmony_ci   }
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   /* The internal position input is in slot zero:
301bf215546Sopenharmony_ci    */
302bf215546Sopenharmony_ci   setup_point_fragcoord_coef(setup, info, 0,
303bf215546Sopenharmony_ci                              fragcoord_usage_mask);
304bf215546Sopenharmony_ci}
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_cistatic inline int
308bf215546Sopenharmony_cisubpixel_snap(float a)
309bf215546Sopenharmony_ci{
310bf215546Sopenharmony_ci   return util_iround(FIXED_ONE * a);
311bf215546Sopenharmony_ci}
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci/**
314bf215546Sopenharmony_ci * Print point vertex attribs (for debug).
315bf215546Sopenharmony_ci */
316bf215546Sopenharmony_cistatic void
317bf215546Sopenharmony_ciprint_point(struct lp_setup_context *setup,
318bf215546Sopenharmony_ci            const float (*v0)[4],
319bf215546Sopenharmony_ci            const float size)
320bf215546Sopenharmony_ci{
321bf215546Sopenharmony_ci   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
322bf215546Sopenharmony_ci   uint i;
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   debug_printf("llvmpipe point, width %f\n", size);
325bf215546Sopenharmony_ci   for (i = 0; i < 1 + key->num_inputs; i++) {
326bf215546Sopenharmony_ci      debug_printf("  v0[%d]:  %f %f %f %f\n", i,
327bf215546Sopenharmony_ci                   v0[i][0], v0[i][1], v0[i][2], v0[i][3]);
328bf215546Sopenharmony_ci   }
329bf215546Sopenharmony_ci}
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_cistatic boolean
333bf215546Sopenharmony_citry_setup_point( struct lp_setup_context *setup,
334bf215546Sopenharmony_ci                 const float (*v0)[4] )
335bf215546Sopenharmony_ci{
336bf215546Sopenharmony_ci   struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
337bf215546Sopenharmony_ci   /* x/y positions in fixed point */
338bf215546Sopenharmony_ci   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
339bf215546Sopenharmony_ci   const int sizeAttr = setup->psize_slot;
340bf215546Sopenharmony_ci   float size
341bf215546Sopenharmony_ci      = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
342bf215546Sopenharmony_ci      : setup->point_size;
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci   if (size > LP_MAX_POINT_WIDTH)
345bf215546Sopenharmony_ci      size = LP_MAX_POINT_WIDTH;
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   /* Yes this is necessary to accurately calculate bounding boxes
348bf215546Sopenharmony_ci    * with the two fill-conventions we support.  GL (normally) ends
349bf215546Sopenharmony_ci    * up needing a bottom-left fill convention, which requires
350bf215546Sopenharmony_ci    * slightly different rounding.
351bf215546Sopenharmony_ci    */
352bf215546Sopenharmony_ci   int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;
353bf215546Sopenharmony_ci   float pixel_offset = setup->multisample ? 0.0 : setup->pixel_offset;
354bf215546Sopenharmony_ci   struct lp_scene *scene = setup->scene;
355bf215546Sopenharmony_ci   struct u_rect bbox;
356bf215546Sopenharmony_ci   int x[2], y[2];
357bf215546Sopenharmony_ci   struct point_info info;
358bf215546Sopenharmony_ci   unsigned viewport_index = 0;
359bf215546Sopenharmony_ci   unsigned layer = 0;
360bf215546Sopenharmony_ci   int fixed_width;
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci   if (setup->viewport_index_slot > 0) {
363bf215546Sopenharmony_ci      unsigned *udata = (unsigned*)v0[setup->viewport_index_slot];
364bf215546Sopenharmony_ci      viewport_index = lp_clamp_viewport_idx(*udata);
365bf215546Sopenharmony_ci   }
366bf215546Sopenharmony_ci   if (setup->layer_slot > 0) {
367bf215546Sopenharmony_ci      layer = *(unsigned*)v0[setup->layer_slot];
368bf215546Sopenharmony_ci      layer = MIN2(layer, scene->fb_max_layer);
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   if (0)
372bf215546Sopenharmony_ci      print_point(setup, v0, size);
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci   /* Bounding rectangle (in pixels) */
375bf215546Sopenharmony_ci   if (!setup->legacy_points) {
376bf215546Sopenharmony_ci      /*
377bf215546Sopenharmony_ci       * Rasterize points as quads.
378bf215546Sopenharmony_ci       */
379bf215546Sopenharmony_ci      int x0, y0;
380bf215546Sopenharmony_ci      /* Point size as fixed point integer, remove rounding errors
381bf215546Sopenharmony_ci       * and gives minimum width for very small points.
382bf215546Sopenharmony_ci       */
383bf215546Sopenharmony_ci      fixed_width = MAX2(FIXED_ONE, subpixel_snap(size));
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci      x0 = subpixel_snap(v0[0][0] - pixel_offset) - fixed_width/2;
386bf215546Sopenharmony_ci      y0 = subpixel_snap(v0[0][1] - pixel_offset) - fixed_width/2;
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci      x[0] = x0;
389bf215546Sopenharmony_ci      x[1] = x0 + fixed_width;
390bf215546Sopenharmony_ci      y[0] = y0;
391bf215546Sopenharmony_ci      y[1] = y0 + fixed_width;
392bf215546Sopenharmony_ci      bbox.x0 = x[0] >> FIXED_ORDER;
393bf215546Sopenharmony_ci      bbox.x1 = (x[1] + (FIXED_ONE-1)) >> FIXED_ORDER;
394bf215546Sopenharmony_ci      bbox.y0 = (y[0] + adj) >> FIXED_ORDER;
395bf215546Sopenharmony_ci      bbox.y1 = (y[1] + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci      /* Inclusive coordinates:
398bf215546Sopenharmony_ci       */
399bf215546Sopenharmony_ci      bbox.x1--;
400bf215546Sopenharmony_ci      bbox.y1--;
401bf215546Sopenharmony_ci   } else {
402bf215546Sopenharmony_ci      /*
403bf215546Sopenharmony_ci       * OpenGL legacy rasterization rules for non-sprite points.
404bf215546Sopenharmony_ci       *
405bf215546Sopenharmony_ci       * Per OpenGL 2.1 spec, section 3.3.1, "Basic Point Rasterization".
406bf215546Sopenharmony_ci       *
407bf215546Sopenharmony_ci       * This type of point rasterization is only available in pre 3.0 contexts
408bf215546Sopenharmony_ci       * (or compatibility contexts which we don't support) anyway.
409bf215546Sopenharmony_ci       */
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci      const int x0 = subpixel_snap(v0[0][0]);
412bf215546Sopenharmony_ci      const int y0 = subpixel_snap(v0[0][1]) - adj;
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_ci      int int_width;
415bf215546Sopenharmony_ci      /* Point size as fixed point integer. For GL legacy points
416bf215546Sopenharmony_ci       * the point size is always a whole integer.
417bf215546Sopenharmony_ci       */
418bf215546Sopenharmony_ci      fixed_width = MAX2(FIXED_ONE,
419bf215546Sopenharmony_ci                         (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
420bf215546Sopenharmony_ci      int_width = fixed_width >> FIXED_ORDER;
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci      assert(setup->pixel_offset != 0);
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci      if (int_width == 1) {
425bf215546Sopenharmony_ci         bbox.x0 = x0 >> FIXED_ORDER;
426bf215546Sopenharmony_ci         bbox.y0 = y0 >> FIXED_ORDER;
427bf215546Sopenharmony_ci         bbox.x1 = bbox.x0;
428bf215546Sopenharmony_ci         bbox.y1 = bbox.y0;
429bf215546Sopenharmony_ci      } else {
430bf215546Sopenharmony_ci         if (int_width & 1) {
431bf215546Sopenharmony_ci            /* Odd width */
432bf215546Sopenharmony_ci            bbox.x0 = (x0 >> FIXED_ORDER) - (int_width - 1)/2;
433bf215546Sopenharmony_ci            bbox.y0 = (y0 >> FIXED_ORDER) - (int_width - 1)/2;
434bf215546Sopenharmony_ci         } else {
435bf215546Sopenharmony_ci            /* Even width */
436bf215546Sopenharmony_ci            bbox.x0 = ((x0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;
437bf215546Sopenharmony_ci            bbox.y0 = ((y0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;
438bf215546Sopenharmony_ci         }
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci         bbox.x1 = bbox.x0 + int_width - 1;
441bf215546Sopenharmony_ci         bbox.y1 = bbox.y0 + int_width - 1;
442bf215546Sopenharmony_ci      }
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci      x[0] = (bbox.x0 - 1) << 8;
445bf215546Sopenharmony_ci      x[1] = (bbox.x1 + 1) << 8;
446bf215546Sopenharmony_ci      y[0] = (bbox.y0 - 1) << 8;
447bf215546Sopenharmony_ci      y[1] = (bbox.y1 + 1) << 8;
448bf215546Sopenharmony_ci   }
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci   if (0) {
451bf215546Sopenharmony_ci      debug_printf("  bbox: (%i, %i) - (%i, %i)\n",
452bf215546Sopenharmony_ci                   bbox.x0, bbox.y0,
453bf215546Sopenharmony_ci                   bbox.x1, bbox.y1);
454bf215546Sopenharmony_ci   }
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci   if (lp_context->active_statistics_queries) {
457bf215546Sopenharmony_ci      lp_context->pipeline_statistics.c_primitives++;
458bf215546Sopenharmony_ci   }
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_ci   if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {
461bf215546Sopenharmony_ci      if (0) debug_printf("no intersection\n");
462bf215546Sopenharmony_ci      LP_COUNT(nr_culled_tris);
463bf215546Sopenharmony_ci      return TRUE;
464bf215546Sopenharmony_ci   }
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci   u_rect_find_intersection(&setup->draw_regions[viewport_index], &bbox);
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci   /* We can't use rectangle reasterizer for non-legacy points for now. */
469bf215546Sopenharmony_ci   if (!setup->legacy_points || setup->multisample) {
470bf215546Sopenharmony_ci      struct lp_rast_triangle *point;
471bf215546Sopenharmony_ci      struct lp_rast_plane *plane;
472bf215546Sopenharmony_ci      unsigned bytes;
473bf215546Sopenharmony_ci      unsigned nr_planes = 4;
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_ci      point = lp_setup_alloc_triangle(scene,
476bf215546Sopenharmony_ci                                      key->num_inputs,
477bf215546Sopenharmony_ci                                      nr_planes,
478bf215546Sopenharmony_ci                                      &bytes);
479bf215546Sopenharmony_ci     if (!point)
480bf215546Sopenharmony_ci        return FALSE;
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_ci#ifdef DEBUG
483bf215546Sopenharmony_ci      point->v[0][0] = v0[0][0];
484bf215546Sopenharmony_ci      point->v[0][1] = v0[0][1];
485bf215546Sopenharmony_ci#endif
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci      LP_COUNT(nr_tris);
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci      if (draw_will_inject_frontface(lp_context->draw) &&
490bf215546Sopenharmony_ci          setup->face_slot > 0) {
491bf215546Sopenharmony_ci         point->inputs.frontfacing = v0[setup->face_slot][0];
492bf215546Sopenharmony_ci      } else {
493bf215546Sopenharmony_ci         point->inputs.frontfacing = TRUE;
494bf215546Sopenharmony_ci      }
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci      info.v0 = v0;
497bf215546Sopenharmony_ci      info.dx01 = 0;
498bf215546Sopenharmony_ci      info.dx12 = fixed_width;
499bf215546Sopenharmony_ci      info.dy01 = fixed_width;
500bf215546Sopenharmony_ci      info.dy12 = 0;
501bf215546Sopenharmony_ci      info.a0 = GET_A0(&point->inputs);
502bf215546Sopenharmony_ci      info.dadx = GET_DADX(&point->inputs);
503bf215546Sopenharmony_ci      info.dady = GET_DADY(&point->inputs);
504bf215546Sopenharmony_ci      info.frontfacing = point->inputs.frontfacing;
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci      /* Setup parameter interpolants:
507bf215546Sopenharmony_ci       */
508bf215546Sopenharmony_ci      setup_point_coefficients(setup, &info);
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_ci      point->inputs.disable = FALSE;
511bf215546Sopenharmony_ci      point->inputs.is_blit = FALSE;
512bf215546Sopenharmony_ci      point->inputs.layer = layer;
513bf215546Sopenharmony_ci      point->inputs.viewport_index = viewport_index;
514bf215546Sopenharmony_ci      point->inputs.view_index = setup->view_index;
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci      plane = GET_PLANES(point);
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci      plane[0].dcdx = ~0U << 8;
519bf215546Sopenharmony_ci      plane[0].dcdy = 0;
520bf215546Sopenharmony_ci      plane[0].c = -MAX2(x[0], bbox.x0 << 8);
521bf215546Sopenharmony_ci      plane[0].eo = 1 << 8;
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci      plane[1].dcdx = 1 << 8;
524bf215546Sopenharmony_ci      plane[1].dcdy = 0;
525bf215546Sopenharmony_ci      plane[1].c = MIN2(x[1], (bbox.x1 + 1) << 8);
526bf215546Sopenharmony_ci      plane[1].eo = 0;
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_ci      plane[2].dcdx = 0;
529bf215546Sopenharmony_ci      plane[2].dcdy = 1 << 8;
530bf215546Sopenharmony_ci      plane[2].c = -MAX2(y[0], (bbox.y0 << 8) - adj);
531bf215546Sopenharmony_ci      plane[2].eo = 1 << 8;
532bf215546Sopenharmony_ci
533bf215546Sopenharmony_ci      plane[3].dcdx = 0;
534bf215546Sopenharmony_ci      plane[3].dcdy = ~0U << 8;
535bf215546Sopenharmony_ci      plane[3].c = MIN2(y[1], (bbox.y1 + 1) << 8);
536bf215546Sopenharmony_ci      plane[3].eo = 0;
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci      if (!setup->legacy_points) {
539bf215546Sopenharmony_ci         /* adjust for fill-rule*/
540bf215546Sopenharmony_ci         plane[0].c++; /* left */
541bf215546Sopenharmony_ci         if (setup->bottom_edge_rule == 0)
542bf215546Sopenharmony_ci            plane[2].c++; /* top-left */
543bf215546Sopenharmony_ci         else
544bf215546Sopenharmony_ci            plane[3].c++; /* bottom-left */
545bf215546Sopenharmony_ci      }
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_ci      int max_szorig = ((bbox.x1 - (bbox.x0 & ~3)) |
548bf215546Sopenharmony_ci                        (bbox.y1 - (bbox.y0 & ~3)));
549bf215546Sopenharmony_ci      boolean use_32bits = max_szorig <= MAX_FIXED_LENGTH32;
550bf215546Sopenharmony_ci
551bf215546Sopenharmony_ci      return lp_setup_bin_triangle(setup, point, use_32bits,
552bf215546Sopenharmony_ci                                   setup->fs.current.variant->opaque,
553bf215546Sopenharmony_ci                                   &bbox, nr_planes, viewport_index);
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci   } else {
556bf215546Sopenharmony_ci      struct lp_rast_rectangle *point;
557bf215546Sopenharmony_ci      point = lp_setup_alloc_rectangle(scene,
558bf215546Sopenharmony_ci                                       key->num_inputs);
559bf215546Sopenharmony_ci      if (!point)
560bf215546Sopenharmony_ci         return FALSE;
561bf215546Sopenharmony_ci#ifdef DEBUG
562bf215546Sopenharmony_ci      point->v[0][0] = v0[0][0];
563bf215546Sopenharmony_ci      point->v[0][1] = v0[0][1];
564bf215546Sopenharmony_ci#endif
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci      point->box.x0 = bbox.x0;
567bf215546Sopenharmony_ci      point->box.x1 = bbox.x1;
568bf215546Sopenharmony_ci      point->box.y0 = bbox.y0;
569bf215546Sopenharmony_ci      point->box.y1 = bbox.y1;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci      LP_COUNT(nr_tris);
572bf215546Sopenharmony_ci
573bf215546Sopenharmony_ci      if (draw_will_inject_frontface(lp_context->draw) &&
574bf215546Sopenharmony_ci          setup->face_slot > 0) {
575bf215546Sopenharmony_ci         point->inputs.frontfacing = v0[setup->face_slot][0];
576bf215546Sopenharmony_ci      } else {
577bf215546Sopenharmony_ci         point->inputs.frontfacing = TRUE;
578bf215546Sopenharmony_ci      }
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci      info.v0 = v0;
581bf215546Sopenharmony_ci      info.dx01 = 0;
582bf215546Sopenharmony_ci      info.dx12 = fixed_width;
583bf215546Sopenharmony_ci      info.dy01 = fixed_width;
584bf215546Sopenharmony_ci      info.dy12 = 0;
585bf215546Sopenharmony_ci      info.a0 = GET_A0(&point->inputs);
586bf215546Sopenharmony_ci      info.dadx = GET_DADX(&point->inputs);
587bf215546Sopenharmony_ci      info.dady = GET_DADY(&point->inputs);
588bf215546Sopenharmony_ci      info.frontfacing = point->inputs.frontfacing;
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci      /* Setup parameter interpolants:
591bf215546Sopenharmony_ci       */
592bf215546Sopenharmony_ci      setup_point_coefficients(setup, &info);
593bf215546Sopenharmony_ci
594bf215546Sopenharmony_ci      point->inputs.disable = FALSE;
595bf215546Sopenharmony_ci      point->inputs.is_blit = FALSE;
596bf215546Sopenharmony_ci      point->inputs.layer = layer;
597bf215546Sopenharmony_ci      point->inputs.viewport_index = viewport_index;
598bf215546Sopenharmony_ci      point->inputs.view_index = setup->view_index;
599bf215546Sopenharmony_ci
600bf215546Sopenharmony_ci      return lp_setup_bin_rectangle(setup, point, setup->fs.current.variant->opaque);
601bf215546Sopenharmony_ci   }
602bf215546Sopenharmony_ci}
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci
605bf215546Sopenharmony_cistatic void
606bf215546Sopenharmony_cilp_setup_point_discard(struct lp_setup_context *setup,
607bf215546Sopenharmony_ci                       const float (*v0)[4])
608bf215546Sopenharmony_ci{
609bf215546Sopenharmony_ci}
610bf215546Sopenharmony_ci
611bf215546Sopenharmony_cistatic void
612bf215546Sopenharmony_cilp_setup_point(struct lp_setup_context *setup,
613bf215546Sopenharmony_ci               const float (*v0)[4])
614bf215546Sopenharmony_ci{
615bf215546Sopenharmony_ci   if (!try_setup_point(setup, v0)) {
616bf215546Sopenharmony_ci      if (!lp_setup_flush_and_restart(setup))
617bf215546Sopenharmony_ci         return;
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci      if (!try_setup_point(setup, v0))
620bf215546Sopenharmony_ci         return;
621bf215546Sopenharmony_ci   }
622bf215546Sopenharmony_ci}
623bf215546Sopenharmony_ci
624bf215546Sopenharmony_ci
625bf215546Sopenharmony_civoid
626bf215546Sopenharmony_cilp_setup_choose_point(struct lp_setup_context *setup)
627bf215546Sopenharmony_ci{
628bf215546Sopenharmony_ci   if (setup->rasterizer_discard) {
629bf215546Sopenharmony_ci      setup->point = lp_setup_point_discard;
630bf215546Sopenharmony_ci   } else {
631bf215546Sopenharmony_ci      setup->point = lp_setup_point;
632bf215546Sopenharmony_ci   }
633bf215546Sopenharmony_ci}
634bf215546Sopenharmony_ci
635bf215546Sopenharmony_ci
636