swrasterizer: implement shadow map sampling

This commit is contained in:
wwylele 2018-03-12 19:42:51 +02:00
parent ce2ad7436e
commit ae75d3032f
2 changed files with 41 additions and 5 deletions

View File

@ -158,7 +158,12 @@ struct TexturingRegs {
return address * 8;
}
INSERT_PADDING_WORDS(0x3);
union {
BitField<0, 1, u32> orthographic; // 0: enable perspective divide
BitField<1, 23, u32> bias; // 23-bit fraction
} shadow;
INSERT_PADDING_WORDS(0x2);
BitField<0, 4, TextureFormat> texture0_format;
BitField<0, 1, u32> fragment_lighting_enable;
INSERT_PADDING_WORDS(0x1);

View File

@ -74,8 +74,9 @@ static int SignedArea(const Math::Vec2<Fix12P4>& vtx1, const Math::Vec2<Fix12P4>
};
/// Convert a 3D vector for cube map coordinates to 2D texture coordinates along with the face name
static std::tuple<float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v, float24 w,
const TexturingRegs& regs) {
static std::tuple<float24, float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v,
float24 w,
const TexturingRegs& regs) {
const float abs_u = std::abs(u.ToFloat32());
const float abs_v = std::abs(v.ToFloat32());
const float abs_w = std::abs(w.ToFloat32());
@ -112,8 +113,9 @@ static std::tuple<float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v
x = u;
z = w;
}
float24 z_abs = float24::FromFloat32(std::abs(z.ToFloat32()));
const float24 half = float24::FromFloat32(0.5f);
return std::make_tuple(x / z * half + half, y / z * half + half, addr);
return std::make_tuple(x / z * half + half, y / z * half + half, z_abs, addr);
}
MICROPROFILE_DEFINE(GPU_Rasterization, "GPU", "Rasterization", MP_RGB(50, 50, 240));
@ -331,13 +333,16 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve
// Only unit 0 respects the texturing type (according to 3DBrew)
// TODO: Refactor so cubemaps and shadowmaps can be handled
PAddr texture_address = texture.config.GetPhysicalAddress();
float24 shadow_z;
if (i == 0) {
switch (texture.config.type) {
case TexturingRegs::TextureConfig::Texture2D:
break;
case TexturingRegs::TextureConfig::ShadowCube:
case TexturingRegs::TextureConfig::TextureCube: {
auto w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w);
std::tie(u, v, texture_address) = ConvertCubeCoord(u, v, w, regs.texturing);
std::tie(u, v, shadow_z, texture_address) =
ConvertCubeCoord(u, v, w, regs.texturing);
break;
}
case TexturingRegs::TextureConfig::Projection2D: {
@ -346,6 +351,16 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve
v /= tc0_w;
break;
}
case TexturingRegs::TextureConfig::Shadow2D: {
auto tc0_w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w);
if (!regs.texturing.shadow.orthographic) {
u /= tc0_w;
v /= tc0_w;
}
shadow_z = float24::FromFloat32(std::abs(tc0_w.ToFloat32()));
break;
}
default:
// TODO: Change to LOG_ERROR when more types are handled.
LOG_DEBUG(HW_GPU, "Unhandled texture type %x", (int)texture.config.type);
@ -394,6 +409,22 @@ static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Ve
// TODO: Apply the min and mag filters to the texture
texture_color[i] = Texture::LookupTexture(texture_data, s, t, info);
}
if (i == 0 && (texture.config.type == TexturingRegs::TextureConfig::Shadow2D ||
texture.config.type == TexturingRegs::TextureConfig::ShadowCube)) {
s32 z_int = static_cast<s32>(std::min(shadow_z.ToFloat32(), 1.0f) * 0xFFFFFF);
z_int -= regs.texturing.shadow.bias << 1;
auto& color = texture_color[i];
s32 z_ref = (color.w << 16) | (color.z << 8) | color.y;
u8 density;
if (z_ref >= z_int) {
density = color.x;
} else {
density = 0;
}
texture_color[i] = {density, density, density, density};
}
}
// sample procedural texture