Skip to content

Commit

Permalink
Fixed the mip level size calculations in several places.
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Aug 8, 2023
1 parent d659903 commit c74d454
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/common/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ namespace nvrhi
assert(mipLevel < desc.mipLevels);

if (width == uint32_t(-1))
ret.width = (desc.width >> mipLevel);
ret.width = std::max(desc.width >> mipLevel, 1u);

if (height == uint32_t(-1))
ret.height = (desc.height >> mipLevel);
ret.height = std::max(desc.height >> mipLevel, 1u);

if (depth == uint32_t(-1))
{
if (desc.dimension == TextureDimension::Texture3D)
ret.depth = (desc.depth >> mipLevel);
ret.depth = std::max(desc.depth >> mipLevel, 1u);
else
ret.depth = 1;
}
Expand Down Expand Up @@ -168,14 +168,14 @@ namespace nvrhi
if (desc.depthAttachment.valid())
{
const TextureDesc& textureDesc = desc.depthAttachment.texture->getDesc();
width = textureDesc.width >> desc.depthAttachment.subresources.baseMipLevel;
height = textureDesc.height >> desc.depthAttachment.subresources.baseMipLevel;
width = std::max(textureDesc.width >> desc.depthAttachment.subresources.baseMipLevel, 1u);
height = std::max(textureDesc.height >> desc.depthAttachment.subresources.baseMipLevel, 1u);
}
else if (!desc.colorAttachments.empty() && desc.colorAttachments[0].valid())
{
const TextureDesc& textureDesc = desc.colorAttachments[0].texture->getDesc();
width = textureDesc.width >> desc.colorAttachments[0].subresources.baseMipLevel;
height = textureDesc.height >> desc.colorAttachments[0].subresources.baseMipLevel;
width = std::max(textureDesc.width >> desc.colorAttachments[0].subresources.baseMipLevel, 1u);
height = std::max(textureDesc.height >> desc.colorAttachments[0].subresources.baseMipLevel, 1u);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/validation/validation-commandlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ namespace nvrhi::validation
anyErrors = true;
}

if (dstDesc.width >> dstSR.baseMipLevel != srcDesc.width >> srcSR.baseMipLevel || dstDesc.height >> dstSR.baseMipLevel != srcDesc.height >> srcSR.baseMipLevel)
const uint32_t srcMipWidth = std::max(srcDesc.width >> srcSR.baseMipLevel, 1u);
const uint32_t srcMipHeight = std::max(srcDesc.height >> srcSR.baseMipLevel, 1u);
const uint32_t dstMipWidth = std::max(dstDesc.width >> dstSR.baseMipLevel, 1u);
const uint32_t dstMipHeight = std::max(dstDesc.height >> dstSR.baseMipLevel, 1u);
if (srcMipWidth != dstMipWidth || srcMipHeight != dstMipHeight)
{
error("resolveTexture: referenced mip levels of source and destination textures must have the same dimensions");
anyErrors = true;
Expand Down
8 changes: 4 additions & 4 deletions src/vulkan/vulkan-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ namespace nvrhi::vulkan
const auto& rt = desc.colorAttachments[i];
Texture* t = checked_cast<Texture*>(rt.texture);

assert(fb->framebufferInfo.width == t->desc.width >> rt.subresources.baseMipLevel);
assert(fb->framebufferInfo.height == t->desc.height >> rt.subresources.baseMipLevel);
assert(fb->framebufferInfo.width == std::max(t->desc.width >> rt.subresources.baseMipLevel, 1u));
assert(fb->framebufferInfo.height == std::max(t->desc.height >> rt.subresources.baseMipLevel, 1u));

const vk::Format attachmentFormat = (rt.format == Format::UNKNOWN ? t->imageInfo.format : convertFormat(rt.format));

Expand Down Expand Up @@ -116,8 +116,8 @@ namespace nvrhi::vulkan

Texture* texture = checked_cast<Texture*>(att.texture);

assert(fb->framebufferInfo.width == texture->desc.width >> att.subresources.baseMipLevel);
assert(fb->framebufferInfo.height == texture->desc.height >> att.subresources.baseMipLevel);
assert(fb->framebufferInfo.width == std::max(texture->desc.width >> att.subresources.baseMipLevel, 1u));
assert(fb->framebufferInfo.height == std::max(texture->desc.height >> att.subresources.baseMipLevel, 1u));

vk::ImageLayout depthLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
if (desc.depthAttachment.isReadOnly)
Expand Down
6 changes: 3 additions & 3 deletions src/vulkan/vulkan-staging-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ namespace nvrhi::vulkan
{
const FormatInfo& formatInfo = getFormatInfo(desc.format);

auto wInBlocks = (desc.width >> mipLevel) / formatInfo.blockSize;
auto hInBlocks = (desc.height >> mipLevel) / formatInfo.blockSize;
auto wInBlocks = std::max(((desc.width >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u);
auto hInBlocks = std::max(((desc.height >> mipLevel) + formatInfo.blockSize - 1) / formatInfo.blockSize, 1u);

auto blockPitchBytes = (wInBlocks >> mipLevel) * formatInfo.bytesPerBlock;
auto blockPitchBytes = wInBlocks * formatInfo.bytesPerBlock;
return blockPitchBytes * hInBlocks;
}

Expand Down
6 changes: 3 additions & 3 deletions src/vulkan/vulkan-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,9 @@ namespace nvrhi::vulkan
.setSrcSubresource(srcLayers)
.setDstSubresource(dstLayers)
.setExtent(vk::Extent3D(
dest->desc.width >> dstLayers.mipLevel,
dest->desc.height >> dstLayers.mipLevel,
dest->desc.depth >> dstLayers.mipLevel)));
std::max(dest->desc.width >> dstLayers.mipLevel, 1u),
std::max(dest->desc.height >> dstLayers.mipLevel, 1u),
std::max(dest->desc.depth >> dstLayers.mipLevel, 1u))));
}

if (m_EnableAutomaticBarriers)
Expand Down

0 comments on commit c74d454

Please sign in to comment.