Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_offset.c
4565 views
/**************************************************************************1*2* Copyright 2007 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* \brief polygon offset state29*30* \author Keith Whitwell <[email protected]>31* \author Brian Paul32*/3334#include "util/format/u_format.h"35#include "util/u_math.h"36#include "util/u_memory.h"37#include "draw_pipe.h"38394041struct offset_stage {42struct draw_stage stage;4344float scale;45float units;46float clamp;47};48495051static inline struct offset_stage *offset_stage( struct draw_stage *stage )52{53return (struct offset_stage *) stage;54}555657585960/**61* Offset tri Z. Some hardware can handle this, but not usually when62* doing unfilled rendering.63*/64static void do_offset_tri( struct draw_stage *stage,65struct prim_header *header )66{67const unsigned pos = draw_current_shader_position_output(stage->draw);68struct offset_stage *offset = offset_stage(stage);69float inv_det = 1.0f / header->det;7071/* Window coords:72*/73float *v0 = header->v[0]->data[pos];74float *v1 = header->v[1]->data[pos];75float *v2 = header->v[2]->data[pos];7677/* edge vectors e = v0 - v2, f = v1 - v2 */78float ex = v0[0] - v2[0];79float ey = v0[1] - v2[1];80float ez = v0[2] - v2[2];81float fx = v1[0] - v2[0];82float fy = v1[1] - v2[1];83float fz = v1[2] - v2[2];8485/* (a,b) = cross(e,f).xy */86float a = ey*fz - ez*fy;87float b = ez*fx - ex*fz;8889float dzdx = fabsf(a * inv_det);90float dzdy = fabsf(b * inv_det);9192float zoffset, mult;9394mult = MAX2(dzdx, dzdy) * offset->scale;9596if (stage->draw->floating_point_depth) {97float bias;98union fi maxz;99maxz.f = MAX3(v0[2], v1[2], v2[2]);100/* just do the math directly on shifted number */101maxz.ui &= 0xff << 23;102maxz.i -= 23 << 23;103/* Clamping to zero means mrd will be zero for very small numbers,104* but specs do not indicate this should be prevented by clamping105* mrd to smallest normal number instead. */106maxz.i = MAX2(maxz.i, 0);107108bias = offset->units * maxz.f;109zoffset = bias + mult;110} else {111zoffset = offset->units + mult;112}113114if (offset->clamp)115zoffset = (offset->clamp < 0.0f) ? MAX2(zoffset, offset->clamp) :116MIN2(zoffset, offset->clamp);117118/*119* Note: we're applying the offset and clamping per-vertex.120* Ideally, the offset is applied per-fragment prior to fragment shading.121*/122v0[2] = SATURATE(v0[2] + zoffset);123v1[2] = SATURATE(v1[2] + zoffset);124v2[2] = SATURATE(v2[2] + zoffset);125126stage->next->tri( stage->next, header );127}128129130static void offset_tri( struct draw_stage *stage,131struct prim_header *header )132{133struct prim_header tmp;134135tmp.det = header->det;136tmp.flags = header->flags;137tmp.pad = header->pad;138tmp.v[0] = dup_vert(stage, header->v[0], 0);139tmp.v[1] = dup_vert(stage, header->v[1], 1);140tmp.v[2] = dup_vert(stage, header->v[2], 2);141142do_offset_tri( stage, &tmp );143}144145146static void offset_first_tri( struct draw_stage *stage,147struct prim_header *header )148{149struct offset_stage *offset = offset_stage(stage);150const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;151unsigned fill_mode = rast->fill_front;152boolean do_offset;153154if (rast->fill_back != rast->fill_front) {155/* Need to check for back-facing triangle */156boolean ccw = header->det < 0.0f;157if (ccw != rast->front_ccw)158fill_mode = rast->fill_back;159}160161/* Now determine if we need to do offsetting for the point/line/fill mode */162switch (fill_mode) {163case PIPE_POLYGON_MODE_FILL:164do_offset = rast->offset_tri;165break;166case PIPE_POLYGON_MODE_LINE:167do_offset = rast->offset_line;168break;169case PIPE_POLYGON_MODE_POINT:170do_offset = rast->offset_point;171break;172default:173assert(!"invalid fill_mode in offset_first_tri()");174do_offset = rast->offset_tri;175}176177if (do_offset) {178offset->scale = rast->offset_scale;179offset->clamp = rast->offset_clamp;180181/*182* If depth is floating point, depth bias is calculated with respect183* to the primitive's maximum Z value. Retain the original depth bias184* value until that stage.185*/186if (stage->draw->floating_point_depth) {187offset->units = (float) rast->offset_units;188} else {189offset->units = (float) (rast->offset_units * stage->draw->mrd * 2);190}191}192else {193offset->scale = 0.0f;194offset->clamp = 0.0f;195offset->units = 0.0f;196}197198199stage->tri = offset_tri;200stage->tri( stage, header );201}202203204205206static void offset_flush( struct draw_stage *stage,207unsigned flags )208{209stage->tri = offset_first_tri;210stage->next->flush( stage->next, flags );211}212213214static void offset_reset_stipple_counter( struct draw_stage *stage )215{216stage->next->reset_stipple_counter( stage->next );217}218219220static void offset_destroy( struct draw_stage *stage )221{222draw_free_temp_verts( stage );223FREE( stage );224}225226227/**228* Create polygon offset drawing stage.229*/230struct draw_stage *draw_offset_stage( struct draw_context *draw )231{232struct offset_stage *offset = CALLOC_STRUCT(offset_stage);233if (!offset)234goto fail;235236offset->stage.draw = draw;237offset->stage.name = "offset";238offset->stage.next = NULL;239offset->stage.point = draw_pipe_passthrough_point;240offset->stage.line = draw_pipe_passthrough_line;241offset->stage.tri = offset_first_tri;242offset->stage.flush = offset_flush;243offset->stage.reset_stipple_counter = offset_reset_stipple_counter;244offset->stage.destroy = offset_destroy;245246if (!draw_alloc_temp_verts( &offset->stage, 3 ))247goto fail;248249return &offset->stage;250251fail:252if (offset)253offset->stage.destroy( &offset->stage );254255return NULL;256}257258259