是什么导致在多边形的每个三角形上渲染纹理?

时间:2018-12-24 16:25:45

标签: c++ vulkan

我正在尝试渲染带纹理的正方形,但是看起来纹理没有得到应有的插值。看起来它反映在我正方形的每个三角形上。行为在下图显示。

请注意,我正在使用this教程。

As you can see, each triangle gets its own texture.

我不知道从哪里开始修复我的代码。另外,当我尝试翻译图像布局时,出现以下错误:"Cannot submit cmd buffer using image 0x25 with layout VK_IMAGE_LAYOUT_UNDEFINED when first use is VK_IMAGE_LAYOUT_TRANSFER_DST_BIT."

此外,首次提交绘图命令缓冲区时,我也会收到此警告:"Cannot submit cmd buffer using image 0x25 with layout VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL when first use is VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL."

此警告会导致我的问题吗?另外,这是我的顶点及其纹理坐标。

vertices->setVertices({
{{-1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f}},
{{1.0f, -1.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
{{1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
{{-1.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}
    });

vertices->setIndices({ 0, 1, 2, 2, 3, 0 });

更新

这是我的图片转换代码:

void Util::transitionImageLayout(VkImage *image, VkFormat format, 
                                 VkImageLayout oldLayout, VkImageLayout newLayout,
                                 VkCommandBuffer recordingBuffer) {
    VkImageMemoryBarrier barrier = {};
    VkImageSubresourceRange subresourceRange = {};
    VkPipelineStageFlags sourceStage = {};
    VkPipelineStageFlags dstStage = {};

    if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
        barrier.srcAccessMask = 0;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;

        sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
        dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
        barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;

        sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
        dstStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
    }
    else {
        throw std::invalid_argument("Layout transition not supported.");
    }

    subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    subresourceRange.baseArrayLayer = 0;
    subresourceRange.baseMipLevel = 0;
    subresourceRange.layerCount = 1;
    subresourceRange.levelCount = 1;

    barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    barrier.image = *image;
    barrier.oldLayout = oldLayout;
    barrier.newLayout = newLayout;
    barrier.subresourceRange = subresourceRange;
    barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;  
    barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;

    vkCmdPipelineBarrier(recordingBuffer, sourceStage, dstStage, 0, 0, nullptr, 0, nullptr, 0, &barrier);
}

这是我对图像代码的复制缓冲区:

void Util::copyBufferToimage(VkCommandBuffer cmdBuffer, VkBuffer buffer, 
                             VkImage *image, uint32_t width, uint32_t height) {
     VkBufferImageCopy region{};
     VkImageSubresourceLayers subresouce{};

     subresouce.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
     subresouce.baseArrayLayer = 0;
     subresouce.mipLevel = 0;
     subresouce.layerCount = 1;

     region.bufferImageHeight = 0;
     region.bufferOffset = 0;                                   
     region.bufferRowLength = 0;
     region.imageOffset = { 0, 0, 0 };                          
     region.imageExtent = { width, height, 1 };                 
     region.imageSubresource = subresouce;                      
     vkCmdCopyBufferToImage(cmdBuffer, buffer, *image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
 }

注释: 我已经尝试从教程的git repo运行项目,并且效果很好。他们没有警告。

1 个答案:

答案 0 :(得分:0)

问题出在我未提供的顶点结构代码中。纹理坐标的格式错误。它应该是R32 G32,并且我将其设置为R32 G32 B32 A32

static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
    std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {};

    attributeDescriptions[0].binding = 0;                           
    attributeDescriptions[0].location = 0;                          
    attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
    attributeDescriptions[0].offset = offsetof(Vertex, position);

    attributeDescriptions[1].binding = 0;
    attributeDescriptions[1].location = 1;
    attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
    attributeDescriptions[1].offset = offsetof(Vertex, color);

    attributeDescriptions[2].binding = 0;
    attributeDescriptions[2].location = 2;
    attributeDescriptions[2].format = VK_FORMAT_R32G32B32A32_SFLOAT;      //This is an error. I guess this has to do something with texCoord being a glm::vec2
    attributeDescriptions[2].offset = offsetof(Vertex, texCoord);

    return attributeDescriptions;
}

因此,有关纹理坐标的属性描述格式应为

attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;

现在我的多边形看起来像这样: