/** * Used to convert current raster distance to a fog factor in [0,1]. */ GLfloat _swrast_z_to_fogfactor(GLcontext *ctx, GLfloat z) { GLfloat d, f; switch (ctx->Fog.Mode) { case GL_LINEAR: if (ctx->Fog.Start == ctx->Fog.End) d = 1.0F; else d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); f = (ctx->Fog.End - z) * d; return CLAMP(f, 0.0F, 1.0F); case GL_EXP: d = ctx->Fog.Density; f = EXPF(-d * z); f = CLAMP(f, 0.0F, 1.0F); return f; case GL_EXP2: d = ctx->Fog.Density; f = EXPF(-(d * d * z * z)); f = CLAMP(f, 0.0F, 1.0F); return f; default: _mesa_problem(ctx, "Bad fog mode in _swrast_z_to_fogfactor"); return 0.0; } }
/** * @internal * @brief Get a reproducible single precision scale * * For any given X, return a reproducible scaling factor Y of the form * * 2^(#SBWIDTH * z) where z is an integer * * such that * * Y * 2^(-@c FLT_MANT_DIG - #SBWIDTH - 1) < X < Y * 2\^(#SBWIDTH + 2) * * @param X single precision number to be scaled * @return reproducible scaling factor * * @author Peter Ahrens * @date 19 Jun 2015 */ float binned_sscale(const float X){ int e = EXPF(X); e = e < SBWIDTH ? SBWIDTH : e; e -= (e - EXPF_BIAS - 1) % SBWIDTH; e -= EXPF_BIAS; return ldexpf(0.5, e); }
/** * Initialize the exp_table[] lookup table for approximating exp(). */ static void init_static_data( void ) { GLfloat f = 0.0F; GLint i = 0; for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) { exp_table[i] = EXPF(-f); } inited = 1; }
/** * @internal * @brief Get index of manually specified binned single precision * * The index of an binned type is the bin that it corresponds to. Higher indicies correspond to smaller bins. * * @param priX X's primary vector * @return X's index * * @author Peter Ahrens * @author Hong Diep Nguyen * @date 23 Sep 2015 */ int binned_smindex(const float *priX){ /* //reference version int exp; if(priX[0] == 0.0){ return (FLT_MAX_EXP - FLT_MIN_EXP)/SBWIDTH + binned_SBMAXFOLD; }else{ frexpf(priX[0], &exp); if(exp == FLT_MAX_EXP){ return 0; } return (FLT_MAX_EXP + FLT_MANT_DIG - SBWIDTH + 1 - exp)/SBWIDTH; } */ return ((FLT_MAX_EXP + FLT_MANT_DIG - SBWIDTH + 1 + EXPF_BIAS) - EXPF(priX[0]))/SBWIDTH; }
DLLEXPORT float rootnf(float x, int y) { int y_odd = (y&1); int y_even = !y_odd; int y_neg = (y<0); int x_neg = (x<0); if (y == 0) return NANF; if (EXPF(x) == 0) /* Flush denormalized input to zero */ return (y > 0) ? 0.0f : (y_even ? INFF : copysign(INFF, x)); if (y == 1) return x; if (ISNEGINFF(x) && y_even) return NANF; if (ISINFF(x)) return y_neg ? copysign(0.0f, x) : x; if (ISANYZEROF(x) && y_odd && y_neg) return copysign(INFF, x); int negresult = 0; if (x_neg && y_odd) { negresult = 1; x = FABSF(x); } float result = powf(x, reciprocalf(y)); if (negresult) result = -result; return result; }
/** * As above, but color index mode. */ void _swrast_fog_ci_span( const GLcontext *ctx, SWspan *span ) { const SWcontext *swrast = SWRAST_CONTEXT(ctx); const GLuint haveW = (span->interpMask & SPAN_W); const GLuint fogIndex = (GLuint) ctx->Fog.Index; GLuint *index = span->array->index; ASSERT(swrast->_FogEnabled); ASSERT(span->arrayMask & SPAN_INDEX); ASSERT((span->interpMask | span->arrayMask) & SPAN_FOG); /* we need to compute fog blend factors */ if (swrast->_PreferPixelFog) { /* The span's fog values are fog coordinates, now compute blend factors * and blend the fragment colors with the fog color. */ switch (ctx->Fog.Mode) { case GL_LINEAR: { const GLfloat fogEnd = ctx->Fog.End; const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End) ? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start); const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; const GLfloat wStep = haveW ? span->attrStepX[FRAG_ATTRIB_WPOS][3] : 0.0F; GLfloat w = haveW ? span->attrStart[FRAG_ATTRIB_WPOS][3] : 1.0F; GLuint i; for (i = 0; i < span->end; i++) { GLfloat f = (fogEnd - fogCoord / w) * fogScale; f = CLAMP(f, 0.0F, 1.0F); index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); fogCoord += fogStep; w += wStep; } } break; case GL_EXP: { const GLfloat density = -ctx->Fog.Density; const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; const GLfloat wStep = haveW ? span->attrStepX[FRAG_ATTRIB_WPOS][3] : 0.0F; GLfloat w = haveW ? span->attrStart[FRAG_ATTRIB_WPOS][3] : 1.0F; GLuint i; for (i = 0; i < span->end; i++) { GLfloat f = EXPF(density * fogCoord / w); f = CLAMP(f, 0.0F, 1.0F); index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); fogCoord += fogStep; w += wStep; } } break; case GL_EXP2: { const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density; const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; const GLfloat wStep = haveW ? span->attrStepX[FRAG_ATTRIB_WPOS][3] : 0.0F; GLfloat w = haveW ? span->attrStart[FRAG_ATTRIB_WPOS][3] : 1.0F; GLuint i; for (i = 0; i < span->end; i++) { const GLfloat coord = fogCoord / w; GLfloat tmp = negDensitySquared * coord * coord; GLfloat f; #if defined(__alpha__) || defined(__alpha) /* XXX this underflow check may be needed for other systems*/ if (tmp < FLT_MIN_10_EXP) tmp = FLT_MIN_10_EXP; #endif f = EXPF(tmp); f = CLAMP(f, 0.0F, 1.0F); index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); fogCoord += fogStep; w += wStep; } } break; default: _mesa_problem(ctx, "Bad fog mode in _swrast_fog_ci_span"); return; } } else if (span->arrayMask & SPAN_FOG) { /* The span's fog array values are blend factors. * They were previously computed per-vertex. */ GLuint i; for (i = 0; i < span->end; i++) { const GLfloat f = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); } } else { /* The span's fog start/step values are blend factors. * They were previously computed per-vertex. */ const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; GLfloat fog = span->attrStart[FRAG_ATTRIB_FOGC][0]; const GLfloat wStep = haveW ? span->attrStepX[FRAG_ATTRIB_WPOS][3] : 0.0F; GLfloat w = haveW ? span->attrStart[FRAG_ATTRIB_WPOS][3] : 1.0F; GLuint i; ASSERT(span->interpMask & SPAN_FOG); for (i = 0; i < span->end; i++) { const GLfloat f = fog / w; index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); fog += fogStep; w += wStep; } } }