diff options
Diffstat (limited to 'src/gallium/auxiliary')
-rw-r--r-- | src/gallium/auxiliary/draw/draw_context.c | 18 | ||||
-rw-r--r-- | src/gallium/auxiliary/draw/draw_context.h | 2 | ||||
-rw-r--r-- | src/gallium/auxiliary/draw/draw_pipe_offset.c | 30 | ||||
-rw-r--r-- | src/gallium/auxiliary/draw/draw_private.h | 2 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_format.c | 16 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_format.h | 18 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_math.h | 13 |
7 files changed, 80 insertions, 19 deletions
diff --git a/src/gallium/auxiliary/draw/draw_context.c b/src/gallium/auxiliary/draw/draw_context.c index 641dd82ee28..2b3bc0d0ece 100644 --- a/src/gallium/auxiliary/draw/draw_context.c +++ b/src/gallium/auxiliary/draw/draw_context.c @@ -38,6 +38,7 @@ #include "util/u_inlines.h" #include "util/u_helpers.h" #include "util/u_prim.h" +#include "util/u_format.h" #include "draw_context.h" #include "draw_pipe.h" #include "draw_prim_assembler.h" @@ -164,6 +165,8 @@ boolean draw_init(struct draw_context *draw) draw->quads_always_flatshade_last = !draw->pipe->screen->get_param( draw->pipe->screen, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION); + draw->floating_point_depth = false; + return TRUE; } @@ -233,15 +236,20 @@ void draw_flush( struct draw_context *draw ) /** - * Specify the Minimum Resolvable Depth factor for polygon offset. + * Specify the depth stencil format for the draw pipeline. This function + * determines the Minimum Resolvable Depth factor for polygon offset. * This factor potentially depends on the number of Z buffer bits, * the rasterization algorithm and the arithmetic performed on Z - * values between vertex shading and rasterization. It will vary - * from one driver to another. + * values between vertex shading and rasterization. */ -void draw_set_mrd(struct draw_context *draw, double mrd) +void draw_set_zs_format(struct draw_context *draw, enum pipe_format format) { - draw->mrd = mrd; + const struct util_format_description *desc = util_format_description(format); + + draw->floating_point_depth = + (util_get_depth_format_type(desc) == UTIL_FORMAT_TYPE_FLOAT); + + draw->mrd = util_get_depth_format_mrd(desc); } diff --git a/src/gallium/auxiliary/draw/draw_context.h b/src/gallium/auxiliary/draw/draw_context.h index 46d0de6f853..e2e0cb4dad6 100644 --- a/src/gallium/auxiliary/draw/draw_context.h +++ b/src/gallium/auxiliary/draw/draw_context.h @@ -111,7 +111,7 @@ void draw_enable_line_stipple(struct draw_context *draw, boolean enable); void draw_enable_point_sprites(struct draw_context *draw, boolean enable); -void draw_set_mrd(struct draw_context *draw, double mrd); +void draw_set_zs_format(struct draw_context *draw, enum pipe_format format); boolean draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe); diff --git a/src/gallium/auxiliary/draw/draw_pipe_offset.c b/src/gallium/auxiliary/draw/draw_pipe_offset.c index 3578525051f..8071bc77b80 100644 --- a/src/gallium/auxiliary/draw/draw_pipe_offset.c +++ b/src/gallium/auxiliary/draw/draw_pipe_offset.c @@ -32,6 +32,7 @@ * \author Brian Paul */ +#include "util/u_format.h" #include "util/u_math.h" #include "util/u_memory.h" #include "draw_pipe.h" @@ -89,7 +90,22 @@ static void do_offset_tri( struct draw_stage *stage, float dzdx = fabsf(a * inv_det); float dzdy = fabsf(b * inv_det); - float zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale; + float zoffset, maxz, bias, mult; + + mult = MAX2(dzdx, dzdy) * offset->scale; + + if (stage->draw->floating_point_depth) { + maxz = MAX3(v0[2], v1[2], v2[2]); + + /** + * XXX: TODO optimize this to quickly resolve a pow2 number through + * an exponent only operation. + */ + bias = offset->units * util_fast_exp2(util_get_float32_exponent(maxz) - 23); + zoffset = bias + mult; + } else { + zoffset = offset->units + mult; + } if (offset->clamp) zoffset = (offset->clamp < 0.0f) ? MAX2(zoffset, offset->clamp) : @@ -157,7 +173,17 @@ static void offset_first_tri( struct draw_stage *stage, if (do_offset) { offset->scale = rast->offset_scale; offset->clamp = rast->offset_clamp; - offset->units = (float) (rast->offset_units * stage->draw->mrd); + + /* + * If depth is floating point, depth bias is calculated with respect + * to the primitive's maximum Z value. Retain the original depth bias + * value until that stage. + */ + if (stage->draw->floating_point_depth) { + offset->units = (float) rast->offset_units; + } else { + offset->units = (float) (rast->offset_units * stage->draw->mrd); + } } else { offset->scale = 0.0f; diff --git a/src/gallium/auxiliary/draw/draw_private.h b/src/gallium/auxiliary/draw/draw_private.h index ba93b093377..fbe25a264c5 100644 --- a/src/gallium/auxiliary/draw/draw_private.h +++ b/src/gallium/auxiliary/draw/draw_private.h @@ -236,6 +236,8 @@ struct draw_context boolean dump_vs; + /** Depth format and bias related settings. */ + boolean floating_point_depth; double mrd; /**< minimum resolvable depth value, for polygon offset */ /** Current rasterizer state given to us by the driver */ diff --git a/src/gallium/auxiliary/util/u_format.c b/src/gallium/auxiliary/util/u_format.c index 9ef3bb53ff2..6b602bf3262 100644 --- a/src/gallium/auxiliary/util/u_format.c +++ b/src/gallium/auxiliary/util/u_format.c @@ -215,9 +215,8 @@ util_format_is_supported(enum pipe_format format, unsigned bind) * default MRD will be 1.0 / ((1 << 24) - 1). */ double -util_get_depth_format_mrd(enum pipe_format format) +util_get_depth_format_mrd(const struct util_format_description *desc) { - struct util_format_description *format_desc; /* * Depth buffer formats without a depth component OR scenarios * without a bound depth buffer default to D24. @@ -225,23 +224,20 @@ util_get_depth_format_mrd(enum pipe_format format) double mrd = 1.0 / ((1 << 24) - 1); unsigned depth_channel; - format_desc = (struct util_format_description *) - util_format_description(format); - - assert(format_desc); + assert(desc); /* * Some depth formats do not store the depth component in the first * channel, detect the format and adjust the depth channel. Get the * swizzled depth component channel. */ - depth_channel = format_desc->swizzle[0]; + depth_channel = desc->swizzle[0]; - if (format_desc->channel[depth_channel].type == UTIL_FORMAT_TYPE_UNSIGNED && - format_desc->channel[depth_channel].normalized) { + if (desc->channel[depth_channel].type == UTIL_FORMAT_TYPE_UNSIGNED && + desc->channel[depth_channel].normalized) { int depth_bits; - depth_bits = format_desc->channel[depth_channel].size; + depth_bits = desc->channel[depth_channel].size; mrd = 1.0 / ((1ULL << depth_bits) - 1); } diff --git a/src/gallium/auxiliary/util/u_format.h b/src/gallium/auxiliary/util/u_format.h index 369c3994ad3..0fbaf4cc154 100644 --- a/src/gallium/auxiliary/util/u_format.h +++ b/src/gallium/auxiliary/util/u_format.h @@ -546,13 +546,29 @@ util_format_is_depth_and_stencil(enum pipe_format format) /** + * Calculates the depth format type based upon the incoming format description. + */ +static INLINE unsigned +util_get_depth_format_type(const struct util_format_description *desc) +{ + unsigned depth_channel = desc->swizzle[0]; + if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS && + depth_channel != UTIL_FORMAT_SWIZZLE_NONE) { + return desc->channel[depth_channel].type; + } else { + return UTIL_FORMAT_TYPE_VOID; + } +} + + +/** * Calculates the MRD for the depth format. MRD is used in depth bias * for UNORM and unbound depth buffers. When the depth buffer is floating * point, the depth bias calculation does not use the MRD. However, the * default MRD will be 1.0 / ((1 << 24) - 1). */ double -util_get_depth_format_mrd(enum pipe_format format); +util_get_depth_format_mrd(const struct util_format_description *desc); /** diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h index f5c14ef8d56..426d5daa78c 100644 --- a/src/gallium/auxiliary/util/u_math.h +++ b/src/gallium/auxiliary/util/u_math.h @@ -246,6 +246,19 @@ union di { /** + * Extract the IEEE float32 exponent. + */ +static INLINE signed +util_get_float32_exponent(float x) { + union fi f; + + f.f = x; + + return ((f.ui >> 23) & 0xff) - 127; +} + + +/** * Fast version of 2^x * Identity: exp2(a + b) = exp2(a) * exp2(b) * Let ipart = int(x) |