Renderer: Blit texture to window instead of using a full renderpass

This may improve performance and allow for render targets to customize how they blit the final texture (allowing for render-to-texture)
This commit is contained in:
SirLynix 2023-11-17 16:59:31 +01:00
parent f2e77fb8a5
commit 97d5640967
38 changed files with 448 additions and 265 deletions

View File

@ -703,52 +703,52 @@ int main(int argc, char* argv[])
});
godRaysTexture = graph.AddAttachment({
"God rays texture",
Nz::PixelFormat::RGBA16F,
Nz::FramePassAttachmentSize::SwapchainFactor,
50'000,
50'000
.name = "God rays texture",
.format = Nz::PixelFormat::RGBA16F,
.size = Nz::FramePassAttachmentSize::SwapchainFactor,
.width = 50'000,
.height = 50'000
});
bloomOutput = graph.AddAttachmentProxy("Bloom output", lightOutput);
unsigned int bloomSize = 50'000;
bloomBrightOutput = graph.AddAttachment({
"Bloom bright output",
Nz::PixelFormat::RGBA16F,
Nz::FramePassAttachmentSize::SwapchainFactor,
bloomSize,
bloomSize
.name = "Bloom bright output",
.format = Nz::PixelFormat::RGBA16F,
.size = Nz::FramePassAttachmentSize::SwapchainFactor,
.width = bloomSize,
.height = bloomSize
});
for (std::size_t i = 0; i < BloomSubdivisionCount; ++i)
{
bloomTextures[i * 2 + 0] = graph.AddAttachment({
"Bloom texture #" + std::to_string(i),
Nz::PixelFormat::RGBA16F,
Nz::FramePassAttachmentSize::SwapchainFactor,
bloomSize,
bloomSize
.name = "Bloom texture #" + std::to_string(i),
.format = Nz::PixelFormat::RGBA16F,
.size = Nz::FramePassAttachmentSize::SwapchainFactor,
.width = bloomSize,
.height = bloomSize
});
bloomTextures[i * 2 + 1] = graph.AddAttachment({
"Bloom texture #" + std::to_string(i),
Nz::PixelFormat::RGBA16F,
Nz::FramePassAttachmentSize::SwapchainFactor,
bloomSize,
bloomSize
.name = "Bloom texture #" + std::to_string(i),
.format = Nz::PixelFormat::RGBA16F,
.size = Nz::FramePassAttachmentSize::SwapchainFactor,
.width = bloomSize,
.height = bloomSize
});
bloomSize /= 2;
}
toneMappingOutput = graph.AddAttachment({
"Tone mapping",
Nz::PixelFormat::RGBA8,
Nz::FramePassAttachmentSize::SwapchainFactor,
100'000,
100'000
.name = "Tone mapping",
.format = Nz::PixelFormat::RGBA8,
.size = Nz::FramePassAttachmentSize::SwapchainFactor,
.width = 100'000,
.height = 100'000
});
Nz::FramePass& gbufferPass = graph.AddPass("GBuffer");

View File

@ -121,7 +121,6 @@ namespace Nz
{
std::size_t finalAttachment;
std::vector<const ViewerData*> viewers;
ShaderBindingPtr blitShaderBinding;
};
struct SkeletonInstanceData

View File

@ -9,6 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <string>
@ -24,6 +25,7 @@ namespace Nz
{
std::string name;
PixelFormat format;
TextureUsage additionalUsage = TextureUsage::TransferSource;
FramePassAttachmentSize size = FramePassAttachmentSize::SwapchainFactor;
unsigned int width = 100'000;
unsigned int height = 100'000;

View File

@ -48,6 +48,7 @@ namespace Nz
inline void BindVertexBuffer(UInt32 binding, GLuint vertexBuffer, UInt64 offset = 0);
inline void BlitTexture(const OpenGLTexture& source, const Boxui& sourceBox, const OpenGLTexture& target, const Boxui& targetBox, SamplerFilter filter = SamplerFilter::Nearest);
inline void BlitTextureToWindow(const OpenGLTexture& source, const Boxui& sourceBox, const Boxui& targetBox, SamplerFilter filter = SamplerFilter::Nearest);
inline void BuildMipmaps(OpenGLTexture& texture, UInt8 baseLevel, UInt8 levelCount);
@ -88,6 +89,7 @@ namespace Nz
#define NAZARA_OPENGL_FOREACH_COMMANDS(cb, lastCb) \
cb(BeginDebugRegionCommand) \
cb(BlitTextureCommand) \
cb(BlitTextureToWindowCommand) \
cb(BuildTextureMipmapsCommand) \
cb(CopyBufferCommand) \
cb(CopyBufferFromMemoryCommand) \
@ -119,6 +121,7 @@ namespace Nz
inline void Execute(const GL::Context* context, const BeginDebugRegionCommand& command);
inline void Execute(const GL::Context* context, const BlitTextureCommand& command);
inline void Execute(const GL::Context* context, const BlitTextureToWindowCommand& command);
inline void Execute(const GL::Context* context, const BuildTextureMipmapsCommand& command);
inline void Execute(const GL::Context* context, const CopyBufferCommand& command);
inline void Execute(const GL::Context* context, const CopyBufferFromMemoryCommand& command);
@ -148,6 +151,14 @@ namespace Nz
SamplerFilter filter;
};
struct BlitTextureToWindowCommand
{
const OpenGLTexture* source;
Boxui sourceBox;
Boxui targetBox;
SamplerFilter filter;
};
struct BuildTextureMipmapsCommand
{
OpenGLTexture* texture;

View File

@ -88,6 +88,18 @@ namespace Nz
m_commands.emplace_back(std::move(blitTexture));
}
inline void OpenGLCommandBuffer::BlitTextureToWindow(const OpenGLTexture& source, const Boxui& sourceBox, const Boxui& targetBox, SamplerFilter filter)
{
BlitTextureToWindowCommand blitTexture = {
&source,
sourceBox,
targetBox,
filter
};
m_commands.emplace_back(std::move(blitTexture));
}
inline void OpenGLCommandBuffer::BuildMipmaps(OpenGLTexture& texture, UInt8 baseLevel, UInt8 levelCount)
{
BuildTextureMipmapsCommand buildMipmaps = {
@ -258,3 +270,4 @@ namespace Nz
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>
#include "OpenGLCommandBuffer.hpp"

View File

@ -37,6 +37,7 @@ namespace Nz
void BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset = 0) override;
void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) override;
void BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex) override;
void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout) override;

View File

@ -32,6 +32,7 @@ namespace Nz
std::shared_ptr<CommandPool> CreateCommandPool(QueueType queueType) override;
inline GL::Context& GetContext();
inline OpenGLDevice& GetDevice();
const OpenGLFramebuffer& GetFramebuffer(std::size_t i) const override;
std::size_t GetFramebufferCount() const override;
PresentMode GetPresentMode() const override;
@ -50,8 +51,9 @@ namespace Nz
private:
std::optional<OpenGLRenderPass> m_renderPass;
std::size_t m_currentFrame;
std::vector<std::unique_ptr<OpenGLRenderImage>> m_renderImage;
std::shared_ptr<GL::Context> m_context;
std::vector<std::unique_ptr<OpenGLRenderImage>> m_renderImage;
OpenGLDevice& m_device;
OpenGLWindowFramebuffer m_framebuffer;
PresentMode m_presentMode;
PresentModeFlags m_supportedPresentModes;

View File

@ -12,6 +12,11 @@ namespace Nz
assert(m_context);
return *m_context;
}
inline OpenGLDevice& OpenGLSwapchain::GetDevice()
{
return m_device;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -29,6 +29,7 @@ namespace Nz
namespace Nz::GL
{
class Framebuffer;
class Texture;
enum class BufferTarget
@ -141,6 +142,7 @@ namespace Nz::GL
void BindVertexArray(GLuint vertexArray, bool force = false) const;
bool BlitTexture(const OpenGLTexture& source, const OpenGLTexture& destination, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const;
bool BlitTextureToWindow(const OpenGLTexture& texture, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const;
bool ClearErrorStack() const;
@ -217,6 +219,7 @@ namespace Nz::GL
private:
void HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const;
bool InitializeBlitFramebuffers() const;
static void BindTextureToFramebuffer(Framebuffer& framebuffer, const OpenGLTexture& texture);
enum class FunctionIndex
{

View File

@ -26,6 +26,7 @@ namespace Nz
class RenderPipeline;
class RenderPipelineLayout;
class ShaderBinding;
class Swapchain;
class Texture;
class NAZARA_RENDERER_API CommandBufferBuilder
@ -53,6 +54,7 @@ namespace Nz
virtual void BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset = 0) = 0;
virtual void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) = 0;
virtual void BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex) = 0;
virtual void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout) = 0;

View File

@ -19,6 +19,7 @@ namespace Nz
{
class CommandBuffer;
class CommandBufferBuilder;
class RenderDevice;
class UploadPool;
class NAZARA_RENDERER_API RenderFrame
@ -30,11 +31,12 @@ namespace Nz
RenderFrame(RenderFrame&&) = delete;
~RenderFrame() = default;
void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags);
inline void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags);
inline std::size_t GetFramebufferIndex() const;
const Vector2ui& GetSize() const;
UploadPool& GetUploadPool();
inline RenderDevice& GetRenderDevice();
inline UploadPool& GetUploadPool();
inline bool IsFramebufferInvalidated() const;

View File

@ -37,6 +37,11 @@ namespace Nz
return m_size;
}
inline RenderDevice& RenderFrame::GetRenderDevice()
{
return m_image->GetRenderDevice();
}
inline UploadPool& RenderFrame::GetUploadPool()
{
if NAZARA_UNLIKELY(!m_image)

View File

@ -15,6 +15,8 @@ namespace Nz
class NAZARA_RENDERER_API RenderImage : public TransientResources
{
public:
using TransientResources::TransientResources;
virtual void Present() = 0;
};
}

View File

@ -14,8 +14,11 @@
namespace Nz
{
class CommandBufferBuilder;
class Framebuffer;
class RenderFrame;
class RenderPass;
class Texture;
class NAZARA_RENDERER_API RenderTarget
{
@ -23,6 +26,8 @@ namespace Nz
RenderTarget() = default;
virtual ~RenderTarget();
virtual void BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const = 0;
virtual const Framebuffer& GetFramebuffer(std::size_t i) const = 0;
virtual std::size_t GetFramebufferCount() const = 0;
virtual const RenderPass& GetRenderPass() const = 0;

View File

@ -29,6 +29,8 @@ namespace Nz
virtual RenderFrame AcquireFrame() = 0;
void BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const override;
virtual std::shared_ptr<CommandPool> CreateCommandPool(QueueType queueType) = 0;
virtual PresentMode GetPresentMode() const = 0;

View File

@ -19,6 +19,7 @@ namespace Nz
{
class CommandBuffer;
class CommandBufferBuilder;
class RenderDevice;
class UploadPool;
class NAZARA_RENDERER_API TransientResources
@ -33,6 +34,7 @@ namespace Nz
inline void FlushReleaseQueue();
inline RenderDevice& GetRenderDevice();
virtual UploadPool& GetUploadPool() = 0;
template<typename T> void PushForRelease(const T& value) = delete;
@ -42,7 +44,7 @@ namespace Nz
virtual void SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags) = 0;
protected:
TransientResources() = default;
inline TransientResources(RenderDevice& renderDvice);
TransientResources(const TransientResources&) = delete;
TransientResources(TransientResources&&) = delete;
@ -53,6 +55,7 @@ namespace Nz
std::vector<Releasable*> m_releaseQueue;
std::vector<Block> m_releaseMemoryPool;
RenderDevice& m_renderDevice;
};
class NAZARA_RENDERER_API TransientResources::Releasable

View File

@ -8,6 +8,11 @@
namespace Nz
{
inline TransientResources::TransientResources(RenderDevice& renderDevice) :
m_renderDevice(renderDevice)
{
}
inline void TransientResources::FlushReleaseQueue()
{
for (Releasable* releasable : m_releaseQueue)
@ -21,6 +26,11 @@ namespace Nz
memoryblock.clear();
}
inline RenderDevice& TransientResources::GetRenderDevice()
{
return m_renderDevice;
}
template<typename T>
void TransientResources::PushForRelease(T&& value)
{
@ -98,3 +108,4 @@ namespace Nz
}
#include <Nazara/Renderer/DebugOff.hpp>
#include "TransientResources.hpp"

View File

@ -29,6 +29,8 @@ namespace Nz
inline RenderFrame AcquireFrame();
inline void BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const override;
inline bool DoesRenderOnlyIfFocused() const;
inline void EnableRenderOnlyIfFocused(bool enable = true);

View File

@ -20,6 +20,11 @@ namespace Nz
return m_swapchain->AcquireFrame();
}
inline void WindowSwapchain::BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const
{
return m_swapchain->BlitTexture(renderFrame, builder, texture);
}
inline bool WindowSwapchain::DoesRenderOnlyIfFocused() const
{
return m_renderOnlyIfFocused;

View File

@ -37,6 +37,7 @@ namespace Nz
void BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset = 0) override;
void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) override;
void BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex) override;
void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout) override;

View File

@ -46,11 +46,12 @@ namespace Nz
std::shared_ptr<CommandPool> CreateCommandPool(QueueType queueType) override;
const VulkanWindowFramebuffer& GetFramebuffer(std::size_t i) const override;
const VulkanWindowFramebuffer& GetFramebuffer(std::size_t imageIndex) const override;
std::size_t GetFramebufferCount() const override;
inline VulkanDevice& GetDevice();
inline const VulkanDevice& GetDevice() const;
inline Vk::QueueHandle& GetGraphicsQueue();
inline VkImage GetImage(std::size_t imageIndex) const;
const VulkanRenderPass& GetRenderPass() const override;
const Vector2ui& GetSize() const override;
PresentMode GetPresentMode() const override;

View File

@ -21,6 +21,11 @@ namespace Nz
return m_graphicsQueue;
}
inline VkImage VulkanSwapchain::GetImage(std::size_t imageIndex) const
{
return m_swapchain.GetImage(imageIndex).image;
}
inline const Vk::Swapchain& VulkanSwapchain::GetSwapchain() const
{
return m_swapchain;

View File

@ -11,48 +11,45 @@
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
#include <Nazara/VulkanRenderer/Wrapper/ImageView.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR, VK_OBJECT_TYPE_SWAPCHAIN_KHR>
{
class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR, VK_OBJECT_TYPE_SWAPCHAIN_KHR>
{
friend DeviceObject;
friend DeviceObject;
public:
struct Image;
public:
struct Image;
Swapchain() = default;
Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) = default;
~Swapchain() = default;
Swapchain() = default;
Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) = default;
~Swapchain() = default;
inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const;
inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const;
inline bool Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline const Image& GetImage(UInt32 index) const;
inline const std::vector<Image>& GetImages() const;
inline UInt32 GetImageCount() const;
inline const Image& GetImage(UInt32 index) const;
inline const std::vector<Image>& GetImages() const;
inline UInt32 GetImageCount() const;
inline bool IsSupported() const;
inline bool IsSupported() const;
Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) = default;
Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) = default;
struct Image
{
VkImage image;
ImageView view;
};
struct Image
{
VkImage image;
ImageView view;
};
private:
static inline VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle);
static inline void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator);
private:
static inline VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle);
static inline void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator);
std::vector<Image> m_images;
};
}
std::vector<Image> m_images;
};
}
#include <Nazara/VulkanRenderer/Wrapper/Swapchain.inl>

View File

@ -7,117 +7,114 @@
#include <Nazara/VulkanRenderer/Wrapper/Device.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
inline bool Swapchain::AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const
{
inline bool Swapchain::AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const
m_lastErrorCode = m_device->vkAcquireNextImageKHR(*m_device, m_handle, timeout, semaphore, fence, imageIndex);
switch (m_lastErrorCode)
{
m_lastErrorCode = m_device->vkAcquireNextImageKHR(*m_device, m_handle, timeout, semaphore, fence, imageIndex);
switch (m_lastErrorCode)
{
case VkResult::VK_SUBOPTIMAL_KHR:
case VkResult::VK_SUCCESS:
return true;
case VkResult::VK_SUBOPTIMAL_KHR:
case VkResult::VK_SUCCESS:
return true;
default:
{
NazaraErrorFmt("failed to acquire next swapchain image: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
default:
{
NazaraErrorFmt("failed to acquire next swapchain image: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
}
}
inline bool Swapchain::Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator)
{
if (!DeviceObject::Create(device, createInfo, allocator))
return false;
UInt32 imageCount = 0;
m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, nullptr);
if (m_lastErrorCode != VkResult::VK_SUCCESS || imageCount == 0)
{
NazaraErrorFmt("failed to query swapchain image count: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
std::vector<VkImage> images(imageCount);
m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, images.data());
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraErrorFmt("failed to query swapchain images: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
m_images.resize(imageCount);
for (UInt32 i = 0; i < imageCount; ++i)
{
m_images[i].image = images[i];
VkImageViewCreateInfo imageViewCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkImageViewCreateFlags flags;
m_images[i].image, // VkImage image;
VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
createInfo.imageFormat, // VkFormat format;
{ // VkComponentMapping components;
VK_COMPONENT_SWIZZLE_R, // VkComponentSwizzle .r;
VK_COMPONENT_SWIZZLE_G, // VkComponentSwizzle .g;
VK_COMPONENT_SWIZZLE_B, // VkComponentSwizzle .b;
VK_COMPONENT_SWIZZLE_A // VkComponentSwizzle .a;
},
{ // VkImageSubresourceRange subresourceRange;
VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags .aspectMask;
0, // uint32_t .baseMipLevel;
1, // uint32_t .levelCount;
0, // uint32_t .baseArrayLayer;
1 // uint32_t .layerCount;
}
}
}
};
inline bool Swapchain::Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator)
{
if (!DeviceObject::Create(device, createInfo, allocator))
return false;
UInt32 imageCount = 0;
m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, nullptr);
if (m_lastErrorCode != VkResult::VK_SUCCESS || imageCount == 0)
if (!m_images[i].view.Create(*m_device, imageViewCreateInfo))
{
NazaraErrorFmt("failed to query swapchain image count: {0}", TranslateVulkanError(m_lastErrorCode));
NazaraErrorFmt("failed to create image view for image #{0}", i);
return false;
}
std::vector<VkImage> images(imageCount);
m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, images.data());
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraErrorFmt("failed to query swapchain images: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
m_images.resize(imageCount);
for (UInt32 i = 0; i < imageCount; ++i)
{
m_images[i].image = images[i];
VkImageViewCreateInfo imageViewCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkImageViewCreateFlags flags;
m_images[i].image, // VkImage image;
VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
createInfo.imageFormat, // VkFormat format;
{ // VkComponentMapping components;
VK_COMPONENT_SWIZZLE_R, // VkComponentSwizzle .r;
VK_COMPONENT_SWIZZLE_G, // VkComponentSwizzle .g;
VK_COMPONENT_SWIZZLE_B, // VkComponentSwizzle .b;
VK_COMPONENT_SWIZZLE_A // VkComponentSwizzle .a;
},
{ // VkImageSubresourceRange subresourceRange;
VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags .aspectMask;
0, // uint32_t .baseMipLevel;
1, // uint32_t .levelCount;
0, // uint32_t .baseArrayLayer;
1 // uint32_t .layerCount;
}
};
if (!m_images[i].view.Create(*m_device, imageViewCreateInfo))
{
NazaraErrorFmt("failed to create image view for image #{0}", i);
return false;
}
}
return true;
}
inline const Swapchain::Image& Swapchain::GetImage(UInt32 index) const
{
return m_images[index];
}
return true;
}
inline const std::vector<Swapchain::Image>& Swapchain::GetImages() const
{
return m_images;
}
inline const Swapchain::Image& Swapchain::GetImage(UInt32 index) const
{
return m_images[index];
}
inline UInt32 Swapchain::GetImageCount() const
{
return static_cast<UInt32>(m_images.size());
}
inline const std::vector<Swapchain::Image>& Swapchain::GetImages() const
{
return m_images;
}
inline bool Swapchain::IsSupported() const
{
if (!m_device->IsExtensionLoaded(VK_KHR_SWAPCHAIN_EXTENSION_NAME))
return false;
inline UInt32 Swapchain::GetImageCount() const
{
return static_cast<UInt32>(m_images.size());
}
return true;
}
inline bool Swapchain::IsSupported() const
{
if (!m_device->IsExtensionLoaded(VK_KHR_SWAPCHAIN_EXTENSION_NAME))
return false;
inline VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle)
{
return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle);
}
return true;
}
inline void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroySwapchainKHR(device, handle, allocator);
}
inline VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle)
{
return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle);
}
inline void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroySwapchainKHR(device, handle, allocator);
}
}

View File

@ -230,11 +230,12 @@ namespace Nz
PerViewerData& viewerData = *Retrieve(m_viewerData, viewer);
viewerData.textureArrayAttachmentIndex = frameGraph.AddAttachmentArray({
"Directional-light cascade shadowmaps",
m_light.GetShadowMapFormat(),
FramePassAttachmentSize::Fixed,
shadowMapSize, shadowMapSize,
}, m_cascadeCount);
.name = "Directional-light cascade shadowmaps",
.format = m_light.GetShadowMapFormat(),
.size = FramePassAttachmentSize::Fixed,
.width = shadowMapSize,
.height = shadowMapSize,
}, SafeCast<unsigned int>(m_cascadeCount));
for (std::size_t i = 0; i < viewerData.cascades.size(); ++i)
{

View File

@ -470,23 +470,6 @@ namespace Nz
}
});
}
for (auto&& [_, renderTargetData] : m_renderTargets)
{
if (renderTargetData.blitShaderBinding)
renderFrame.PushForRelease(std::move(renderTargetData.blitShaderBinding));
renderTargetData.blitShaderBinding = graphics->GetBlitPipelineLayout()->AllocateShaderBinding(0);
renderTargetData.blitShaderBinding->Update({
{
0,
ShaderBinding::SampledTextureBinding {
m_bakedFrameGraph.GetAttachmentTexture(renderTargetData.finalAttachment).get(),
sampler.get()
}
}
});
}
}
// Update UBOs and materials
@ -511,39 +494,14 @@ namespace Nz
m_rebuildFrameGraph = false;
// Final blit (TODO: Make part of frame graph)
const Vector2ui& frameSize = renderFrame.GetSize();
for (auto&& [renderTargetPtr, renderTargetData] : m_renderTargets)
{
Recti renderRegion(0, 0, frameSize.x, frameSize.y);
const RenderTarget& renderTarget = *renderTargetPtr;
const auto& data = renderTargetData;
renderFrame.Execute([&](CommandBufferBuilder& builder)
{
const std::shared_ptr<Texture>& sourceTexture = m_bakedFrameGraph.GetAttachmentTexture(data.finalAttachment);
builder.TextureBarrier(PipelineStage::ColorOutput, PipelineStage::FragmentShader, MemoryAccess::ColorWrite, MemoryAccess::ShaderRead, TextureLayout::ColorOutput, TextureLayout::ColorInput, *sourceTexture);
std::array<CommandBufferBuilder::ClearValues, 2> clearValues;
clearValues[0].color = Color::Black();
clearValues[1].depth = 1.f;
clearValues[1].stencil = 0;
builder.BeginRenderPass(renderTarget.GetFramebuffer(renderFrame.GetFramebufferIndex()), renderTarget.GetRenderPass(), renderRegion, { clearValues[0], clearValues[1] });
{
builder.BeginDebugRegion("Main window rendering", Color::Green());
{
builder.SetScissor(renderRegion);
builder.SetViewport(renderRegion);
builder.BindRenderPipeline(*graphics->GetBlitPipeline(false));
builder.BindRenderShaderBinding(0, *data.blitShaderBinding);
builder.Draw(3);
}
builder.EndDebugRegion();
}
builder.EndRenderPass();
renderTarget.BlitTexture(renderFrame, builder, *sourceTexture);
}, QueueType::Graphics);
}
@ -748,13 +706,7 @@ namespace Nz
return lhs.second->renderOrder < rhs.second->renderOrder;
});
for (auto&& [_, renderTargetData] : m_renderTargets)
{
if (renderTargetData.blitShaderBinding)
renderFrame.PushForRelease(std::move(renderTargetData.blitShaderBinding));
}
m_renderTargets.clear();
for (auto&& [renderTarget, viewerData] : viewers)
{
auto& renderTargetData = m_renderTargets[renderTarget];

View File

@ -1035,6 +1035,7 @@ namespace Nz
data.height = attachmentData.height;
data.size = attachmentData.size;
data.layerCount = 1;
data.usage = attachmentData.additionalUsage;
return textureId;
}
@ -1077,6 +1078,7 @@ namespace Nz
data.height = attachmentData.height;
data.size = attachmentData.size;
data.layerCount = attachmentData.layerCount;
data.usage = attachmentData.additionalUsage;
return textureId;
}
@ -1118,6 +1120,7 @@ namespace Nz
data.height = attachmentData.height;
data.size = attachmentData.size;
data.layerCount = 1;
data.usage = attachmentData.additionalUsage;
return textureId;
}

View File

@ -148,10 +148,11 @@ namespace Nz
UInt32 shadowMapSize = m_light.GetShadowMapSize();
m_cubeAttachmentIndex = frameGraph.AddAttachmentCube({
"Point-light shadowmap",
m_light.GetShadowMapFormat(),
FramePassAttachmentSize::Fixed,
shadowMapSize, shadowMapSize,
.name = "Point-light shadowmap",
.format = m_light.GetShadowMapFormat(),
.size = FramePassAttachmentSize::Fixed,
.width = shadowMapSize,
.height = shadowMapSize,
});
for (std::size_t i = 0; i < m_directions.size(); ++i)

View File

@ -102,10 +102,11 @@ namespace Nz
UInt32 shadowMapSize = m_light.GetShadowMapSize();
m_attachmentIndex = frameGraph.AddAttachment({
"Shadowmap",
m_light.GetShadowMapFormat(),
FramePassAttachmentSize::Fixed,
shadowMapSize, shadowMapSize,
.name = "Shadowmap",
.format = m_light.GetShadowMapFormat(),
.size = FramePassAttachmentSize::Fixed,
.width = shadowMapSize,
.height = shadowMapSize,
});
FramePipelinePass::PassInputOuputs passInputOuputs;

View File

@ -146,6 +146,11 @@ namespace Nz
context->BlitTexture(*command.source, *command.target, command.sourceBox, command.targetBox, command.filter);
}
inline void OpenGLCommandBuffer::Execute(const GL::Context* context, const BlitTextureToWindowCommand& command)
{
context->BlitTextureToWindow(*command.source, command.sourceBox, command.targetBox, command.filter);
}
inline void OpenGLCommandBuffer::Execute(const GL::Context* /*context*/, const BuildTextureMipmapsCommand& command)
{
command.texture->GenerateMipmaps(command.baseLevel, command.levelCount);

View File

@ -9,6 +9,7 @@
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
#include <Nazara/OpenGLRenderer/OpenGLSwapchain.hpp>
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
#include <Nazara/OpenGLRenderer/OpenGLUploadPool.hpp>
#include <NazaraUtils/StackArray.hpp>
@ -24,101 +25,114 @@ namespace Nz
void OpenGLCommandBufferBuilder::BeginRenderPass(const Framebuffer& framebuffer, const RenderPass& renderPass, const Recti& /*renderRect*/, const ClearValues* clearValues, std::size_t clearValueCount)
{
m_commandBuffer.SetFramebuffer(static_cast<const OpenGLFramebuffer&>(framebuffer), static_cast<const OpenGLRenderPass&>(renderPass), clearValues, clearValueCount);
m_commandBuffer.SetFramebuffer(SafeCast<const OpenGLFramebuffer&>(framebuffer), SafeCast<const OpenGLRenderPass&>(renderPass), clearValues, clearValueCount);
}
void OpenGLCommandBufferBuilder::BindComputePipeline(const ComputePipeline& pipeline)
{
const OpenGLComputePipeline& glPipeline = static_cast<const OpenGLComputePipeline&>(pipeline);
const OpenGLComputePipeline& glPipeline = SafeCast<const OpenGLComputePipeline&>(pipeline);
m_commandBuffer.BindComputePipeline(&glPipeline);
}
void OpenGLCommandBufferBuilder::BindComputeShaderBinding(UInt32 set, const ShaderBinding& binding)
{
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
m_commandBuffer.BindComputeShaderBinding(glBinding.GetOwner(), set, &glBinding);
}
void OpenGLCommandBufferBuilder::BindComputeShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
{
const OpenGLRenderPipelineLayout& glPipelineLayout = static_cast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
const OpenGLRenderPipelineLayout& glPipelineLayout = SafeCast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
m_commandBuffer.BindComputeShaderBinding(glPipelineLayout, set, &glBinding);
}
void OpenGLCommandBufferBuilder::BindIndexBuffer(const RenderBuffer& indexBuffer, IndexType indexType, UInt64 offset)
{
const OpenGLBuffer& glBuffer = static_cast<const OpenGLBuffer&>(indexBuffer);
const OpenGLBuffer& glBuffer = SafeCast<const OpenGLBuffer&>(indexBuffer);
m_commandBuffer.BindIndexBuffer(glBuffer.GetBuffer().GetObjectId(), indexType, offset);
}
void OpenGLCommandBufferBuilder::BindRenderPipeline(const RenderPipeline& pipeline)
{
const OpenGLRenderPipeline& glPipeline = static_cast<const OpenGLRenderPipeline&>(pipeline);
const OpenGLRenderPipeline& glPipeline = SafeCast<const OpenGLRenderPipeline&>(pipeline);
m_commandBuffer.BindRenderPipeline(&glPipeline);
}
void OpenGLCommandBufferBuilder::BindRenderShaderBinding(UInt32 set, const ShaderBinding& binding)
{
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
m_commandBuffer.BindRenderShaderBinding(glBinding.GetOwner(), set, &glBinding);
}
void OpenGLCommandBufferBuilder::BindRenderShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
{
const OpenGLRenderPipelineLayout& glPipelineLayout = static_cast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
const OpenGLRenderPipelineLayout& glPipelineLayout = SafeCast<const OpenGLRenderPipelineLayout&>(pipelineLayout);
const OpenGLShaderBinding& glBinding = SafeCast<const OpenGLShaderBinding&>(binding);
m_commandBuffer.BindRenderShaderBinding(glPipelineLayout, set, &glBinding);
}
void OpenGLCommandBufferBuilder::BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset)
{
const OpenGLBuffer& glBuffer = static_cast<const OpenGLBuffer&>(vertexBuffer);
const OpenGLBuffer& glBuffer = SafeCast<const OpenGLBuffer&>(vertexBuffer);
m_commandBuffer.BindVertexBuffer(binding, glBuffer.GetBuffer().GetObjectId(), offset);
}
void OpenGLCommandBufferBuilder::BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout /*fromLayout*/, const Texture& toTexture, const Boxui& toBox, TextureLayout /*toLayout*/, SamplerFilter filter)
{
const OpenGLTexture& sourceTexture = static_cast<const OpenGLTexture&>(fromTexture);
const OpenGLTexture& targetTexture = static_cast<const OpenGLTexture&>(toTexture);
const OpenGLTexture& sourceTexture = SafeCast<const OpenGLTexture&>(fromTexture);
const OpenGLTexture& targetTexture = SafeCast<const OpenGLTexture&>(toTexture);
m_commandBuffer.BlitTexture(sourceTexture, fromBox, targetTexture, toBox, filter);
}
void OpenGLCommandBufferBuilder::BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex)
{
const OpenGLTexture& glTexture = SafeCast<const OpenGLTexture&>(fromTexture);
const OpenGLSwapchain& glSwapchain = SafeCast<const OpenGLSwapchain&>(swapchain);
Vector2ui swapchainSize = glSwapchain.GetSize();
// We set the framebuffer to ensure the correct OpenGL context is activated (in case we're using multiple contextes)
m_commandBuffer.SetFramebuffer(glSwapchain.GetFramebuffer(imageIndex), glSwapchain.GetRenderPass(), nullptr, 0);
m_commandBuffer.BlitTextureToWindow(glTexture, fromBox, Boxui(0, 0, 0, swapchainSize.x, swapchainSize.y, 1), SamplerFilter::Linear);
}
void OpenGLCommandBufferBuilder::BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags /*dstStageMask*/, MemoryAccessFlags /*srcAccessMask*/, MemoryAccessFlags /*dstAccessMask*/, TextureLayout /*oldLayout*/, TextureLayout /*newLayout*/)
{
OpenGLTexture& glTexture = static_cast<OpenGLTexture&>(texture);
OpenGLTexture& glTexture = SafeCast<OpenGLTexture&>(texture);
m_commandBuffer.BuildMipmaps(glTexture, baseLevel, levelCount);
}
void OpenGLCommandBufferBuilder::CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
OpenGLBuffer& sourceBuffer = *static_cast<OpenGLBuffer*>(source.GetBuffer());
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
OpenGLBuffer& sourceBuffer = *SafeCast<OpenGLBuffer*>(source.GetBuffer());
OpenGLBuffer& targetBuffer = *SafeCast<OpenGLBuffer*>(target.GetBuffer());
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer().GetObjectId(), targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
}
void OpenGLCommandBufferBuilder::CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
OpenGLBuffer& targetBuffer = *SafeCast<OpenGLBuffer*>(target.GetBuffer());
m_commandBuffer.CopyBuffer(allocation, targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset, target.GetOffset() + targetOffset);
}
void OpenGLCommandBufferBuilder::CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout /*fromLayout*/, const Texture& toTexture, const Vector3ui& toPos, TextureLayout /*toLayout*/)
{
const OpenGLTexture& sourceTexture = static_cast<const OpenGLTexture&>(fromTexture);
const OpenGLTexture& targetTexture = static_cast<const OpenGLTexture&>(toTexture);
const OpenGLTexture& sourceTexture = SafeCast<const OpenGLTexture&>(fromTexture);
const OpenGLTexture& targetTexture = SafeCast<const OpenGLTexture&>(toTexture);
m_commandBuffer.CopyTexture(sourceTexture, fromBox, targetTexture, toPos);
}

View File

@ -12,6 +12,7 @@
namespace Nz
{
OpenGLRenderImage::OpenGLRenderImage(OpenGLSwapchain& owner) :
RenderImage(owner.GetDevice()),
m_owner(owner),
m_uploadPool(2 * 1024 * 1024)
{

View File

@ -12,6 +12,7 @@ namespace Nz
{
OpenGLSwapchain::OpenGLSwapchain(OpenGLDevice& device, WindowHandle windowHandle, const Vector2ui& windowSize, const SwapchainParameters& parameters) :
m_currentFrame(0),
m_device(device),
m_framebuffer(*this),
m_size(windowSize),
m_sizeInvalidated(false)
@ -22,7 +23,7 @@ namespace Nz
#endif
//TODO: Pass swapchain parameters to context
m_context = device.CreateContext(contextParams, windowHandle);
m_context = m_device.CreateContext(contextParams, windowHandle);
if (!m_context)
throw std::runtime_error("failed to create swapchain context");

View File

@ -310,7 +310,7 @@ namespace Nz::GL
}
}
bool Context::BlitTexture(const OpenGLTexture& texture, const OpenGLTexture& destination, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
bool Context::BlitTexture(const OpenGLTexture& source, const OpenGLTexture& destination, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
{
if (!m_blitFramebuffers && !InitializeBlitFramebuffers())
return false;
@ -363,7 +363,7 @@ namespace Nz::GL
};
// Attach textures to color attachment
BindTexture(m_blitFramebuffers->readFBO, texture);
BindTexture(m_blitFramebuffers->readFBO, source);
BindTexture(m_blitFramebuffers->drawFBO, destination);
// Validate framebuffer completeness
@ -383,6 +383,29 @@ namespace Nz::GL
return true;
}
bool Context::BlitTextureToWindow(const OpenGLTexture& texture, const Boxui& srcBox, const Boxui& dstBox, SamplerFilter filter) const
{
if (!m_blitFramebuffers && !InitializeBlitFramebuffers())
return false;
// Bind framebuffers before configuring them (so they won't override each other)
BindFramebuffer(FramebufferTarget::Draw, 0);
BindFramebuffer(FramebufferTarget::Read, m_blitFramebuffers->readFBO.GetObjectId());
// Attach textures to color attachment
BindTextureToFramebuffer(m_blitFramebuffers->readFBO, texture);
// Validate framebuffer completeness
if (GLenum checkResult = m_blitFramebuffers->readFBO.Check(); checkResult != GL_FRAMEBUFFER_COMPLETE)
{
NazaraErrorFmt("blit read FBO is incomplete: {0}", TranslateOpenGLError(checkResult));
return false;
}
glBlitFramebuffer(srcBox.x, srcBox.y, srcBox.x + srcBox.width, srcBox.y + srcBox.height, dstBox.x, dstBox.y + srcBox.height, dstBox.x + dstBox.width, dstBox.y, GL_COLOR_BUFFER_BIT, ToOpenGL(filter));
return true;
}
bool Context::ClearErrorStack() const
{
assert(GetCurrentContext() == this);
@ -1196,4 +1219,47 @@ namespace Nz::GL
return true;
}
void Context::BindTextureToFramebuffer(Framebuffer& framebuffer, const OpenGLTexture& texture)
{
if (texture.RequiresTextureViewEmulation())
{
const TextureViewInfo& texViewInfo = texture.GetTextureViewInfo();
if (texViewInfo.viewType != ImageType::E2D)
throw std::runtime_error("unrestricted texture views can only be used as 2D texture attachment");
const OpenGLTexture& parentTexture = *texture.GetParentTexture();
switch (parentTexture.GetType())
{
case ImageType::Cubemap:
{
constexpr std::array<GLenum, 6> faceTargets = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
assert(texViewInfo.baseArrayLayer < faceTargets.size());
GLenum texTarget = faceTargets[texViewInfo.baseArrayLayer];
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, texTarget, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseMipLevel);
break;
}
case ImageType::E1D:
case ImageType::E2D:
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseMipLevel);
break;
case ImageType::E1D_Array:
case ImageType::E2D_Array:
case ImageType::E3D:
framebuffer.TextureLayer(GL_COLOR_ATTACHMENT0, parentTexture.GetTexture().GetObjectId(), texViewInfo.baseArrayLayer, texViewInfo.baseMipLevel);
break;
}
}
else
{
if (texture.GetTexture().GetTarget() != TextureTarget::Target2D)
throw std::runtime_error("blit is not yet supported from/to other texture type than 2D textures");
framebuffer.Texture2D(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.GetTexture().GetObjectId(), 0);
}
}
}

View File

@ -3,12 +3,28 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Swapchain.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
Swapchain::~Swapchain() = default;
void Swapchain::BlitTexture(RenderFrame& renderFrame, CommandBufferBuilder& builder, const Texture& texture) const
{
Vector2ui textureSize = Vector2ui(texture.GetSize());
Boxui blitRegion(0, 0, 0, textureSize.x, textureSize.y, 1);
builder.TextureBarrier(PipelineStage::ColorOutput, PipelineStage::Transfer, MemoryAccess::ColorWrite, MemoryAccess::TransferRead, TextureLayout::ColorOutput, TextureLayout::TransferSource, texture);
builder.BeginDebugRegion("Blit to swapchain", Color::Blue());
{
builder.BlitTextureToSwapchain(texture, blitRegion, TextureLayout::TransferSource, *this, renderFrame.GetFramebufferIndex());
}
builder.EndDebugRegion();
}
void Swapchain::BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies)
{
assert(colorFormat != PixelFormat::Undefined);

View File

@ -10,10 +10,12 @@
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
#include <Nazara/VulkanRenderer/VulkanSwapchain.hpp>
#include <Nazara/VulkanRenderer/VulkanTexture.hpp>
#include <Nazara/VulkanRenderer/VulkanTextureFramebuffer.hpp>
#include <Nazara/VulkanRenderer/VulkanUploadPool.hpp>
#include <Nazara/VulkanRenderer/VulkanWindowFramebuffer.hpp>
#include <NazaraUtils/Algorithm.hpp>
#include <NazaraUtils/StackArray.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
@ -31,8 +33,8 @@ namespace Nz
void VulkanCommandBufferBuilder::BeginRenderPass(const Framebuffer& framebuffer, const RenderPass& renderPass, const Recti& renderRect, const ClearValues* clearValues, std::size_t clearValueCount)
{
const VulkanRenderPass& vkRenderPass = static_cast<const VulkanRenderPass&>(renderPass);
const VulkanFramebuffer& vkFramebuffer = static_cast<const VulkanFramebuffer&>(framebuffer);
const VulkanRenderPass& vkRenderPass = SafeCast<const VulkanRenderPass&>(renderPass);
const VulkanFramebuffer& vkFramebuffer = SafeCast<const VulkanFramebuffer&>(framebuffer);
std::size_t attachmentCount = vkRenderPass.GetAttachmentCount();
@ -80,14 +82,14 @@ namespace Nz
void VulkanCommandBufferBuilder::BindComputePipeline(const ComputePipeline& pipeline)
{
const VulkanComputePipeline& vkPipeline = static_cast<const VulkanComputePipeline&>(pipeline);
const VulkanComputePipeline& vkPipeline = SafeCast<const VulkanComputePipeline&>(pipeline);
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_COMPUTE, vkPipeline.GetPipeline());
}
void VulkanCommandBufferBuilder::BindComputeShaderBinding(UInt32 set, const ShaderBinding& binding)
{
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
const VulkanRenderPipelineLayout& pipelineLayout = vkBinding.GetOwner();
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
@ -95,15 +97,15 @@ namespace Nz
void VulkanCommandBufferBuilder::BindComputeShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
{
const VulkanRenderPipelineLayout& vkPipelineLayout = static_cast<const VulkanRenderPipelineLayout&>(pipelineLayout);
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
const VulkanRenderPipelineLayout& vkPipelineLayout = SafeCast<const VulkanRenderPipelineLayout&>(pipelineLayout);
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, vkPipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
}
void VulkanCommandBufferBuilder::BindIndexBuffer(const RenderBuffer& indexBuffer, IndexType indexType, UInt64 offset)
{
const VulkanBuffer& vkBuffer = static_cast<const VulkanBuffer&>(indexBuffer);
const VulkanBuffer& vkBuffer = SafeCast<const VulkanBuffer&>(indexBuffer);
m_commandBuffer.BindIndexBuffer(vkBuffer.GetBuffer(), offset, ToVulkan(indexType));
}
@ -113,14 +115,14 @@ namespace Nz
if (!m_currentRenderPass)
throw std::runtime_error("BindPipeline must be called in a RenderPass");
const VulkanRenderPipeline& vkPipeline = static_cast<const VulkanRenderPipeline&>(pipeline);
const VulkanRenderPipeline& vkPipeline = SafeCast<const VulkanRenderPipeline&>(pipeline);
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipeline.Get(*m_currentRenderPass, m_currentSubpassIndex));
}
void VulkanCommandBufferBuilder::BindRenderShaderBinding(UInt32 set, const ShaderBinding& binding)
{
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
const VulkanRenderPipelineLayout& pipelineLayout = vkBinding.GetOwner();
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
@ -128,23 +130,23 @@ namespace Nz
void VulkanCommandBufferBuilder::BindRenderShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding)
{
const VulkanRenderPipelineLayout& vkPipelineLayout = static_cast<const VulkanRenderPipelineLayout&>(pipelineLayout);
const VulkanShaderBinding& vkBinding = static_cast<const VulkanShaderBinding&>(binding);
const VulkanRenderPipelineLayout& vkPipelineLayout = SafeCast<const VulkanRenderPipelineLayout&>(pipelineLayout);
const VulkanShaderBinding& vkBinding = SafeCast<const VulkanShaderBinding&>(binding);
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipelineLayout.GetPipelineLayout(), set, vkBinding.GetDescriptorSet());
}
void VulkanCommandBufferBuilder::BindVertexBuffer(UInt32 binding, const RenderBuffer& vertexBuffer, UInt64 offset)
{
const VulkanBuffer& vkBuffer = static_cast<const VulkanBuffer&>(vertexBuffer);
const VulkanBuffer& vkBuffer = SafeCast<const VulkanBuffer&>(vertexBuffer);
m_commandBuffer.BindVertexBuffer(binding, vkBuffer.GetBuffer(), offset);
}
void VulkanCommandBufferBuilder::BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter)
{
const VulkanTexture& vkFromTexture = static_cast<const VulkanTexture&>(fromTexture);
const VulkanTexture& vkToTexture = static_cast<const VulkanTexture&>(toTexture);
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
const VulkanTexture& vkToTexture = SafeCast<const VulkanTexture&>(toTexture);
unsigned int fromBaseLayer, fromLayerCount;
Image::RegionToArray(vkFromTexture.GetType(), fromBox, fromBaseLayer, fromLayerCount);
@ -184,9 +186,52 @@ namespace Nz
m_commandBuffer.BlitImage(vkFromTexture.GetImage(), ToVulkan(fromLayout), vkToTexture.GetImage(), ToVulkan(toLayout), region, ToVulkan(filter));
}
void VulkanCommandBufferBuilder::BlitTextureToSwapchain(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Swapchain& swapchain, std::size_t imageIndex)
{
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
const VulkanSwapchain& vkSwapchain = SafeCast<const VulkanSwapchain&>(swapchain);
VkImage swapchainImage = vkSwapchain.GetImage(imageIndex);
VkImageSubresourceRange swapchainImageRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
};
Boxi fromBoxInt = Boxi(fromBox);
Vector2i swapchainSize = Vector2i(vkSwapchain.GetSize());
VkImageBlit swapchainBlit = {
.srcSubresource = vkFromTexture.BuildSubresourceLayers(0),
.srcOffsets = {
{ fromBoxInt.x, fromBoxInt.y, fromBoxInt.z },
{ fromBoxInt.x + fromBoxInt.width, fromBoxInt.y + fromBoxInt.height, fromBoxInt.z + fromBoxInt.depth }
},
.dstSubresource = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
.baseArrayLayer = 0,
.layerCount = 1,
},
.dstOffsets = {
{ 0, 0, 0 },
{ swapchainSize.x, swapchainSize.y, 1 }
},
};
m_commandBuffer.SetImageLayout(swapchainImage, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, swapchainImageRange);
m_commandBuffer.BlitImage(vkFromTexture.GetImage(), ToVulkan(fromLayout), vkSwapchain.GetImage(imageIndex), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, swapchainBlit, VK_FILTER_LINEAR);
m_commandBuffer.SetImageLayout(swapchainImage, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, swapchainImageRange);
}
void VulkanCommandBufferBuilder::BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 levelCount, PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout)
{
VulkanTexture& vkTexture = static_cast<VulkanTexture&>(texture);
VulkanTexture& vkTexture = SafeCast<VulkanTexture&>(texture);
VkImage vkImage = vkTexture.GetImage();
const TextureInfo& textureInfo = vkTexture.GetTextureInfo();
@ -246,24 +291,24 @@ namespace Nz
void VulkanCommandBufferBuilder::CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
VulkanBuffer& sourceBuffer = *static_cast<VulkanBuffer*>(source.GetBuffer());
VulkanBuffer& targetBuffer = *static_cast<VulkanBuffer*>(target.GetBuffer());
VulkanBuffer& sourceBuffer = *SafeCast<VulkanBuffer*>(source.GetBuffer());
VulkanBuffer& targetBuffer = *SafeCast<VulkanBuffer*>(target.GetBuffer());
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer(), targetBuffer.GetBuffer(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
}
void VulkanCommandBufferBuilder::CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
const auto& vkAllocation = static_cast<const VulkanUploadPool::VulkanAllocation&>(allocation);
VulkanBuffer& targetBuffer = *static_cast<VulkanBuffer*>(target.GetBuffer());
const auto& vkAllocation = SafeCast<const VulkanUploadPool::VulkanAllocation&>(allocation);
VulkanBuffer& targetBuffer = *SafeCast<VulkanBuffer*>(target.GetBuffer());
m_commandBuffer.CopyBuffer(vkAllocation.buffer, targetBuffer.GetBuffer(), size, vkAllocation.offset + sourceOffset, target.GetOffset() + targetOffset);
}
void VulkanCommandBufferBuilder::CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Vector3ui& toPos, TextureLayout toLayout)
{
const VulkanTexture& vkFromTexture = static_cast<const VulkanTexture&>(fromTexture);
const VulkanTexture& vkToTexture = static_cast<const VulkanTexture&>(toTexture);
const VulkanTexture& vkFromTexture = SafeCast<const VulkanTexture&>(fromTexture);
const VulkanTexture& vkToTexture = SafeCast<const VulkanTexture&>(toTexture);
unsigned int fromBaseLayer, fromLayerCount;
Image::RegionToArray(vkFromTexture.GetType(), fromBox, fromBaseLayer, fromLayerCount);
@ -358,7 +403,7 @@ namespace Nz
void VulkanCommandBufferBuilder::TextureBarrier(PipelineStageFlags srcStageMask, PipelineStageFlags dstStageMask, MemoryAccessFlags srcAccessMask, MemoryAccessFlags dstAccessMask, TextureLayout oldLayout, TextureLayout newLayout, const Texture& texture)
{
const VulkanTexture& vkTexture = static_cast<const VulkanTexture&>(texture);
const VulkanTexture& vkTexture = SafeCast<const VulkanTexture&>(texture);
m_commandBuffer.ImageBarrier(ToVulkan(srcStageMask), ToVulkan(dstStageMask), VkDependencyFlags(0), ToVulkan(srcAccessMask), ToVulkan(dstAccessMask), ToVulkan(oldLayout), ToVulkan(newLayout), vkTexture.GetImage(), vkTexture.GetSubresourceRange());
}

View File

@ -13,6 +13,7 @@
namespace Nz
{
VulkanRenderImage::VulkanRenderImage(VulkanSwapchain& owner) :
RenderImage(owner.GetDevice()),
m_freeCommandBufferIndex(0),
m_owner(owner),
m_uploadPool(m_owner.GetDevice(), 2 * 1024 * 1024)

View File

@ -293,10 +293,10 @@ namespace Nz
return true;
}
const VulkanWindowFramebuffer& VulkanSwapchain::GetFramebuffer(std::size_t i) const
const VulkanWindowFramebuffer& VulkanSwapchain::GetFramebuffer(std::size_t imageIndex) const
{
assert(i < m_framebuffers.size());
return m_framebuffers[i];
assert(imageIndex < m_framebuffers.size());
return m_framebuffers[imageIndex];
}
std::size_t VulkanSwapchain::GetFramebufferCount() const
@ -628,7 +628,7 @@ namespace Nz
m_surfaceFormat.colorSpace,
extent,
1,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_SHARING_MODE_EXCLUSIVE,
0, nullptr,
surfaceCapabilities.currentTransform,