pid
pid

Reputation: 11607

Array of textures performance impact

Following Kyle Halladay's tutorial on "Using Arrays of Textures in Vulkan Shaders" I managed to make my code work.

At some point, Kyle says:

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

I'm concerned about performance. This is the shader:

#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);
}

The part I'm concerned about is sampler2D(textures[inTexIndex], baseSampler), where it looks like a sampler2D is set up based on baseSampler. This looks horrendous and I don't know if it's per-fragment or if glslc can optimize it away, somehow.

Does someone know how much of an impact sampler2D() has?

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).

Also, are these kinds of optimizations crucial or are they irrelevant?


EDIT: cleaned up original question without changing the meaning, added same/corollary question with better wording below.

I apologize for my English.

What does this specific piece of code do:

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

Is this executed per-fragment? And if so, is the sampler2D() a function? A type cast? A constructor that allocates memory? This "function" is what I'm concerned about most. I presume indexing is inevitable.

In the comment I wonder if, as an alternative, I could use VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors and have an array of sampler2D in the shader. Would this choice increase performance?

Finally, I wonder if switching to a sampler2Darray really makes much difference (performance-wise).

Upvotes: 1

Views: 1920

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474436

The cost of not using combined image/sampler objects will of course depend on the hardware. However, consider this.

HLSL, from Shader Model 4 onwards (so D3D10+) has never had combined image/samplers. They've always used separate texture and sampler objects.

So if an entire API has been doing this for over a decade, it's probably safe to say that this is not going to be a performance problem.

Upvotes: 3

Related Questions