Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/api_vulkan_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class ApiVulkanSample : public vkb::VulkanSampleC
/**
* @brief Creates a new (graphics) command pool object storing command buffers
*/
void create_command_pool();
virtual void create_command_pool();

/**
* @brief Setup default depth and stencil views
Expand Down
6 changes: 6 additions & 0 deletions framework/vulkan_type_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ struct HPPType<VkPhysicalDeviceDescriptorBufferFeaturesEXT>
using Type = vk::PhysicalDeviceDescriptorBufferFeaturesEXT;
};

template <>
struct HPPType<VkPhysicalDeviceDescriptorHeapFeaturesEXT>
{
using Type = vk::PhysicalDeviceDescriptorHeapFeaturesEXT;
};

template <>
struct HPPType<VkPhysicalDeviceDescriptorIndexingFeaturesEXT>
{
Expand Down
36 changes: 36 additions & 0 deletions samples/extensions/descriptor_heap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2026 Sascha Willems
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 the "License";
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH)
get_filename_component(CATEGORY_NAME ${PARENT_DIR} NAME)

add_sample(
ID ${FOLDER_NAME}
CATEGORY ${CATEGORY_NAME}
AUTHOR "Sascha Willems"
NAME "Descriptor Heap"
DESCRIPTION "Demonstrates the descriptor heap extension to streamline descriptor setup"
SHADER_FILES_GLSL
"descriptor_heap/glsl/cube.vert"
"descriptor_heap/glsl/cube.frag"
SHADER_FILES_HLSL
"descriptor_heap/hlsl/cube.vert.hlsl"
"descriptor_heap/hlsl/cube.frag.hlsl"
SHADER_FILES_SLANG
"descriptor_heap/slang/cube.vert.slang"
"descriptor_heap/slang/cube.frag.slang")
581 changes: 581 additions & 0 deletions samples/extensions/descriptor_heap/descriptor_heap.cpp

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions samples/extensions/descriptor_heap/descriptor_heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2026 Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "api_vulkan_sample.h"

class DescriptorHeap : public ApiVulkanSample
{
public:
DescriptorHeap();
~DescriptorHeap() override;

bool prepare(const vkb::ApplicationOptions &options) override;

void render(float delta_time) override;
void create_command_pool() override;
void build_command_buffers() override;
void build_command_buffer();
void request_gpu_features(vkb::core::PhysicalDeviceC &gpu) override;
void setup_render_pass() override;
void setup_framebuffer() override;
void on_update_ui_overlay(vkb::Drawer &drawer) override;

private:
bool animate = true;

struct Cube
{
Texture texture;
glm::vec3 rotation;
};
std::array<Cube, 2> cubes;

struct UniformData
{
glm::mat4 projection_matrix;
glm::mat4 view_matrix;
glm::mat4 model_matrix[2];
} uniform_data;

VkPhysicalDeviceDescriptorHeapPropertiesEXT descriptor_heap_properties{};
std::unique_ptr<vkb::core::BufferC> descriptor_heap_resources;
std::unique_ptr<vkb::core::BufferC> descriptor_heap_samplers;
std::vector<std::unique_ptr<vkb::core::BufferC>> uniform_buffers;

int32_t selected_sampler{0};

std::unique_ptr<vkb::sg::SubMesh> cube;

// Size and offset values for heap objects
VkDeviceSize buffer_heap_offset{0};
VkDeviceSize buffer_descriptor_size{0};
VkDeviceSize image_heap_offset{0};
VkDeviceSize image_descriptor_size{0};
VkDeviceSize sampler_heap_offset{0};
VkDeviceSize sampler_descriptor_size{0};

std::vector<std::string> sampler_names{"Linear", "Nearest"};

VkPipeline pipeline{nullptr};

uint32_t get_api_version() const override;

void load_assets();
void prepare_uniform_buffers();
void update_uniform_buffers(float delta_time);
void create_descriptor_heaps();
void create_pipeline();
void draw(float delta_time);
};

std::unique_ptr<vkb::VulkanSampleC> create_descriptor_heap();
35 changes: 35 additions & 0 deletions shaders/descriptor_heap/glsl/cube.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#version 450
/* Copyright (c) 2026, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

layout (set = 1, binding = 0) uniform texture2D textureImage[2];
layout (set = 2, binding = 0) uniform sampler textureSampler[2];

layout (location = 0) in vec2 inUV;
layout (location = 1) flat in int inInstanceIndex;

layout(push_constant) uniform PushConsts {
int samplerIndex;
int frameIndex;
} pushConsts;

layout (location = 0) out vec4 outFragColor;

void main()
{
outFragColor = texture(sampler2D(textureImage[inInstanceIndex], textureSampler[pushConsts.samplerIndex]), inUV);
}
Binary file added shaders/descriptor_heap/glsl/cube.frag.spv
Binary file not shown.
41 changes: 41 additions & 0 deletions shaders/descriptor_heap/glsl/cube.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 450
/* Copyright (c) 2026, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;

layout(push_constant) uniform PushConsts {
int samplerIndex;
int frameIndex;
} pushConsts;

layout (set = 0, binding = 0) uniform UBO {
mat4 projection;
mat4 view;
mat4 model[2];
} ubo[2];

layout (location = 0) out vec2 outUV;
layout (location = 1) flat out int outInstanceIndex;

void main()
{
outUV = inUV;
gl_Position = ubo[pushConsts.frameIndex].projection * ubo[pushConsts.frameIndex].view * ubo[pushConsts.frameIndex].model[gl_InstanceIndex] * vec4(inPos.xyz, 1.0);
outInstanceIndex = gl_InstanceIndex;
}
Binary file added shaders/descriptor_heap/glsl/cube.vert.spv
Binary file not shown.
40 changes: 40 additions & 0 deletions shaders/descriptor_heap/hlsl/cube.frag.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright (c) 2026, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

[[vk::binding(0, 1)]]
Texture2D textureImage[2] : register(t1, space2);
[[vk::binding(0, 2)]]
SamplerState textureSampler[2] : register(s1, space2);

struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
[[vk::location(1)]] int InstanceIndex : TEXCOORD1;
};

struct PushConsts
{
int samplerIndex;
int frameIndex;
};
[[vk::push_constant]] PushConsts pushConsts;

float4 main(VSOutput input) : SV_TARGET0
{
return textureImage[input.InstanceIndex].Sample(textureSampler[pushConsts.samplerIndex], input.UV);
}
Binary file added shaders/descriptor_heap/hlsl/cube.frag.spv
Binary file not shown.
53 changes: 53 additions & 0 deletions shaders/descriptor_heap/hlsl/cube.vert.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright (c) 2026, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
};

struct UBOCamera
{
float4x4 projection;
float4x4 view;
float4x4 model[2];
};
[[vk::binding(0, 0)]] ConstantBuffer<UBOCamera> ubo[2] : register(b0, space0);

struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
[[vk::location(1)]] int InstanceIndex : TEXCOORD1;
};

struct PushConsts
{
int samplerIndex;
int frameIndex;
};
[[vk::push_constant]] PushConsts pushConsts;

VSOutput main(VSInput input, uint InstanceIndex: SV_InstanceID)
{
VSOutput output;
output.UV = input.UV;
output.Pos = mul(ubo[pushConsts.frameIndex].projection, mul(ubo[pushConsts.frameIndex].view, mul(ubo[pushConsts.frameIndex].model[InstanceIndex], float4(input.Pos.xyz, 1.0))));
output.InstanceIndex = InstanceIndex;
return output;
};
Binary file added shaders/descriptor_heap/hlsl/cube.vert.spv
Binary file not shown.
32 changes: 32 additions & 0 deletions shaders/descriptor_heap/slang/cube.frag.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright (c) 2026, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

struct VSOutput
{
float4 Pos : SV_POSITION;
float2 UV;
int InstanceIndex;
};

[[vk::binding(0, 1)]] Texture2D textureImage[2];
[[vk::binding(0, 2)]] SamplerState textureSampler[2];

[shader("fragment")]
float4 main(VSOutput input, uniform int samplerIndex, uniform int frameIndex)
{
return textureImage[input.InstanceIndex].Sample(textureSampler[samplerIndex], input.UV);
}
Binary file added shaders/descriptor_heap/slang/cube.frag.spv
Binary file not shown.
Loading
Loading