Renderer: Add mipmaps generation support

This commit is contained in:
SirLynix
2023-05-14 18:55:41 +02:00
parent 3712b641f8
commit 1d32af53c5
33 changed files with 488 additions and 183 deletions

View File

@@ -8,7 +8,6 @@
#define NAZARA_OPENGLRENDERER_OPENGLCOMMANDBUFFER_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <NazaraUtils/TypeList.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
@@ -17,6 +16,7 @@
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
#include <Nazara/Renderer/CommandBuffer.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <NazaraUtils/TypeList.hpp>
#include <optional>
#include <variant>
#include <vector>
@@ -46,8 +46,11 @@ namespace Nz
inline void BindRenderPipeline(const OpenGLRenderPipeline* pipeline);
inline void BindRenderShaderBinding(const OpenGLRenderPipelineLayout& pipelineLayout, UInt32 set, const OpenGLShaderBinding* binding);
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 BuildMipmaps(OpenGLTexture& texture, UInt8 baseLevel, UInt8 levelCount);
inline void CopyBuffer(GLuint source, GLuint target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0);
inline void CopyBuffer(const UploadPool::Allocation& allocation, GLuint target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0);
inline void CopyTexture(const OpenGLTexture& source, const Boxui& sourceBox, const OpenGLTexture& target, const Vector3ui& targetPoint);
@@ -83,6 +86,7 @@ namespace Nz
#define NAZARA_OPENGL_FOREACH_COMMANDS(cb, lastCb) \
cb(BeginDebugRegionCommand) \
cb(BlitTextureCommand) \
cb(BuildTextureMipmapsCommand) \
cb(CopyBufferCommand) \
cb(CopyBufferFromMemoryCommand) \
cb(CopyTextureCommand) \
@@ -112,6 +116,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 BuildTextureMipmapsCommand& command);
inline void Execute(const GL::Context* context, const CopyBufferCommand& command);
inline void Execute(const GL::Context* context, const CopyBufferFromMemoryCommand& command);
inline void Execute(const GL::Context* context, const CopyTextureCommand& command);
@@ -139,6 +144,13 @@ namespace Nz
SamplerFilter filter;
};
struct BuildTextureMipmapsCommand
{
OpenGLTexture* texture;
UInt8 baseLevel;
UInt8 levelCount;
};
struct ComputeStates
{
const OpenGLComputePipeline* pipeline = nullptr;

View File

@@ -88,6 +88,17 @@ namespace Nz
m_commands.emplace_back(std::move(blitTexture));
}
inline void OpenGLCommandBuffer::BuildMipmaps(OpenGLTexture& texture, UInt8 baseLevel, UInt8 levelCount)
{
BuildTextureMipmapsCommand buildMipmaps = {
&texture,
baseLevel,
levelCount
};
m_commands.emplace_back(std::move(buildMipmaps));
}
inline void OpenGLCommandBuffer::CopyBuffer(GLuint source, GLuint target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
CopyBufferCommand copyBuffer = {

View File

@@ -38,6 +38,8 @@ namespace Nz
void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) override;
void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 maxLevel) override;
void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0) override;
void CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0) override;
void CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Vector3ui& toPos, TextureLayout toLayout) override;

View File

@@ -47,6 +47,7 @@ namespace Nz
std::shared_ptr<ShaderModule> InstantiateShaderModule(nzsl::ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const nzsl::ShaderWriter::States& states) override;
std::shared_ptr<Swapchain> InstantiateSwapchain(WindowHandle windowHandle, const Vector2ui& windowSize, const SwapchainParameters& parameters) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params, const void* initialData, bool buildMipmaps, unsigned int srcWidth = 0, unsigned int srcHeight = 0) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const override;

View File

@@ -20,6 +20,7 @@ namespace Nz
{
public:
OpenGLTexture(OpenGLDevice& device, const TextureInfo& textureInfo);
OpenGLTexture(OpenGLDevice& device, const TextureInfo& textureInfo, const void* initialData, bool buildMipmaps, unsigned int srcWidth = 0, unsigned int srcHeight = 0);
OpenGLTexture(std::shared_ptr<OpenGLTexture> parentTexture, const TextureViewInfo& viewInfo);
OpenGLTexture(const OpenGLTexture&) = delete;
OpenGLTexture(OpenGLTexture&&) = delete;
@@ -28,6 +29,8 @@ namespace Nz
bool Copy(const Texture& source, const Boxui& srcBox, const Vector3ui& dstPos) override;
std::shared_ptr<Texture> CreateView(const TextureViewInfo& viewInfo) override;
inline void GenerateMipmaps(UInt8 baseLevel, UInt8 levelCount);
inline PixelFormat GetFormat() const override;
inline UInt8 GetLevelCount() const override;
inline OpenGLTexture* GetParentTexture() const override;

View File

@@ -2,11 +2,42 @@
// This file is part of the "Nazara Engine - OpenGL renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
inline void OpenGLTexture::GenerateMipmaps(UInt8 baseLevel, UInt8 levelCount)
{
NazaraAssert(baseLevel + levelCount < m_textureInfo.levelCount, "out of bounds");
GL::Texture* targetTexture;
if (RequiresTextureViewEmulation())
{
baseLevel += m_viewInfo->baseMipLevel;
levelCount += m_viewInfo->baseMipLevel;
targetTexture = &m_parentTexture->m_texture;
}
else
targetTexture = &m_texture;
if (baseLevel != 0)
targetTexture->SetParameteri(GL_TEXTURE_BASE_LEVEL, baseLevel);
if (levelCount != m_textureInfo.levelCount)
targetTexture->SetParameteri(GL_TEXTURE_MAX_LEVEL, levelCount);
targetTexture->GenerateMipmap();
// Reset level config
if (baseLevel != 0)
targetTexture->SetParameteri(GL_TEXTURE_BASE_LEVEL, 0);
if (levelCount != m_textureInfo.levelCount)
targetTexture->SetParameteri(GL_TEXTURE_MAX_LEVEL, m_textureInfo.levelCount);
}
inline PixelFormat OpenGLTexture::GetFormat() const
{
return m_textureInfo.pixelFormat;

View File

@@ -24,6 +24,8 @@ namespace Nz::GL
Texture(Texture&&) noexcept = default;
~Texture() = default;
inline void GenerateMipmap();
inline TextureTarget GetTarget() const;
inline void SetParameterf(GLenum pname, GLfloat param);

View File

@@ -7,6 +7,13 @@
namespace Nz::GL
{
inline void Texture::GenerateMipmap()
{
const Context& context = EnsureDeviceContext();
context.BindTexture(m_target, m_objectId);
context.glGenerateMipmap(ToOpenGL(m_target));
}
inline TextureTarget Texture::GetTarget() const
{
return m_target;

View File

@@ -54,6 +54,8 @@ namespace Nz
virtual void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) = 0;
virtual void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 maxLevel) = 0;
inline void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target);
virtual void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 fromOffset = 0, UInt64 toOffset = 0) = 0;
inline void CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target);

View File

@@ -53,6 +53,7 @@ namespace Nz
std::shared_ptr<ShaderModule> InstantiateShaderModule(nzsl::ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath, const nzsl::ShaderWriter::States& states);
virtual std::shared_ptr<Swapchain> InstantiateSwapchain(WindowHandle windowHandle, const Vector2ui& windowSize, const SwapchainParameters& parameters) = 0;
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params, const void* initialData, bool buildMipmaps, unsigned int srcWidth = 0, unsigned int srcHeight = 0) = 0;
virtual std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) = 0;
virtual bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const = 0;

View File

@@ -25,8 +25,8 @@ namespace Nz
{
PixelFormat pixelFormat;
ImageType type;
TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination;
UInt8 levelCount = 1;
TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination | TextureUsage::TransferSource;
UInt8 levelCount = 0xFF;
unsigned int layerCount = 1;
unsigned int depth = 1;
unsigned int height;
@@ -46,7 +46,8 @@ namespace Nz
struct NAZARA_RENDERER_API TextureParams : ImageParams
{
std::shared_ptr<RenderDevice> renderDevice;
TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination;
TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination | TextureUsage::TransferSource;
bool buildMipmaps = true;
bool IsValid() const;
void Merge(const TextureParams& params);

View File

@@ -62,8 +62,9 @@ namespace Nz
Vertex,
Storage,
Uniform,
Upload,
Max = Uniform
Max = Upload
};
enum class BufferUsage

View File

@@ -110,9 +110,11 @@ namespace Nz
Image& operator=(const Image& image);
inline Image& operator=(Image&& image) noexcept;
static inline void ArrayToRegion(ImageType type, unsigned int baseLayer, unsigned int layerCount, Boxui& region);
static void Copy(UInt8* destination, const UInt8* source, PixelFormat format, unsigned int width, unsigned int height, unsigned int depth = 1, unsigned int dstWidth = 0, unsigned int dstHeight = 0, unsigned int srcWidth = 0, unsigned int srcHeight = 0);
static UInt8 GetMaxLevel(unsigned int width, unsigned int height, unsigned int depth = 1);
static UInt8 GetMaxLevel(ImageType type, unsigned int width, unsigned int height, unsigned int depth = 1);
static inline Boxui RegionToArray(ImageType type, Boxui region, unsigned int& baseLayer, unsigned int& layerCount);
// Load
static std::shared_ptr<Image> LoadFromFile(const std::filesystem::path& filePath, const ImageParams& params = ImageParams());

View File

@@ -18,6 +18,70 @@ namespace Nz
return *this;
}
inline void Image::ArrayToRegion(ImageType type, unsigned int baseLayer, unsigned int layerCount, Boxui& region)
{
switch (type)
{
case ImageType::E1D_Array:
region.y = baseLayer;
region.height = layerCount;
break;
case ImageType::Cubemap:
case ImageType::E2D_Array:
region.z = baseLayer;
region.depth = layerCount;
break;
case ImageType::E1D:
NazaraAssert(baseLayer == 0, "out of bounds");
NazaraAssert(layerCount <= 1, "out of bounds");
case ImageType::E2D:
NazaraAssert(baseLayer == 0, "out of bounds");
NazaraAssert(layerCount <= 1, "out of bounds");
case ImageType::E3D:
region.z = 0;
region.depth = 1;
break;
}
}
inline Boxui Image::RegionToArray(ImageType type, Boxui region, unsigned int& baseLayer, unsigned int& layerCount)
{
switch (type)
{
case ImageType::E1D_Array:
baseLayer = region.y;
layerCount = region.height;
region.y = 0;
region.height = 1;
break;
case ImageType::Cubemap:
case ImageType::E2D_Array:
baseLayer = region.z;
layerCount = region.depth;
region.z = 0;
region.depth = 1;
break;
case ImageType::E1D:
NazaraAssert(region.y == 0, "out of bounds");
NazaraAssert(region.height <= 1, "out of bounds");
case ImageType::E2D:
NazaraAssert(region.z == 0, "out of bounds");
NazaraAssert(region.depth <= 1, "out of bounds");
case ImageType::E3D:
baseLayer = 0;
layerCount = 1;
break;
}
return region;
}
}
#include <Nazara/Utility/DebugOff.hpp>

View File

@@ -100,10 +100,11 @@ namespace Nz
{
switch (bufferType)
{
case BufferType::Index: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
case BufferType::Index: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
case BufferType::Storage: return VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
case BufferType::Vertex: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
case BufferType::Vertex: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
case BufferType::Uniform: return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
case BufferType::Upload: return VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
}
NazaraError("Unhandled BufferType 0x" + NumberToString(UnderlyingCast(bufferType), 16));

View File

@@ -38,6 +38,8 @@ namespace Nz
void BlitTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Boxui& toBox, TextureLayout toLayout, SamplerFilter filter) override;
void BuildMipmaps(Texture& texture, UInt8 baseLevel, UInt8 maxLevel) override;
void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0) override;
void CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0) override;
void CopyTexture(const Texture& fromTexture, const Boxui& fromBox, TextureLayout fromLayout, const Texture& toTexture, const Vector3ui& toPos, TextureLayout toLayout) override;

View File

@@ -37,6 +37,7 @@ namespace Nz
std::shared_ptr<ShaderModule> InstantiateShaderModule(nzsl::ShaderStageTypeFlags stages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const nzsl::ShaderWriter::States& states) override;
std::shared_ptr<Swapchain> InstantiateSwapchain(WindowHandle windowHandle, const Vector2ui& windowSize, const SwapchainParameters& parameters) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params, const void* initialData, bool buildMipmaps, unsigned int srcWidth = 0, unsigned int srcHeight = 0) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const override;

View File

@@ -16,15 +16,29 @@
namespace Nz
{
class VulkanBuffer;
class VulkanDevice;
namespace Vk
{
class CommandBuffer;
}
class NAZARA_VULKANRENDERER_API VulkanTexture final : public Texture
{
public:
VulkanTexture(Vk::Device& device, const TextureInfo& textureInfo);
VulkanTexture(VulkanDevice& device, const TextureInfo& textureInfo);
VulkanTexture(VulkanDevice& device, const TextureInfo& textureInfo, const void* initialData, bool buildMipmaps, unsigned int srcWidth = 0, unsigned int srcHeight = 0);
VulkanTexture(std::shared_ptr<VulkanTexture> parentTexture, const TextureViewInfo& viewInfo);
VulkanTexture(const VulkanTexture&) = delete;
VulkanTexture(VulkanTexture&&) = delete;
~VulkanTexture();
inline VkImageSubresourceLayers BuildSubresourceLayers(UInt32 level) const;
inline VkImageSubresourceLayers BuildSubresourceLayers(UInt32 level, UInt32 baseLayer, UInt32 layerCount) const;
inline VkImageSubresourceRange BuildSubresourceRange(UInt32 baseLevel, UInt32 levelCount) const;
inline VkImageSubresourceRange BuildSubresourceRange(UInt32 baseLevel, UInt32 levelCount, UInt32 baseLayer, UInt32 layerCount) const;
bool Copy(const Texture& source, const Boxui& srcBox, const Vector3ui& dstPos) override;
std::shared_ptr<Texture> CreateView(const TextureViewInfo& viewInfo) override;
@@ -40,6 +54,7 @@ namespace Nz
using Texture::Update;
bool Update(const void* ptr, const Boxui& box, unsigned int srcWidth, unsigned int srcHeight, UInt8 level) override;
bool Update(Vk::CommandBuffer& commandBuffer, std::unique_ptr<VulkanBuffer>& uploadBuffer, const void* ptr, const Boxui& box, unsigned int srcWidth, unsigned int srcHeight, UInt8 level);
void UpdateDebugName(std::string_view name) override;
@@ -51,12 +66,13 @@ namespace Nz
std::optional<TextureViewInfo> m_viewInfo;
std::shared_ptr<VulkanTexture> m_parentTexture;
VulkanDevice& m_device;
VkImage m_image;
VkImageSubresourceRange m_imageRange;
VkImageSubresourceRange m_subresourceRange;
VmaAllocation m_allocation;
Vk::Device& m_device;
Vk::ImageView m_imageView;
TextureInfo m_textureInfo;
TextureInfo m_textureViewInfo;
};
}

View File

@@ -2,13 +2,53 @@
// This file is part of the "Nazara Engine - Vulkan renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
inline VkImageSubresourceLayers VulkanTexture::BuildSubresourceLayers(UInt32 level) const
{
return BuildSubresourceLayers(level, 0, m_textureViewInfo.layerCount);
}
inline VkImageSubresourceLayers VulkanTexture::BuildSubresourceLayers(UInt32 level, UInt32 baseLayer, UInt32 layerCount) const
{
NazaraAssert(m_subresourceRange.baseMipLevel + level < m_textureInfo.levelCount, "mipmap level out of bounds");
NazaraAssert(m_subresourceRange.baseArrayLayer + baseLayer + layerCount <= m_textureInfo.layerCount, "mipmap level out of bounds");
VkImageSubresourceLayers subresourceLayers;
subresourceLayers.aspectMask = m_subresourceRange.aspectMask;
subresourceLayers.mipLevel = m_subresourceRange.baseMipLevel + level;
subresourceLayers.baseArrayLayer = baseLayer;
subresourceLayers.layerCount = layerCount;
return subresourceLayers;
}
inline VkImageSubresourceRange VulkanTexture::BuildSubresourceRange(UInt32 baseLevel, UInt32 levelCount) const
{
return BuildSubresourceRange(baseLevel, levelCount, 0, m_textureInfo.layerCount);
}
inline VkImageSubresourceRange VulkanTexture::BuildSubresourceRange(UInt32 baseLevel, UInt32 levelCount, UInt32 baseLayer, UInt32 layerCount) const
{
VkImageSubresourceLayers subresourceLayers = BuildSubresourceLayers(baseLevel, baseLayer, layerCount);
NazaraAssert(subresourceLayers.mipLevel + levelCount <= m_textureInfo.levelCount, "mipmap level out of bounds");
return {
subresourceLayers.aspectMask,
subresourceLayers.mipLevel,
levelCount,
subresourceLayers.baseArrayLayer,
subresourceLayers.layerCount
};
}
inline PixelFormat VulkanTexture::GetFormat() const
{
return m_textureInfo.pixelFormat;
return m_textureViewInfo.pixelFormat;
}
inline VkImage VulkanTexture::GetImage() const
@@ -23,7 +63,7 @@ namespace Nz
inline UInt8 VulkanTexture::GetLevelCount() const
{
return m_textureInfo.levelCount;
return m_textureViewInfo.levelCount;
}
inline VulkanTexture* VulkanTexture::GetParentTexture() const
@@ -33,22 +73,22 @@ namespace Nz
inline Vector3ui VulkanTexture::GetSize(UInt8 level) const
{
return Vector3ui(GetLevelSize(m_textureInfo.width, level), GetLevelSize(m_textureInfo.height, level), GetLevelSize(m_textureInfo.depth, level));
return Vector3ui(GetLevelSize(m_textureViewInfo.width, level), GetLevelSize(m_textureViewInfo.height, level), GetLevelSize(m_textureViewInfo.depth, level));
}
inline const VkImageSubresourceRange& VulkanTexture::GetSubresourceRange() const
{
return m_imageRange;
return m_subresourceRange;
}
inline const TextureInfo& VulkanTexture::GetTextureInfo() const
{
return m_textureInfo;
return m_textureViewInfo;
}
inline ImageType VulkanTexture::GetType() const
{
return m_textureInfo.type;
return m_textureViewInfo.type;
}
}