Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gl/vk: Improvements to overlay rendering #5633

Merged
merged 1 commit into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 51 additions & 8 deletions rpcs3/Emu/RSX/GL/GLOverlays.h
Expand Up @@ -18,6 +18,7 @@ namespace gl
gl::glsl::shader fs;

gl::fbo fbo;
gl::sampler_state m_sampler;

gl::vao m_vao;
gl::buffer m_vertex_data_buffer;
Expand All @@ -26,6 +27,29 @@ namespace gl

u32 num_drawable_elements = 4;
GLenum primitives = GL_TRIANGLE_STRIP;
GLenum input_filter = GL_NEAREST;

struct saved_sampler_state
{
GLuint saved = GL_NONE;
GLuint unit = 0;

saved_sampler_state(GLuint _unit, const gl::sampler_state& sampler)
{
glActiveTexture(GL_TEXTURE0 + _unit);
glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&saved);

unit = _unit;
sampler.bind(_unit);
}

saved_sampler_state(const saved_sampler_state&) = delete;

~saved_sampler_state()
{
glBindSampler(unit, saved);
}
};

void create()
{
Expand All @@ -46,6 +70,9 @@ namespace gl

fbo.create();

m_sampler.create();
m_sampler.apply_defaults(input_filter);

m_vertex_data_buffer.create();

int old_vao;
Expand Down Expand Up @@ -76,6 +103,8 @@ namespace gl
m_vao.remove();
m_vertex_data_buffer.remove();

m_sampler.remove();

compiled = false;
}
}
Expand Down Expand Up @@ -283,7 +312,7 @@ namespace gl

program_handle.uniforms["tex_scale"] = color2f(src_ratio_x, src_ratio_y);

glActiveTexture(GL_TEXTURE31);
saved_sampler_state saved(31, m_sampler);
glBindTexture(GL_TEXTURE_2D, source->id());

overlay_pass::run(dst_area.x2, dst_area.y2, target->id(), true);
Expand Down Expand Up @@ -322,7 +351,7 @@ namespace gl

void run(u16 w, u16 h, GLuint target, GLuint source)
{
glActiveTexture(GL_TEXTURE31);
saved_sampler_state saved(31, m_sampler);
glBindTexture(GL_TEXTURE_2D, source);

overlay_pass::run(w, h, target, false);
Expand All @@ -346,19 +375,26 @@ namespace gl
"#version 420\n\n"
"layout(location=0) in vec4 in_pos;\n"
"layout(location=0) out vec2 tc0;\n"
"layout(location=1) out vec4 clip_rect;\n"
"layout(location=1) flat out vec4 clip_rect;\n"
"uniform vec4 ui_scale;\n"
"uniform vec2 viewport;\n"
"uniform vec4 clip_bounds;\n"
"\n"
"vec2 snap_to_grid(vec2 normalized)\n"
"{\n"
" return (floor(normalized * viewport) + 0.5) / viewport;\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" tc0.xy = in_pos.zw;\n"
" clip_rect = clip_bounds;\n"
" clip_rect.yw = ui_scale.yy - clip_rect.wy; // Invert y axis\n"
" clip_rect *= (ui_scale.zwzw * viewport.xyxy) / ui_scale.xyxy; // Normalize and convert to window coords\n"
" vec4 pos = vec4((in_pos.xy * ui_scale.zw) / ui_scale.xy, 0., 1.);\n"
" pos.y = (1. - pos.y); //invert y axis\n"
" vec2 window_coord = (in_pos.xy * ui_scale.zw) / ui_scale.xy;\n"
" window_coord = snap_to_grid(window_coord); // Half-integer offset\n"
" window_coord.y = (1. - window_coord.y); // Invert y axis\n"
" vec4 pos = vec4(window_coord, 0., 1.);\n"
" gl_Position = (pos + pos) - 1.;\n"
"}\n"
};
Expand All @@ -368,7 +404,7 @@ namespace gl
"#version 420\n\n"
"layout(binding=31) uniform sampler2D fs0;\n"
"layout(location=0) in vec2 tc0;\n"
"layout(location=1) in vec4 clip_rect;\n"
"layout(location=1) flat in vec4 clip_rect;\n"
"layout(location=0) out vec4 ocol;\n"
"uniform vec4 color;\n"
"uniform float time;\n"
Expand Down Expand Up @@ -448,6 +484,9 @@ namespace gl
" ocol = diff_color;\n"
"}\n"
};

// Smooth filtering required for inputs
input_filter = GL_LINEAR;
}

gl::texture_view* load_simple_image(rsx::overlays::image_info* desc, bool temp_resource, u32 owner_uid)
Expand Down Expand Up @@ -586,14 +625,16 @@ namespace gl
program_handle.uniforms["viewport"] = color2f(f32(w), f32(h));
program_handle.uniforms["ui_scale"] = color4f((f32)ui.virtual_width, (f32)ui.virtual_height, 1.f, 1.f);
program_handle.uniforms["time"] = (f32)(get_system_time() / 1000) * 0.005f;

saved_sampler_state saved(31, m_sampler);

for (auto &cmd : ui.get_compiled().draw_commands)
{
upload_vertex_data((f32*)cmd.verts.data(), (u32)cmd.verts.size() * 4u);
num_drawable_elements = (u32)cmd.verts.size();
is_font_draw = false;
GLint texture_exists = GL_TRUE;

glActiveTexture(GL_TEXTURE31);
switch (cmd.config.texture_ref)
{
case rsx::overlays::image_resource_id::game_icon:
Expand Down Expand Up @@ -679,6 +720,8 @@ namespace gl
" ocol = color;\n"
"}\n"
};

input_filter = GL_LINEAR;
}

void run(u16 w, u16 h, GLuint source, const areai& region, f32 gamma, bool limited_rgb)
Expand All @@ -695,7 +738,7 @@ namespace gl
program_handle.uniforms["gamma"] = gamma;
program_handle.uniforms["limit_range"] = (int)limited_rgb;

glActiveTexture(GL_TEXTURE31);
saved_sampler_state saved(31, m_sampler);
glBindTexture(GL_TEXTURE_2D, source);
overlay_pass::run(w, h, GL_NONE, false, false);
}
Expand Down
6 changes: 3 additions & 3 deletions rpcs3/Emu/RSX/GL/GLTexture.cpp
Expand Up @@ -290,13 +290,13 @@ namespace gl
glSamplerParameteri(samplerHandle, GL_TEXTURE_COMPARE_MODE, GL_NONE);
}

void sampler_state::apply_defaults()
void sampler_state::apply_defaults(GLenum default_filter)
{
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_T, GL_REPEAT);
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_R, GL_REPEAT);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MIN_FILTER, default_filter);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MAG_FILTER, default_filter);
glSamplerParameterf(samplerHandle, GL_TEXTURE_LOD_BIAS, 0.f);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MIN_LOD, 0);
glSamplerParameteri(samplerHandle, GL_TEXTURE_MAX_LOD, 0);
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/RSX/GL/GLTexture.h
Expand Up @@ -49,14 +49,14 @@ namespace gl
glDeleteSamplers(1, &samplerHandle);
}

void bind(int index)
void bind(int index) const
{
glBindSampler(index, samplerHandle);
}

void apply(const rsx::fragment_texture& tex, const rsx::sampled_image_descriptor_base* sampled_image);
void apply(const rsx::vertex_texture& tex, const rsx::sampled_image_descriptor_base* sampled_image);

void apply_defaults();
void apply_defaults(GLenum default_filter = GL_NEAREST);
};
}
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/Overlays/overlay_controls.h
Expand Up @@ -248,7 +248,7 @@ namespace rsx
return{};

stbtt_aligned_quad quad;
stbtt_GetPackedQuad(pack_info.data(), width, height, u8(c), &x_advance, &y_advance, &quad, true);
stbtt_GetPackedQuad(pack_info.data(), width, height, u8(c), &x_advance, &y_advance, &quad, false);
return quad;
}

Expand Down
6 changes: 6 additions & 0 deletions rpcs3/Emu/RSX/VK/VKOverlays.h
Expand Up @@ -494,6 +494,11 @@ namespace vk
"layout(location=3) out vec4 clip_rect;\n"
"layout(location=4) out vec4 parameters2;\n"
"\n"
"vec2 snap_to_grid(vec2 normalized)\n"
"{\n"
" return (floor(normalized * regs[5].xy) + 0.5) / regs[5].xy;\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" tc0.xy = in_pos.zw;\n"
Expand All @@ -503,6 +508,7 @@ namespace vk
" clip_rect = (regs[3] * regs[0].zwzw) / regs[0].xyxy; // Normalized coords\n"
" clip_rect *= regs[5].xyxy; // Window coords\n"
" vec4 pos = vec4((in_pos.xy * regs[0].zw) / regs[0].xy, 0.5, 1.);\n"
" pos.xy = snap_to_grid(pos.xy);\n"
" gl_Position = (pos + pos) - 1.;\n"
"}\n"
};
Expand Down