/*!
*/
void CTRTextureVertexAlpha2::scanline_bilinear (  )
{
	tVideoSample *dst;

#ifdef USE_ZBUFFER
	fp24 *z;
#endif

	s32 xStart;
	s32 xEnd;
	s32 dx;


#ifdef SUBTEXEL
	f32 subPixel;
#endif

#ifdef IPOL_Z
	f32 slopeZ;
#endif
#ifdef IPOL_W
	fp24 slopeW;
#endif
#ifdef IPOL_C0
	sVec4 slopeC;
#endif
#ifdef IPOL_T0
	sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
#endif

	// apply top-left fill-convention, left
	xStart = core::ceil32( line.x[0] );
	xEnd = core::ceil32( line.x[1] ) - 1;

	dx = xEnd - xStart;

	if ( dx < 0 )
		return;

	// slopes
	const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );

#ifdef IPOL_Z
	slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
#endif
#ifdef IPOL_W
	slopeW = (line.w[1] - line.w[0]) * invDeltaX;
#endif
#ifdef IPOL_C0
	slopeC = (line.c[0][1] - line.c[0][0]) * invDeltaX;
#endif
#ifdef IPOL_T0
	slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
#endif
#ifdef IPOL_T1
	slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
#endif

#ifdef SUBTEXEL
	subPixel = ( (f32) xStart ) - line.x[0];
#ifdef IPOL_Z
	line.z[0] += slopeZ * subPixel;
#endif
#ifdef IPOL_W
	line.w[0] += slopeW * subPixel;
#endif
#ifdef IPOL_C0
	line.c[0][0] += slopeC * subPixel;
#endif
#ifdef IPOL_T0
	line.t[0][0] += slopeT[0] * subPixel;
#endif
#ifdef IPOL_T1
	line.t[1][0] += slopeT[1] * subPixel;
#endif
#endif

	dst = (tVideoSample*)RenderTarget->getData() + ( line.y * RenderTarget->getDimension().Width ) + xStart;

#ifdef USE_ZBUFFER
	z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
#endif


	f32 inversew = FIX_POINT_F32_MUL;

//#define __TEST_THIS

#ifdef __TEST_THIS

#else
	tFixPoint tx0;
	tFixPoint ty0;

	tFixPoint r0, g0, b0;
	tFixPoint r1, g1, b1;
	tFixPoint r2, g2, b2;
#endif


#ifdef IPOL_C0
	tFixPoint a3;
#endif


	for ( s32 i = 0; i <= dx; ++i )
	{
#ifdef CMP_Z
		if ( line.z[0] < z[i] )
#endif
#ifdef CMP_W
		if ( line.w[0] >= z[i] )
#endif

		{
#ifdef __TEST_THIS

			inversew = fix_inverse32 ( line.w[0] );

			dst[i] = PixelAdd32 (
						dst[i],
					getTexel_plain ( &IT[0],	tofix ( line.t[0][0].x,inversew),
												tofix ( line.t[0][0].y,inversew) )
												  );

#else

#ifdef INVERSE_W
			inversew = fix_inverse32 ( line.w[0] );
#endif
			tx0 = tofix ( line.t[0][0].x,inversew);
			ty0 = tofix ( line.t[0][0].y,inversew);

#ifdef IPOL_C0
			a3 = tofix ( line.c[0][0].y,inversew );
#endif

			getSample_texture ( r0, g0, b0, &IT[0], tx0, ty0 );
			color_to_fix ( r1, g1, b1, dst[i] );

#ifdef IPOL_C0
			r2 = clampfix_maxcolor ( r1 + imulFix ( r0, a3 ) );
			g2 = clampfix_maxcolor ( g1 + imulFix ( g0, a3 ) );
			b2 = clampfix_maxcolor ( b1 + imulFix ( b0, a3 ) );
#else
			r2 = clampfix_maxcolor ( r1 + r0 );
			g2 = clampfix_maxcolor ( g1 + g0 );
			b2 = clampfix_maxcolor ( b1 + b0 );
#endif

			dst[i] = fix_to_color ( r2, g2, b2 );

#ifdef WRITE_Z
			z[i] = line.z[0];
#endif
#ifdef WRITE_W
			z[i] = line.w[0];
#endif
#endif
		}

#ifdef IPOL_Z
		line.z[0] += slopeZ;
#endif
#ifdef IPOL_W
		line.w[0] += slopeW;
#endif
#ifdef IPOL_C0
		line.c[0][0] += slopeC;
#endif
#ifdef IPOL_T0
		line.t[0][0] += slopeT[0];
#endif
#ifdef IPOL_T1
		line.t[1][0] += slopeT[1];
#endif
	}

}
/*!
*/
REALINLINE void CTRTextureLightMap2_Add::scanline_bilinear ()
{
	tVideoSample *dst;

#ifdef USE_ZBUFFER
	fp24 *z;
#endif

	s32 xStart;
	s32 xEnd;
	s32 dx;


#ifdef SUBTEXEL
	f32 subPixel;
#endif

#ifdef IPOL_Z
	f32 slopeZ;
#endif
#ifdef IPOL_W
	fp24 slopeW;
#endif
#ifdef IPOL_C0
	sVec4 slopeC;
#endif
#ifdef IPOL_T0
	sVec2 slopeT0;
#endif
#ifdef IPOL_T1
	sVec2 slopeT1;
#endif

	// apply top-left fill-convention, left
	xStart = core::ceil32( line.x[0] );
	xEnd = core::ceil32( line.x[1] ) - 1;

	dx = xEnd - xStart;

	if ( dx < 0 )
		return;

	// slopes
	const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );

#ifdef IPOL_Z
	slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
#endif
#ifdef IPOL_W
	slopeW = (line.w[1] - line.w[0]) * invDeltaX;
#endif
#ifdef IPOL_C0
	slopeC = (line.c[1] - line.c[0]) * invDeltaX;
#endif
#ifdef IPOL_T0
	slopeT0 = (line.t0[1] - line.t0[0]) * invDeltaX;
#endif
#ifdef IPOL_T1
	slopeT1 = (line.t1[1] - line.t1[0]) * invDeltaX;
#endif

#ifdef SUBTEXEL
	subPixel = ( (f32) xStart ) - line.x[0];
#ifdef IPOL_Z
	line.z[0] += slopeZ * subPixel;
#endif
#ifdef IPOL_W
	line.w[0] += slopeW * subPixel;
#endif
#ifdef IPOL_C0
	line.c[0] += slopeC * subPixel;
#endif
#ifdef IPOL_T0
	line.t0[0] += slopeT0 * subPixel;
#endif
#ifdef IPOL_T1
	line.t1[0] += slopeT1 * subPixel;
#endif
#endif

	dst = lockedSurface + ( line.y * SurfaceWidth ) + xStart;

#ifdef USE_ZBUFFER
	z = lockedZBuffer + ( line.y * SurfaceWidth ) + xStart;
#endif




#ifdef BURNINGVIDEO_RENDERER_FAST
	u32 dIndex = ( line.y & 3 ) << 2;


#else
	// 
	tFixPoint r0, g0, b0;
	tFixPoint r1, g1, b1;
#endif


	for ( s32 i = 0; i <= dx; i++ )
	{
#ifdef CMP_Z
		if ( line.z[0] < z[i] )
#endif
#ifdef CMP_W
		if ( line.w[0] >= z[i] )
#endif
		{

#ifdef WRITE_Z
			z[i] = line.z[0];
#endif
#ifdef WRITE_W
			z[i] = line.w[0];
#endif

#ifdef BURNINGVIDEO_RENDERER_FAST

#ifdef INVERSE_W

			const f32 inversew = fix_inverse32 ( line.w[0] );

			const tFixPointu d = dithermask [ dIndex | ( i ) & 3 ];

			dst[i] = PixelAdd32 (
					getTexel_plain ( &IT[0],	d + f32_to_fixPoint ( line.t0[0].x,inversew), 
												d + f32_to_fixPoint ( line.t0[0].y,inversew) ),
					getTexel_plain ( &IT[1],	d + f32_to_fixPoint ( line.t1[0].x,inversew), 
												d + f32_to_fixPoint ( line.t1[0].y,inversew) )
							);
#else
			const tFixPointu d = dithermask [ dIndex | ( i ) & 3 ];

			dst[i] = PixelAdd32 (
					getTexel_plain ( &IT[0],	d + f32_to_fixPoint ( line.t0[0].x), 
												d + f32_to_fixPoint ( line.t0[0].y) ),
					getTexel_plain ( &IT[1],	d + f32_to_fixPoint ( line.t1[0].x), 
												d + f32_to_fixPoint ( line.t1[0].y) )
							);

#endif

#else
			const f32 inversew = fix_inverse32 ( line.w[0] );

			getSample_texture ( r0, g0, b0, &IT[0], f32_to_fixPoint ( line.t0[0].x,inversew), f32_to_fixPoint ( line.t0[0].y,inversew) );
			getSample_texture ( r1, g1, b1, &IT[1], f32_to_fixPoint ( line.t0[1].x,inversew), f32_to_fixPoint ( line.t0[1].y,inversew) );

			dst[i] = fix_to_color ( clampfix_maxcolor ( r0 + r1 ),
									clampfix_maxcolor ( g0 + g1 ),
									clampfix_maxcolor ( b0 + b1 )
								);
#endif

		}

#ifdef IPOL_Z
		line.z[0] += slopeZ;
#endif
#ifdef IPOL_W
		line.w[0] += slopeW;
#endif
#ifdef IPOL_C0
		line.c[0] += slopeC;
#endif
#ifdef IPOL_T0
		line.t0[0] += slopeT0;
#endif
#ifdef IPOL_T1
		line.t1[0] += slopeT1;
#endif
	}

}
/*!
*/
void CTRTextureVertexAlpha2::scanline_bilinear (  )
{
    tVideoSample *dst;

#ifdef USE_ZBUFFER
    fp24 *z;
#endif

    s32 xStart;
    s32 xEnd;
    s32 dx;


#ifdef SUBTEXEL
    f32 subPixel;
#endif

#ifdef IPOL_Z
    f32 slopeZ;
#endif
#ifdef IPOL_W
    fp24 slopeW;
#endif
#ifdef IPOL_C0
    sVec4 slopeC;
#endif
#ifdef IPOL_T0
    sVec2 slopeT0;
#endif
#ifdef IPOL_T1
    sVec2 slopeT1;
#endif

    // apply top-left fill-convention, left
    xStart = core::ceil32( line.x[0] );
    xEnd = core::ceil32( line.x[1] ) - 1;

    dx = xEnd - xStart;

    if ( dx < 0 )
        return;

    // slopes
    const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );

#ifdef IPOL_Z
    slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
#endif
#ifdef IPOL_W
    slopeW = (line.w[1] - line.w[0]) * invDeltaX;
#endif
#ifdef IPOL_C0
    slopeC = (line.c[1] - line.c[0]) * invDeltaX;
#endif
#ifdef IPOL_T0
    slopeT0 = (line.t0[1] - line.t0[0]) * invDeltaX;
#endif
#ifdef IPOL_T1
    slopeT1 = (line.t1[1] - line.t1[0]) * invDeltaX;
#endif

#ifdef SUBTEXEL
    subPixel = ( (f32) xStart ) - line.x[0];
#ifdef IPOL_Z
    line.z[0] += slopeZ * subPixel;
#endif
#ifdef IPOL_W
    line.w[0] += slopeW * subPixel;
#endif
#ifdef IPOL_C0
    line.c[0] += slopeC * subPixel;
#endif
#ifdef IPOL_T0
    line.t0[0] += slopeT0 * subPixel;
#endif
#ifdef IPOL_T1
    line.t1[0] += slopeT1 * subPixel;
#endif
#endif

    dst = lockedSurface + ( line.y * SurfaceWidth ) + xStart;

#ifdef USE_ZBUFFER
    z = lockedZBuffer + ( line.y * SurfaceWidth ) + xStart;
#endif


#ifdef INVERSE_W
    f32 inversew;
#endif

//#define __TEST_THIS

#ifdef __TEST_THIS

#else
    tFixPoint tx0;
    tFixPoint ty0;

    tFixPoint r0, g0, b0;
    tFixPoint r1, g1, b1;
    tFixPoint r2, g2, b2;
#endif


#ifdef IPOL_C0
    tFixPoint a3;
#endif


    for ( s32 i = 0; i <= dx; ++i )
    {
#ifdef CMP_Z
        if ( line.z[0] < z[i] )
#endif
#ifdef CMP_W
            if ( line.w[0] >= z[i] )
#endif

            {
#ifdef __TEST_THIS

                inversew = fix_inverse32 ( line.w[0] );

                dst[i] = PixelAdd32 (
                             dst[i],
                             getTexel_plain ( &IT[0],	f32_to_fixPoint ( line.t0[0].x,inversew),
                                              f32_to_fixPoint ( line.t0[0].y,inversew) )
                         );

#else

#ifdef INVERSE_W
                inversew = fix_inverse32 ( line.w[0] );

                tx0 = f32_to_fixPoint ( line.t0[0].x,inversew);
                ty0 = f32_to_fixPoint ( line.t0[0].y,inversew);

#ifdef IPOL_C0
                a3 = f32_to_fixPoint ( line.c[0].y,inversew );
#endif

#else
                tx0 = f32_to_fixPoint ( line.t0[0].x );
                ty0 = f32_to_fixPoint ( line.t0[0].y );

#ifdef IPOL_C0
                a3 = f32_to_fixPoint ( line.c[0].y );
#endif


#endif

                getSample_texture ( r0, g0, b0, &IT[0], tx0, ty0 );
                color_to_fix ( r1, g1, b1, dst[i] );

#ifdef IPOL_C0
                r2 = clampfix_maxcolor ( r1 + imulFix ( r0, a3 ) );
                g2 = clampfix_maxcolor ( g1 + imulFix ( g0, a3 ) );
                b2 = clampfix_maxcolor ( b1 + imulFix ( b0, a3 ) );
#else
                r2 = clampfix_maxcolor ( r1 + r0 );
                g2 = clampfix_maxcolor ( g1 + g0 );
                b2 = clampfix_maxcolor ( b1 + b0 );
#endif

                dst[i] = fix_to_color ( r2, g2, b2 );

#ifdef WRITE_Z
                z[i] = line.z[0];
#endif
#ifdef WRITE_W
                z[i] = line.w[0];
#endif
#endif
            }

#ifdef IPOL_Z
        line.z[0] += slopeZ;
#endif
#ifdef IPOL_W
        line.w[0] += slopeW;
#endif
#ifdef IPOL_C0
        line.c[0] += slopeC;
#endif
#ifdef IPOL_T0
        line.t0[0] += slopeT0;
#endif
#ifdef IPOL_T1
        line.t1[0] += slopeT1;
#endif
    }

}