3

如果我的纹理尺寸是 2 的幂次方,我的 2D 纹理加载器可以正常工作,但如果不是,则纹理数据显示为倾斜。我该如何解决?我认为这个问题与内存对齐和行间距有关。这是我的加载程序代码的相关部分:

VkMemoryRequirements memReqs;
vkGetImageMemoryRequirements( GfxDeviceGlobal::device, mappableImage, &memReqs );

VkMemoryAllocateInfo memAllocInfo = {};
memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memAllocInfo.pNext = nullptr;
memAllocInfo.memoryTypeIndex = 0;
memAllocInfo.allocationSize = memReqs.size;

GetMemoryType( memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &memAllocInfo.memoryTypeIndex );

VkDeviceMemory mappableMemory;
err = vkAllocateMemory( GfxDeviceGlobal::device, &memAllocInfo, nullptr, &mappableMemory );
CheckVulkanResult( err, "vkAllocateMemory in Texture2D" );

err = vkBindImageMemory( GfxDeviceGlobal::device, mappableImage, mappableMemory, 0 );
CheckVulkanResult( err, "vkBindImageMemory in Texture2D" );

VkImageSubresource subRes = {};
subRes.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
subRes.mipLevel = 0;
subRes.arrayLayer = 0;

VkSubresourceLayout subResLayout;
vkGetImageSubresourceLayout( GfxDeviceGlobal::device, mappableImage, &subRes, &subResLayout );

void* mapped;
err = vkMapMemory( GfxDeviceGlobal::device, mappableMemory, 0, memReqs.size, 0, &mapped );
CheckVulkanResult( err, "vkMapMemory in Texture2D" );

const int bytesPerPixel = 4;
std::size_t dataSize = bytesPerPixel * width * height;
std::memcpy( mapped, data, dataSize );

vkUnmapMemory( GfxDeviceGlobal::device, mappableMemory );
4

2 回答 2

4

VkSubresourceLayout您从中获得的将vkGetImageSubresourceLayout包含rowPitch成员中纹理的音高。它很可能不等于width,因此,当您memcpy对整个数据块执行 a 时,您正在将相关数据复制到纹理的填充部分。

相反,您需要memcpy逐行跳过映射纹理中的填充内存:

const int bytesPerPixel = 4;
std::size_t dataRowSize = bytesPerPixel * width;
char* mappedBytes = (char*)mapped;
for(int i = 0; i < height; ++i)
{
    std::memcpy(mapped, data, dataSize);
    mappedBytes += rowPitch;
    data += dataRowSize;
}

(此代码假设 data 也是 a char *- 没有给出它的声明)

于 2016-03-16T11:32:40.387 回答
0
for(int i = 0; i < height; ++i)
{
    std::memcpy(mappedBytes, data, dataRowSize);
    mappedBytes += layout.rowPitch;
    data += dataRowSize;
}
于 2020-03-26T10:27:59.107 回答