Renderer: RenderWindow now requires a RenderDevice

This commit is contained in:
Lynix
2021-05-16 23:13:00 +02:00
parent 40772f2137
commit 13feaf4aab
30 changed files with 132 additions and 107 deletions

View File

@@ -16,8 +16,8 @@ namespace Nz
m_attachmentToTextureMapping(std::move(attachmentIdToTextureMapping)),
m_passIdToPhysicalPassMapping(std::move(passIdToPhysicalPassMapping))
{
RenderDevice& renderDevice = Graphics::Instance()->GetRenderDevice();
m_commandPool = renderDevice.InstantiateCommandPool(QueueType::Graphics);
const std::shared_ptr<RenderDevice>& renderDevice = Graphics::Instance()->GetRenderDevice();
m_commandPool = renderDevice->InstantiateCommandPool(QueueType::Graphics);
}
void BakedFrameGraph::Execute(RenderFrame& renderFrame)
@@ -115,7 +115,7 @@ namespace Nz
void BakedFrameGraph::Resize(unsigned int width, unsigned int height)
{
RenderDevice& renderDevice = Graphics::Instance()->GetRenderDevice();
const std::shared_ptr<RenderDevice>& renderDevice = Graphics::Instance()->GetRenderDevice();
// Delete previous textures to make some room in VRAM
for (auto& passData : m_passes)
@@ -136,7 +136,7 @@ namespace Nz
textureCreationParams.usageFlags = textureData.usage;
textureCreationParams.pixelFormat = textureData.format;
textureData.texture = renderDevice.InstantiateTexture(textureCreationParams);
textureData.texture = renderDevice->InstantiateTexture(textureCreationParams);
}
std::vector<std::shared_ptr<Texture>> textures;
@@ -160,7 +160,7 @@ namespace Nz
passData.renderRect.Set(0, 0, int(framebufferWidth), int(framebufferHeight));
passData.framebuffer = renderDevice.InstantiateFramebuffer(framebufferWidth, framebufferHeight, passData.renderPass, textures);
passData.framebuffer = renderDevice->InstantiateFramebuffer(framebufferWidth, framebufferHeight, passData.renderPass, textures);
}
}
}

View File

@@ -612,7 +612,7 @@ namespace Nz
void FrameGraph::BuildPhysicalPasses()
{
RenderDevice& renderDevice = Graphics::Instance()->GetRenderDevice();
const std::shared_ptr<RenderDevice>& renderDevice = Graphics::Instance()->GetRenderDevice();
std::unordered_map<std::size_t /*textureId*/, TextureLayout> textureLayouts;
@@ -845,7 +845,7 @@ namespace Nz
BuildPhysicalPassDependencies(colorAttachmentCount, depthStencilAttachmentIndex.has_value(), renderPassAttachments, subpassesDesc, subpassesDeps);
m_pending.renderPasses.push_back(renderDevice.InstantiateRenderPass(std::move(renderPassAttachments), std::move(subpassesDesc), std::move(subpassesDeps)));
m_pending.renderPasses.push_back(renderDevice->InstantiateRenderPass(std::move(renderPassAttachments), std::move(subpassesDesc), std::move(subpassesDeps)));
physicalPassIndex++;
}

View File

@@ -15,7 +15,7 @@ namespace Nz
{
assert(mesh->GetAnimationType() == AnimationType_Static);
RenderDevice& renderDevice = Graphics::Instance()->GetRenderDevice();
const std::shared_ptr<RenderDevice>& renderDevice = Graphics::Instance()->GetRenderDevice();
m_subMeshes.reserve(mesh->GetSubMeshCount());
for (std::size_t i = 0; i < mesh->GetSubMeshCount(); ++i)
@@ -34,7 +34,7 @@ namespace Nz
const SoftwareBuffer* vertexBufferContent = static_cast<const SoftwareBuffer*>(vertexBuffer->GetBuffer()->GetImpl());
auto& submeshData = m_subMeshes.emplace_back();
submeshData.indexBuffer = renderDevice.InstantiateBuffer(BufferType_Index);
submeshData.indexBuffer = renderDevice->InstantiateBuffer(BufferType_Index);
if (!submeshData.indexBuffer->Initialize(indexBuffer->GetStride() * indexBuffer->GetIndexCount(), BufferUsage_DeviceLocal))
throw std::runtime_error("failed to create index buffer");
@@ -43,7 +43,7 @@ namespace Nz
submeshData.indexCount = indexBuffer->GetIndexCount();
submeshData.vertexBuffer = renderDevice.InstantiateBuffer(BufferType_Vertex);
submeshData.vertexBuffer = renderDevice->InstantiateBuffer(BufferType_Vertex);
if (!submeshData.vertexBuffer->Initialize(vertexBuffer->GetStride() * vertexBuffer->GetVertexCount(), BufferUsage_DeviceLocal))
throw std::runtime_error("failed to create vertex buffer");

View File

@@ -19,7 +19,6 @@ namespace Nz
ModuleBase("Graphics", this)
{
Renderer* renderer = Renderer::Instance();
RendererImpl* rendererImpl = renderer->GetRendererImpl(); //< FIXME
std::vector<RenderDeviceInfo> renderDeviceInfo = rendererImpl->QueryRenderDevices();
if (renderDeviceInfo.empty())
throw std::runtime_error("no render device available");
@@ -35,7 +34,7 @@ namespace Nz
}
}
m_renderDevice = rendererImpl->InstanciateRenderDevice(bestRenderDeviceIndex);
m_renderDevice = renderer->InstanciateRenderDevice(bestRenderDeviceIndex);
if (!m_renderDevice)
throw std::runtime_error("failed to instantiate render device");

View File

@@ -43,7 +43,7 @@ namespace Nz
{
auto& uniformBuffer = m_uniformBuffers.emplace_back();
uniformBuffer.buffer = Graphics::Instance()->GetRenderDevice().InstantiateBuffer(Nz::BufferType_Uniform);
uniformBuffer.buffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(Nz::BufferType_Uniform);
if (!uniformBuffer.buffer->Initialize(uniformBufferInfo.blockSize, BufferUsage_Dynamic))
throw std::runtime_error("failed to initialize UBO memory");

View File

@@ -56,7 +56,7 @@ namespace Nz
renderPipelineInfo.vertexBuffers = vertexBuffers;
return m_renderPipelines.emplace_back(Graphics::Instance()->GetRenderDevice().InstantiateRenderPipeline(std::move(renderPipelineInfo)));
return m_renderPipelines.emplace_back(Graphics::Instance()->GetRenderDevice()->InstantiateRenderPipeline(std::move(renderPipelineInfo)));
}
/*!
* \brief Returns a reference to a MaterialPipeline built with MaterialPipelineInfo

View File

@@ -15,7 +15,7 @@ namespace Nz
{
Nz::PredefinedInstanceData instanceUboOffsets = Nz::PredefinedInstanceData::GetOffsets();
m_instanceDataBuffer = Graphics::Instance()->GetRenderDevice().InstantiateBuffer(BufferType_Uniform);
m_instanceDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType_Uniform);
if (!m_instanceDataBuffer->Initialize(instanceUboOffsets.totalSize, Nz::BufferUsage_DeviceLocal | Nz::BufferUsage_Dynamic))
throw std::runtime_error("failed to initialize viewer data UBO");

View File

@@ -61,7 +61,7 @@ namespace Nz
states.enabledOptions = combination;
states.sanitized = true;
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice().InstantiateShaderModule(m_shaderStage, m_shaderAst, std::move(states));
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice()->InstantiateShaderModule(m_shaderStage, m_shaderAst, std::move(states));
it = m_combinations.emplace(combination, std::move(stage)).first;
}

View File

@@ -41,11 +41,11 @@ namespace Nz
DummySurface* dummySurface = static_cast<DummySurface*>(surface);
OpenGLRenderer* glRenderer = static_cast<OpenGLRenderer*>(renderer);
m_device = std::static_pointer_cast<OpenGLDevice>(glRenderer->InstanciateRenderDevice(0));
OpenGLDevice& device = static_cast<OpenGLDevice&>(*m_owner.GetRenderDevice());
GL::ContextParams contextParams;
m_context = m_device->CreateContext(contextParams, dummySurface->GetWindowHandle());
m_context = device.CreateContext(contextParams, dummySurface->GetWindowHandle());
if (!m_context)
return false;
@@ -75,11 +75,6 @@ namespace Nz
return m_renderPass;
}
std::shared_ptr<RenderDevice> OpenGLRenderWindow::GetRenderDevice()
{
return m_device;
}
void OpenGLRenderWindow::Present()
{
m_context->SwapBuffers();

View File

@@ -24,6 +24,13 @@
namespace Nz
{
OpenGLRenderer::OpenGLRenderer()
{
auto& dummyDevice = m_deviceInfos.emplace_back();
dummyDevice.name = "OpenGL Default Device";
dummyDevice.type = RenderDeviceType::Unknown;
}
OpenGLRenderer::~OpenGLRenderer()
{
m_device.reset();
@@ -107,13 +114,8 @@ namespace Nz
return 300;
}
std::vector<RenderDeviceInfo> OpenGLRenderer::QueryRenderDevices() const
const std::vector<RenderDeviceInfo>& OpenGLRenderer::QueryRenderDevices() const
{
std::vector<RenderDeviceInfo> devices;
auto& dummyDevice = devices.emplace_back();
dummyDevice.name = "OpenGL Default Device";
dummyDevice.type = RenderDeviceType::Unknown;
return devices;
return m_deviceInfos;
}
}

View File

@@ -11,6 +11,22 @@
namespace Nz
{
bool RenderWindow::Create(std::shared_ptr<RenderDevice> renderDevice, VideoMode mode, const std::string& title, WindowStyleFlags style, const RenderWindowParameters& parameters)
{
m_parameters = parameters;
m_renderDevice = std::move(renderDevice);
return Window::Create(mode, title, style);
}
bool RenderWindow::Create(std::shared_ptr<RenderDevice> renderDevice, void* handle, const RenderWindowParameters& parameters)
{
m_parameters = parameters;
m_renderDevice = std::move(renderDevice);
return Window::Create(handle);
}
void RenderWindow::Display()
{
if (m_framerateLimit > 0)
@@ -28,17 +44,9 @@ namespace Nz
///TODO
}
std::shared_ptr<RenderDevice> RenderWindow::GetRenderDevice()
{
if (!m_impl)
return std::shared_ptr<RenderDevice>();
return m_impl->GetRenderDevice();
}
bool RenderWindow::OnWindowCreated()
{
RendererImpl *rendererImpl = Renderer::Instance()->GetRendererImpl();
RendererImpl* rendererImpl = Renderer::Instance()->GetRendererImpl();
auto surface = rendererImpl->CreateRenderSurfaceImpl();
if (!surface->Create(GetSystemHandle()))
{
@@ -64,6 +72,7 @@ namespace Nz
void RenderWindow::OnWindowDestroy()
{
m_impl.reset();
m_renderDevice.reset();
m_surface.reset();
}

View File

@@ -134,5 +134,30 @@ namespace Nz
m_rendererImpl.reset();
}
std::shared_ptr<RenderDevice> Renderer::InstanciateRenderDevice(std::size_t deviceIndex)
{
return m_rendererImpl->InstanciateRenderDevice(deviceIndex);
}
RenderAPI Renderer::QueryAPI() const
{
return m_rendererImpl->QueryAPI();
}
std::string Renderer::QueryAPIString() const
{
return m_rendererImpl->QueryAPIString();
}
UInt32 Renderer::QueryAPIVersion() const
{
return m_rendererImpl->QueryAPIVersion();
}
const std::vector<RenderDeviceInfo>& Renderer::QueryRenderDevices() const
{
return m_rendererImpl->QueryRenderDevices();
}
Renderer* Renderer::s_instance = nullptr;
}

View File

@@ -105,14 +105,16 @@ namespace Nz
bool VkRenderWindow::Create(RendererImpl* /*renderer*/, RenderSurface* surface, const RenderWindowParameters& parameters)
{
const auto& deviceInfo = Vulkan::GetPhysicalDevices()[0];
VulkanDevice& referenceDevice = static_cast<VulkanDevice&>(*m_owner.GetRenderDevice());
const auto& physDeviceInfo = referenceDevice.GetPhysicalDeviceInfo();
Vk::Surface& vulkanSurface = static_cast<VulkanSurface*>(surface)->GetSurface();
UInt32 graphicsFamilyQueueIndex;
UInt32 presentableFamilyQueueIndex;
UInt32 transferFamilyQueueIndex;
m_device = Vulkan::SelectDevice(deviceInfo, vulkanSurface, &graphicsFamilyQueueIndex, &presentableFamilyQueueIndex, &transferFamilyQueueIndex);
m_device = Vulkan::SelectDevice(physDeviceInfo, vulkanSurface, &graphicsFamilyQueueIndex, &presentableFamilyQueueIndex, &transferFamilyQueueIndex);
if (!m_device)
{
NazaraError("Failed to get compatible Vulkan device");
@@ -124,7 +126,7 @@ namespace Nz
m_transferQueue = m_device->GetQueue(transferFamilyQueueIndex, 0);
std::vector<VkSurfaceFormatKHR> surfaceFormats;
if (!vulkanSurface.GetFormats(deviceInfo.physDevice, &surfaceFormats))
if (!vulkanSurface.GetFormats(physDeviceInfo.physDevice, &surfaceFormats))
{
NazaraError("Failed to query supported surface formats");
return false;
@@ -192,7 +194,7 @@ namespace Nz
if (m_depthStencilFormat != VK_FORMAT_MAX_ENUM)
{
VkFormatProperties formatProperties = m_device->GetInstance().GetPhysicalDeviceFormatProperties(deviceInfo.physDevice, m_depthStencilFormat);
VkFormatProperties formatProperties = m_device->GetInstance().GetPhysicalDeviceFormatProperties(physDeviceInfo.physDevice, m_depthStencilFormat);
if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
break; //< Found it

View File

@@ -40,7 +40,16 @@ namespace Nz
bool VulkanRenderer::Prepare(const ParameterList& parameters)
{
return Vulkan::Initialize(APIVersion, parameters);
if (!Vulkan::Initialize(APIVersion, parameters))
return false;
const auto& physDevices = Vulkan::GetPhysicalDevices();
m_deviceInfos.reserve(physDevices.size());
for (const Vk::PhysicalDevice& physDevice : physDevices)
m_deviceInfos.push_back(Vulkan::BuildRenderDeviceInfo(physDevice));
return true;
}
RenderAPI VulkanRenderer::QueryAPI() const
@@ -61,16 +70,8 @@ namespace Nz
return APIVersion;
}
std::vector<RenderDeviceInfo> VulkanRenderer::QueryRenderDevices() const
const std::vector<RenderDeviceInfo>& VulkanRenderer::QueryRenderDevices() const
{
const auto& physDevices = Vulkan::GetPhysicalDevices();
std::vector<RenderDeviceInfo> devices;
devices.reserve(physDevices.size());
for (const Vk::PhysicalDevice& physDevice : physDevices)
devices.push_back(Vulkan::BuildRenderDeviceInfo(physDevice));
return devices;
return m_deviceInfos;
}
}