mirror of
https://github.com/PabloMK7/citra
synced 2024-11-15 05:08:23 +00:00
OpenGL: Implement fog
This commit is contained in:
parent
ebee2513a9
commit
a12571c709
5 changed files with 124 additions and 7 deletions
|
@ -62,6 +62,8 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
|
|||
uniform_block_data.lut_dirty[index] = true;
|
||||
}
|
||||
|
||||
uniform_block_data.fog_lut_dirty = true;
|
||||
|
||||
// Set vertex attributes
|
||||
glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
|
||||
glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
|
||||
|
@ -102,6 +104,18 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
|
|||
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
}
|
||||
|
||||
// Setup the LUT for the fog
|
||||
{
|
||||
fog_lut.Create();
|
||||
state.fog_lut.texture_1d = fog_lut.handle;
|
||||
}
|
||||
state.Apply();
|
||||
|
||||
glActiveTexture(GL_TEXTURE9);
|
||||
glTexImage1D(GL_TEXTURE_1D, 0, GL_R32UI, 128, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr);
|
||||
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Sync fixed function OpenGL state
|
||||
SyncCullMode();
|
||||
SyncBlendEnabled();
|
||||
|
@ -215,6 +229,12 @@ void RasterizerOpenGL::DrawTriangles() {
|
|||
}
|
||||
}
|
||||
|
||||
// Sync the fog lut
|
||||
if (uniform_block_data.fog_lut_dirty) {
|
||||
SyncFogLUT();
|
||||
uniform_block_data.fog_lut_dirty = false;
|
||||
}
|
||||
|
||||
// Sync the uniform data
|
||||
if (uniform_block_data.dirty) {
|
||||
glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data, GL_STATIC_DRAW);
|
||||
|
@ -280,6 +300,21 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
|
|||
SyncBlendColor();
|
||||
break;
|
||||
|
||||
// Fog state
|
||||
case PICA_REG_INDEX(fog_color):
|
||||
SyncFogColor();
|
||||
break;
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[0], 0xe8):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[1], 0xe9):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[2], 0xea):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[3], 0xeb):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[4], 0xec):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[5], 0xed):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[6], 0xee):
|
||||
case PICA_REG_INDEX_WORKAROUND(fog_lut_data[7], 0xef):
|
||||
uniform_block_data.fog_lut_dirty = true;
|
||||
break;
|
||||
|
||||
// Alpha test
|
||||
case PICA_REG_INDEX(output_merger.alpha_test):
|
||||
SyncAlphaTest();
|
||||
|
@ -329,6 +364,7 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
|
|||
break;
|
||||
|
||||
// TEV stages
|
||||
// (This also syncs fog_mode and fog_flip which are part of tev_combiner_buffer_input)
|
||||
case PICA_REG_INDEX(tev_stage0.color_source1):
|
||||
case PICA_REG_INDEX(tev_stage0.color_modifier1):
|
||||
case PICA_REG_INDEX(tev_stage0.color_op):
|
||||
|
@ -950,9 +986,15 @@ void RasterizerOpenGL::SetShader() {
|
|||
uniform_lut = glGetUniformLocation(shader->shader.handle, "lut[5]");
|
||||
if (uniform_lut != -1) { glUniform1i(uniform_lut, 8); }
|
||||
|
||||
GLuint uniform_fog_lut = glGetUniformLocation(shader->shader.handle, "fog_lut");
|
||||
if (uniform_fog_lut != -1) { glUniform1i(uniform_fog_lut, 9); }
|
||||
|
||||
current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
|
||||
|
||||
unsigned int block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
|
||||
GLint block_size;
|
||||
glGetActiveUniformBlockiv(current_shader->shader.handle, block_index, GL_UNIFORM_BLOCK_DATA_SIZE, &block_size);
|
||||
ASSERT_MSG(block_size == sizeof(UniformData), "Uniform block size did not match!");
|
||||
glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
|
||||
|
||||
// Update uniforms
|
||||
|
@ -974,6 +1016,8 @@ void RasterizerOpenGL::SetShader() {
|
|||
SyncLightDistanceAttenuationBias(light_index);
|
||||
SyncLightDistanceAttenuationScale(light_index);
|
||||
}
|
||||
|
||||
SyncFogColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1040,6 +1084,30 @@ void RasterizerOpenGL::SyncBlendColor() {
|
|||
state.blend.color.alpha = blend_color[3];
|
||||
}
|
||||
|
||||
void RasterizerOpenGL::SyncFogColor() {
|
||||
const auto& regs = Pica::g_state.regs;
|
||||
uniform_block_data.data.fog_color = {
|
||||
regs.fog_color.r.Value() / 255.0f,
|
||||
regs.fog_color.g.Value() / 255.0f,
|
||||
regs.fog_color.b.Value() / 255.0f
|
||||
};
|
||||
uniform_block_data.dirty = true;
|
||||
}
|
||||
|
||||
void RasterizerOpenGL::SyncFogLUT() {
|
||||
std::array<GLuint, 128> new_data;
|
||||
|
||||
std::transform(Pica::g_state.fog.lut.begin(), Pica::g_state.fog.lut.end(), new_data.begin(), [](const auto& entry) {
|
||||
return entry.raw;
|
||||
});
|
||||
|
||||
if (new_data != fog_lut_data) {
|
||||
fog_lut_data = new_data;
|
||||
glActiveTexture(GL_TEXTURE9);
|
||||
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RED_INTEGER, GL_UNSIGNED_INT, fog_lut_data.data());
|
||||
}
|
||||
}
|
||||
|
||||
void RasterizerOpenGL::SyncAlphaTest() {
|
||||
const auto& regs = Pica::g_state.regs;
|
||||
if (regs.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
|
||||
|
|
|
@ -76,6 +76,9 @@ union PicaShaderConfig {
|
|||
state.tev_stages[i].scales_raw = tev_stage.scales_raw;
|
||||
}
|
||||
|
||||
state.fog_mode = regs.fog_mode;
|
||||
state.fog_flip = regs.fog_flip;
|
||||
|
||||
state.combiner_buffer_input =
|
||||
regs.tev_combiner_buffer_input.update_mask_rgb.Value() |
|
||||
regs.tev_combiner_buffer_input.update_mask_a.Value() << 4;
|
||||
|
@ -168,13 +171,14 @@ union PicaShaderConfig {
|
|||
};
|
||||
|
||||
struct State {
|
||||
|
||||
Pica::Regs::CompareFunc alpha_test_func;
|
||||
Pica::Regs::TextureConfig::TextureType texture0_type;
|
||||
std::array<TevStageConfigRaw, 6> tev_stages;
|
||||
u8 combiner_buffer_input;
|
||||
|
||||
Pica::Regs::DepthBuffering depthmap_enable;
|
||||
Pica::Regs::FogMode fog_mode;
|
||||
bool fog_flip;
|
||||
|
||||
struct {
|
||||
struct {
|
||||
|
@ -324,13 +328,14 @@ private:
|
|||
GLint alphatest_ref;
|
||||
GLfloat depth_scale;
|
||||
GLfloat depth_offset;
|
||||
alignas(16) GLvec3 fog_color;
|
||||
alignas(16) GLvec3 lighting_global_ambient;
|
||||
LightSrc light_src[8];
|
||||
alignas(16) GLvec4 const_color[6]; // A vec4 color for each of the six tev stages
|
||||
alignas(16) GLvec4 tev_combiner_buffer_color;
|
||||
};
|
||||
|
||||
static_assert(sizeof(UniformData) == 0x390, "The size of the UniformData structure has changed, update the structure in the shader");
|
||||
static_assert(sizeof(UniformData) == 0x3A0, "The size of the UniformData structure has changed, update the structure in the shader");
|
||||
static_assert(sizeof(UniformData) < 16384, "UniformData structure must be less than 16kb as per the OpenGL spec");
|
||||
|
||||
/// Sets the OpenGL shader in accordance with the current PICA register state
|
||||
|
@ -354,6 +359,10 @@ private:
|
|||
/// Syncs the blend color to match the PICA register
|
||||
void SyncBlendColor();
|
||||
|
||||
/// Syncs the fog states to match the PICA register
|
||||
void SyncFogColor();
|
||||
void SyncFogLUT();
|
||||
|
||||
/// Syncs the alpha test states to match the PICA register
|
||||
void SyncAlphaTest();
|
||||
|
||||
|
@ -421,6 +430,7 @@ private:
|
|||
struct {
|
||||
UniformData data;
|
||||
bool lut_dirty[6];
|
||||
bool fog_lut_dirty;
|
||||
bool dirty;
|
||||
} uniform_block_data = {};
|
||||
|
||||
|
@ -432,4 +442,7 @@ private:
|
|||
|
||||
std::array<OGLTexture, 6> lighting_luts;
|
||||
std::array<std::array<GLvec4, 256>, 6> lighting_lut_data{};
|
||||
|
||||
OGLTexture fog_lut;
|
||||
std::array<GLuint, 128> fog_lut_data{};
|
||||
};
|
||||
|
|
|
@ -555,6 +555,7 @@ layout (std140) uniform shader_data {
|
|||
int alphatest_ref;
|
||||
float depth_scale;
|
||||
float depth_offset;
|
||||
vec3 fog_color;
|
||||
vec3 lighting_global_ambient;
|
||||
LightSrc light_src[NUM_LIGHTS];
|
||||
vec4 const_color[NUM_TEV_STAGES];
|
||||
|
@ -563,6 +564,7 @@ layout (std140) uniform shader_data {
|
|||
|
||||
uniform sampler2D tex[3];
|
||||
uniform sampler1D lut[6];
|
||||
uniform usampler1D fog_lut;
|
||||
|
||||
// Rotate the vector v by the quaternion q
|
||||
vec3 quaternion_rotate(vec4 q, vec3 v) {
|
||||
|
@ -580,6 +582,12 @@ vec4 secondary_fragment_color = vec4(0.0);
|
|||
return out;
|
||||
}
|
||||
|
||||
out += "float z_over_w = 1.0 - gl_FragCoord.z * 2.0;\n";
|
||||
out += "float depth = z_over_w * depth_scale + depth_offset;\n";
|
||||
if (state.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) {
|
||||
out += "depth /= gl_FragCoord.w;\n";
|
||||
}
|
||||
|
||||
if (state.lighting.enable)
|
||||
WriteLighting(out, config);
|
||||
|
||||
|
@ -596,14 +604,30 @@ vec4 secondary_fragment_color = vec4(0.0);
|
|||
out += ") discard;\n";
|
||||
}
|
||||
|
||||
out += "color = last_tex_env_out;\n";
|
||||
// Append fog combiner
|
||||
if (state.fog_mode == Regs::FogMode::Fog) {
|
||||
// Get index into fog LUT
|
||||
if (state.fog_flip) {
|
||||
out += "float fog_index = (1.0 - depth) * 128.0;\n";
|
||||
} else {
|
||||
out += "float fog_index = depth * 128.0;\n";
|
||||
}
|
||||
|
||||
out += "float z_over_w = 1.0 - gl_FragCoord.z * 2.0;\n";
|
||||
out += "float depth = z_over_w * depth_scale + depth_offset;\n";
|
||||
if (state.depthmap_enable == Pica::Regs::DepthBuffering::WBuffering) {
|
||||
out += "depth /= gl_FragCoord.w;\n";
|
||||
// Generate clamped fog factor from LUT for given fog index
|
||||
out += "float fog_i = clamp(floor(fog_index), 0.0, 127.0);\n";
|
||||
out += "float fog_f = fog_index - fog_i;\n";
|
||||
out += "uint fog_lut_entry = texelFetch(fog_lut, int(fog_i), 0).r;\n";
|
||||
out += "float fog_lut_entry_difference = float(int((fog_lut_entry & 0x1FFFU) << 19U) >> 19);\n"; // Extract signed difference
|
||||
out += "float fog_lut_entry_value = float((fog_lut_entry >> 13U) & 0x7FFU);\n";
|
||||
out += "float fog_factor = (fog_lut_entry_value + fog_lut_entry_difference * fog_f) / 2047.0;\n";
|
||||
out += "fog_factor = clamp(fog_factor, 0.0, 1.0);\n";
|
||||
|
||||
// Blend the fog
|
||||
out += "last_tex_env_out.rgb = mix(fog_color.rgb, last_tex_env_out.rgb, fog_factor);\n";
|
||||
}
|
||||
|
||||
out += "gl_FragDepth = depth;\n";
|
||||
out += "color = last_tex_env_out;\n";
|
||||
|
||||
out += "}";
|
||||
|
||||
|
|
|
@ -58,6 +58,8 @@ OpenGLState::OpenGLState() {
|
|||
lut.texture_1d = 0;
|
||||
}
|
||||
|
||||
fog_lut.texture_1d = 0;
|
||||
|
||||
draw.read_framebuffer = 0;
|
||||
draw.draw_framebuffer = 0;
|
||||
draw.vertex_array = 0;
|
||||
|
@ -195,6 +197,12 @@ void OpenGLState::Apply() const {
|
|||
}
|
||||
}
|
||||
|
||||
// Fog LUT
|
||||
if (fog_lut.texture_1d != cur_state.fog_lut.texture_1d) {
|
||||
glActiveTexture(GL_TEXTURE9);
|
||||
glBindTexture(GL_TEXTURE_1D, fog_lut.texture_1d);
|
||||
}
|
||||
|
||||
// Framebuffer
|
||||
if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
|
||||
|
|
|
@ -67,6 +67,10 @@ public:
|
|||
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
|
||||
} lighting_luts[6];
|
||||
|
||||
struct {
|
||||
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
|
||||
} fog_lut;
|
||||
|
||||
struct {
|
||||
GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
|
||||
GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
|
||||
|
|
Loading…
Reference in a new issue