renderer_opengl: Move Frame/FrameMailbox to OpenGL namespace.

This commit is contained in:
bunnei 2020-02-26 18:28:50 -05:00
parent 795893a9a5
commit aef159354c
3 changed files with 42 additions and 77 deletions

View file

@ -12,45 +12,6 @@
namespace Core::Frontend { namespace Core::Frontend {
struct Frame;
/**
* For smooth Vsync rendering, we want to always present the latest frame that the core generates,
* but also make sure that rendering happens at the pace that the frontend dictates. This is a
* helper class that the renderer can define to sync frames between the render thread and the
* presentation thread
*/
class TextureMailbox {
public:
virtual ~TextureMailbox() = default;
/**
* Recreate the render objects attached to this frame with the new specified width/height
*/
virtual void ReloadRenderFrame(Frontend::Frame* frame, u32 width, u32 height) = 0;
/**
* Recreate the presentation objects attached to this frame with the new specified width/height
*/
virtual void ReloadPresentFrame(Frontend::Frame* frame, u32 width, u32 height) = 0;
/**
* Render thread calls this to get an available frame to present
*/
virtual Frontend::Frame* GetRenderFrame() = 0;
/**
* Render thread calls this after draw commands are done to add to the presentation mailbox
*/
virtual void ReleaseRenderFrame(Frame* frame) = 0;
/**
* Presentation thread calls this to get the latest frame available to present. If there is no
* frame available after timeout, returns the previous frame. If there is no previous frame it
* returns nullptr
*/
virtual Frontend::Frame* TryGetPresentFrame(int timeout_ms) = 0;
};
/** /**
* Represents a graphics context that can be used for background computation or drawing. If the * Represents a graphics context that can be used for background computation or drawing. If the
* graphics backend doesn't require the context, then the implementation of these methods can be * graphics backend doesn't require the context, then the implementation of these methods can be
@ -168,8 +129,6 @@ public:
*/ */
void UpdateCurrentFramebufferLayout(unsigned width, unsigned height); void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
std::unique_ptr<TextureMailbox> mailbox;
protected: protected:
EmuWindow(); EmuWindow();
virtual ~EmuWindow(); virtual ~EmuWindow();

View file

@ -22,12 +22,17 @@
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/renderer_opengl.h" #include "video_core/renderer_opengl/renderer_opengl.h"
namespace Core::Frontend { namespace OpenGL {
// If the size of this is too small, it ends up creating a soft cap on FPS as the renderer will have
// to wait on available presentation frames. There doesn't seem to be much of a downside to a larger
// number but 9 swap textures at 60FPS presentation allows for 800% speed so thats probably fine
constexpr std::size_t SWAP_CHAIN_SIZE = 9;
struct Frame { struct Frame {
u32 width{}; /// Width of the frame (to detect resize) u32 width{}; /// Width of the frame (to detect resize)
u32 height{}; /// Height of the frame u32 height{}; /// Height of the frame
bool color_reloaded = false; /// Texture attachment was recreated (ie: resized) bool color_reloaded{}; /// Texture attachment was recreated (ie: resized)
OpenGL::OGLRenderbuffer color{}; /// Buffer shared between the render/present FBO OpenGL::OGLRenderbuffer color{}; /// Buffer shared between the render/present FBO
OpenGL::OGLFramebuffer render{}; /// FBO created on the render thread OpenGL::OGLFramebuffer render{}; /// FBO created on the render thread
OpenGL::OGLFramebuffer present{}; /// FBO created on the present thread OpenGL::OGLFramebuffer present{}; /// FBO created on the present thread
@ -36,40 +41,37 @@ struct Frame {
bool is_srgb{}; /// Framebuffer is sRGB or RGB bool is_srgb{}; /// Framebuffer is sRGB or RGB
}; };
} // namespace Core::Frontend /**
* For smooth Vsync rendering, we want to always present the latest frame that the core generates,
namespace OpenGL { * but also make sure that rendering happens at the pace that the frontend dictates. This is a
* helper class that the renderer uses to sync frames between the render thread and the presentation
// If the size of this is too small, it ends up creating a soft cap on FPS as the renderer will have * thread
// to wait on available presentation frames. There doesn't seem to be much of a downside to a larger */
// number but 9 swap textures at 60FPS presentation allows for 800% speed so thats probably fine class FrameMailbox {
constexpr std::size_t SWAP_CHAIN_SIZE = 9;
class OGLTextureMailbox : public Core::Frontend::TextureMailbox {
public: public:
std::mutex swap_chain_lock; std::mutex swap_chain_lock;
std::condition_variable present_cv; std::condition_variable present_cv;
std::array<Core::Frontend::Frame, SWAP_CHAIN_SIZE> swap_chain{}; std::array<Frame, SWAP_CHAIN_SIZE> swap_chain{};
std::queue<Core::Frontend::Frame*> free_queue; std::queue<Frame*> free_queue;
std::deque<Core::Frontend::Frame*> present_queue; std::deque<Frame*> present_queue;
Core::Frontend::Frame* previous_frame{}; Frame* previous_frame{};
OGLTextureMailbox() { FrameMailbox() {
for (auto& frame : swap_chain) { for (auto& frame : swap_chain) {
free_queue.push(&frame); free_queue.push(&frame);
} }
} }
~OGLTextureMailbox() override { ~FrameMailbox() {
// lock the mutex and clear out the present and free_queues and notify any people who are // lock the mutex and clear out the present and free_queues and notify any people who are
// blocked to prevent deadlock on shutdown // blocked to prevent deadlock on shutdown
std::scoped_lock lock(swap_chain_lock); std::scoped_lock lock(swap_chain_lock);
std::queue<Core::Frontend::Frame*>().swap(free_queue); std::queue<Frame*>().swap(free_queue);
present_queue.clear(); present_queue.clear();
present_cv.notify_all(); present_cv.notify_all();
} }
void ReloadPresentFrame(Core::Frontend::Frame* frame, u32 height, u32 width) override { void ReloadPresentFrame(Frame* frame, u32 height, u32 width) {
frame->present.Release(); frame->present.Release();
frame->present.Create(); frame->present.Create();
GLint previous_draw_fbo{}; GLint previous_draw_fbo{};
@ -84,7 +86,7 @@ public:
frame->color_reloaded = false; frame->color_reloaded = false;
} }
void ReloadRenderFrame(Core::Frontend::Frame* frame, u32 width, u32 height) override { void ReloadRenderFrame(Frame* frame, u32 width, u32 height) {
OpenGLState prev_state = OpenGLState::GetCurState(); OpenGLState prev_state = OpenGLState::GetCurState();
OpenGLState state = OpenGLState::GetCurState(); OpenGLState state = OpenGLState::GetCurState();
@ -103,7 +105,7 @@ public:
state.Apply(); state.Apply();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
frame->color.handle); frame->color.handle);
if (!glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
LOG_CRITICAL(Render_OpenGL, "Failed to recreate render FBO!"); LOG_CRITICAL(Render_OpenGL, "Failed to recreate render FBO!");
} }
prev_state.Apply(); prev_state.Apply();
@ -112,7 +114,7 @@ public:
frame->color_reloaded = true; frame->color_reloaded = true;
} }
Core::Frontend::Frame* GetRenderFrame() override { Frame* GetRenderFrame() {
std::unique_lock<std::mutex> lock(swap_chain_lock); std::unique_lock<std::mutex> lock(swap_chain_lock);
// If theres no free frames, we will reuse the oldest render frame // If theres no free frames, we will reuse the oldest render frame
@ -122,18 +124,18 @@ public:
return frame; return frame;
} }
Core::Frontend::Frame* frame = free_queue.front(); Frame* frame = free_queue.front();
free_queue.pop(); free_queue.pop();
return frame; return frame;
} }
void ReleaseRenderFrame(Core::Frontend::Frame* frame) override { void ReleaseRenderFrame(Frame* frame) {
std::unique_lock<std::mutex> lock(swap_chain_lock); std::unique_lock<std::mutex> lock(swap_chain_lock);
present_queue.push_front(frame); present_queue.push_front(frame);
present_cv.notify_one(); present_cv.notify_one();
} }
Core::Frontend::Frame* TryGetPresentFrame(int timeout_ms) override { Frame* TryGetPresentFrame(int timeout_ms) {
std::unique_lock<std::mutex> lock(swap_chain_lock); std::unique_lock<std::mutex> lock(swap_chain_lock);
// wait for new entries in the present_queue // wait for new entries in the present_queue
present_cv.wait_for(lock, std::chrono::milliseconds(timeout_ms), present_cv.wait_for(lock, std::chrono::milliseconds(timeout_ms),
@ -149,7 +151,7 @@ public:
} }
// the newest entries are pushed to the front of the queue // the newest entries are pushed to the front of the queue
Core::Frontend::Frame* frame = present_queue.front(); Frame* frame = present_queue.front();
present_queue.pop_front(); present_queue.pop_front();
// remove all old entries from the present queue and move them back to the free_queue // remove all old entries from the present queue and move them back to the free_queue
for (auto f : present_queue) { for (auto f : present_queue) {
@ -295,9 +297,8 @@ void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severit
} // Anonymous namespace } // Anonymous namespace
RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system) RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system)
: VideoCore::RendererBase{emu_window}, emu_window{emu_window}, system{system} { : VideoCore::RendererBase{emu_window}, emu_window{emu_window}, system{system},
emu_window.mailbox = std::make_unique<OGLTextureMailbox>(); frame_mailbox{std::make_unique<FrameMailbox>()} {}
}
RendererOpenGL::~RendererOpenGL() = default; RendererOpenGL::~RendererOpenGL() = default;
@ -319,11 +320,11 @@ void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
PrepareRendertarget(framebuffer); PrepareRendertarget(framebuffer);
RenderScreenshot(); RenderScreenshot();
Core::Frontend::Frame* frame; Frame* frame;
{ {
MICROPROFILE_SCOPE(OpenGL_WaitPresent); MICROPROFILE_SCOPE(OpenGL_WaitPresent);
frame = render_window.mailbox->GetRenderFrame(); frame = frame_mailbox->GetRenderFrame();
// Clean up sync objects before drawing // Clean up sync objects before drawing
@ -356,7 +357,7 @@ void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
is_srgb != frame->is_srgb) { is_srgb != frame->is_srgb) {
LOG_DEBUG(Render_OpenGL, "Reloading render frame"); LOG_DEBUG(Render_OpenGL, "Reloading render frame");
is_srgb = frame->is_srgb = screen_info.display_srgb; is_srgb = frame->is_srgb = screen_info.display_srgb;
render_window.mailbox->ReloadRenderFrame(frame, layout.width, layout.height); frame_mailbox->ReloadRenderFrame(frame, layout.width, layout.height);
} }
state.draw.draw_framebuffer = frame->render.handle; state.draw.draw_framebuffer = frame->render.handle;
state.Apply(); state.Apply();
@ -364,7 +365,7 @@ void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
// Create a fence for the frontend to wait on and swap this frame to OffTex // Create a fence for the frontend to wait on and swap this frame to OffTex
frame->render_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); frame->render_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
glFlush(); glFlush();
render_window.mailbox->ReleaseRenderFrame(frame); frame_mailbox->ReleaseRenderFrame(frame);
m_current_frame++; m_current_frame++;
rasterizer->TickFrame(); rasterizer->TickFrame();
} }
@ -615,7 +616,7 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
void RendererOpenGL::TryPresent(int timeout_ms) { void RendererOpenGL::TryPresent(int timeout_ms) {
const auto& layout = render_window.GetFramebufferLayout(); const auto& layout = render_window.GetFramebufferLayout();
auto frame = render_window.mailbox->TryGetPresentFrame(timeout_ms); auto frame = frame_mailbox->TryGetPresentFrame(timeout_ms);
if (!frame) { if (!frame) {
LOG_DEBUG(Render_OpenGL, "TryGetPresentFrame returned no frame to present"); LOG_DEBUG(Render_OpenGL, "TryGetPresentFrame returned no frame to present");
return; return;
@ -628,7 +629,7 @@ void RendererOpenGL::TryPresent(int timeout_ms) {
// Recreate the presentation FBO if the color attachment was changed // Recreate the presentation FBO if the color attachment was changed
if (frame->color_reloaded) { if (frame->color_reloaded) {
LOG_DEBUG(Render_OpenGL, "Reloading present frame"); LOG_DEBUG(Render_OpenGL, "Reloading present frame");
render_window.mailbox->ReloadPresentFrame(frame, layout.width, layout.height); frame_mailbox->ReloadPresentFrame(frame, layout.width, layout.height);
} }
glWaitSync(frame->render_fence, 0, GL_TIMEOUT_IGNORED); glWaitSync(frame->render_fence, 0, GL_TIMEOUT_IGNORED);
// INTEL workaround. // INTEL workaround.

View file

@ -50,6 +50,8 @@ struct PresentationTexture {
OGLTexture texture; OGLTexture texture;
}; };
class FrameMailbox;
class RendererOpenGL final : public VideoCore::RendererBase { class RendererOpenGL final : public VideoCore::RendererBase {
public: public:
explicit RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system); explicit RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system);
@ -111,6 +113,9 @@ private:
/// Represents if the final render frame is sRGB /// Represents if the final render frame is sRGB
bool is_srgb{}; bool is_srgb{};
/// Frame presentation mailbox
std::unique_ptr<FrameMailbox> frame_mailbox;
}; };
} // namespace OpenGL } // namespace OpenGL