From 08f02a35040dd65e898d9464139a1089d321a909 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Wed, 16 Oct 2024 20:07:22 +0200 Subject: [PATCH] shader can use sampler --- include/application.hpp | 1 + include/vulkan/descriptor_pool.hpp | 3 ++- shaders/shader.frag | 5 ++++- src/application.cpp | 7 +++++-- src/vulkan/descriptor_pool.cpp | 24 +++++++++++++++++++++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/application.hpp b/include/application.hpp index 0a7ca54..debe8e7 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -58,6 +58,7 @@ private: unique_ptr graphicsPipeline; unique_ptr cameraUniformBuffer; + unique_ptr firstImage; unique_ptr camera; unique_ptr grabber; diff --git a/include/vulkan/descriptor_pool.hpp b/include/vulkan/descriptor_pool.hpp index a00829e..22d3c52 100644 --- a/include/vulkan/descriptor_pool.hpp +++ b/include/vulkan/descriptor_pool.hpp @@ -5,12 +5,12 @@ #include class Buffer; +class Image; enum class DescriptorSet { WORLD, SIMULATION, MESH - }; class DescriptorPool { @@ -19,6 +19,7 @@ public: ~DescriptorPool(); void bindBuffer(const Buffer& buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding); + void bindImage(const Image& image, VkDescriptorType type, DescriptorSet set, uint32_t binding); std::map sets; std::map layouts; private: diff --git a/shaders/shader.frag b/shaders/shader.frag index 6fd2d13..83be0f0 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -5,6 +5,8 @@ layout (location = 0) out vec4 outColor; layout (location = 0) in vec2 uv; layout (location = 1) in vec3 normal; +layout (set = 0, binding = 2) uniform sampler2D texSampler; + void main() { // outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0); @@ -13,7 +15,8 @@ void main() { vec3 N = normal; - vec3 albedo = vec3(1, 1, 1); + vec3 albedo = vec3(uv.xy, 1); + albedo = texture(texSampler, uv).rgb; float lambertian = max(dot(L, N), 0); vec3 diffuse = lambertian * albedo; diff --git a/src/application.cpp b/src/application.cpp index 6baf3f7..f4e32cc 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -106,11 +106,14 @@ Application::Application() { VkDeviceSize imageSize = w * h * 4; u_char* data = new u_char[imageSize]; memset(data, 0xFF, imageSize); - Image image(data, imageSize, w, h, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT); - image.createSampler(); + memset(data, 0x44, 3); + firstImage = make_unique(data, imageSize, w, h, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT); + firstImage->createView(VK_IMAGE_ASPECT_COLOR_BIT); + firstImage->createSampler(); } descriptorPool->bindBuffer(*cameraUniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::WORLD, 0); + descriptorPool->bindImage(*firstImage, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, DescriptorSet::WORLD, 2); descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); descriptorPool->bindBuffer(*faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1); diff --git a/src/vulkan/descriptor_pool.cpp b/src/vulkan/descriptor_pool.cpp index bfe1eca..d816ef5 100644 --- a/src/vulkan/descriptor_pool.cpp +++ b/src/vulkan/descriptor_pool.cpp @@ -1,12 +1,14 @@ #include "vulkan/descriptor_pool.hpp" #include "vulkan/instance.hpp" #include "vulkan/buffer.hpp" +#include "vulkan/image.hpp" DescriptorPool::DescriptorPool() { VkDescriptorPoolSize poolSizes[] = { {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 20}, - {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 20} + {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 20}, + {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 10} }; VkDescriptorPoolCreateInfo poolInfo {}; @@ -32,6 +34,8 @@ DescriptorPool::DescriptorPool() { addBinding(DescriptorSet::WORLD, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT); // lights addBinding(DescriptorSet::WORLD, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT); + // texture + addBinding(DescriptorSet::WORLD, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT); // vertices addBinding(DescriptorSet::MESH, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT); @@ -89,6 +93,24 @@ void DescriptorPool::bindBuffer(const Buffer& buffer, VkDescriptorType type, Des vkUpdateDescriptorSets(Instance::GetDevice(), 1, &descriptorWrite, 0, nullptr); } +void DescriptorPool::bindImage(const Image &image, VkDescriptorType type, DescriptorSet set, uint32_t binding) { + VkDescriptorImageInfo imageInfo {}; + imageInfo.imageView = image.view; + imageInfo.sampler = image.sampler; + imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkWriteDescriptorSet descriptorWrite {}; + descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrite.dstSet = sets[set]; + descriptorWrite.dstBinding = binding; + descriptorWrite.dstArrayElement = 0; + descriptorWrite.descriptorType = type; + descriptorWrite.descriptorCount = 1; + descriptorWrite.pImageInfo = &imageInfo; + + vkUpdateDescriptorSets(Instance::GetDevice(), 1, &descriptorWrite, 0, nullptr); +} + void DescriptorPool::createLayout(DescriptorSet set, const std::vector &bindings) { VkDescriptorSetLayoutCreateInfo layoutCreateInfo {}; layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;