From c22816591dc7a3ef46ff66d660aaf9e5836f7b58 Mon Sep 17 00:00:00 2001 From: KojoZero Date: Thu, 23 Apr 2026 20:03:42 -0700 Subject: [PATCH] implemented cursor draw in opengl --- .../host_shaders/opengl_present.frag | 44 ++++++++++++++++++- .../host_shaders/opengl_present.vert | 4 +- .../renderer_opengl/renderer_opengl.cpp | 22 ++++++---- .../renderer_opengl/renderer_opengl.h | 3 ++ 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/video_core/host_shaders/opengl_present.frag b/src/video_core/host_shaders/opengl_present.frag index 3285301c8..067eaecb4 100644 --- a/src/video_core/host_shaders/opengl_present.frag +++ b/src/video_core/host_shaders/opengl_present.frag @@ -11,8 +11,48 @@ layout(binding = 0) uniform sampler2D color_texture; uniform vec4 i_resolution; uniform vec4 o_resolution; +uniform vec2 cursor_pos; +uniform bool cursor_enable; uniform int layer; - +in vec2 pixelUnit; void main() { - color = texture(color_texture, frag_tex_coord); + vec4 pixel = texture(color_texture, frag_tex_coord); + vec2 rfrag_tex_coord = vec2(frag_tex_coord.yx); + //Cursor + if (cursor_enable){ + //Black Outline + if (rfrag_tex_coord.x <= (cursor_pos.x + (2.0*pixelUnit.x)) && + rfrag_tex_coord.x >= (cursor_pos.x - (1.0*pixelUnit.x))) { + if (rfrag_tex_coord.y <= (cursor_pos.y + (5.0*pixelUnit.y)) && + rfrag_tex_coord.y >= (cursor_pos.y - (4.0*pixelUnit.y))) { + pixel = vec4(0.0, 0.0, 0.0, 1.0); + } + } + + if (rfrag_tex_coord.y <= (cursor_pos.y + (2.0*pixelUnit.y)) && + rfrag_tex_coord.y >= (cursor_pos.y - (1.0*pixelUnit.y))) { + if (rfrag_tex_coord.x <= (cursor_pos.x + (5.0*pixelUnit.x)) && + rfrag_tex_coord.x >= (cursor_pos.x - (4.0*pixelUnit.x))) { + pixel = vec4(0.0, 0.0, 0.0, 1.0); + } + } + //White Cross + if (rfrag_tex_coord.x <= (cursor_pos.x + (1.0*pixelUnit.x)) && + rfrag_tex_coord.x >= (cursor_pos.x - (0.0*pixelUnit.x))) { + if (rfrag_tex_coord.y <= (cursor_pos.y + (4.0*pixelUnit.y)) && + rfrag_tex_coord.y >= (cursor_pos.y - (3.0*pixelUnit.y))) { + pixel = vec4(1.0, 1.0, 1.0, 1.0); + } + } + + if (rfrag_tex_coord.y <= (cursor_pos.y + (1.0*pixelUnit.y)) && + rfrag_tex_coord.y >= (cursor_pos.y - (0.0*pixelUnit.y))) { + if (rfrag_tex_coord.x <= (cursor_pos.x + (4.0*pixelUnit.x)) && + rfrag_tex_coord.x >= (cursor_pos.x - (3.0*pixelUnit.x))) { + pixel = vec4(1.0, 1.0, 1.0, 1.0); + } + } + } + + color = vec4(pixel.rgb, 1.0); } diff --git a/src/video_core/host_shaders/opengl_present.vert b/src/video_core/host_shaders/opengl_present.vert index f2a7a0b11..8e00a5be9 100644 --- a/src/video_core/host_shaders/opengl_present.vert +++ b/src/video_core/host_shaders/opengl_present.vert @@ -13,11 +13,13 @@ layout(location = 0) out vec2 frag_tex_coord; // The third row could be used for projection, which we don't need in 2D. It hence is assumed to // implicitly be [0, 0, 1] uniform mat3x2 modelview_matrix; - +out vec2 pixelUnit; void main() { // Multiply input position by the rotscale part of the matrix and then manually translate by // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector // to `vec3(vert_position.xy, 1.0)` gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0); frag_tex_coord = vert_tex_coord; + pixelUnit.x = 1/320.0; + pixelUnit.y = 1/240.0; } diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index c8cb6c000..1274c08dc 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -433,6 +433,8 @@ void RendererOpenGL::ReloadShader(Settings::StereoRenderOption render_3d) { uniform_i_resolution = glGetUniformLocation(shader.handle, "i_resolution"); uniform_o_resolution = glGetUniformLocation(shader.handle, "o_resolution"); uniform_layer = glGetUniformLocation(shader.handle, "layer"); + uniform_cursor_pos = glGetUniformLocation(shader.handle, "cursor_pos"); + uniform_cursor_enable = glGetUniformLocation(shader.handle, "cursor_enable"); attrib_position = glGetAttribLocation(shader.handle, "vert_position"); attrib_tex_coord = glGetAttribLocation(shader.handle, "vert_tex_coord"); } @@ -556,11 +558,13 @@ void RendererOpenGL::DrawSingleScreen(const ScreenInfo& screen_info, float x, fl const u32 scale_factor = GetResolutionScaleFactor(); const GLuint sampler = samplers[Settings::values.filter_mode.GetValue()].handle; - glUniform4f(uniform_i_resolution, static_cast(screen_info.texture.width * scale_factor), - static_cast(screen_info.texture.height * scale_factor), - 1.0f / static_cast(screen_info.texture.width * scale_factor), - 1.0f / static_cast(screen_info.texture.height * scale_factor)); - glUniform4f(uniform_o_resolution, h, w, 1.0f / h, 1.0f / w); + glUniform4f(uniform_i_resolution, static_cast(screen_info.texture.height * scale_factor), + static_cast(screen_info.texture.width * scale_factor), + 1.0f / static_cast(screen_info.texture.height * scale_factor), + 1.0f / static_cast(screen_info.texture.width * scale_factor)); + glUniform4f(uniform_o_resolution, w, h, 1.0f / w, 1.0f / h); + glUniform1i(uniform_cursor_enable, 1); + glUniform2f(uniform_cursor_pos, 159.0f/320.0f, 119.0f/240.0f); state.texture_units[0].texture_2d = screen_info.display_texture; state.texture_units[0].sampler = sampler; state.Apply(); @@ -627,11 +631,11 @@ void RendererOpenGL::DrawSingleScreenStereo(const ScreenInfo& screen_info_l, const u32 scale_factor = GetResolutionScaleFactor(); const GLuint sampler = samplers[Settings::values.filter_mode.GetValue()].handle; glUniform4f(uniform_i_resolution, - static_cast(screen_info_l.texture.width * scale_factor), static_cast(screen_info_l.texture.height * scale_factor), - 1.0f / static_cast(screen_info_l.texture.width * scale_factor), - 1.0f / static_cast(screen_info_l.texture.height * scale_factor)); - glUniform4f(uniform_o_resolution, h, w, 1.0f / h, 1.0f / w); + static_cast(screen_info_l.texture.width * scale_factor), + 1.0f / static_cast(screen_info_l.texture.height * scale_factor), + 1.0f / static_cast(screen_info_l.texture.width * scale_factor)); + glUniform4f(uniform_o_resolution, w, h, 1.0f / w, 1.0f / h); state.texture_units[0].texture_2d = screen_info_l.display_texture; state.texture_units[1].texture_2d = screen_info_r.display_texture; state.texture_units[0].sampler = sampler; diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index 2f2b318ed..264b3c4fe 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -107,6 +107,9 @@ private: GLuint uniform_o_resolution; GLuint uniform_layer; + // Shader uniform for onscreen cursor + GLuint uniform_cursor_pos; + GLuint uniform_cursor_enable; // Shader attribute input indices GLuint attrib_position; GLuint attrib_tex_coord;