Skip to content

Commit 7d976cc

Browse files
committed
feat(linux): add combined image sampler
1 parent 60517ef commit 7d976cc

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Linux ❘ Texture mapping ❘ Image view and sampler
1+
# Linux ❘ Texture mapping ❘ Combined image sampler
22

33
Summary [here](https://github.com/Pacheco95/khronos-vulkan-tutorial-cpp/tree/linux-summary).
44

@@ -7,7 +7,7 @@ Summary [here](https://github.com/Pacheco95/khronos-vulkan-tutorial-cpp/tree/lin
77
# Navigation
88

99
[🌐 Original tutorial](
10-
https://docs.vulkan.org/tutorial/latest/06_Texture_mapping/01_Image_view_and_sampler.html)
10+
https://docs.vulkan.org/tutorial/latest/06_Texture_mapping/02_Combined_image_sampler.html)
1111

1212
[⏮ Texture mapping / Images](
1313
https://github.com/Pacheco95/khronos-vulkan-tutorial-cpp/tree/linux/05-texture-mapping/01-images)

res/shaders/shader.frag

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#version 450
22

3+
layout (binding = 1) uniform sampler2D texSampler;
4+
35
layout (location = 0) in vec3 fragColor;
6+
layout (location = 1) in vec2 fragTexCoord;
47

58
layout (location = 0) out vec4 outColor;
69

710
void main() {
8-
outColor = vec4(fragColor, 1.0);
11+
outColor = texture(texSampler, fragTexCoord);
912
}

res/shaders/shader.vert

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ layout (binding = 0) uniform UniformBufferObject {
88

99
layout (location = 0) in vec2 inPosition;
1010
layout (location = 1) in vec3 inColor;
11+
layout (location = 2) in vec2 inTexCoord;
1112

1213
layout (location = 0) out vec3 fragColor;
14+
layout (location = 1) out vec2 fragTexCoord;
1315

1416
void main() {
1517
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
1618
fragColor = inColor;
19+
fragTexCoord = inTexCoord;
1720
}

src/Application.cpp

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
constexpr auto NO_TIMEOUT = std::numeric_limits<uint64_t>::max();
1919

2020
const std::vector<Vertex> VERTICES = {
21-
{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
22-
{{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
23-
{{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
24-
{{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}}};
21+
{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f}},
22+
{{0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
23+
{{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
24+
{{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}};
25+
2526

2627
const std::vector<uint16_t> INDICES = {0, 1, 2, 2, 3, 0};
2728

@@ -60,6 +61,8 @@ void Application::initVulkan() {
6061
createFrameBuffers();
6162
createCommandPool();
6263
createTextureImage();
64+
createTextureImageView();
65+
createTextureSampler();
6366
createVertexBuffer();
6467
createIndexBuffer();
6568
createUniformBuffers();
@@ -401,8 +404,18 @@ void Application::createDescriptorSetLayout() {
401404
.setDescriptorType(vk::DescriptorType::eUniformBuffer)
402405
.setStageFlags(vk::ShaderStageFlagBits::eVertex);
403406

407+
const auto samplerLayoutBinding =
408+
vk::DescriptorSetLayoutBinding()
409+
.setBinding(1)
410+
.setDescriptorCount(1)
411+
.setDescriptorType(vk::DescriptorType::eCombinedImageSampler)
412+
.setStageFlags(vk::ShaderStageFlagBits::eFragment);
413+
414+
415+
std::array bindings = {uboLayoutBinding, samplerLayoutBinding};
416+
404417
vk::DescriptorSetLayoutCreateInfo layoutInfo;
405-
layoutInfo.setBindings(uboLayoutBinding);
418+
layoutInfo.setBindings(bindings);
406419

407420
m_descriptorSetLayout = m_device.createDescriptorSetLayout(layoutInfo);
408421
}
@@ -728,14 +741,21 @@ void Application::createUniformBuffers() {
728741
}
729742

730743
void Application::createDescriptorPool() {
731-
vk::DescriptorPoolSize poolSize;
732-
poolSize.type = vk::DescriptorType::eUniformBuffer;
733-
poolSize.descriptorCount =
744+
const auto maxFramesInFlight =
734745
static_cast<uint32_t>(Config::MAX_FRAMES_IN_FLIGHT);
735746

747+
std::array<vk::DescriptorPoolSize, 2> poolSizes;
748+
749+
poolSizes[0].type = vk::DescriptorType::eUniformBuffer;
750+
poolSizes[0].descriptorCount = maxFramesInFlight;
751+
752+
poolSizes[1].type = vk::DescriptorType::eCombinedImageSampler;
753+
poolSizes[1].descriptorCount = maxFramesInFlight;
754+
755+
736756
vk::DescriptorPoolCreateInfo poolInfo;
737-
poolInfo.setPoolSizes(poolSize);
738-
poolInfo.maxSets = static_cast<uint32_t>(Config::MAX_FRAMES_IN_FLIGHT);
757+
poolInfo.setPoolSizes(poolSizes);
758+
poolInfo.maxSets = maxFramesInFlight;
739759

740760
m_descriptorPool = m_device.createDescriptorPool(poolInfo);
741761
}
@@ -759,15 +779,30 @@ void Application::createDescriptorSets() {
759779
bufferInfo.offset = 0;
760780
bufferInfo.range = sizeof(UniformBufferObject);
761781

762-
vk::WriteDescriptorSet descriptorWrite;
763-
descriptorWrite.dstSet = m_descriptorSets[i];
764-
descriptorWrite.dstBinding = 0;
765-
descriptorWrite.dstArrayElement = 0;
766-
descriptorWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
767-
descriptorWrite.descriptorCount = 1;
768-
descriptorWrite.setBufferInfo(bufferInfo);
782+
vk::DescriptorImageInfo imageInfo;
783+
imageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
784+
imageInfo.imageView = textureImageView;
785+
imageInfo.sampler = textureSampler;
786+
787+
std::array<vk::WriteDescriptorSet, 2> descriptorWrites;
788+
789+
descriptorWrites[0].dstSet = m_descriptorSets[i];
790+
descriptorWrites[0].dstBinding = 0;
791+
descriptorWrites[0].dstArrayElement = 0;
792+
descriptorWrites[0].descriptorType = vk::DescriptorType::eUniformBuffer;
793+
descriptorWrites[0].descriptorCount = 1;
794+
descriptorWrites[0].pBufferInfo = &bufferInfo;
769795

770-
m_device.updateDescriptorSets(descriptorWrite, {});
796+
descriptorWrites[1].dstSet = m_descriptorSets[i];
797+
descriptorWrites[1].dstBinding = 1;
798+
descriptorWrites[1].dstArrayElement = 0;
799+
descriptorWrites[1].descriptorType =
800+
vk::DescriptorType::eCombinedImageSampler;
801+
descriptorWrites[1].descriptorCount = 1;
802+
descriptorWrites[1].pImageInfo = &imageInfo;
803+
804+
805+
m_device.updateDescriptorSets(descriptorWrites, {});
771806
}
772807
}
773808

@@ -1074,7 +1109,7 @@ void Application::createImage(
10741109
vk::MemoryRequirements memRequirements =
10751110
m_device.getImageMemoryRequirements(image);
10761111

1077-
vk::MemoryAllocateInfo allocInfo{};
1112+
vk::MemoryAllocateInfo allocInfo;
10781113
allocInfo.allocationSize = memRequirements.size;
10791114
allocInfo.memoryTypeIndex =
10801115
findMemoryType(memRequirements.memoryTypeBits, properties);
@@ -1091,7 +1126,7 @@ void Application::transitionImageLayout(
10911126
) {
10921127
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
10931128

1094-
vk::ImageMemoryBarrier barrier{};
1129+
vk::ImageMemoryBarrier barrier;
10951130
barrier.oldLayout = oldLayout;
10961131
barrier.newLayout = newLayout;
10971132
barrier.srcQueueFamilyIndex = vk::QueueFamilyIgnored;
@@ -1142,7 +1177,7 @@ void Application::copyBufferToImage(
11421177
) {
11431178
vk::CommandBuffer commandBuffer = beginSingleTimeCommands();
11441179

1145-
vk::BufferImageCopy region{};
1180+
vk::BufferImageCopy region;
11461181
region.bufferOffset = 0;
11471182
region.bufferRowLength = 0;
11481183
region.bufferImageHeight = 0;

src/Vertex.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ vk::VertexInputBindingDescription Vertex::getBindingDescription() {
88
return bindingDescription;
99
}
1010

11-
std::array<vk::VertexInputAttributeDescription, 2>
11+
std::array<vk::VertexInputAttributeDescription, 3>
1212
Vertex::getAttributeDescriptions() {
13-
std::array<vk::VertexInputAttributeDescription, 2> attributeDescriptions{};
13+
std::array<vk::VertexInputAttributeDescription, 3> attributeDescriptions{};
1414

1515
attributeDescriptions[0].binding = 0;
1616
attributeDescriptions[0].location = 0;
@@ -22,5 +22,10 @@ Vertex::getAttributeDescriptions() {
2222
attributeDescriptions[1].format = vk::Format::eR32G32B32Sfloat;
2323
attributeDescriptions[1].offset = offsetof(Vertex, color);
2424

25+
attributeDescriptions[2].binding = 0;
26+
attributeDescriptions[2].location = 2;
27+
attributeDescriptions[2].format = vk::Format::eR32G32Sfloat;
28+
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
29+
2530
return attributeDescriptions;
2631
}

src/Vertex.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
struct Vertex {
88
glm::vec2 pos;
99
glm::vec3 color;
10+
glm::vec2 texCoord;
1011

1112
static vk::VertexInputBindingDescription getBindingDescription();
1213

13-
static std::array<vk::VertexInputAttributeDescription, 2>
14+
static std::array<vk::VertexInputAttributeDescription, 3>
1415
getAttributeDescriptions();
1516
};

0 commit comments

Comments
 (0)