Add initial support for texture views
This commit is contained in:
committed by
Jérôme Leclercq
parent
902dee6121
commit
42f8cdb151
@@ -329,7 +329,7 @@ namespace Nz
|
||||
throw std::runtime_error("couldn't find a sampling-compatible depth pixel format");
|
||||
|
||||
TextureInfo texInfo;
|
||||
texInfo.width = texInfo.height = texInfo.depth = texInfo.mipmapLevel = 1;
|
||||
texInfo.width = texInfo.height = texInfo.depth = texInfo.levelCount = 1;
|
||||
texInfo.pixelFormat = depthFormat;
|
||||
|
||||
std::array<UInt8, 6> whitePixels = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
@@ -350,7 +350,7 @@ namespace Nz
|
||||
// White texture 2D
|
||||
{
|
||||
TextureInfo texInfo;
|
||||
texInfo.width = texInfo.height = texInfo.depth = texInfo.mipmapLevel = 1;
|
||||
texInfo.width = texInfo.height = texInfo.depth = texInfo.levelCount = 1;
|
||||
texInfo.pixelFormat = PixelFormat::L8;
|
||||
|
||||
std::array<UInt8, 6> whitePixels = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
@@ -10,57 +10,87 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLTexture::OpenGLTexture(OpenGLDevice& device, const TextureInfo& params) :
|
||||
m_params(params)
|
||||
OpenGLTexture::OpenGLTexture(OpenGLDevice& device, const TextureInfo& textureInfo) :
|
||||
m_textureInfo(textureInfo)
|
||||
{
|
||||
if (!m_texture.Create(device))
|
||||
throw std::runtime_error("failed to create texture object");
|
||||
|
||||
auto format = DescribeTextureFormat(params.pixelFormat);
|
||||
auto format = DescribeTextureFormat(textureInfo.pixelFormat);
|
||||
if (!format)
|
||||
throw std::runtime_error("unsupported texture format");
|
||||
|
||||
const GL::Context& context = m_texture.EnsureDeviceContext();
|
||||
context.ClearErrorStack();
|
||||
|
||||
switch (params.type)
|
||||
switch (textureInfo.type)
|
||||
{
|
||||
case ImageType::E2D:
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, params.mipmapLevel, format->internalFormat, params.width, params.height);
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, textureInfo.levelCount, format->internalFormat, textureInfo.width, textureInfo.height);
|
||||
break;
|
||||
|
||||
case ImageType::E2D_Array:
|
||||
m_texture.TexStorage3D(GL::TextureTarget::Target2D_Array, params.mipmapLevel, format->internalFormat, params.width, params.height, params.layerCount);
|
||||
m_texture.TexStorage3D(GL::TextureTarget::Target2D_Array, textureInfo.levelCount, format->internalFormat, textureInfo.width, textureInfo.height, textureInfo.layerCount);
|
||||
break;
|
||||
|
||||
case ImageType::E3D:
|
||||
m_texture.TexStorage3D(GL::TextureTarget::Target3D, params.mipmapLevel, format->internalFormat, params.width, params.height, params.depth);
|
||||
m_texture.TexStorage3D(GL::TextureTarget::Target3D, textureInfo.levelCount, format->internalFormat, textureInfo.width, textureInfo.height, textureInfo.depth);
|
||||
break;
|
||||
|
||||
case ImageType::Cubemap:
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Cubemap, params.mipmapLevel, format->internalFormat, params.width, params.height);
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Cubemap, textureInfo.levelCount, format->internalFormat, textureInfo.width, textureInfo.height);
|
||||
break;
|
||||
|
||||
// OpenGL ES doesn't support 1D textures, use 2D textures with a height of 1 instead
|
||||
case ImageType::E1D:
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, params.mipmapLevel, format->internalFormat, params.width, 1);
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, textureInfo.levelCount, format->internalFormat, textureInfo.width, 1);
|
||||
break;
|
||||
|
||||
case ImageType::E1D_Array:
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, params.mipmapLevel, format->internalFormat, params.width, params.layerCount);
|
||||
m_texture.TexStorage2D(GL::TextureTarget::Target2D, textureInfo.levelCount, format->internalFormat, textureInfo.width, textureInfo.layerCount);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!context.DidLastCallSucceed())
|
||||
throw std::runtime_error("failed to create texture");
|
||||
|
||||
m_texture.SetParameteri(GL_TEXTURE_MAX_LEVEL, m_params.mipmapLevel);
|
||||
m_texture.SetParameteri(GL_TEXTURE_MAX_LEVEL, m_textureInfo.levelCount);
|
||||
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_R, format->swizzleR);
|
||||
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_G, format->swizzleG);
|
||||
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_B, format->swizzleB);
|
||||
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_A, format->swizzleA);
|
||||
}
|
||||
|
||||
OpenGLTexture::OpenGLTexture(std::shared_ptr<OpenGLTexture> parentTexture, const TextureViewInfo& viewInfo) :
|
||||
m_parentTexture(std::move(parentTexture))
|
||||
{
|
||||
const GL::Context& context = m_parentTexture->m_texture.EnsureDeviceContext();
|
||||
|
||||
NazaraAssert(viewInfo.layerCount <= m_parentTexture->m_textureInfo.layerCount - viewInfo.baseArrayLayer, "layer count exceeds number of layers");
|
||||
NazaraAssert(viewInfo.levelCount <= m_parentTexture->m_textureInfo.levelCount - viewInfo.baseMipLevel, "level count exceeds number of levels");
|
||||
|
||||
m_viewInfo = viewInfo;
|
||||
|
||||
// Try to use texture views if supported (core in GL 4.3 or extension)
|
||||
if (context.IsExtensionSupported(GL::Extension::TextureView))
|
||||
{
|
||||
if (m_texture.Create(*m_parentTexture->m_texture.GetDevice()))
|
||||
{
|
||||
auto format = DescribeTextureFormat(viewInfo.reinterpretFormat);
|
||||
GLenum target = ToOpenGL(ToTextureTarget(viewInfo.viewType));
|
||||
|
||||
context.ClearErrorStack();
|
||||
|
||||
m_texture.TextureView(target, m_parentTexture->m_texture.GetObjectId(), format->internalFormat, viewInfo.baseMipLevel, viewInfo.levelCount, viewInfo.baseArrayLayer, viewInfo.layerCount);
|
||||
|
||||
if (!context.DidLastCallSucceed())
|
||||
m_texture.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
// If texture views are not supported, they will be emulated when using them as attachments
|
||||
}
|
||||
|
||||
bool OpenGLTexture::Copy(const Texture& source, const Boxui& srcBox, const Vector3ui& dstPos)
|
||||
{
|
||||
const OpenGLTexture& glTexture = static_cast<const OpenGLTexture&>(source);
|
||||
@@ -69,34 +99,57 @@ namespace Nz
|
||||
return context.CopyTexture(glTexture.GetTexture(), m_texture, srcBox, dstPos);
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> OpenGLTexture::CreateView(const TextureViewInfo& viewInfo)
|
||||
{
|
||||
if (m_parentTexture)
|
||||
{
|
||||
assert(m_viewInfo);
|
||||
NazaraAssert(viewInfo.layerCount <= m_viewInfo->layerCount - viewInfo.baseArrayLayer, "layer count exceeds number of layers");
|
||||
NazaraAssert(viewInfo.levelCount <= m_viewInfo->levelCount - viewInfo.baseMipLevel, "level count exceeds number of levels");
|
||||
|
||||
TextureViewInfo ajustedView = viewInfo;
|
||||
ajustedView.baseArrayLayer += m_viewInfo->baseArrayLayer;
|
||||
ajustedView.baseMipLevel += m_viewInfo->baseMipLevel;
|
||||
|
||||
return m_parentTexture->CreateView(ajustedView);
|
||||
}
|
||||
|
||||
return std::make_shared<OpenGLTexture>(std::static_pointer_cast<OpenGLTexture>(shared_from_this()), viewInfo);
|
||||
}
|
||||
|
||||
PixelFormat OpenGLTexture::GetFormat() const
|
||||
{
|
||||
return m_params.pixelFormat;
|
||||
return m_textureInfo.pixelFormat;
|
||||
}
|
||||
|
||||
UInt8 OpenGLTexture::GetLevelCount() const
|
||||
{
|
||||
return m_params.mipmapLevel;
|
||||
return m_textureInfo.levelCount;
|
||||
}
|
||||
|
||||
OpenGLTexture* OpenGLTexture::GetParentTexture() const
|
||||
{
|
||||
return m_parentTexture.get();
|
||||
}
|
||||
|
||||
Vector3ui OpenGLTexture::GetSize(UInt8 level) const
|
||||
{
|
||||
return Vector3ui(GetLevelSize(m_params.width, level), GetLevelSize(m_params.height, level), GetLevelSize(m_params.depth, level));
|
||||
return Vector3ui(GetLevelSize(m_textureInfo.width, level), GetLevelSize(m_textureInfo.height, level), GetLevelSize(m_textureInfo.depth, level));
|
||||
}
|
||||
|
||||
ImageType OpenGLTexture::GetType() const
|
||||
{
|
||||
return m_params.type;
|
||||
return m_textureInfo.type;
|
||||
}
|
||||
|
||||
bool OpenGLTexture::Update(const void* ptr, const Boxui& box, unsigned int srcWidth, unsigned int srcHeight, UInt8 level)
|
||||
{
|
||||
auto format = DescribeTextureFormat(m_params.pixelFormat);
|
||||
auto format = DescribeTextureFormat(m_textureInfo.pixelFormat);
|
||||
assert(format);
|
||||
|
||||
const GL::Context& context = m_texture.EnsureDeviceContext();
|
||||
|
||||
UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_params.pixelFormat);
|
||||
UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_textureInfo.pixelFormat);
|
||||
if (bpp % 8 == 0)
|
||||
context.glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
|
||||
else if (bpp % 4 == 0)
|
||||
@@ -109,7 +162,7 @@ namespace Nz
|
||||
context.glPixelStorei(GL_UNPACK_ROW_LENGTH, srcWidth);
|
||||
context.glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, srcHeight);
|
||||
|
||||
switch (m_params.type)
|
||||
switch (m_textureInfo.type)
|
||||
{
|
||||
case ImageType::E1D:
|
||||
break;
|
||||
@@ -129,7 +182,7 @@ namespace Nz
|
||||
|
||||
case ImageType::Cubemap:
|
||||
{
|
||||
std::size_t faceSize = PixelFormatInfo::ComputeSize(m_params.pixelFormat, m_params.width, m_params.height, 1);
|
||||
std::size_t faceSize = PixelFormatInfo::ComputeSize(m_textureInfo.pixelFormat, m_textureInfo.width, m_textureInfo.height, 1);
|
||||
const UInt8* facePtr = static_cast<const UInt8*>(ptr);
|
||||
|
||||
for (GL::TextureTarget face : { GL::TextureTarget::CubemapPositiveX, GL::TextureTarget::CubemapNegativeX, GL::TextureTarget::CubemapPositiveY, GL::TextureTarget::CubemapNegativeY, GL::TextureTarget::CubemapPositiveZ, GL::TextureTarget::CubemapNegativeZ })
|
||||
|
||||
@@ -446,6 +446,16 @@ namespace Nz::GL
|
||||
else if (m_supportedExtensions.count("GL_EXT_texture_filter_anisotropic"))
|
||||
m_extensionStatus[UnderlyingCast(Extension::TextureFilterAnisotropic)] = ExtensionStatus::EXT;
|
||||
|
||||
// Texture view
|
||||
if (m_params.type == ContextType::OpenGL && glVersion >= 430)
|
||||
m_extensionStatus[UnderlyingCast(Extension::TextureView)] = ExtensionStatus::Core;
|
||||
else if (m_supportedExtensions.count("GL_ARB_texture_view"))
|
||||
m_extensionStatus[UnderlyingCast(Extension::TextureView)] = ExtensionStatus::ARB;
|
||||
else if (m_supportedExtensions.count("GL_OES_texture_view"))
|
||||
m_extensionStatus[UnderlyingCast(Extension::TextureView)] = ExtensionStatus::KHR; //< not sure about the OES => KHR mapping
|
||||
else if (m_supportedExtensions.count("GL_EXT_texture_view"))
|
||||
m_extensionStatus[UnderlyingCast(Extension::TextureView)] = ExtensionStatus::EXT; //< not sure about the OES => KHR mapping
|
||||
|
||||
#define NAZARA_OPENGLRENDERER_FUNC(name, sig)
|
||||
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, false);
|
||||
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
|
||||
@@ -959,6 +969,12 @@ namespace Nz::GL
|
||||
constexpr std::size_t functionIndex = UnderlyingCast(FunctionIndex::glSpecializeShader);
|
||||
return loader.Load<PFNGLSPECIALIZESHADERPROC, functionIndex>(glSpecializeShader, "glSpecializeShaderARB", false); //< from GL_ARB_spirv_extensions
|
||||
}
|
||||
else if (function == "glTextureView")
|
||||
{
|
||||
constexpr std::size_t functionIndex = UnderlyingCast(FunctionIndex::glTextureView);
|
||||
return loader.Load<PFNGLTEXTUREVIEWPROC, functionIndex>(glTextureView, "glTextureViewOES", false) ||
|
||||
loader.Load<PFNGLTEXTUREVIEWPROC, functionIndex>(glTextureView, "glTextureViewEXT", false); //< from GL_EXT_texture_view
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -13,15 +13,15 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
VulkanTexture::VulkanTexture(Vk::Device& device, const TextureInfo& params) :
|
||||
VulkanTexture::VulkanTexture(Vk::Device& device, const TextureInfo& textureInfo) :
|
||||
m_image(VK_NULL_HANDLE),
|
||||
m_allocation(nullptr),
|
||||
m_device(device),
|
||||
m_params(params)
|
||||
m_textureInfo(textureInfo)
|
||||
{
|
||||
VkImageViewCreateInfo createInfoView = {};
|
||||
createInfoView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
InitViewForFormat(params.pixelFormat, createInfoView);
|
||||
InitViewForFormat(textureInfo.pixelFormat, createInfoView);
|
||||
|
||||
VkImageCreateInfo createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||
@@ -29,66 +29,66 @@ namespace Nz
|
||||
createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
createInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
createInfo.usage = ToVulkan(params.usageFlags);
|
||||
createInfo.usage = ToVulkan(textureInfo.usageFlags);
|
||||
|
||||
switch (params.type)
|
||||
switch (textureInfo.type)
|
||||
{
|
||||
case ImageType::E1D:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height == 1, "Height must be one");
|
||||
NazaraAssert(params.depth == 1, "Depth must be one");
|
||||
NazaraAssert(params.layerCount == 1, "Array count must be one");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height == 1, "Height must be one");
|
||||
NazaraAssert(textureInfo.depth == 1, "Depth must be one");
|
||||
NazaraAssert(textureInfo.layerCount == 1, "Array count must be one");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_1D;
|
||||
createInfoView.viewType = VK_IMAGE_VIEW_TYPE_1D;
|
||||
createInfo.extent.width = params.width;
|
||||
createInfo.extent.width = textureInfo.width;
|
||||
break;
|
||||
|
||||
case ImageType::E1D_Array:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height == 1, "Height must be one");
|
||||
NazaraAssert(params.depth == 1, "Depth must be one");
|
||||
NazaraAssert(params.layerCount > 0, "Array count must be over zero");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height == 1, "Height must be one");
|
||||
NazaraAssert(textureInfo.depth == 1, "Depth must be one");
|
||||
NazaraAssert(textureInfo.layerCount > 0, "Array count must be over zero");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_1D;
|
||||
createInfoView.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
|
||||
break;
|
||||
|
||||
case ImageType::E2D:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height > 0, "Height must be over zero");
|
||||
NazaraAssert(params.depth == 1, "Depth must be one");
|
||||
NazaraAssert(params.layerCount == 1, "Array count must be one");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height > 0, "Height must be over zero");
|
||||
NazaraAssert(textureInfo.depth == 1, "Depth must be one");
|
||||
NazaraAssert(textureInfo.layerCount == 1, "Array count must be one");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||
createInfoView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
break;
|
||||
|
||||
case ImageType::E2D_Array:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height > 0, "Height must be over zero");
|
||||
NazaraAssert(params.depth == 1, "Depth must be one");
|
||||
NazaraAssert(params.layerCount > 0, "Array count must be over zero");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height > 0, "Height must be over zero");
|
||||
NazaraAssert(textureInfo.depth == 1, "Depth must be one");
|
||||
NazaraAssert(textureInfo.layerCount > 0, "Array count must be over zero");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||
createInfoView.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
||||
break;
|
||||
|
||||
case ImageType::E3D:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height > 0, "Height must be over zero");
|
||||
NazaraAssert(params.depth > 0, "Depth must be over zero");
|
||||
NazaraAssert(params.layerCount == 1, "Array count must be one");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height > 0, "Height must be over zero");
|
||||
NazaraAssert(textureInfo.depth > 0, "Depth must be over zero");
|
||||
NazaraAssert(textureInfo.layerCount == 1, "Array count must be one");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_3D;
|
||||
createInfoView.viewType = VK_IMAGE_VIEW_TYPE_3D;
|
||||
break;
|
||||
|
||||
case ImageType::Cubemap:
|
||||
NazaraAssert(params.width > 0, "Width must be over zero");
|
||||
NazaraAssert(params.height > 0, "Height must be over zero");
|
||||
NazaraAssert(params.depth == 1, "Depth must be one");
|
||||
NazaraAssert(params.layerCount % 6 == 0, "Array count must be a multiple of 6");
|
||||
NazaraAssert(textureInfo.width > 0, "Width must be over zero");
|
||||
NazaraAssert(textureInfo.height > 0, "Height must be over zero");
|
||||
NazaraAssert(textureInfo.depth == 1, "Depth must be one");
|
||||
NazaraAssert(textureInfo.layerCount % 6 == 0, "Array count must be a multiple of 6");
|
||||
|
||||
createInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||
createInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
|
||||
@@ -99,11 +99,11 @@ namespace Nz
|
||||
break;
|
||||
}
|
||||
|
||||
createInfo.extent.width = params.width;
|
||||
createInfo.extent.height = params.height;
|
||||
createInfo.extent.depth = params.depth;
|
||||
createInfo.arrayLayers = params.layerCount;
|
||||
createInfo.mipLevels = params.mipmapLevel;
|
||||
createInfo.extent.width = textureInfo.width;
|
||||
createInfo.extent.height = textureInfo.height;
|
||||
createInfo.extent.depth = textureInfo.depth;
|
||||
createInfo.arrayLayers = textureInfo.layerCount;
|
||||
createInfo.mipLevels = textureInfo.levelCount;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo = {};
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
|
||||
@@ -115,7 +115,7 @@ namespace Nz
|
||||
CallOnExit releaseImage([&]{ vmaDestroyImage(m_device.GetMemoryAllocator(), m_image, m_allocation); });
|
||||
|
||||
createInfoView.subresourceRange = {
|
||||
ToVulkan(PixelFormatInfo::GetContent(params.pixelFormat)),
|
||||
ToVulkan(PixelFormatInfo::GetContent(textureInfo.pixelFormat)),
|
||||
0, //< baseMipLevel
|
||||
createInfo.mipLevels, //< levelCount
|
||||
0, //< baseArrayLayer
|
||||
@@ -128,17 +128,40 @@ namespace Nz
|
||||
throw std::runtime_error("Failed to create default image view: " + TranslateVulkanError(m_imageView.GetLastErrorCode()));
|
||||
|
||||
releaseImage.Reset();
|
||||
createInfoView.image = m_image;
|
||||
}
|
||||
|
||||
{
|
||||
// FIXME
|
||||
vmaDestroyImage(m_device.GetMemoryAllocator(), m_image, m_allocation);
|
||||
}
|
||||
VulkanTexture::VulkanTexture(std::shared_ptr<VulkanTexture> parentTexture, const TextureViewInfo& viewInfo) :
|
||||
m_parentTexture(std::move(parentTexture)),
|
||||
m_image(m_parentTexture->m_image),
|
||||
m_allocation(nullptr),
|
||||
m_device(m_parentTexture->m_device)
|
||||
{
|
||||
m_textureInfo = ApplyView(m_parentTexture->m_textureInfo, viewInfo);
|
||||
|
||||
NazaraAssert(viewInfo.layerCount <= m_parentTexture->m_textureInfo.layerCount - viewInfo.baseArrayLayer, "layer count exceeds number of layers");
|
||||
NazaraAssert(viewInfo.levelCount <= m_parentTexture->m_textureInfo.levelCount - viewInfo.baseMipLevel, "level count exceeds number of levels");
|
||||
|
||||
VkImageViewCreateInfo createInfoView = {};
|
||||
createInfoView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
createInfoView.image = m_image;
|
||||
createInfoView.subresourceRange = {
|
||||
ToVulkan(PixelFormatInfo::GetContent(m_textureInfo.pixelFormat)),
|
||||
0, //< baseMipLevel
|
||||
m_textureInfo.levelCount, //< levelCount
|
||||
0, //< baseArrayLayer
|
||||
m_textureInfo.layerCount //< layerCount, will be set if the type is an array/cubemap
|
||||
};
|
||||
|
||||
InitViewForFormat(viewInfo.reinterpretFormat, createInfoView);
|
||||
|
||||
if (!m_imageView.Create(m_device, createInfoView))
|
||||
throw std::runtime_error("Failed to create image view: " + TranslateVulkanError(m_imageView.GetLastErrorCode()));
|
||||
}
|
||||
|
||||
VulkanTexture::~VulkanTexture()
|
||||
{
|
||||
vmaDestroyImage(m_device.GetMemoryAllocator(), m_image, m_allocation);
|
||||
if (m_allocation)
|
||||
vmaDestroyImage(m_device.GetMemoryAllocator(), m_image, m_allocation);
|
||||
}
|
||||
|
||||
bool VulkanTexture::Copy(const Texture& source, const Boxui& srcBox, const Vector3ui& dstPos)
|
||||
@@ -192,30 +215,43 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> VulkanTexture::CreateView(const TextureViewInfo& viewInfo)
|
||||
{
|
||||
if (m_parentTexture)
|
||||
return m_parentTexture->CreateView(viewInfo);
|
||||
|
||||
return std::make_shared<VulkanTexture>(std::static_pointer_cast<VulkanTexture>(shared_from_this()), viewInfo);
|
||||
}
|
||||
|
||||
PixelFormat VulkanTexture::GetFormat() const
|
||||
{
|
||||
return m_params.pixelFormat;
|
||||
return m_textureInfo.pixelFormat;
|
||||
}
|
||||
|
||||
UInt8 VulkanTexture::GetLevelCount() const
|
||||
{
|
||||
return m_params.mipmapLevel;
|
||||
return m_textureInfo.levelCount;
|
||||
}
|
||||
|
||||
VulkanTexture* VulkanTexture::GetParentTexture() const
|
||||
{
|
||||
return m_parentTexture.get();
|
||||
}
|
||||
|
||||
Vector3ui VulkanTexture::GetSize(UInt8 level) const
|
||||
{
|
||||
return Vector3ui(GetLevelSize(m_params.width, level), GetLevelSize(m_params.height, level), GetLevelSize(m_params.depth, level));
|
||||
return Vector3ui(GetLevelSize(m_textureInfo.width, level), GetLevelSize(m_textureInfo.height, level), GetLevelSize(m_textureInfo.depth, level));
|
||||
}
|
||||
|
||||
ImageType VulkanTexture::GetType() const
|
||||
{
|
||||
return m_params.type;
|
||||
return m_textureInfo.type;
|
||||
}
|
||||
|
||||
bool VulkanTexture::Update(const void* ptr, const Boxui& box, unsigned int srcWidth, unsigned int srcHeight, UInt8 level)
|
||||
{
|
||||
std::size_t textureSize = box.width * box.height * box.depth * PixelFormatInfo::GetBytesPerPixel(m_params.pixelFormat);
|
||||
if (m_params.type == ImageType::Cubemap)
|
||||
std::size_t textureSize = box.width * box.height * box.depth * PixelFormatInfo::GetBytesPerPixel(m_textureInfo.pixelFormat);
|
||||
if (m_textureInfo.type == ImageType::Cubemap)
|
||||
textureSize *= 6;
|
||||
|
||||
VkBufferCreateInfo createInfo = {};
|
||||
@@ -256,7 +292,7 @@ namespace Nz
|
||||
unsigned int dstWidth = box.width;
|
||||
unsigned int dstHeight = box.height;
|
||||
|
||||
unsigned int bpp = PixelFormatInfo::GetBytesPerPixel(m_params.pixelFormat);
|
||||
unsigned int bpp = PixelFormatInfo::GetBytesPerPixel(m_textureInfo.pixelFormat);
|
||||
unsigned int lineStride = box.width * bpp;
|
||||
unsigned int dstLineStride = dstWidth * bpp;
|
||||
unsigned int dstFaceStride = dstLineStride * dstHeight;
|
||||
@@ -287,14 +323,14 @@ namespace Nz
|
||||
return false;
|
||||
|
||||
VkImageAspectFlags aspect = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
if (PixelFormatInfo::GetContent(m_params.pixelFormat) == PixelFormatContent::Depth)
|
||||
if (PixelFormatInfo::GetContent(m_textureInfo.pixelFormat) == PixelFormatContent::Depth)
|
||||
aspect = VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||
|
||||
VkImageSubresourceLayers subresourceLayers = { //< FIXME
|
||||
aspect,
|
||||
level, //< mipLevel
|
||||
0, //< baseArrayLayer
|
||||
UInt32((m_params.type == ImageType::Cubemap) ? 6 : 1) //< layerCount
|
||||
UInt32((m_textureInfo.type == ImageType::Cubemap) ? 6 : 1) //< layerCount
|
||||
};
|
||||
|
||||
VkImageSubresourceRange subresourceRange = { //< FIXME
|
||||
|
||||
Reference in New Issue
Block a user