shader can use sampler

feature/softbody-runtime-control
Benjamin Kraft 3 months ago
parent 247c8cb9fa
commit 08f02a3504
  1. 1
      include/application.hpp
  2. 3
      include/vulkan/descriptor_pool.hpp
  3. 5
      shaders/shader.frag
  4. 7
      src/application.cpp
  5. 24
      src/vulkan/descriptor_pool.cpp

@ -58,6 +58,7 @@ private:
unique_ptr<GraphicsPipeline> graphicsPipeline;
unique_ptr<Buffer> cameraUniformBuffer;
unique_ptr<Image> firstImage;
unique_ptr<Camera> camera;
unique_ptr<Grabber> grabber;

@ -5,12 +5,12 @@
#include <map>
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<DescriptorSet, VkDescriptorSet> sets;
std::map<DescriptorSet, VkDescriptorSetLayout> layouts;
private:

@ -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;

@ -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<Image>(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);

@ -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<VkDescriptorSetLayoutBinding> &bindings) {
VkDescriptorSetLayoutCreateInfo layoutCreateInfo {};
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;

Loading…
Cancel
Save