Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_cliptest_tmp.h
4565 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#include "util/u_bitcast.h"28#include <math.h>2930static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,31struct draw_vertex_info *info,32const struct draw_prim_info *prim_info )33{34struct vertex_header *out = info->verts;35/* const */ float (*plane)[4] = pvs->draw->plane;36const unsigned pos = draw_current_shader_position_output(pvs->draw);37const unsigned cv = draw_current_shader_clipvertex_output(pvs->draw);38unsigned cd[2];39const unsigned ef = pvs->draw->vs.edgeflag_output;40unsigned ucp_enable = pvs->draw->rasterizer->clip_plane_enable;41unsigned flags = (FLAGS);42unsigned need_pipeline = 0;43unsigned j;44unsigned i;45bool have_cd = false;46bool uses_vp_idx = draw_current_shader_uses_viewport_index(pvs->draw);47unsigned viewport_index_output =48draw_current_shader_viewport_index_output(pvs->draw);49int viewport_index = 0;50int num_written_clipdistance =51draw_current_shader_num_written_clipdistances(pvs->draw);5253if (uses_vp_idx) {54viewport_index = u_bitcast_f2u(out->data[viewport_index_output][0]);55viewport_index = draw_clamp_viewport_idx(viewport_index);56}5758cd[0] = draw_current_shader_ccdistance_output(pvs->draw, 0);59cd[1] = draw_current_shader_ccdistance_output(pvs->draw, 1);6061if (cd[0] != pos || cd[1] != pos)62have_cd = true;6364/* If clipdistance semantic has been written by the shader65* that means we're expected to do 'user plane clipping' */66if (num_written_clipdistance && !(flags & DO_CLIP_USER)) {67flags |= DO_CLIP_USER;68ucp_enable = (1 << num_written_clipdistance) - 1;69}7071assert(pos != -1);72unsigned prim_idx = 0, prim_vert_idx = 0;73for (j = 0; j < info->count; j++) {74float *position = out->data[pos];75unsigned mask = 0x0;7677if (uses_vp_idx) {78/* only change the viewport_index for the leading vertex */79if (prim_vert_idx == (prim_info->primitive_lengths[prim_idx])) {80prim_idx++;81prim_vert_idx = 0;82viewport_index = u_bitcast_f2u(out->data[viewport_index_output][0]);83viewport_index = draw_clamp_viewport_idx(viewport_index);84}85prim_vert_idx++;86}87float *scale = pvs->draw->viewports[viewport_index].scale;88float *trans = pvs->draw->viewports[viewport_index].translate;89initialize_vertex_header(out);9091if (flags & (DO_CLIP_XY | DO_CLIP_XY_GUARD_BAND |92DO_CLIP_FULL_Z | DO_CLIP_HALF_Z | DO_CLIP_USER)) {93float *clipvertex = position;9495if ((flags & DO_CLIP_USER) && cv != pos) {96assert(cv != -1);97clipvertex = out->data[cv];98}99100for (i = 0; i < 4; i++) {101out->clip_pos[i] = position[i];102}103104/* Be careful with NaNs. Comparisons must be true for them. */105/* Do the hardwired planes first:106*/107if (flags & DO_CLIP_XY_GUARD_BAND) {108if (!(-0.50 * position[0] + position[3] >= 0)) mask |= (1<<0);109if (!( 0.50 * position[0] + position[3] >= 0)) mask |= (1<<1);110if (!(-0.50 * position[1] + position[3] >= 0)) mask |= (1<<2);111if (!( 0.50 * position[1] + position[3] >= 0)) mask |= (1<<3);112}113else if (flags & DO_CLIP_XY) {114if (!(-position[0] + position[3] >= 0)) mask |= (1<<0);115if (!( position[0] + position[3] >= 0)) mask |= (1<<1);116if (!(-position[1] + position[3] >= 0)) mask |= (1<<2);117if (!( position[1] + position[3] >= 0)) mask |= (1<<3);118}119120/* Clip Z planes according to full cube, half cube or none.121*/122if (flags & DO_CLIP_FULL_Z) {123if (!( position[2] + position[3] >= 0)) mask |= (1<<4);124if (!(-position[2] + position[3] >= 0)) mask |= (1<<5);125}126else if (flags & DO_CLIP_HALF_Z) {127if (!( position[2] >= 0)) mask |= (1<<4);128if (!(-position[2] + position[3] >= 0)) mask |= (1<<5);129}130131if (flags & DO_CLIP_USER) {132unsigned ucp_mask = ucp_enable;133134while (ucp_mask) {135unsigned plane_idx = ffs(ucp_mask)-1;136ucp_mask &= ~(1 << plane_idx);137plane_idx += 6;138139/*140* for user clipping check if we have a clip distance output141* and the shader has written to it, otherwise use clipvertex142* to decide when the plane is clipping.143*/144if (have_cd && num_written_clipdistance) {145float clipdist;146i = plane_idx - 6;147/* first four clip distance in first vector etc. */148if (i < 4)149clipdist = out->data[cd[0]][i];150else151clipdist = out->data[cd[1]][i-4];152if (clipdist < 0 || util_is_inf_or_nan(clipdist))153mask |= 1 << plane_idx;154} else {155if (!(dot4(clipvertex, plane[plane_idx]) >= 0))156mask |= 1 << plane_idx;157}158}159}160161out->clipmask = mask;162need_pipeline |= out->clipmask;163}164165/*166* Transform the vertex position from clip coords to window coords,167* if the vertex is unclipped.168*/169if ((flags & DO_VIEWPORT) && mask == 0)170{171/* divide by w */172float w = 1.0f / position[3];173174/* Viewport mapping */175position[0] = position[0] * w * scale[0] + trans[0];176position[1] = position[1] * w * scale[1] + trans[1];177position[2] = position[2] * w * scale[2] + trans[2];178position[3] = w;179}180#ifdef DEBUG181/* For debug builds, set the clipped vertex's window coordinate182* to NaN to help catch potential errors later.183*/184else {185position[0] =186position[1] =187position[2] =188position[3] = NAN;189}190#endif191192if ((flags & DO_EDGEFLAG) && ef) {193const float *edgeflag = out->data[ef];194out->edgeflag = !(edgeflag[0] != 1.0f);195need_pipeline |= !out->edgeflag;196}197198out = (struct vertex_header *)( (char *)out + info->stride );199}200return need_pipeline != 0;201}202203204#undef FLAGS205#undef TAG206207208