Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_setup_point.c
4570 views
/**************************************************************************1*2* Copyright 2010, VMware Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/*28* Binning code for points29*/3031#include "util/u_math.h"32#include "util/u_memory.h"33#include "lp_setup_context.h"34#include "lp_perf.h"35#include "lp_rast.h"36#include "lp_state_fs.h"37#include "lp_state_setup.h"38#include "lp_context.h"39#include "tgsi/tgsi_scan.h"40#include "draw/draw_context.h"4142#define NUM_CHANNELS 44344struct point_info {45/* x,y deltas */46int dy01, dy12;47int dx01, dx12;4849const float (*v0)[4];5051float (*a0)[4];52float (*dadx)[4];53float (*dady)[4];5455boolean frontfacing;56};575859/**60* Compute a0 for a constant-valued coefficient (GL_FLAT shading).61*/62static void63constant_coef(struct lp_setup_context *setup,64struct point_info *info,65unsigned slot,66const float value,67unsigned i)68{69info->a0[slot][i] = value;70info->dadx[slot][i] = 0.0f;71info->dady[slot][i] = 0.0f;72}737475static void76point_persp_coeff(struct lp_setup_context *setup,77const struct point_info *info,78unsigned slot,79unsigned i)80{81/*82* Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A83* better strategy would be to take the primitive in consideration when84* generating the fragment shader key, and therefore avoid the per-fragment85* perspective divide.86*/8788float w0 = info->v0[0][3];8990assert(i < 4);9192info->a0[slot][i] = info->v0[slot][i]*w0;93info->dadx[slot][i] = 0.0f;94info->dady[slot][i] = 0.0f;95}969798/**99* Setup automatic texcoord coefficients (for sprite rendering).100* \param slot the vertex attribute slot to setup101* \param i the attribute channel in [0,3]102* \param sprite_coord_origin one of PIPE_SPRITE_COORD_x103* \param perspective does the shader expects pre-multiplied w, i.e.,104* LP_INTERP_PERSPECTIVE is specified in the shader key105*/106static void107texcoord_coef(struct lp_setup_context *setup,108const struct point_info *info,109unsigned slot,110unsigned i,111unsigned sprite_coord_origin,112boolean perspective)113{114float w0 = info->v0[0][3];115116assert(i < 4);117118if (i == 0) {119float dadx = FIXED_ONE / (float)info->dx12;120float dady = 0.0f;121float x0 = info->v0[0][0] - setup->pixel_offset;122float y0 = info->v0[0][1] - setup->pixel_offset;123124info->dadx[slot][0] = dadx;125info->dady[slot][0] = dady;126info->a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);127128if (perspective) {129info->dadx[slot][0] *= w0;130info->dady[slot][0] *= w0;131info->a0[slot][0] *= w0;132}133}134else if (i == 1) {135float dadx = 0.0f;136float dady = FIXED_ONE / (float)info->dx12;137float x0 = info->v0[0][0] - setup->pixel_offset;138float y0 = info->v0[0][1] - setup->pixel_offset;139140if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {141dady = -dady;142}143144info->dadx[slot][1] = dadx;145info->dady[slot][1] = dady;146info->a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);147148if (perspective) {149info->dadx[slot][1] *= w0;150info->dady[slot][1] *= w0;151info->a0[slot][1] *= w0;152}153}154else if (i == 2) {155info->a0[slot][2] = 0.0f;156info->dadx[slot][2] = 0.0f;157info->dady[slot][2] = 0.0f;158}159else {160info->a0[slot][3] = perspective ? w0 : 1.0f;161info->dadx[slot][3] = 0.0f;162info->dady[slot][3] = 0.0f;163}164}165166167/**168* Special coefficient setup for gl_FragCoord.169* X and Y are trivial170* Z and W are copied from position_coef which should have already been computed.171* We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.172*/173static void174setup_point_fragcoord_coef(struct lp_setup_context *setup,175struct point_info *info,176unsigned slot,177unsigned usage_mask)178{179/*X*/180if (usage_mask & TGSI_WRITEMASK_X) {181info->a0[slot][0] = 0.0;182info->dadx[slot][0] = 1.0;183info->dady[slot][0] = 0.0;184}185186/*Y*/187if (usage_mask & TGSI_WRITEMASK_Y) {188info->a0[slot][1] = 0.0;189info->dadx[slot][1] = 0.0;190info->dady[slot][1] = 1.0;191}192193/*Z*/194if (usage_mask & TGSI_WRITEMASK_Z) {195constant_coef(setup, info, slot, info->v0[0][2], 2);196}197198/*W*/199if (usage_mask & TGSI_WRITEMASK_W) {200constant_coef(setup, info, slot, info->v0[0][3], 3);201}202}203204205/**206* Compute the point->coef[] array dadx, dady, a0 values.207*/208static void209setup_point_coefficients( struct lp_setup_context *setup,210struct point_info *info)211{212const struct lp_setup_variant_key *key = &setup->setup.variant->key;213const struct lp_fragment_shader *shader = setup->fs.current.variant->shader;214unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;215unsigned slot;216217/* setup interpolation for all the remaining attributes:218*/219for (slot = 0; slot < key->num_inputs; slot++) {220unsigned vert_attr = key->inputs[slot].src_index;221unsigned usage_mask = key->inputs[slot].usage_mask;222enum lp_interp interp = key->inputs[slot].interp;223boolean perspective = !!(interp == LP_INTERP_PERSPECTIVE);224unsigned i;225226if (perspective && usage_mask) {227fragcoord_usage_mask |= TGSI_WRITEMASK_W;228}229230switch (interp) {231case LP_INTERP_POSITION:232/*233* The generated pixel interpolators will pick up the coeffs from234* slot 0, so all need to ensure that the usage mask is covers all235* usages.236*/237fragcoord_usage_mask |= usage_mask;238break;239240case LP_INTERP_LINEAR:241/* Sprite tex coords may use linear interpolation someday */242FALLTHROUGH;243case LP_INTERP_PERSPECTIVE: {244/* check if the sprite coord flag is set for this attribute.245* If so, set it up so it up so x and y vary from 0 to 1.246*/247bool do_texcoord_coef = false;248if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_PCOORD) {249do_texcoord_coef = true;250}251else if (shader->info.base.input_semantic_name[slot] == TGSI_SEMANTIC_TEXCOORD) {252unsigned semantic_index = shader->info.base.input_semantic_index[slot];253/* Note that sprite_coord enable is a bitfield of254* PIPE_MAX_SHADER_OUTPUTS bits.255*/256if (semantic_index < PIPE_MAX_SHADER_OUTPUTS &&257(setup->sprite_coord_enable & (1u << semantic_index))) {258do_texcoord_coef = true;259}260}261if (do_texcoord_coef) {262for (i = 0; i < NUM_CHANNELS; i++) {263if (usage_mask & (1 << i)) {264texcoord_coef(setup, info, slot + 1, i,265setup->sprite_coord_origin,266perspective);267}268}269break;270}271}272FALLTHROUGH;273case LP_INTERP_CONSTANT:274for (i = 0; i < NUM_CHANNELS; i++) {275if (usage_mask & (1 << i)) {276if (perspective) {277point_persp_coeff(setup, info, slot+1, i);278}279else {280constant_coef(setup, info, slot+1, info->v0[vert_attr][i], i);281}282}283}284break;285286case LP_INTERP_FACING:287for (i = 0; i < NUM_CHANNELS; i++)288if (usage_mask & (1 << i))289constant_coef(setup, info, slot+1,290info->frontfacing ? 1.0f : -1.0f, i);291break;292293default:294assert(0);295break;296}297}298299/* The internal position input is in slot zero:300*/301setup_point_fragcoord_coef(setup, info, 0,302fragcoord_usage_mask);303}304305306static inline int307subpixel_snap(float a)308{309return util_iround(FIXED_ONE * a);310}311312/**313* Print point vertex attribs (for debug).314*/315static void316print_point(struct lp_setup_context *setup,317const float (*v0)[4],318const float size)319{320const struct lp_setup_variant_key *key = &setup->setup.variant->key;321uint i;322323debug_printf("llvmpipe point, width %f\n", size);324for (i = 0; i < 1 + key->num_inputs; i++) {325debug_printf(" v0[%d]: %f %f %f %f\n", i,326v0[i][0], v0[i][1], v0[i][2], v0[i][3]);327}328}329330331static boolean332try_setup_point( struct lp_setup_context *setup,333const float (*v0)[4] )334{335struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;336/* x/y positions in fixed point */337const struct lp_setup_variant_key *key = &setup->setup.variant->key;338const int sizeAttr = setup->psize_slot;339float size340= (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]341: setup->point_size;342343if (size > LP_MAX_POINT_WIDTH)344size = LP_MAX_POINT_WIDTH;345346/* Yes this is necessary to accurately calculate bounding boxes347* with the two fill-conventions we support. GL (normally) ends348* up needing a bottom-left fill convention, which requires349* slightly different rounding.350*/351int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;352float pixel_offset = setup->multisample ? 0.0 : setup->pixel_offset;353struct lp_scene *scene = setup->scene;354struct lp_rast_triangle *point;355unsigned bytes;356struct u_rect bbox;357int x[2], y[2];358unsigned nr_planes = 4;359struct point_info info;360unsigned viewport_index = 0;361unsigned layer = 0;362int fixed_width;363364if (setup->viewport_index_slot > 0) {365unsigned *udata = (unsigned*)v0[setup->viewport_index_slot];366viewport_index = lp_clamp_viewport_idx(*udata);367}368if (setup->layer_slot > 0) {369layer = *(unsigned*)v0[setup->layer_slot];370layer = MIN2(layer, scene->fb_max_layer);371}372373if (0)374print_point(setup, v0, size);375376/* Bounding rectangle (in pixels) */377if (!setup->legacy_points || setup->multisample) {378/*379* Rasterize points as quads.380*/381int x0, y0;382/* Point size as fixed point integer, remove rounding errors383* and gives minimum width for very small points.384*/385fixed_width = MAX2(FIXED_ONE, subpixel_snap(size));386387x0 = subpixel_snap(v0[0][0] - pixel_offset) - fixed_width/2;388y0 = subpixel_snap(v0[0][1] - pixel_offset) - fixed_width/2;389390x[0] = x0;391x[1] = x0 + fixed_width;392y[0] = y0;393y[1] = y0 + fixed_width;394bbox.x0 = x[0] >> FIXED_ORDER;395bbox.x1 = (x[1] + (FIXED_ONE-1)) >> FIXED_ORDER;396bbox.y0 = (y[0] + adj) >> FIXED_ORDER;397bbox.y1 = (y[1] + (FIXED_ONE-1) + adj) >> FIXED_ORDER;398399/* Inclusive coordinates:400*/401bbox.x1--;402bbox.y1--;403} else {404/*405* OpenGL legacy rasterization rules for non-sprite points.406*407* Per OpenGL 2.1 spec, section 3.3.1, "Basic Point Rasterization".408*409* This type of point rasterization is only available in pre 3.0 contexts410* (or compatibility contexts which we don't support) anyway.411*/412413const int x0 = subpixel_snap(v0[0][0]);414const int y0 = subpixel_snap(v0[0][1]) - adj;415416int int_width;417/* Point size as fixed point integer. For GL legacy points418* the point size is always a whole integer.419*/420fixed_width = MAX2(FIXED_ONE,421(subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));422int_width = fixed_width >> FIXED_ORDER;423424assert(setup->pixel_offset != 0);425426if (int_width == 1) {427bbox.x0 = x0 >> FIXED_ORDER;428bbox.y0 = y0 >> FIXED_ORDER;429bbox.x1 = bbox.x0;430bbox.y1 = bbox.y0;431} else {432if (int_width & 1) {433/* Odd width */434bbox.x0 = (x0 >> FIXED_ORDER) - (int_width - 1)/2;435bbox.y0 = (y0 >> FIXED_ORDER) - (int_width - 1)/2;436} else {437/* Even width */438bbox.x0 = ((x0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;439bbox.y0 = ((y0 + FIXED_ONE/2) >> FIXED_ORDER) - int_width/2;440}441442bbox.x1 = bbox.x0 + int_width - 1;443bbox.y1 = bbox.y0 + int_width - 1;444}445446x[0] = (bbox.x0 - 1) << 8;447x[1] = (bbox.x1 + 1) << 8;448y[0] = (bbox.y0 - 1) << 8;449y[1] = (bbox.y1 + 1) << 8;450}451452if (0) {453debug_printf(" bbox: (%i, %i) - (%i, %i)\n",454bbox.x0, bbox.y0,455bbox.x1, bbox.y1);456}457458if (lp_context->active_statistics_queries) {459lp_context->pipeline_statistics.c_primitives++;460}461462if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {463if (0) debug_printf("offscreen\n");464LP_COUNT(nr_culled_tris);465return TRUE;466}467468u_rect_find_intersection(&setup->draw_regions[viewport_index], &bbox);469470point = lp_setup_alloc_triangle(scene,471key->num_inputs,472nr_planes,473&bytes);474if (!point)475return FALSE;476477#ifdef DEBUG478point->v[0][0] = v0[0][0];479point->v[0][1] = v0[0][1];480#endif481482LP_COUNT(nr_tris);483484if (draw_will_inject_frontface(lp_context->draw) &&485setup->face_slot > 0) {486point->inputs.frontfacing = v0[setup->face_slot][0];487} else {488point->inputs.frontfacing = TRUE;489}490491info.v0 = v0;492info.dx01 = 0;493info.dx12 = fixed_width;494info.dy01 = fixed_width;495info.dy12 = 0;496info.a0 = GET_A0(&point->inputs);497info.dadx = GET_DADX(&point->inputs);498info.dady = GET_DADY(&point->inputs);499info.frontfacing = point->inputs.frontfacing;500501/* Setup parameter interpolants:502*/503setup_point_coefficients(setup, &info);504505point->inputs.disable = FALSE;506point->inputs.opaque = FALSE;507point->inputs.layer = layer;508point->inputs.viewport_index = viewport_index;509point->inputs.view_index = setup->view_index;510511{512struct lp_rast_plane *plane = GET_PLANES(point);513514plane[0].dcdx = ~0U << 8;515plane[0].dcdy = 0;516plane[0].c = -x[0];517plane[0].eo = 1 << 8;518519plane[1].dcdx = 1 << 8;520plane[1].dcdy = 0;521plane[1].c = x[1];522plane[1].eo = 0;523524plane[2].dcdx = 0;525plane[2].dcdy = 1 << 8;526plane[2].c = -y[0];527plane[2].eo = 1 << 8;528529plane[3].dcdx = 0;530plane[3].dcdy = ~0U << 8;531plane[3].c = y[1];532plane[3].eo = 0;533534if (!setup->legacy_points || setup->multisample) {535/* adjust for fill-rule*/536plane[0].c++; /* left */537if (setup->bottom_edge_rule == 0)538plane[2].c++; /* top-left */539else540plane[3].c++; /* bottom-left */541}542}543544return lp_setup_bin_triangle(setup, point, &bbox, &bbox, nr_planes, viewport_index);545}546547548static void549lp_setup_point_discard(struct lp_setup_context *setup,550const float (*v0)[4])551{552}553554static void555lp_setup_point(struct lp_setup_context *setup,556const float (*v0)[4])557{558if (!try_setup_point(setup, v0)) {559if (!lp_setup_flush_and_restart(setup))560return;561562if (!try_setup_point(setup, v0))563return;564}565}566567568void569lp_setup_choose_point(struct lp_setup_context *setup)570{571if (setup->rasterizer_discard) {572setup->point = lp_setup_point_discard;573} else {574setup->point = lp_setup_point;575}576}577578579580581