mirror of
https://github.com/citra-emu/citra.git
synced 2024-11-25 10:40:13 +00:00
Implement w-buffer (#13)
* Pica: Implement W-Buffer in SW rasterizer * OpenGL: Implement W-Buffers and fix depth-mapping
This commit is contained in:
parent
28f64f98f7
commit
c7032066b8
@ -64,8 +64,6 @@ static void InitScreenCoordinates(OutputVertex& vtx)
|
|||||||
viewport.halfsize_y = float24::FromRaw(regs.viewport_size_y);
|
viewport.halfsize_y = float24::FromRaw(regs.viewport_size_y);
|
||||||
viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.viewport_corner.x));
|
viewport.offset_x = float24::FromFloat32(static_cast<float>(regs.viewport_corner.x));
|
||||||
viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.viewport_corner.y));
|
viewport.offset_y = float24::FromFloat32(static_cast<float>(regs.viewport_corner.y));
|
||||||
viewport.zscale = float24::FromRaw(regs.viewport_depth_range);
|
|
||||||
viewport.offset_z = float24::FromRaw(regs.viewport_depth_far_plane);
|
|
||||||
|
|
||||||
float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w;
|
float24 inv_w = float24::FromFloat32(1.f) / vtx.pos.w;
|
||||||
vtx.color *= inv_w;
|
vtx.color *= inv_w;
|
||||||
@ -78,7 +76,7 @@ static void InitScreenCoordinates(OutputVertex& vtx)
|
|||||||
|
|
||||||
vtx.screenpos[0] = (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x;
|
vtx.screenpos[0] = (vtx.pos.x * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_x + viewport.offset_x;
|
||||||
vtx.screenpos[1] = (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y;
|
vtx.screenpos[1] = (vtx.pos.y * inv_w + float24::FromFloat32(1.0)) * viewport.halfsize_y + viewport.offset_y;
|
||||||
vtx.screenpos[2] = viewport.offset_z + vtx.pos.z * inv_w * viewport.zscale;
|
vtx.screenpos[2] = vtx.pos.z * inv_w;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessTriangle(const OutputVertex &v0, const OutputVertex &v1, const OutputVertex &v2) {
|
void ProcessTriangle(const OutputVertex &v0, const OutputVertex &v1, const OutputVertex &v2) {
|
||||||
|
@ -69,7 +69,7 @@ struct Regs {
|
|||||||
INSERT_PADDING_WORDS(0x9);
|
INSERT_PADDING_WORDS(0x9);
|
||||||
|
|
||||||
BitField<0, 24, u32> viewport_depth_range; // float24
|
BitField<0, 24, u32> viewport_depth_range; // float24
|
||||||
BitField<0, 24, u32> viewport_depth_far_plane; // float24
|
BitField<0, 24, u32> viewport_depth_near_plane; // float24
|
||||||
|
|
||||||
BitField<0, 3, u32> vs_output_total;
|
BitField<0, 3, u32> vs_output_total;
|
||||||
|
|
||||||
@ -121,7 +121,20 @@ struct Regs {
|
|||||||
BitField<16, 10, s32> y;
|
BitField<16, 10, s32> y;
|
||||||
} viewport_corner;
|
} viewport_corner;
|
||||||
|
|
||||||
INSERT_PADDING_WORDS(0x17);
|
INSERT_PADDING_WORDS(0x1);
|
||||||
|
|
||||||
|
//TODO: early depth
|
||||||
|
INSERT_PADDING_WORDS(0x1);
|
||||||
|
|
||||||
|
INSERT_PADDING_WORDS(0x2);
|
||||||
|
|
||||||
|
enum DepthBuffering : u32 {
|
||||||
|
WBuffering = 0,
|
||||||
|
ZBuffering = 1,
|
||||||
|
};
|
||||||
|
BitField< 0, 1, DepthBuffering> depthmap_enable;
|
||||||
|
|
||||||
|
INSERT_PADDING_WORDS(0x12);
|
||||||
|
|
||||||
struct TextureConfig {
|
struct TextureConfig {
|
||||||
enum WrapMode : u32 {
|
enum WrapMode : u32 {
|
||||||
@ -1273,10 +1286,11 @@ ASSERT_REG_POSITION(cull_mode, 0x40);
|
|||||||
ASSERT_REG_POSITION(viewport_size_x, 0x41);
|
ASSERT_REG_POSITION(viewport_size_x, 0x41);
|
||||||
ASSERT_REG_POSITION(viewport_size_y, 0x43);
|
ASSERT_REG_POSITION(viewport_size_y, 0x43);
|
||||||
ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
|
ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
|
||||||
ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
|
ASSERT_REG_POSITION(viewport_depth_near_plane, 0x4e);
|
||||||
ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
|
ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
|
||||||
ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
|
ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
|
||||||
ASSERT_REG_POSITION(viewport_corner, 0x68);
|
ASSERT_REG_POSITION(viewport_corner, 0x68);
|
||||||
|
ASSERT_REG_POSITION(depthmap_enable, 0x6D);
|
||||||
ASSERT_REG_POSITION(texture0_enable, 0x80);
|
ASSERT_REG_POSITION(texture0_enable, 0x80);
|
||||||
ASSERT_REG_POSITION(texture0, 0x81);
|
ASSERT_REG_POSITION(texture0, 0x81);
|
||||||
ASSERT_REG_POSITION(texture0_format, 0x8e);
|
ASSERT_REG_POSITION(texture0_format, 0x8e);
|
||||||
|
@ -859,10 +859,30 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format);
|
// interpolated_z = z / w
|
||||||
u32 z = (u32)((v0.screenpos[2].ToFloat32() * w0 +
|
float interpolated_z_over_w = (v0.screenpos[2].ToFloat32() * w0 +
|
||||||
v1.screenpos[2].ToFloat32() * w1 +
|
v1.screenpos[2].ToFloat32() * w1 +
|
||||||
v2.screenpos[2].ToFloat32() * w2) * ((1 << num_bits) - 1) / wsum);
|
v2.screenpos[2].ToFloat32() * w2) / wsum;
|
||||||
|
|
||||||
|
// Not fully accurate. About 3 bits in precision are missing.
|
||||||
|
// Z-Buffer (z / w * scale + offset)
|
||||||
|
float depth_scale = float24::FromRaw(regs.viewport_depth_range).ToFloat32();
|
||||||
|
float depth_offset = float24::FromRaw(regs.viewport_depth_near_plane).ToFloat32();
|
||||||
|
float depth = interpolated_z_over_w * depth_scale + depth_offset;
|
||||||
|
|
||||||
|
// Potentially switch to W-Buffer
|
||||||
|
if (regs.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) {
|
||||||
|
|
||||||
|
// W-Buffer (z * scale + w * offset = (z / w * scale + offset) * w)
|
||||||
|
depth *= interpolated_w_inverse.ToFloat32() * wsum;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clamp the result
|
||||||
|
depth = MathUtil::Clamp(depth, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Convert float to integer
|
||||||
|
unsigned num_bits = Regs::DepthBitsPerPixel(regs.framebuffer.depth_format);
|
||||||
|
u32 z = (u32)(depth * ((1 << num_bits) - 1));
|
||||||
|
|
||||||
if (output_merger.depth_test_enable) {
|
if (output_merger.depth_test_enable) {
|
||||||
u32 ref_z = GetDepth(x >> 4, y >> 4);
|
u32 ref_z = GetDepth(x >> 4, y >> 4);
|
||||||
|
@ -250,10 +250,15 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
|
|||||||
|
|
||||||
// Depth modifiers
|
// Depth modifiers
|
||||||
case PICA_REG_INDEX(viewport_depth_range):
|
case PICA_REG_INDEX(viewport_depth_range):
|
||||||
case PICA_REG_INDEX(viewport_depth_far_plane):
|
case PICA_REG_INDEX(viewport_depth_near_plane):
|
||||||
SyncDepthModifiers();
|
SyncDepthModifiers();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// Depth buffering
|
||||||
|
case PICA_REG_INDEX(depthmap_enable):
|
||||||
|
state.draw.shader_dirty = true;
|
||||||
|
break;
|
||||||
|
|
||||||
// Blending
|
// Blending
|
||||||
case PICA_REG_INDEX(output_merger.alphablend_enable):
|
case PICA_REG_INDEX(output_merger.alphablend_enable):
|
||||||
SyncBlendEnabled();
|
SyncBlendEnabled();
|
||||||
@ -865,9 +870,9 @@ void RasterizerOpenGL::SyncCullMode() {
|
|||||||
|
|
||||||
void RasterizerOpenGL::SyncDepthModifiers() {
|
void RasterizerOpenGL::SyncDepthModifiers() {
|
||||||
float depth_scale = -Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32();
|
float depth_scale = -Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_range).ToFloat32();
|
||||||
float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_far_plane).ToFloat32() / 2.0f;
|
float depth_offset = Pica::float24::FromRaw(Pica::g_state.regs.viewport_depth_near_plane).ToFloat32();
|
||||||
|
|
||||||
// TODO: Implement scale modifier
|
uniform_block_data.data.depth_scale = depth_scale;
|
||||||
uniform_block_data.data.depth_offset = depth_offset;
|
uniform_block_data.data.depth_offset = depth_offset;
|
||||||
uniform_block_data.dirty = true;
|
uniform_block_data.dirty = true;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,8 @@ struct PicaShaderConfig {
|
|||||||
PicaShaderConfig res;
|
PicaShaderConfig res;
|
||||||
const auto& regs = Pica::g_state.regs;
|
const auto& regs = Pica::g_state.regs;
|
||||||
|
|
||||||
|
res.depthmap_enable = regs.depthmap_enable;
|
||||||
|
|
||||||
res.alpha_test_func = regs.output_merger.alpha_test.enable ?
|
res.alpha_test_func = regs.output_merger.alpha_test.enable ?
|
||||||
regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always;
|
regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always;
|
||||||
|
|
||||||
@ -141,6 +143,8 @@ struct PicaShaderConfig {
|
|||||||
return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
|
return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Pica::Regs::DepthBuffering depthmap_enable = Pica::Regs::DepthBuffering::WBuffering;
|
||||||
|
|
||||||
Pica::Regs::CompareFunc alpha_test_func = Pica::Regs::CompareFunc::Never;
|
Pica::Regs::CompareFunc alpha_test_func = Pica::Regs::CompareFunc::Never;
|
||||||
std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
|
std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
|
||||||
u8 combiner_buffer_input = 0;
|
u8 combiner_buffer_input = 0;
|
||||||
@ -303,6 +307,7 @@ private:
|
|||||||
GLvec4 const_color[6];
|
GLvec4 const_color[6];
|
||||||
GLvec4 tev_combiner_buffer_color;
|
GLvec4 tev_combiner_buffer_color;
|
||||||
GLint alphatest_ref;
|
GLint alphatest_ref;
|
||||||
|
GLfloat depth_scale;
|
||||||
GLfloat depth_offset;
|
GLfloat depth_offset;
|
||||||
alignas(16) GLvec3 lighting_global_ambient;
|
alignas(16) GLvec3 lighting_global_ambient;
|
||||||
LightSrc light_src[8];
|
LightSrc light_src[8];
|
||||||
|
@ -525,6 +525,7 @@ layout (std140) uniform shader_data {
|
|||||||
vec4 const_color[NUM_TEV_STAGES];
|
vec4 const_color[NUM_TEV_STAGES];
|
||||||
vec4 tev_combiner_buffer_color;
|
vec4 tev_combiner_buffer_color;
|
||||||
int alphatest_ref;
|
int alphatest_ref;
|
||||||
|
float depth_scale;
|
||||||
float depth_offset;
|
float depth_offset;
|
||||||
vec3 lighting_global_ambient;
|
vec3 lighting_global_ambient;
|
||||||
LightSrc light_src[NUM_LIGHTS];
|
LightSrc light_src[NUM_LIGHTS];
|
||||||
@ -566,7 +567,15 @@ vec4 secondary_fragment_color = vec4(0.0);
|
|||||||
}
|
}
|
||||||
|
|
||||||
out += "color = last_tex_env_out;\n";
|
out += "color = last_tex_env_out;\n";
|
||||||
out += "gl_FragDepth = gl_FragCoord.z + depth_offset;\n}";
|
|
||||||
|
out += "float z_over_w = gl_FragCoord.z * 2.0 - 1.0;\n";
|
||||||
|
out += "float depth = z_over_w * depth_scale + depth_offset;\n";
|
||||||
|
if (config.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) {
|
||||||
|
out += "depth /= gl_FragCoord.w;\n";
|
||||||
|
}
|
||||||
|
out += "gl_FragDepth = depth;\n";
|
||||||
|
|
||||||
|
out += "}";
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user