gpu: Update shader code for different buffer/sampler sizes

Use specialization constants for that.
This commit is contained in:
Benjamin Otte 2023-11-03 02:30:12 +01:00
parent 47a13e601f
commit 95e36af46b
2 changed files with 124 additions and 36 deletions

View File

@ -799,6 +799,8 @@ struct _GskVulkanShaderSpecialization
{
guint32 clip;
guint32 n_immutable_samplers;
guint32 n_samplers;
guint32 n_buffers;
};
VkPipeline
@ -840,8 +842,8 @@ gsk_vulkan_device_get_vk_pipeline (GskVulkanDevice *self,
.module = gdk_display_get_vk_shader_module (display, vertex_shader_name),
.pName = "main",
.pSpecializationInfo = &(VkSpecializationInfo) {
.mapEntryCount = 2,
.pMapEntries = (VkSpecializationMapEntry[2]) {
.mapEntryCount = 4,
.pMapEntries = (VkSpecializationMapEntry[4]) {
{
.constantID = 0,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, clip),
@ -852,11 +854,23 @@ gsk_vulkan_device_get_vk_pipeline (GskVulkanDevice *self,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_immutable_samplers),
.size = sizeof (guint32),
},
{
.constantID = 2,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_samplers),
.size = sizeof (guint32),
},
{
.constantID = 3,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_buffers),
.size = sizeof (guint32),
},
},
.dataSize = sizeof (GskVulkanShaderSpecialization),
.pData = &(GskVulkanShaderSpecialization) {
.clip = clip,
.n_immutable_samplers = layout->setup.n_immutable_samplers,
.n_samplers = layout->setup.n_samplers,
.n_buffers = layout->setup.n_buffers,
},
},
},
@ -866,8 +880,8 @@ gsk_vulkan_device_get_vk_pipeline (GskVulkanDevice *self,
.module = gdk_display_get_vk_shader_module (display, fragment_shader_name),
.pName = "main",
.pSpecializationInfo = &(VkSpecializationInfo) {
.mapEntryCount = 2,
.pMapEntries = (VkSpecializationMapEntry[2]) {
.mapEntryCount = 4,
.pMapEntries = (VkSpecializationMapEntry[4]) {
{
.constantID = 0,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, clip),
@ -878,11 +892,23 @@ gsk_vulkan_device_get_vk_pipeline (GskVulkanDevice *self,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_immutable_samplers),
.size = sizeof (guint32),
},
{
.constantID = 2,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_samplers),
.size = sizeof (guint32),
},
{
.constantID = 3,
.offset = G_STRUCT_OFFSET (GskVulkanShaderSpecialization, n_buffers),
.size = sizeof (guint32),
},
},
.dataSize = sizeof (GskVulkanShaderSpecialization),
.pData = &(GskVulkanShaderSpecialization) {
.clip = clip,
.n_immutable_samplers = layout->setup.n_immutable_samplers,
.n_samplers = layout->setup.n_samplers,
.n_buffers = layout->setup.n_buffers,
},
},
},

View File

@ -9,7 +9,9 @@ layout(push_constant) uniform PushConstants {
} push;
layout(constant_id=0) const uint GSK_SHADER_CLIP = GSK_GPU_SHADER_CLIP_NONE;
layout(constant_id=1) const uint GSK_IMMUTABLE_SAMPLERS = 8;
layout(constant_id=1) const uint GSK_N_IMMUTABLE_SAMPLERS = 8;
layout(constant_id=2) const uint GSK_N_SAMPLERS = 16;
layout(constant_id=3) const uint GSK_N_BUFFERS = 16;
#define GSK_GLOBAL_MVP push.mvp
#define GSK_GLOBAL_CLIP push.clip
@ -29,11 +31,11 @@ layout(constant_id=1) const uint GSK_IMMUTABLE_SAMPLERS = 8;
#define PASS(_loc) layout(location = _loc) in
#define PASS_FLAT(_loc) layout(location = _loc) flat in
layout(set = 0, binding = 0) uniform sampler2D immutable_textures[GSK_IMMUTABLE_SAMPLERS];
layout(set = 0, binding = 1) uniform sampler2D textures[50000];
layout(set = 0, binding = 0) uniform sampler2D immutable_textures[GSK_N_IMMUTABLE_SAMPLERS];
layout(set = 0, binding = 1) uniform sampler2D textures[GSK_N_SAMPLERS];
layout(set = 1, binding = 0) readonly buffer FloatBuffers {
float floats[];
} buffers[50000];
} buffers[GSK_N_BUFFERS];
layout(location = 0) out vec4 out_color;
@ -46,25 +48,45 @@ gsk_texture (uint id,
id >>= 1;
if (id == 0)
return texture (immutable_textures[0], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 1 && id == 1)
else if (GSK_N_IMMUTABLE_SAMPLERS > 1 && id == 1)
return texture (immutable_textures[1], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 2 && id == 2)
else if (GSK_N_IMMUTABLE_SAMPLERS > 2 && id == 2)
return texture (immutable_textures[2], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 3 && id == 3)
else if (GSK_N_IMMUTABLE_SAMPLERS > 3 && id == 3)
return texture (immutable_textures[3], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 4 && id == 4)
else if (GSK_N_IMMUTABLE_SAMPLERS > 4 && id == 4)
return texture (immutable_textures[4], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 5 && id == 5)
else if (GSK_N_IMMUTABLE_SAMPLERS > 5 && id == 5)
return texture (immutable_textures[5], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 6 && id == 6)
else if (GSK_N_IMMUTABLE_SAMPLERS > 6 && id == 6)
return texture (immutable_textures[6], pos);
else if (GSK_IMMUTABLE_SAMPLERS > 7 && id == 7)
else if (GSK_N_IMMUTABLE_SAMPLERS > 7 && id == 7)
return texture (immutable_textures[7], pos);
else
return vec4 (1.0, 0.0, 0.8, 1.0);
}
return texture (textures[nonuniformEXT (id >> 1)], pos);
else
{
id >>= 1;
if (id == 0)
return texture (textures[0], pos);
else if (GSK_N_SAMPLERS > 1 && id == 1)
return texture (textures[1], pos);
else if (GSK_N_SAMPLERS > 2 && id == 2)
return texture (textures[2], pos);
else if (GSK_N_SAMPLERS > 3 && id == 3)
return texture (textures[3], pos);
else if (GSK_N_SAMPLERS > 4 && id == 4)
return texture (textures[4], pos);
else if (GSK_N_SAMPLERS > 5 && id == 5)
return texture (textures[5], pos);
else if (GSK_N_SAMPLERS > 6 && id == 6)
return texture (textures[6], pos);
else if (GSK_N_SAMPLERS > 7 && id == 7)
return texture (textures[7], pos);
else
return texture (textures[nonuniformEXT (id)], pos);
}
}
ivec2
@ -76,25 +98,45 @@ gsk_texture_size (uint id,
id >>= 1;
if (id == 0)
return textureSize (immutable_textures[0], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 1 && id == 1)
else if (GSK_N_IMMUTABLE_SAMPLERS > 1 && id == 1)
return textureSize (immutable_textures[1], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 2 && id == 2)
else if (GSK_N_IMMUTABLE_SAMPLERS > 2 && id == 2)
return textureSize (immutable_textures[2], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 3 && id == 3)
else if (GSK_N_IMMUTABLE_SAMPLERS > 3 && id == 3)
return textureSize (immutable_textures[3], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 4 && id == 4)
else if (GSK_N_IMMUTABLE_SAMPLERS > 4 && id == 4)
return textureSize (immutable_textures[4], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 5 && id == 5)
else if (GSK_N_IMMUTABLE_SAMPLERS > 5 && id == 5)
return textureSize (immutable_textures[5], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 6 && id == 6)
else if (GSK_N_IMMUTABLE_SAMPLERS > 6 && id == 6)
return textureSize (immutable_textures[6], lod);
else if (GSK_IMMUTABLE_SAMPLERS > 7 && id == 7)
else if (GSK_N_IMMUTABLE_SAMPLERS > 7 && id == 7)
return textureSize (immutable_textures[7], lod);
else
return ivec2 (1, 1);
}
return textureSize (textures[nonuniformEXT (id >> 1)], lod);
else
{
id >>= 1;
if (id == 0)
return textureSize (textures[0], lod);
else if (GSK_N_SAMPLERS > 1 && id == 1)
return textureSize (textures[1], lod);
else if (GSK_N_SAMPLERS > 2 && id == 2)
return textureSize (textures[2], lod);
else if (GSK_N_SAMPLERS > 3 && id == 3)
return textureSize (textures[3], lod);
else if (GSK_N_SAMPLERS > 4 && id == 4)
return textureSize (textures[4], lod);
else if (GSK_N_SAMPLERS > 5 && id == 5)
return textureSize (textures[5], lod);
else if (GSK_N_SAMPLERS > 6 && id == 6)
return textureSize (textures[6], lod);
else if (GSK_N_SAMPLERS > 7 && id == 7)
return textureSize (textures[7], lod);
else
return textureSize (textures[nonuniformEXT (id)], lod);
}
}
vec4
@ -107,29 +149,49 @@ gsk_texel_fetch (uint id,
id >>= 1;
if (id == 0)
return texelFetch (immutable_textures[0], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 1 && id == 1)
else if (GSK_N_IMMUTABLE_SAMPLERS > 1 && id == 1)
return texelFetch (immutable_textures[1], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 2 && id == 2)
else if (GSK_N_IMMUTABLE_SAMPLERS > 2 && id == 2)
return texelFetch (immutable_textures[2], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 3 && id == 3)
else if (GSK_N_IMMUTABLE_SAMPLERS > 3 && id == 3)
return texelFetch (immutable_textures[3], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 4 && id == 4)
else if (GSK_N_IMMUTABLE_SAMPLERS > 4 && id == 4)
return texelFetch (immutable_textures[4], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 5 && id == 5)
else if (GSK_N_IMMUTABLE_SAMPLERS > 5 && id == 5)
return texelFetch (immutable_textures[5], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 6 && id == 6)
else if (GSK_N_IMMUTABLE_SAMPLERS > 6 && id == 6)
return texelFetch (immutable_textures[6], pos, lod);
else if (GSK_IMMUTABLE_SAMPLERS > 7 && id == 7)
else if (GSK_N_IMMUTABLE_SAMPLERS > 7 && id == 7)
return texelFetch (immutable_textures[7], pos, lod);
else
return vec4 (1.0, 0.0, 0.8, 1.0);
}
return texelFetch (textures[nonuniformEXT (id >> 1)], pos, lod);
else
{
id >>= 1;
if (id == 0)
return texelFetch (textures[0], pos, lod);
else if (GSK_N_SAMPLERS > 1 && id == 1)
return texelFetch (textures[1], pos, lod);
else if (GSK_N_SAMPLERS > 2 && id == 2)
return texelFetch (textures[2], pos, lod);
else if (GSK_N_SAMPLERS > 3 && id == 3)
return texelFetch (textures[3], pos, lod);
else if (GSK_N_SAMPLERS > 4 && id == 4)
return texelFetch (textures[4], pos, lod);
else if (GSK_N_SAMPLERS > 5 && id == 5)
return texelFetch (textures[5], pos, lod);
else if (GSK_N_SAMPLERS > 6 && id == 6)
return texelFetch (textures[6], pos, lod);
else if (GSK_N_SAMPLERS > 7 && id == 7)
return texelFetch (textures[7], pos, lod);
else
return texelFetch (textures[nonuniformEXT (id)], pos, lod);
}
}
#define gsk_get_buffer(id) buffers[nonuniformEXT (id)]
#define gsk_get_float(id) gsk_get_buffer(0).floats[id]
#define gsk_get_float(id) gsk_get_buffer(id >> 22).floats[id & 0x3FFFFF]
#define gsk_get_int(id) (floatBitsToInt(gsk_get_float(id)))
#define gsk_get_uint(id) (floatBitsToUint(gsk_get_float(id)))