Graphics is now a global object, instead of a per-window one

This commit is contained in:
HOEGLER Stefan
2020-03-15 15:58:01 +01:00
parent 30e256d0aa
commit 3b55755f0c
20 changed files with 121 additions and 59 deletions
+5 -2
View File
@@ -26,6 +26,7 @@ namespace Seele
, arraySize(size)
{
_data = (T*)malloc(size * sizeof(T));
assert(_data != nullptr);
for (int i = 0; i < size; ++i)
{
_data[i] = value;
@@ -38,10 +39,10 @@ namespace Seele
{
_data = (T*)malloc(init.size() * sizeof(T));
auto it = init.begin();
for (size_t i = 0; i < init.size(); i++)
for (size_t i = 0; it != init.end(); i++, it++)
{
assert(i < init.size());
_data[i] = *it;
it++;
}
refreshIterators();
}
@@ -50,6 +51,7 @@ namespace Seele
, arraySize(other.arraySize)
{
_data = (T*)malloc(other.allocated * sizeof(T));
assert(_data != nullptr);
std::memcpy(_data, other._data, sizeof(T) * allocated);
refreshIterators();
}
@@ -174,6 +176,7 @@ namespace Seele
{
allocated += DEFAULT_ALLOC_SIZE;
void* tempArray = malloc(sizeof(T) * allocated);
assert(tempArray != nullptr);
std::memcpy(tempArray, _data, arraySize * sizeof(T));
memset(tempArray, 0, sizeof(T) * allocated);
delete _data;
+2 -2
View File
@@ -8,13 +8,13 @@ namespace Seele {
class Graphics
{
public:
Graphics();
~Graphics();
virtual void init(GraphicsInitializer initializer) = 0;
virtual void beginFrame(void* windowHandle) = 0;
virtual void endFrame(void* windowHandle) = 0;
virtual void* createWindow(const WindowCreateInfo& createInfo) = 0;
protected:
Graphics();
~Graphics();
friend class Window;
};
DEFINE_REF(Graphics);
+4
View File
@@ -20,6 +20,7 @@ namespace Seele
const char* windowLayoutFile;
const char* applicationName;
const char* engineName;
void* windowHandle;
/**
* layers defines the enabled Vulkan layers used in the instance,
* if ENABLE_VALIDATION is defined, standard validation is already enabled
@@ -34,6 +35,7 @@ namespace Seele
, layers{ "VK_LAYER_LUNARG_standard_validation" }
, instanceExtensions{}
, deviceExtensions{ "VK_KHR_swapchain" }
, windowHandle(nullptr)
{}
GraphicsInitializer(const GraphicsInitializer& other)
: applicationName(other.applicationName)
@@ -44,12 +46,14 @@ namespace Seele
{
}
};
DECLARE_REF(Graphics);
struct WindowCreateInfo
{
int32 width;
int32 height;
const char* title;
bool bFullscreen;
PGraphics graphics;
};
class RenderCommandBase
+1 -1
View File
@@ -1,6 +1,6 @@
#include "RenderPath.h"
Seele::RenderPath::RenderPath(Graphics* graphics)
Seele::RenderPath::RenderPath(PGraphics graphics)
: graphics(graphics)
{
}
+2 -2
View File
@@ -6,7 +6,7 @@ namespace Seele
class RenderPath
{
public:
RenderPath(Graphics* graphics);
RenderPath(PGraphics graphics);
virtual ~RenderPath();
virtual void applyArea(Rect area) = 0;
virtual void init() = 0;
@@ -14,7 +14,7 @@ namespace Seele
virtual void render() = 0;
virtual void endFrame() = 0;
protected:
Graphics* graphics;
PGraphics graphics;
Rect area;
};
DEFINE_REF(RenderPath);
+1 -1
View File
@@ -1,6 +1,6 @@
#include "SceneRenderPath.h"
Seele::SceneRenderPath::SceneRenderPath(Graphics* graphics)
Seele::SceneRenderPath::SceneRenderPath(PGraphics graphics)
: RenderPath(graphics)
{
}
+1 -1
View File
@@ -6,7 +6,7 @@ namespace Seele
class SceneRenderPath : public RenderPath
{
public:
SceneRenderPath(Graphics* graphics);
SceneRenderPath(PGraphics graphics);
virtual ~SceneRenderPath();
virtual void applyArea(Rect area) override;
virtual void init() override;
+1 -1
View File
@@ -1,7 +1,7 @@
#include "SceneView.h"
#include "SceneRenderPath.h"
Seele::SceneView::SceneView(Graphics* graphics)
Seele::SceneView::SceneView(PGraphics graphics)
: View(graphics)
{
renderer = new SceneRenderPath(graphics);
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Seele
class SceneView : public View
{
public:
SceneView(Graphics* graphics);
SceneView(PGraphics graphics);
~SceneView();
};
}
+1 -1
View File
@@ -1,6 +1,6 @@
#include "View.h"
Seele::View::View(Graphics* graphics)
Seele::View::View(PGraphics graphics)
: graphics(graphics)
{
}
+3 -3
View File
@@ -2,18 +2,18 @@
#include "RenderPath.h"
namespace Seele
{
class Graphics;
DECLARE_REF(Graphics);
// A view is a part of the window, which can be anything from a viewport to an editor
class View
{
public:
View(Graphics* graphics);
View(PGraphics graphics);
virtual ~View();
void beginFrame();
void endFrame();
void applyArea(Rect area);
protected:
Graphics* graphics;
PGraphics graphics;
PRenderPath renderer;
};
+62 -37
View File
@@ -4,34 +4,27 @@
#include "VulkanInitializer.h"
#include <GLFW/glfw3.h>
Seele::VulkanGraphics::VulkanGraphics()
: callback(VK_NULL_HANDLE)
, handle(VK_NULL_HANDLE)
, instance(VK_NULL_HANDLE)
, physicalDevice(VK_NULL_HANDLE)
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
}
Seele::VulkanGraphics::~VulkanGraphics()
{
glfwTerminate();
}
void Seele::VulkanGraphics::init(GraphicsInitializer initInfo)
{
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = initInfo.applicationName;
appInfo.applicationVersion = VK_MAKE_VERSION(0, 0, 1);
appInfo.pEngineName = initInfo.engineName;
appInfo.engineVersion = VK_MAKE_VERSION(0, 0, 1);
appInfo.apiVersion = VK_API_VERSION_1_1;
VkInstanceCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
info.pApplicationInfo = &appInfo;
Array<const char*> extensions = getRequiredExtensions();
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i)
{
extensions.add(initInfo.instanceExtensions[i]);
}
info.enabledExtensionCount = (uint32)extensions.size();
info.ppEnabledExtensionNames = extensions.data();
#ifdef ENABLE_VALIDATION
info.enabledLayerCount = (uint32)initInfo.layers.size();
info.ppEnabledLayerNames = initInfo.layers.data();
#else
info.enabledLayerCount = 0;
#endif
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
initInstance(initInfo);
setupDebugCallback();
pickPhysicalDevice();
allocator = new VulkanAllocator(this);
}
@@ -52,17 +45,6 @@ void* Seele::VulkanGraphics::createWindow(const WindowCreateInfo& createInfo)
return window;
}
Seele::VulkanGraphics::VulkanGraphics()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
}
Seele::VulkanGraphics::~VulkanGraphics()
{
glfwTerminate();
}
VkDevice Seele::VulkanGraphics::getDevice() const
{
return handle;
@@ -88,6 +70,34 @@ Seele::Array<const char*> Seele::VulkanGraphics::getRequiredExtensions()
#endif // ENABLE_VALIDATION
return extensions;
}
void Seele::VulkanGraphics::initInstance(GraphicsInitializer initInfo)
{
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = initInfo.applicationName;
appInfo.applicationVersion = VK_MAKE_VERSION(0, 0, 1);
appInfo.pEngineName = initInfo.engineName;
appInfo.engineVersion = VK_MAKE_VERSION(0, 0, 1);
appInfo.apiVersion = VK_API_VERSION_1_1;
VkInstanceCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
info.pApplicationInfo = &appInfo;
Array<const char*> extensions = getRequiredExtensions();
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i)
{
extensions.add(initInfo.instanceExtensions[i]);
}
info.enabledExtensionCount = (uint32)extensions.size();
info.ppEnabledExtensionNames = extensions.data();
#ifdef ENABLE_VALIDATION
info.enabledLayerCount = (uint32)initInfo.layers.size();
info.ppEnabledLayerNames = initInfo.layers.data();
#else
info.enabledLayerCount = 0;
#endif
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
}
void Seele::VulkanGraphics::setupDebugCallback()
{
VkDebugReportCallbackCreateInfoEXT createInfo =
@@ -95,4 +105,19 @@ void Seele::VulkanGraphics::setupDebugCallback()
VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT);
VK_CHECK(CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback));
}
}
void Seele::VulkanGraphics::pickPhysicalDevice()
{
uint32 physicalDeviceCount;
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
Array<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data());
VkPhysicalDevice bestDevice;
uint32 deviceRating = 0;
for (auto physicalDevice : physicalDevices)
{
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(physicalDevice, &props);
}
}
@@ -19,12 +19,15 @@ namespace Seele
virtual void* createWindow(const WindowCreateInfo& createInfo) override;
protected:
Array<const char*> getRequiredExtensions();
void initInstance(GraphicsInitializer initInfo);
void setupDebugCallback();
void pickPhysicalDevice();
VkDevice handle;
VkPhysicalDevice physicalDevice;
VkInstance instance;
VkDebugReportCallbackEXT callback;
Array<PVulkanViewport> viewports;
PVulkanAllocator allocator;
friend class Window;
};
@@ -35,6 +35,7 @@ VkDescriptorType Seele::cast(const SeDescriptorType& descriptorType)
default:
break;
}
return VK_DESCRIPTOR_TYPE_MAX_ENUM;
}
SeDescriptorType Seele::cast(const VkDescriptorType& descriptorType)
@@ -71,6 +72,7 @@ SeDescriptorType Seele::cast(const VkDescriptorType& descriptorType)
default:
break;
}
return SE_DESCRIPTOR_TYPE_MAX_ENUM;
}
VkShaderStageFlagBits Seele::cast(const SeShaderStageFlagBits& stage)
@@ -113,6 +115,7 @@ VkShaderStageFlagBits Seele::cast(const SeShaderStageFlagBits& stage)
default:
break;
}
return VK_SHADER_STAGE_ALL;
}
SeShaderStageFlagBits Seele::cast(const VkShaderStageFlagBits& stage)
@@ -154,4 +157,5 @@ SeShaderStageFlagBits Seele::cast(const VkShaderStageFlagBits& stage)
default:
break;
}
return SE_SHADER_STAGE_ALL;
}
@@ -11,6 +11,7 @@ namespace Seele
public:
VulkanDescriptorLayout(PVulkanGraphics graphics)
: graphics(graphics)
, layoutHandle(VK_NULL_HANDLE)
{}
virtual ~VulkanDescriptorLayout();
virtual void create();
@@ -30,6 +31,8 @@ namespace Seele
public:
VulkanPipelineLayout(PVulkanGraphics graphics)
: graphics(graphics)
, layoutHash(0)
, layoutHandle(VK_NULL_HANDLE)
{}
virtual ~VulkanPipelineLayout();
virtual void create();
@@ -53,7 +56,11 @@ namespace Seele
class VulkanDescriptorSet : public DescriptorSet
{
public:
VulkanDescriptorSet(PVulkanGraphics graphics, PVulkanDescriptorAllocator owner) : graphics(graphics), owner(owner) {}
VulkanDescriptorSet(PVulkanGraphics graphics, PVulkanDescriptorAllocator owner)
: graphics(graphics)
, owner(owner)
, setHandle(VK_NULL_HANDLE)
{}
virtual ~VulkanDescriptorSet();
virtual void updateBuffer(uint32_t binding, PUniformBuffer uniformBuffer);
virtual void updateBuffer(uint32_t binding, PStructuredBuffer uniformBuffer);
@@ -132,4 +139,17 @@ namespace Seele
VkSampler sampler;
};
DEFINE_REF(VulkanSamplerState);
class VulkanViewport : public Viewport
{
public:
private:
uint32 sizeX;
uint32 sizeY;
uint32 offsetX;
uint32 offsetY;
VkSwapchainKHR swapchain;
void* windowHandle;
};
DECLARE_REF(VulkanViewport);
}
+1 -3
View File
@@ -1,17 +1,15 @@
#include "Window.h"
#include "Vulkan/VulkanGraphics.h"
#include "SceneView.h"
Seele::Window::Window(const WindowCreateInfo& createInfo)
: width(createInfo.width)
, height(createInfo.height)
{
graphics = new VulkanGraphics();
graphics = createInfo.graphics;
center = new Section();
center->resizeArea(Rect(1, 1, 0, 0));
center->addView(new SceneView(graphics));
windowHandle = graphics->createWindow(createInfo);
graphics->init(GraphicsInitializer());
}
Seele::Window::~Window()
+1 -1
View File
@@ -62,7 +62,7 @@ namespace Seele {
PSection center;
uint32 width;
uint32 height;
Graphics* graphics;
PGraphics graphics;
};
DEFINE_REF(Window)
}
+4
View File
@@ -1,7 +1,11 @@
#include "WindowManager.h"
#include "Vulkan/VulkanGraphics.h"
Seele::WindowManager::WindowManager()
{
graphics = new VulkanGraphics();
GraphicsInitializer initializer;
graphics->init(initializer);
}
Seele::WindowManager::~WindowManager()
+1
View File
@@ -15,6 +15,7 @@ namespace Seele
void endFrame();
private:
Array<PWindow> windows;
PGraphics graphics;
};
DEFINE_REF(WindowManager);
}
+2 -2
View File
@@ -104,13 +104,13 @@ namespace Seele
}
template<typename F>
RefPtr<F>& cast()
RefPtr<F> cast()
{
T* t = object->getHandle();
F* f = dynamic_cast<F*>(t);
if (f == nullptr)
{
return RefPtr<F>();
return nullptr;
}
RefObject<F>* newObject = (RefObject<F>*)object;
RefPtr<F> result(newObject);