| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* OpenGL Synchronization code: |
| 9 |
* either use fence sync objects or glFinish() to prevent the GPU from |
| 10 |
* lagging behind too much. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdlib.h> |
| 14 |
#include <SDL.h> |
| 15 |
|
| 16 |
#include "args.h" |
| 17 |
#include "config.h" |
| 18 |
#include "console.h" |
| 19 |
#include "game.h" |
| 20 |
#include "maths.h" |
| 21 |
#include "multi.h" |
| 22 |
#include "ogl_sync.h" |
| 23 |
#include "timer.h" |
| 24 |
|
| 25 |
namespace dcx { |
| 26 |
|
| 27 |
ogl_sync::~ogl_sync() |
| 28 |
{ |
| 29 |
if (fence) |
| 30 |
con_puts(CON_URGENT, "DXX-Rebirth: OpenGL: fence sync object was never destroyed!"); |
| 31 |
} |
| 32 |
|
| 33 |
void ogl_sync::sync_deleter::operator()(GLsync fence_func) const |
| 34 |
{ |
| 35 |
glDeleteSyncFunc(fence_func); |
| 36 |
} |
| 37 |
|
| 38 |
void ogl_sync::before_swap() |
| 39 |
{ |
| 40 |
if (const auto local_fence = std::move(fence)) |
| 41 |
{ |
| 42 |
/// use a fence sync object to prevent the GPU from queuing up more than one frame |
| 43 |
const auto waitsync{glClientWaitSyncFunc}; |
| 44 |
if (method == SYNC_GL_FENCE_SLEEP) { |
| 45 |
const auto local_wait_timeout{wait_timeout}; |
| 46 |
const auto multiplayer{Game_mode & GM_MULTI}; |
| 47 |
while (waitsync(local_fence.get(), GL_SYNC_FLUSH_COMMANDS_BIT, 0ULL) == GL_TIMEOUT_EXPIRED) { |
| 48 |
if (multiplayer) { |
| 49 |
multi_do_frame(); // during long wait, keep packets flowing |
| 50 |
} |
| 51 |
timer_delay_ms(local_wait_timeout); |
| 52 |
|
| 53 |
} |
| 54 |
} else { |
| 55 |
waitsync(local_fence.get(), GL_SYNC_FLUSH_COMMANDS_BIT, 34000000ULL); |
| 56 |
} |
| 57 |
} else if (method == SYNC_GL_FINISH_BEFORE_SWAP) { |
| 58 |
glFinish(); |
| 59 |
} |
| 60 |
} |
| 61 |
void ogl_sync::after_swap() |
| 62 |
{ |
| 63 |
if (method == SYNC_GL_FENCE || method == SYNC_GL_FENCE_SLEEP ) { |
| 64 |
fence.reset(glFenceSyncFunc(GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 65 |
} else if (method == SYNC_GL_FINISH_AFTER_SWAP) { |
| 66 |
glFinish(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void ogl_sync::init(SyncGLMethod sync_method, int wait) |
| 71 |
{ |
| 72 |
method = sync_method; |
| 73 |
fence = NULL; |
| 74 |
fix a = i2f(wait); |
| 75 |
fix b = i2f(1000); |
| 76 |
wait_timeout = f2i(fixdiv(a, b) * 1000); |
| 77 |
|
| 78 |
bool need_ARB_sync; |
| 79 |
|
| 80 |
switch (method) { |
| 81 |
case SYNC_GL_FENCE: |
| 82 |
case SYNC_GL_FENCE_SLEEP: |
| 83 |
case SYNC_GL_AUTO: |
| 84 |
need_ARB_sync = true; |
| 85 |
break; |
| 86 |
default: |
| 87 |
need_ARB_sync = false; |
| 88 |
} |
| 89 |
|
| 90 |
if (method == SYNC_GL_AUTO) { |
| 91 |
if (!CGameCfg.VSync) { |
| 92 |
con_puts(CON_NORMAL, "DXX-Rebirth: OpenGL: disabling automatic GL sync since VSync is turned off"); |
| 93 |
method = SYNC_GL_NONE; |
| 94 |
need_ARB_sync = false; |
| 95 |
} else if (!ogl_have_ARB_sync) { |
| 96 |
con_puts(CON_NORMAL, "DXX-Rebirth: OpenGL: GL_ARB_sync not available, disabling sync"); |
| 97 |
method = SYNC_GL_NONE; |
| 98 |
need_ARB_sync = false; |
| 99 |
} else { |
| 100 |
method = SYNC_GL_FENCE_SLEEP; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (need_ARB_sync && !ogl_have_ARB_sync) { |
| 105 |
con_puts(CON_URGENT, "DXX-Rebirth: OpenGL: GL_ARB_sync not available, using fallback"); |
| 106 |
method = SYNC_GL_FINISH_BEFORE_SWAP; |
| 107 |
} |
| 108 |
switch(method) { |
| 109 |
case SYNC_GL_FENCE: |
| 110 |
con_puts(CON_VERBOSE, "DXX-Rebirth: OpenGL: using GL_ARB_sync for synchronization (direct)"); |
| 111 |
break; |
| 112 |
case SYNC_GL_FENCE_SLEEP: |
| 113 |
con_printf(CON_VERBOSE, "DXX-Rebirth: OpenGL: using GL_ARB_sync for synchronization with interval %dms", wait); |
| 114 |
break; |
| 115 |
case SYNC_GL_FINISH_AFTER_SWAP: |
| 116 |
con_puts(CON_VERBOSE, "DXX-Rebirth: OpenGL: using glFinish synchronization (method 1: after swap)"); |
| 117 |
break; |
| 118 |
case SYNC_GL_FINISH_BEFORE_SWAP: |
| 119 |
con_puts(CON_VERBOSE, "DXX-Rebirth: OpenGL: using glFinish synchronization (method 2: before swap)"); |
| 120 |
break; |
| 121 |
default: |
| 122 |
con_puts(CON_VERBOSE, "DXX-Rebirth: OpenGL: using no explicit GPU synchronization"); |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void ogl_sync::deinit() |
| 128 |
{ |
| 129 |
fence.reset(); |
| 130 |
} |
| 131 |
|
| 132 |
} |