From 6d986f496ebad1f5f04d9d2b0f67b5c35b3ae087 Mon Sep 17 00:00:00 2001 From: Ano-sys Date: Sat, 5 Apr 2025 15:04:30 +0200 Subject: [PATCH] Image is projected onto the sqaure now --- shaders/shader.frag | 6 ++++- shaders/shader.vert | 17 +++----------- vulkan/vulkan_app.cpp | 54 ++++++++++++++++++++++++++++++------------- vulkan/vulkan_app.hpp | 20 +++++++++++----- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/shaders/shader.frag b/shaders/shader.frag index c5523dc..382ed0a 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,9 +1,13 @@ #version 450 layout(location = 0) in vec3 fragColor; +layout(location = 1) in vec2 fragTexCoord; layout(location = 0) out vec4 outColor; +layout(binding = 1) uniform sampler2D texSampler; + void main(){ - outColor = vec4(fragColor, 1.0); + // outColor = vec4(fragTexCoord, 0.0, 1.0); + outColor = texture(texSampler, fragTexCoord); } diff --git a/shaders/shader.vert b/shaders/shader.vert index 52f9d74..97811c9 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -8,22 +8,10 @@ layout(set = 0, binding = 0) uniform UniformBufferObject{ layout(location = 0) in vec2 inPosition; layout(location = 1) in vec3 inColor; +layout(location = 2) in vec2 inTexCoord; layout(location = 0) out vec3 fragColor; - -/* -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[]( - vec3(0.0, 1.0, 1.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) -); -*/ +layout(location = 1) out vec2 fragTexCoord; void main(){ // gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); @@ -31,4 +19,5 @@ void main(){ // gl_Position = vec4(inPosition, 0.0, 1.0); gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); fragColor = inColor; + fragTexCoord = inTexCoord; } diff --git a/vulkan/vulkan_app.cpp b/vulkan/vulkan_app.cpp index 9699840..c289ddb 100644 --- a/vulkan/vulkan_app.cpp +++ b/vulkan/vulkan_app.cpp @@ -419,10 +419,19 @@ namespace vapp{ uboLayoutBinding.descriptorCount = 1; uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + VkDescriptorSetLayoutBinding samplerLayoutBinding{}; + samplerLayoutBinding.binding = 1; + samplerLayoutBinding.descriptorCount = 1; + samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + samplerLayoutBinding.pImmutableSamplers = nullptr; + samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; + + std::array bindings = { uboLayoutBinding, samplerLayoutBinding }; + VkDescriptorSetLayoutCreateInfo layoutInfo{}; layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - layoutInfo.bindingCount = 1; - layoutInfo.pBindings = &uboLayoutBinding; + layoutInfo.bindingCount = static_cast(bindings.size()); + layoutInfo.pBindings = bindings.data(); if(vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS){ throw std::runtime_error("Func: createDescriptorSetLayout\nError: Failed to create Descriptor Set Layout!\n"); @@ -1026,14 +1035,16 @@ namespace vapp{ } void Vulkan::createDescriptorPool(){ - VkDescriptorPoolSize poolSize{}; - poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - poolSize.descriptorCount = static_cast(MAX_FRAMES_IN_FLIGHT); + std::array poolSizes{}; + poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + poolSizes[0].descriptorCount = static_cast(MAX_FRAMES_IN_FLIGHT); + poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + poolSizes[1].descriptorCount = static_cast(MAX_FRAMES_IN_FLIGHT); VkDescriptorPoolCreateInfo poolInfo{}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - poolInfo.poolSizeCount = 1; - poolInfo.pPoolSizes = &poolSize; + poolInfo.poolSizeCount = static_cast(poolSizes.size()); + poolInfo.pPoolSizes = poolSizes.data(); poolInfo.maxSets = static_cast(MAX_FRAMES_IN_FLIGHT); if(vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS){ @@ -1062,18 +1073,29 @@ namespace vapp{ bufferInfo.offset = 0; bufferInfo.range = sizeof(UniformBufferObject); - VkWriteDescriptorSet descriptorWrite{}; - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrite.dstSet = descriptorSets[i]; - descriptorWrite.dstBinding = 0; - descriptorWrite.dstArrayElement = 0; + VkDescriptorImageInfo imageInfo{}; + imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + imageInfo.imageView = textureImageView; + imageInfo.sampler = textureSampler; - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptorWrite.descriptorCount = 1; + std::array descriptorWrites{}; + descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[0].dstSet = descriptorSets[i]; + descriptorWrites[0].dstBinding = 0; + descriptorWrites[0].dstArrayElement = 0; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorCount = 1; + descriptorWrites[0].pBufferInfo = &bufferInfo; - descriptorWrite.pBufferInfo = &bufferInfo; + descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[1].dstSet = descriptorSets[i]; + descriptorWrites[1].dstBinding = 1; + descriptorWrites[1].dstArrayElement = 0; + descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + descriptorWrites[1].descriptorCount = 1; + descriptorWrites[1].pImageInfo = &imageInfo; - vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr); + vkUpdateDescriptorSets(device, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); } } diff --git a/vulkan/vulkan_app.hpp b/vulkan/vulkan_app.hpp index f2ab29c..129d1b5 100644 --- a/vulkan/vulkan_app.hpp +++ b/vulkan/vulkan_app.hpp @@ -34,17 +34,20 @@ namespace vapp{ struct Vertex{ glm::vec2 pos; glm::vec3 color; + glm::vec2 texCoord; static VkVertexInputBindingDescription getBindingDescription(){ VkVertexInputBindingDescription bindingDescription{}; + bindingDescription.binding = 0; bindingDescription.stride = sizeof(Vertex); bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + return bindingDescription; } - static std::array getAttributeDescriptions(){ - std::array attributeDescriptions{}; + static std::array getAttributeDescriptions(){ + std::array attributeDescriptions{}; attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; @@ -56,16 +59,21 @@ namespace vapp{ attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; attributeDescriptions[1].offset = offsetof(Vertex, color); + attributeDescriptions[2].binding = 0; + attributeDescriptions[2].location = 2; + attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[2].offset = offsetof(Vertex, texCoord); + return attributeDescriptions; } }; // TODO: Remove const std::vector vertices = { - {{-0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}}, - {{0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}}, -{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}}, -{{-0.5f, 0.5f}, {1.0f, 0.0f, 0.0f}} + {{-0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, + {{0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}, +{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f}}, +{{-0.5f, 0.5f}, {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f}} }; // uint16_t also possible -> change in vkCmdBindIndexBuffer also