Commit 130c27a3 by Nicolas Capens

Move blit routine generation to a method.

Bug 21716622 Change-Id: Ida62ad1f1f51f035969754f2c5ff800a0afc6fd5 Reviewed-on: https://swiftshader-review.googlesource.com/3452Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com> Tested-by: 's avatarNicolas Capens <capn@google.com>
parent a8b364b7
...@@ -170,236 +170,246 @@ namespace sw ...@@ -170,236 +170,246 @@ namespace sw
return true; return true;
} }
bool Blitter::blitReactor(Surface *source, const SliceRect &sourceRect, Surface *dest, const SliceRect &destRect, bool filter) Routine *Blitter::generate(BlitState &state)
{ {
Rect dRect = destRect; Function<Void, Pointer<Byte> > function;
Rect sRect = sourceRect;
if(destRect.x0 > destRect.x1)
{ {
swap(dRect.x0, dRect.x1); Pointer<Byte> blit(function.arg(0));
swap(sRect.x0, sRect.x1);
}
if(destRect.y0 > destRect.y1)
{
swap(dRect.y0, dRect.y1);
swap(sRect.y0, sRect.y1);
}
BlitState state;
state.sourceFormat = source->getInternalFormat(); Pointer<Byte> source = *Pointer<Pointer<Byte> >(blit + OFFSET(BlitData,source));
state.destFormat = dest->getInternalFormat(); Pointer<Byte> dest = *Pointer<Pointer<Byte> >(blit + OFFSET(BlitData,dest));
state.filter = filter; Int sPitchB = *Pointer<Int>(blit + OFFSET(BlitData,sPitchB));
Int dPitchB = *Pointer<Int>(blit + OFFSET(BlitData,dPitchB));
Routine *blitRoutine = blitCache->query(state);
if(!blitRoutine)
{
Function<Void, Pointer<Byte> > function;
{
Pointer<Byte> blit(function.arg(0));
Pointer<Byte> source = *Pointer<Pointer<Byte> >(blit + OFFSET(BlitData,source)); Float x0 = *Pointer<Float>(blit + OFFSET(BlitData,x0));
Pointer<Byte> dest = *Pointer<Pointer<Byte> >(blit + OFFSET(BlitData,dest)); Float y0 = *Pointer<Float>(blit + OFFSET(BlitData,y0));
Int sPitchB = *Pointer<Int>(blit + OFFSET(BlitData,sPitchB)); Float w = *Pointer<Float>(blit + OFFSET(BlitData,w));
Int dPitchB = *Pointer<Int>(blit + OFFSET(BlitData,dPitchB)); Float h = *Pointer<Float>(blit + OFFSET(BlitData,h));
Float x0 = *Pointer<Float>(blit + OFFSET(BlitData,x0)); Int x0d = *Pointer<Int>(blit + OFFSET(BlitData,x0d));
Float y0 = *Pointer<Float>(blit + OFFSET(BlitData,y0)); Int x1d = *Pointer<Int>(blit + OFFSET(BlitData,x1d));
Float w = *Pointer<Float>(blit + OFFSET(BlitData,w)); Int y0d = *Pointer<Int>(blit + OFFSET(BlitData,y0d));
Float h = *Pointer<Float>(blit + OFFSET(BlitData,h)); Int y1d = *Pointer<Int>(blit + OFFSET(BlitData,y1d));
Int x0d = *Pointer<Int>(blit + OFFSET(BlitData,x0d)); Int sWidth = *Pointer<Int>(blit + OFFSET(BlitData,sWidth));
Int x1d = *Pointer<Int>(blit + OFFSET(BlitData,x1d)); Int sHeight = *Pointer<Int>(blit + OFFSET(BlitData,sHeight));
Int y0d = *Pointer<Int>(blit + OFFSET(BlitData,y0d));
Int y1d = *Pointer<Int>(blit + OFFSET(BlitData,y1d));
Int sWidth = *Pointer<Int>(blit + OFFSET(BlitData,sWidth)); Float y = y0;
Int sHeight = *Pointer<Int>(blit + OFFSET(BlitData,sHeight));
Float y = y0; For(Int j = y0d, j < y1d, j++)
{
Float x = x0;
Pointer<Byte> destLine = dest + j * dPitchB;
For(Int j = y0d, j < y1d, j++) For(Int i = x0d, i < x1d, i++)
{ {
Float x = x0; Float4 color;
Pointer<Byte> destLine = dest + j * dPitchB;
For(Int i = x0d, i < x1d, i++) if(!state.filter)
{ {
Float4 color; Int X = Int(x);
Int Y = Int(y);
if(!filter)
{
Int X = Int(x);
Int Y = Int(y);
Pointer<Byte> s = source + Y * sPitchB + X * Surface::bytes(state.sourceFormat); Pointer<Byte> s = source + Y * sPitchB + X * Surface::bytes(state.sourceFormat);
if(!read(color, s, state.sourceFormat)) if(!read(color, s, state.sourceFormat))
{
return false;
}
}
else // Bilinear filtering
{ {
Float x0 = x - 0.5f; return nullptr;
Float y0 = y - 0.5f; }
}
else // Bilinear filtering
{
Float x0 = x - 0.5f;
Float y0 = y - 0.5f;
Int X0 = Max(Int(x0), 0); Int X0 = Max(Int(x0), 0);
Int Y0 = Max(Int(y0), 0); Int Y0 = Max(Int(y0), 0);
Int X1 = IfThenElse(X0 + 1 >= sWidth, X0, X0 + 1); Int X1 = IfThenElse(X0 + 1 >= sWidth, X0, X0 + 1);
Int Y1 = IfThenElse(Y0 + 1 >= sHeight, Y0, Y0 + 1); Int Y1 = IfThenElse(Y0 + 1 >= sHeight, Y0, Y0 + 1);
Pointer<Byte> s00 = source + Y0 * sPitchB + X0 * Surface::bytes(state.sourceFormat); Pointer<Byte> s00 = source + Y0 * sPitchB + X0 * Surface::bytes(state.sourceFormat);
Pointer<Byte> s01 = source + Y0 * sPitchB + X1 * Surface::bytes(state.sourceFormat); Pointer<Byte> s01 = source + Y0 * sPitchB + X1 * Surface::bytes(state.sourceFormat);
Pointer<Byte> s10 = source + Y1 * sPitchB + X0 * Surface::bytes(state.sourceFormat); Pointer<Byte> s10 = source + Y1 * sPitchB + X0 * Surface::bytes(state.sourceFormat);
Pointer<Byte> s11 = source + Y1 * sPitchB + X1 * Surface::bytes(state.sourceFormat); Pointer<Byte> s11 = source + Y1 * sPitchB + X1 * Surface::bytes(state.sourceFormat);
Float4 c00; if(!read(c00, s00, state.sourceFormat)) return false; Float4 c00; if(!read(c00, s00, state.sourceFormat)) return nullptr;
Float4 c01; if(!read(c01, s01, state.sourceFormat)) return false; Float4 c01; if(!read(c01, s01, state.sourceFormat)) return nullptr;
Float4 c10; if(!read(c10, s10, state.sourceFormat)) return false; Float4 c10; if(!read(c10, s10, state.sourceFormat)) return nullptr;
Float4 c11; if(!read(c11, s11, state.sourceFormat)) return false; Float4 c11; if(!read(c11, s11, state.sourceFormat)) return nullptr;
Float4 fx = Float4(x0 - Float(X0)); Float4 fx = Float4(x0 - Float(X0));
Float4 fy = Float4(y0 - Float(Y0)); Float4 fy = Float4(y0 - Float(Y0));
color = c00 * (Float4(1.0f) - fx) * (Float4(1.0f) - fy) + color = c00 * (Float4(1.0f) - fx) * (Float4(1.0f) - fy) +
c01 * fx * (Float4(1.0f) - fy) + c01 * fx * (Float4(1.0f) - fy) +
c10 * (Float4(1.0f) - fx) * fy + c10 * (Float4(1.0f) - fx) * fy +
c11 * fx * fy; c11 * fx * fy;
} }
float4 unscale; float4 unscale;
switch(state.sourceFormat) switch(state.sourceFormat)
{ {
case FORMAT_L8: case FORMAT_L8:
case FORMAT_A8: case FORMAT_A8:
case FORMAT_A8R8G8B8: case FORMAT_A8R8G8B8:
case FORMAT_A8B8G8R8: case FORMAT_A8B8G8R8:
case FORMAT_X8R8G8B8: case FORMAT_X8R8G8B8:
case FORMAT_X8B8G8R8: case FORMAT_X8B8G8R8:
unscale = vector(255, 255, 255, 255); unscale = vector(255, 255, 255, 255);
break; break;
case FORMAT_A16B16G16R16: case FORMAT_A16B16G16R16:
case FORMAT_G16R16: case FORMAT_G16R16:
unscale = vector(65535, 65535, 65535, 65535); unscale = vector(65535, 65535, 65535, 65535);
break; break;
case FORMAT_A32B32G32R32F: case FORMAT_A32B32G32R32F:
case FORMAT_G32R32F: case FORMAT_G32R32F:
case FORMAT_R32F: case FORMAT_R32F:
unscale = vector(1.0f, 1.0f, 1.0f, 1.0f); unscale = vector(1.0f, 1.0f, 1.0f, 1.0f);
break; break;
default: default:
return false; return false;
} }
float4 scale;
switch(state.destFormat)
{
case FORMAT_L8:
case FORMAT_A8:
case FORMAT_A8R8G8B8:
case FORMAT_A8B8G8R8:
case FORMAT_X8R8G8B8:
case FORMAT_X8B8G8R8:
scale = vector(255, 255, 255, 255);
break;
case FORMAT_A16B16G16R16:
case FORMAT_G16R16:
scale = vector(65535, 65535, 65535, 65535);
break;
case FORMAT_A32B32G32R32F:
case FORMAT_G32R32F:
case FORMAT_R32F:
scale = vector(1.0f, 1.0f, 1.0f, 1.0f);
break;
default:
return false;
}
if(unscale != scale)
{
color *= Float4(scale.x / unscale.x, scale.y / unscale.y, scale.z / unscale.z, scale.w / unscale.w);
}
if(Surface::isFloatFormat(state.sourceFormat) && !Surface::isFloatFormat(state.destFormat))
{
color = Min(color, Float4(1.0f, 1.0f, 1.0f, 1.0f));
color = Max(color, Float4(Surface::isUnsignedComponent(state.destFormat, 0) ? 0.0f : -1.0f,
Surface::isUnsignedComponent(state.destFormat, 1) ? 0.0f : -1.0f,
Surface::isUnsignedComponent(state.destFormat, 2) ? 0.0f : -1.0f,
Surface::isUnsignedComponent(state.destFormat, 3) ? 0.0f : -1.0f));
}
float4 scale; Pointer<Byte> d = destLine + i * Surface::bytes(state.destFormat);
switch(state.destFormat) switch(state.destFormat)
{
case FORMAT_L8:
*Pointer<Byte>(d) = Byte(RoundInt(Float(color.x)));
break;
case FORMAT_A8:
*Pointer<Byte>(d) = Byte(RoundInt(Float(color.w)));
break;
case FORMAT_A8R8G8B8:
{ {
case FORMAT_L8: UShort4 c0 = As<UShort4>(RoundShort4(color.zyxw));
case FORMAT_A8: Byte8 c1 = Pack(c0, c0);
case FORMAT_A8R8G8B8: *Pointer<UInt>(d) = UInt(As<Long>(c1));
case FORMAT_A8B8G8R8:
case FORMAT_X8R8G8B8:
case FORMAT_X8B8G8R8:
scale = vector(255, 255, 255, 255);
break;
case FORMAT_A16B16G16R16:
case FORMAT_G16R16:
scale = vector(65535, 65535, 65535, 65535);
break;
case FORMAT_A32B32G32R32F:
case FORMAT_G32R32F:
case FORMAT_R32F:
scale = vector(1.0f, 1.0f, 1.0f, 1.0f);
break;
default:
return false;
} }
break;
if(unscale != scale) case FORMAT_A8B8G8R8:
{ {
color *= Float4(scale.x / unscale.x, scale.y / unscale.y, scale.z / unscale.z, scale.w / unscale.w); UShort4 c0 = As<UShort4>(RoundShort4(color));
Byte8 c1 = Pack(c0, c0);
*Pointer<UInt>(d) = UInt(As<Long>(c1));
} }
break;
if(Surface::isFloatFormat(state.sourceFormat) && !Surface::isFloatFormat(state.destFormat)) case FORMAT_X8R8G8B8:
{ {
color = Min(color, Float4(1.0f, 1.0f, 1.0f, 1.0f)); UShort4 c0 = As<UShort4>(RoundShort4(color.zyxw));
Byte8 c1 = Pack(c0, c0);
color = Max(color, Float4(Surface::isUnsignedComponent(state.destFormat, 0) ? 0.0f : -1.0f, *Pointer<UInt>(d) = UInt(As<Long>(c1)) | 0xFF000000;
Surface::isUnsignedComponent(state.destFormat, 1) ? 0.0f : -1.0f,
Surface::isUnsignedComponent(state.destFormat, 2) ? 0.0f : -1.0f,
Surface::isUnsignedComponent(state.destFormat, 3) ? 0.0f : -1.0f));
} }
break;
Pointer<Byte> d = destLine + i * Surface::bytes(state.destFormat); case FORMAT_X8B8G8R8:
switch(state.destFormat)
{ {
case FORMAT_L8: UShort4 c0 = As<UShort4>(RoundShort4(color));
*Pointer<Byte>(d) = Byte(RoundInt(Float(color.x))); Byte8 c1 = Pack(c0, c0);
break; *Pointer<UInt>(d) = UInt(As<Long>(c1)) | 0xFF000000;
case FORMAT_A8:
*Pointer<Byte>(d) = Byte(RoundInt(Float(color.w)));
break;
case FORMAT_A8R8G8B8:
{
UShort4 c0 = As<UShort4>(RoundShort4(color.zyxw));
Byte8 c1 = Pack(c0, c0);
*Pointer<UInt>(d) = UInt(As<Long>(c1));
}
break;
case FORMAT_A8B8G8R8:
{
UShort4 c0 = As<UShort4>(RoundShort4(color));
Byte8 c1 = Pack(c0, c0);
*Pointer<UInt>(d) = UInt(As<Long>(c1));
}
break;
case FORMAT_X8R8G8B8:
{
UShort4 c0 = As<UShort4>(RoundShort4(color.zyxw));
Byte8 c1 = Pack(c0, c0);
*Pointer<UInt>(d) = UInt(As<Long>(c1)) | 0xFF000000;
}
break;
case FORMAT_X8B8G8R8:
{
UShort4 c0 = As<UShort4>(RoundShort4(color));
Byte8 c1 = Pack(c0, c0);
*Pointer<UInt>(d) = UInt(As<Long>(c1)) | 0xFF000000;
}
break;
case FORMAT_A16B16G16R16:
*Pointer<UShort4>(d) = UShort4(RoundInt(color));
break;
case FORMAT_G16R16:
*Pointer<UInt>(d) = UInt(As<Long>(UShort4(RoundInt(color))));
break;
case FORMAT_A32B32G32R32F:
*Pointer<Float4>(d) = color;
break;
case FORMAT_G32R32F:
*Pointer<Float2>(d) = Float2(color);
break;
case FORMAT_R32F:
*Pointer<Float>(d) = color.x;
break;
default:
return false;
} }
break;
x += w; case FORMAT_A16B16G16R16:
*Pointer<UShort4>(d) = UShort4(RoundInt(color));
break;
case FORMAT_G16R16:
*Pointer<UInt>(d) = UInt(As<Long>(UShort4(RoundInt(color))));
break;
case FORMAT_A32B32G32R32F:
*Pointer<Float4>(d) = color;
break;
case FORMAT_G32R32F:
*Pointer<Float2>(d) = Float2(color);
break;
case FORMAT_R32F:
*Pointer<Float>(d) = color.x;
break;
default:
return false;
} }
y += h; x += w;
} }
y += h;
} }
}
return function(L"BlitRoutine");
}
bool Blitter::blitReactor(Surface *source, const SliceRect &sourceRect, Surface *dest, const SliceRect &destRect, bool filter)
{
Rect dRect = destRect;
Rect sRect = sourceRect;
if(destRect.x0 > destRect.x1)
{
swap(dRect.x0, dRect.x1);
swap(sRect.x0, sRect.x1);
}
if(destRect.y0 > destRect.y1)
{
swap(dRect.y0, dRect.y1);
swap(sRect.y0, sRect.y1);
}
BlitState state;
blitRoutine = function(L"BlitRoutine"); state.sourceFormat = source->getInternalFormat();
state.destFormat = dest->getInternalFormat();
state.filter = filter;
Routine *blitRoutine = blitCache->query(state);
if(!blitRoutine)
{
blitRoutine = generate(state);
if(!blitRoutine)
{
return false;
}
blitCache->add(state, blitRoutine); blitCache->add(state, blitRoutine);
} }
......
...@@ -22,18 +22,6 @@ namespace sw ...@@ -22,18 +22,6 @@ namespace sw
{ {
class Blitter class Blitter
{ {
public:
Blitter();
virtual ~Blitter();
void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
void blit3D(Surface *source, Surface *dest);
private:
bool read(Float4 &color, Pointer<Byte> element, Format format);
bool blitReactor(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
struct BlitState struct BlitState
{ {
bool operator==(const BlitState &state) const bool operator==(const BlitState &state) const
...@@ -67,6 +55,19 @@ namespace sw ...@@ -67,6 +55,19 @@ namespace sw
int sHeight; int sHeight;
}; };
public:
Blitter();
virtual ~Blitter();
void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
void blit3D(Surface *source, Surface *dest);
private:
bool read(Float4 &color, Pointer<Byte> element, Format format);
bool blitReactor(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
Routine *generate(BlitState &state);
RoutineCache<BlitState> *blitCache; RoutineCache<BlitState> *blitCache;
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment