Skip to content

[RFC] Use DXGI to present OpenGL frames on Windows #94503

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions drivers/gles3/rasterizer_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
#define strcpy strcpy_s
#endif

#ifdef WINDOWS_ENABLED
bool RasterizerGLES3::screen_flipped_y = false;
#endif

bool RasterizerGLES3::gles_over_gl = true;

void RasterizerGLES3::begin_frame(double frame_step) {
Expand Down Expand Up @@ -380,6 +384,12 @@ void RasterizerGLES3::_blit_render_target_to_screen(RID p_render_target, Display
flip_y = false;
}

#ifdef WINDOWS_ENABLED
if (screen_flipped_y) {
flip_y = !flip_y;
}
#endif

GLuint read_fbo = 0;
glGenFramebuffers(1, &read_fbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, read_fbo);
Expand Down Expand Up @@ -476,9 +486,14 @@ void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_c
screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
}

// Flip Y.
screenrect.position.y = win_size.y - screenrect.position.y;
screenrect.size.y = -screenrect.size.y;
#ifdef WINDOWS_ENABLED
if (!screen_flipped_y)
#endif
{
// Flip Y.
screenrect.position.y = win_size.y - screenrect.position.y;
screenrect.size.y = -screenrect.size.y;
}

// Normalize texture coordinates to window size.
screenrect.position /= win_size;
Expand Down
10 changes: 10 additions & 0 deletions drivers/gles3/rasterizer_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class RasterizerGLES3 : public RendererCompositor {
double time_total = 0.0;
bool flip_xy_workaround = false;

#ifdef WINDOWS_ENABLED
static bool screen_flipped_y;
#endif

static bool gles_over_gl;

protected:
Expand Down Expand Up @@ -117,6 +121,12 @@ class RasterizerGLES3 : public RendererCompositor {
low_end = true;
}

#ifdef WINDOWS_ENABLED
static void set_screen_flipped_y(bool p_flipped) {
screen_flipped_y = p_flipped;
}
#endif

_ALWAYS_INLINE_ uint64_t get_frame_number() const { return frame; }
_ALWAYS_INLINE_ double get_frame_delta_time() const { return delta; }
_ALWAYS_INLINE_ double get_total_time() const { return time_total; }
Expand Down
13 changes: 13 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3003,6 +3003,12 @@ String DisplayServerWindows::keyboard_get_layout_name(int p_index) const {
void DisplayServerWindows::process_events() {
ERR_FAIL_COND(!Thread::is_main_thread());

#if defined(GLES3_ENABLED)
if (gl_manager_native) {
gl_manager_native->wait_for_present(get_focused_window());
}
#endif

if (!drop_events) {
joypad->process_joypads();
}
Expand Down Expand Up @@ -5477,6 +5483,11 @@ DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode,
windows.erase(id);
ERR_FAIL_V_MSG(INVALID_WINDOW_ID, "Failed to create an OpenGL window.");
}
if (id == MAIN_WINDOW_ID && gl_manager_native->is_using_dxgi_swap_chain()) {
// When presenting with DXGI the screen FBO is "upside down"
// w.r.t. OpenGL.
RasterizerGLES3::set_screen_flipped_y(true);
}
window_set_vsync_mode(p_vsync_mode, id);
}

Expand Down Expand Up @@ -6025,6 +6036,8 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
if (rendering_driver == "opengl3") {
gl_manager_native = memnew(GLManagerNative_Windows);

gl_manager_native->set_prefer_dxgi_swap_chain(true);

if (gl_manager_native->initialize() != OK) {
memdelete(gl_manager_native);
gl_manager_native = nullptr;
Expand Down
Loading
Loading