纹理阵列性能影响

Array of textures performance impact

关注 Kyle Halladay's tutorial on "Using Arrays of Textures in Vulkan Shaders" 我设法让我的代码工作。

有时,凯尔说:

"I don’t know how much (if any) of a performance penalty you pay for using an array of textures over a sampler2DArray." (cit.)

我担心性能。这是着色器:

#version 450
#extension GL_ARB_separate_shader_objects : enable

layout(binding = 1) uniform sampler baseSampler;
layout(binding = 2) uniform texture2D textures[2];

layout(location = 0) in vec2 inUV;
layout(location = 1) flat in vec4 inTint;
layout(location = 2) flat in uint inTexIndex;

layout(location = 0) out vec4 outColor;

void main()
{
    outColor = inTint * texture(sampler2D(textures[inTexIndex], baseSampler), inUV);
}

我关心的部分是 sampler2D(textures[inTexIndex], baseSampler),其中 sampler2D 似乎是基于 baseSampler 设置的。这看起来很可怕,我不知道它是否是每个片段,或者 glslc 是否可以以某种方式优化它。

有人知道 sampler2D() 有多大影响吗?

Obsolete question which received answers in the comments: What if I bind an array of VkSampler descriptors (VK_DESCRIPTOR_TYPE_SAMPLER) in place of the VkImageView descriptors (VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)? The shader wouldn't index into a texture2D array but into a sampler2D array with attached ImageInfo (the textures).

此外,这些优化是重要的还是无关紧要的?


编辑: 在不改变意思的情况下清理了原始问题,在下面添加了 same/corollary 问题,措辞更好。

我为我的英语道歉。

这段特定的代码是做什么的:

texture(sampler2D(textures[inTexIndex], baseSampler), inUV)

这是每个片段执行的吗?如果是这样,sampler2D() 是一个函数吗?类型转换?分配内存的构造函数?这个“功能”是我最关心的。我认为索引是不可避免的。

在评论中我想知道,作为替代方案,我是否可以使用 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER 描述符并在着色器中有一个 sampler2D 数组。这个选择会提高性能吗?

最后,我想知道切换到 sampler2Darray 是否真的有很大的不同(性能方面)。

不使用组合 image/sampler 对象的成本当然取决于硬件。但是,请考虑一下。

HLSL,从 Shader Model 4 开始(所以 D3D10+)从未 合并过 image/samplers。他们 always used separate texture and sampler objects.

因此,如果整个 API 已经这样做了十多年,那么可以肯定地说这不会成为性能问题。