Moving graphics context creation around to hide actual creation.
Makes it easier to support APIs that don't have a concept of sharing.
This commit is contained in:
@@ -58,24 +58,20 @@ void FatalGLError(std::string error) {
|
||||
"of supported GPUs.");
|
||||
}
|
||||
|
||||
std::unique_ptr<GLContext> GLContext::Create(Window* target_window,
|
||||
std::unique_ptr<GLContext> GLContext::Create(GraphicsProvider* provider,
|
||||
Window* target_window,
|
||||
GLContext* share_context) {
|
||||
auto context = std::unique_ptr<GLContext>(new GLContext(target_window));
|
||||
if (!context->Initialize(target_window, share_context)) {
|
||||
auto context =
|
||||
std::unique_ptr<GLContext>(new GLContext(provider, target_window));
|
||||
if (!context->Initialize(share_context)) {
|
||||
return nullptr;
|
||||
}
|
||||
context->AssertExtensionsPresent();
|
||||
return context;
|
||||
}
|
||||
|
||||
GLContext::GLContext(Window* target_window) : GraphicsContext(target_window) {
|
||||
glew_context_.reset(new GLEWContext());
|
||||
wglew_context_.reset(new WGLEWContext());
|
||||
}
|
||||
|
||||
GLContext::GLContext(Window* target_window, HGLRC glrc)
|
||||
: GraphicsContext(target_window), glrc_(glrc) {
|
||||
dc_ = GetDC(HWND(target_window_->native_handle()));
|
||||
GLContext::GLContext(GraphicsProvider* provider, Window* target_window)
|
||||
: GraphicsContext(provider, target_window) {
|
||||
glew_context_.reset(new GLEWContext());
|
||||
wglew_context_.reset(new WGLEWContext());
|
||||
}
|
||||
@@ -93,8 +89,7 @@ GLContext::~GLContext() {
|
||||
}
|
||||
}
|
||||
|
||||
bool GLContext::Initialize(Window* target_window, GLContext* share_context) {
|
||||
target_window_ = target_window;
|
||||
bool GLContext::Initialize(GLContext* share_context) {
|
||||
dc_ = GetDC(HWND(target_window_->native_handle()));
|
||||
|
||||
PIXELFORMATDESCRIPTOR pfd = {0};
|
||||
@@ -193,12 +188,13 @@ bool GLContext::Initialize(Window* target_window, GLContext* share_context) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<GraphicsContext> GLContext::CreateShared() {
|
||||
assert_not_null(glrc_);
|
||||
std::unique_ptr<GLContext> GLContext::CreateOffscreen(
|
||||
GraphicsProvider* provider, GLContext* parent_context) {
|
||||
assert_not_null(parent_context->glrc_);
|
||||
|
||||
HGLRC new_glrc = nullptr;
|
||||
{
|
||||
GraphicsContextLock context_lock(this);
|
||||
GraphicsContextLock context_lock(parent_context);
|
||||
|
||||
int context_flags = 0;
|
||||
if (FLAGS_gl_debug) {
|
||||
@@ -214,15 +210,19 @@ std::unique_ptr<GraphicsContext> GLContext::CreateShared() {
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB,
|
||||
WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
||||
0};
|
||||
new_glrc = wglCreateContextAttribsARB(dc_, glrc_, attrib_list);
|
||||
new_glrc = wglCreateContextAttribsARB(parent_context->dc_,
|
||||
parent_context->glrc_, attrib_list);
|
||||
if (!new_glrc) {
|
||||
FatalGLError("Could not create shared context.");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
auto new_context =
|
||||
std::unique_ptr<GLContext>(new GLContext(target_window_, new_glrc));
|
||||
auto new_context = std::unique_ptr<GLContext>(
|
||||
new GLContext(provider, parent_context->target_window_));
|
||||
new_context->glrc_ = new_glrc;
|
||||
new_context->dc_ =
|
||||
GetDC(HWND(parent_context->target_window_->native_handle()));
|
||||
if (!new_context->MakeCurrent()) {
|
||||
FatalGLError("Could not make new GL context current.");
|
||||
return nullptr;
|
||||
@@ -244,18 +244,16 @@ std::unique_ptr<GraphicsContext> GLContext::CreateShared() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SetupDebugging();
|
||||
new_context->SetupDebugging();
|
||||
|
||||
if (!new_context->blitter_.Initialize()) {
|
||||
FatalGLError("Unable to initialize blitter on shared context.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
new_context->immediate_drawer_ = std::make_unique<GLImmediateDrawer>(this);
|
||||
|
||||
new_context->ClearCurrent();
|
||||
|
||||
return std::unique_ptr<GraphicsContext>(new_context.release());
|
||||
return new_context;
|
||||
}
|
||||
|
||||
void GLContext::AssertExtensionsPresent() {
|
||||
|
||||
@@ -29,18 +29,14 @@ namespace ui {
|
||||
namespace gl {
|
||||
|
||||
class GLImmediateDrawer;
|
||||
class GLProvider;
|
||||
|
||||
class GLContext : public GraphicsContext {
|
||||
public:
|
||||
static std::unique_ptr<GLContext> Create(Window* target_window,
|
||||
GLContext* share_context = nullptr);
|
||||
|
||||
~GLContext() override;
|
||||
|
||||
HDC dc() const { return dc_; }
|
||||
|
||||
std::unique_ptr<GraphicsContext> CreateShared() override;
|
||||
|
||||
ImmediateDrawer* immediate_drawer() override;
|
||||
|
||||
bool is_current() override;
|
||||
@@ -53,10 +49,18 @@ class GLContext : public GraphicsContext {
|
||||
Blitter* blitter() { return &blitter_; }
|
||||
|
||||
private:
|
||||
explicit GLContext(Window* target_window);
|
||||
GLContext(Window* target_window, HGLRC glrc);
|
||||
friend class GLProvider;
|
||||
|
||||
bool Initialize(Window* target_window, GLContext* share_context);
|
||||
static std::unique_ptr<GLContext> Create(GraphicsProvider* provider,
|
||||
Window* target_window,
|
||||
GLContext* share_context = nullptr);
|
||||
static std::unique_ptr<GLContext> CreateOffscreen(GraphicsProvider* provider,
|
||||
GLContext* parent_context);
|
||||
|
||||
private:
|
||||
GLContext(GraphicsProvider* provider, Window* target_window);
|
||||
|
||||
bool Initialize(GLContext* share_context);
|
||||
void AssertExtensionsPresent();
|
||||
|
||||
void SetupDebugging();
|
||||
|
||||
49
src/xenia/ui/gl/gl_provider.cc
Normal file
49
src/xenia/ui/gl/gl_provider.cc
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "xenia/ui/gl/gl_provider.h"
|
||||
|
||||
#include "xenia/ui/gl/gl_context.h"
|
||||
#include "xenia/ui/window.h"
|
||||
|
||||
namespace xe {
|
||||
namespace ui {
|
||||
namespace gl {
|
||||
|
||||
std::unique_ptr<GraphicsProvider> GLProvider::Create(Window* main_window) {
|
||||
std::unique_ptr<GLProvider> provider(new GLProvider(main_window));
|
||||
|
||||
//
|
||||
|
||||
return std::unique_ptr<GraphicsProvider>(provider.release());
|
||||
}
|
||||
|
||||
GLProvider::GLProvider(Window* main_window) : GraphicsProvider(main_window) {}
|
||||
|
||||
GLProvider::~GLProvider() = default;
|
||||
|
||||
std::unique_ptr<GraphicsContext> GLProvider::CreateContext(
|
||||
Window* target_window) {
|
||||
auto share_context = main_window_->context();
|
||||
return std::unique_ptr<GraphicsContext>(
|
||||
GLContext::Create(this, target_window,
|
||||
static_cast<GLContext*>(share_context))
|
||||
.release());
|
||||
}
|
||||
|
||||
std::unique_ptr<GraphicsContext> GLProvider::CreateOffscreenContext() {
|
||||
auto share_context = main_window_->context();
|
||||
return std::unique_ptr<GraphicsContext>(
|
||||
GLContext::CreateOffscreen(this, static_cast<GLContext*>(share_context))
|
||||
.release());
|
||||
}
|
||||
|
||||
} // namespace gl
|
||||
} // namespace ui
|
||||
} // namespace xe
|
||||
40
src/xenia/ui/gl/gl_provider.h
Normal file
40
src/xenia/ui/gl/gl_provider.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_UI_GL_GL_PROVIDER_H_
|
||||
#define XENIA_UI_GL_GL_PROVIDER_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "xenia/ui/graphics_provider.h"
|
||||
|
||||
namespace xe {
|
||||
namespace ui {
|
||||
namespace gl {
|
||||
|
||||
class GLProvider : public GraphicsProvider {
|
||||
public:
|
||||
~GLProvider() override;
|
||||
|
||||
static std::unique_ptr<GraphicsProvider> Create(Window* main_window);
|
||||
|
||||
std::unique_ptr<GraphicsContext> CreateContext(
|
||||
Window* target_window) override;
|
||||
|
||||
std::unique_ptr<GraphicsContext> CreateOffscreenContext() override;
|
||||
|
||||
protected:
|
||||
explicit GLProvider(Window* main_window);
|
||||
};
|
||||
|
||||
} // namespace gl
|
||||
} // namespace ui
|
||||
} // namespace xe
|
||||
|
||||
#endif // XENIA_UI_GL_GL_PROVIDER_H_
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "xenia/base/main.h"
|
||||
#include "xenia/ui/gl/gl_context.h"
|
||||
#include "xenia/ui/gl/gl_provider.h"
|
||||
#include "xenia/ui/window.h"
|
||||
|
||||
namespace xe {
|
||||
@@ -19,8 +19,8 @@ namespace ui {
|
||||
|
||||
int window_demo_main(const std::vector<std::wstring>& args);
|
||||
|
||||
std::unique_ptr<GraphicsContext> CreateDemoContext(Window* window) {
|
||||
return xe::ui::gl::GLContext::Create(window);
|
||||
std::unique_ptr<GraphicsProvider> CreateDemoGraphicsProvider(Window* window) {
|
||||
return xe::ui::gl::GLProvider::Create(window);
|
||||
}
|
||||
|
||||
} // namespace ui
|
||||
|
||||
@@ -9,11 +9,14 @@
|
||||
|
||||
#include "xenia/ui/graphics_context.h"
|
||||
|
||||
#include "xenia/ui/graphics_provider.h"
|
||||
|
||||
namespace xe {
|
||||
namespace ui {
|
||||
|
||||
GraphicsContext::GraphicsContext(Window* target_window)
|
||||
: target_window_(target_window) {}
|
||||
GraphicsContext::GraphicsContext(GraphicsProvider* provider,
|
||||
Window* target_window)
|
||||
: provider_(provider), target_window_(target_window) {}
|
||||
|
||||
GraphicsContext::~GraphicsContext() = default;
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
namespace xe {
|
||||
namespace ui {
|
||||
|
||||
class GraphicsProvider;
|
||||
class ImmediateDrawer;
|
||||
class Window;
|
||||
|
||||
@@ -22,9 +23,9 @@ class GraphicsContext {
|
||||
public:
|
||||
virtual ~GraphicsContext();
|
||||
|
||||
GraphicsProvider* provider() const { return provider_; }
|
||||
Window* target_window() const { return target_window_; }
|
||||
|
||||
virtual std::unique_ptr<GraphicsContext> CreateShared() = 0;
|
||||
bool is_offscreen() { return immediate_drawer() == nullptr; }
|
||||
|
||||
virtual ImmediateDrawer* immediate_drawer() = 0;
|
||||
|
||||
@@ -36,8 +37,9 @@ class GraphicsContext {
|
||||
virtual void EndSwap() = 0;
|
||||
|
||||
protected:
|
||||
explicit GraphicsContext(Window* target_window);
|
||||
explicit GraphicsContext(GraphicsProvider* provider, Window* target_window);
|
||||
|
||||
GraphicsProvider* provider_ = nullptr;
|
||||
Window* target_window_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
48
src/xenia/ui/graphics_provider.h
Normal file
48
src/xenia/ui/graphics_provider.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2015 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_UI_GRAPHICS_PROVIDER_H_
|
||||
#define XENIA_UI_GRAPHICS_PROVIDER_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace xe {
|
||||
namespace ui {
|
||||
|
||||
class GraphicsContext;
|
||||
class Window;
|
||||
|
||||
// Factory for graphics contexts.
|
||||
// All contexts created by the same provider will be able to share resources
|
||||
// according to the rules of the backing graphics API.
|
||||
class GraphicsProvider {
|
||||
public:
|
||||
virtual ~GraphicsProvider() = default;
|
||||
|
||||
// The 'main' window of an application, used to query provider information.
|
||||
Window* main_window() const { return main_window_; }
|
||||
|
||||
// Creates a new graphics context and swapchain for presenting to a window.
|
||||
virtual std::unique_ptr<GraphicsContext> CreateContext(
|
||||
Window* target_window) = 0;
|
||||
|
||||
// Creates a new offscreen graphics context without a swapchain or immediate
|
||||
// drawer.
|
||||
virtual std::unique_ptr<GraphicsContext> CreateOffscreenContext() = 0;
|
||||
|
||||
protected:
|
||||
explicit GraphicsProvider(Window* main_window) : main_window_(main_window) {}
|
||||
|
||||
Window* main_window_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace ui
|
||||
} // namespace xe
|
||||
|
||||
#endif // XENIA_UI_GRAPHICS_PROVIDER_H_
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "xenia/base/platform_win.h"
|
||||
#include "xenia/base/profiling.h"
|
||||
#include "xenia/base/threading.h"
|
||||
#include "xenia/ui/graphics_provider.h"
|
||||
#include "xenia/ui/imgui_drawer.h"
|
||||
#include "xenia/ui/window.h"
|
||||
|
||||
@@ -26,9 +27,7 @@ namespace xe {
|
||||
namespace ui {
|
||||
|
||||
// Implemented in one of the window_*_demo.cc files under a subdir.
|
||||
std::unique_ptr<GraphicsContext> CreateDemoContext(Window* window);
|
||||
|
||||
std::unique_ptr<xe::ui::ImGuiDrawer> imgui_drawer_;
|
||||
std::unique_ptr<GraphicsProvider> CreateDemoGraphicsProvider(Window* window);
|
||||
|
||||
int window_demo_main(const std::vector<std::wstring>& args) {
|
||||
Profiler::Initialize();
|
||||
@@ -45,13 +44,6 @@ int window_demo_main(const std::vector<std::wstring>& args) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
window->on_closed.AddListener([&loop](xe::ui::UIEvent* e) {
|
||||
loop->Quit();
|
||||
XELOGI("User-initiated death!");
|
||||
imgui_drawer_.reset();
|
||||
exit(1);
|
||||
});
|
||||
loop->on_quit.AddListener([&window](xe::ui::UIEvent* e) { window.reset(); });
|
||||
|
||||
// Main menu.
|
||||
auto main_menu = MenuItem::Create(MenuItem::Type::kNormal);
|
||||
@@ -78,25 +70,37 @@ int window_demo_main(const std::vector<std::wstring>& args) {
|
||||
window->Resize(1920, 1200);
|
||||
|
||||
// Create the graphics context used for drawing and setup the window.
|
||||
loop->PostSynchronous([&window]() {
|
||||
// Create context and give it to the window.
|
||||
std::unique_ptr<GraphicsProvider> graphics_provider;
|
||||
std::unique_ptr<xe::ui::ImGuiDrawer> imgui_drawer;
|
||||
loop->PostSynchronous([&window, &graphics_provider, &imgui_drawer]() {
|
||||
// Create graphics provider and an initial context for the window.
|
||||
// The window will finish initialization wtih the context (loading
|
||||
// resources, etc).
|
||||
auto context = CreateDemoContext(window.get());
|
||||
window->set_context(std::move(context));
|
||||
graphics_provider = CreateDemoGraphicsProvider(window.get());
|
||||
window->set_context(graphics_provider->CreateContext(window.get()));
|
||||
|
||||
// Setup the profiler display.
|
||||
GraphicsContextLock context_lock(window->context());
|
||||
Profiler::set_window(window.get());
|
||||
|
||||
// Initialize the ImGui renderer we'll use.
|
||||
imgui_drawer_ = std::make_unique<xe::ui::ImGuiDrawer>(window.get());
|
||||
imgui_drawer_->SetupDefaultInput();
|
||||
imgui_drawer = std::make_unique<xe::ui::ImGuiDrawer>(window.get());
|
||||
imgui_drawer->SetupDefaultInput();
|
||||
|
||||
// Show the elemental-forms debug UI so we can see it working.
|
||||
el::util::ShowDebugInfoSettingsForm(window->root_element());
|
||||
});
|
||||
|
||||
window->on_closed.AddListener(
|
||||
[&loop, &graphics_provider, &imgui_drawer](xe::ui::UIEvent* e) {
|
||||
loop->Quit();
|
||||
XELOGI("User-initiated death!");
|
||||
imgui_drawer.reset();
|
||||
graphics_provider.reset();
|
||||
exit(1);
|
||||
});
|
||||
loop->on_quit.AddListener([&window](xe::ui::UIEvent* e) { window.reset(); });
|
||||
|
||||
window->on_key_down.AddListener([](xe::ui::KeyEvent* e) {
|
||||
switch (e->key_code()) {
|
||||
case 0x72: { // F3
|
||||
@@ -130,8 +134,9 @@ int window_demo_main(const std::vector<std::wstring>& args) {
|
||||
// Wait until we are exited.
|
||||
loop->AwaitQuit();
|
||||
|
||||
imgui_drawer_.reset();
|
||||
imgui_drawer.reset();
|
||||
|
||||
loop->PostSynchronous([&graphics_provider]() { graphics_provider.reset(); });
|
||||
window.reset();
|
||||
loop.reset();
|
||||
Profiler::Dump();
|
||||
|
||||
Reference in New Issue
Block a user