Skip to content
Snippets Groups Projects
Commit 4ed3dea8 authored by Katharina Krämer's avatar Katharina Krämer
Browse files

[#69] first try to set up shaders and uniforms

parent 84c7c24c
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #25483 failed
......@@ -2,4 +2,5 @@
# Add new projects/examples here:
add_subdirectory(first_triangle)
add_subdirectory(first_mesh)
add_subdirectory(cmd_sync_test)
\ No newline at end of file
add_subdirectory(cmd_sync_test)
add_subdirectory(particle_simulation)
particle_simulation
\ No newline at end of file
cmake_minimum_required(VERSION 3.16)
project(particle_simulation)
# setting c++ standard for the project
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# this should fix the execution path to load local files from the project
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
# adding source files to the project
add_executable(particle_simulation src/main.cpp)
# this should fix the execution path to load local files from the project (for MSVC)
if(MSVC)
set_target_properties(particle_simulation PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set_target_properties(particle_simulation PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
# in addition to setting the output directory, the working directory has to be set
# by default visual studio sets the working directory to the build directory, when using the debugger
set_target_properties(particle_simulation PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif()
# including headers of dependencies and the VkCV framework
target_include_directories(particle_simulation SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include})
# linking with libraries from all dependencies and the VkCV framework
target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera)
%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv
%VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv
pause
\ No newline at end of file
File added
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec4 outColor;
layout(set=0, binding=0) uniform vec4 uColor;
void main()
{
outColor = uColor;
}
\ No newline at end of file
#version 450 core
#extension GL_ARB_separate_shader_objects : enable
layout (location = 0) in vec3 position;
layout( push_constant ) uniform constants{
mat4 mvp;
};
void main()
{
gl_Position = mvp * vec4(position, 1.0);
}
\ No newline at end of file
File added
#include <iostream>
#include <vkcv/Core.hpp>
#include <GLFW/glfw3.h>
#include <vkcv/camera/CameraManager.hpp>
#include <chrono>
int main(int argc, const char** argv) {
const char* applicationName = "First Triangle";
const int windowWidth = 800;
const int windowHeight = 600;
vkcv::Window window = vkcv::Window::create(
applicationName,
windowWidth,
windowHeight,
false
);
vkcv::CameraManager cameraManager(window, windowWidth, windowHeight);
window.initEvents();
vkcv::Core core = vkcv::Core::create(
window,
applicationName,
VK_MAKE_VERSION(0, 0, 1),
{ vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute },
{},
{ "VK_KHR_swapchain" }
);
const auto& context = core.getContext();
const vk::Instance& instance = context.getInstance();
const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice();
const vk::Device& device = context.getDevice();
struct vec3 {
float x, y, z;
};
const size_t n = 5027;
auto testBuffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL);
vec3 vec_data[n];
for (size_t i = 0; i < n; i++) {
vec_data[i] = { 42, static_cast<float>(i), 7 };
}
testBuffer.fill(vec_data);
auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL);
uint16_t indices[3] = { 0, 1, 2 };
triangleIndexBuffer.fill(&indices[0], sizeof(indices));
vkcv::SamplerHandle sampler = core.createSampler(
vkcv::SamplerFilterType::NEAREST,
vkcv::SamplerFilterType::NEAREST,
vkcv::SamplerMipmapMode::NEAREST,
vkcv::SamplerAddressMode::REPEAT
);
std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl;
switch (physicalDevice.getProperties().vendorID) {
case 0x1002: std::cout << "Running AMD huh? You like underdogs, are you a Linux user?" << std::endl; break;
case 0x10DE: std::cout << "An NVidia GPU, how predictable..." << std::endl; break;
case 0x8086: std::cout << "Poor child, running on an Intel GPU, probably integrated..."
"or perhaps you are from the future with a dedicated one?" << std::endl; break;
case 0x13B5: std::cout << "ARM? What the hell are you running on, next thing I know you're trying to run Vulkan on a leg..." << std::endl; break;
default: std::cout << "Unknown GPU vendor?! Either you're on an exotic system or your driver is broken..." << std::endl;
}
// an example attachment for passes that output to the window
const vkcv::AttachmentDescription present_color_attachment(
vkcv::AttachmentLayout::UNDEFINED,
vkcv::AttachmentLayout::COLOR_ATTACHMENT,
vkcv::AttachmentLayout::PRESENTATION,
vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::CLEAR,
core.getSwapchainImageFormat());
vkcv::PassConfig particlePassDefinition({ present_color_attachment });
vkcv::PassHandle particlePass = core.createPass(particlePassDefinition);
if (!particlePass)
{
std::cout << "Error. Could not create renderpass. Exiting." << std::endl;
return EXIT_FAILURE;
}
vkcv::ShaderProgram particleShaderProgram{};
particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv"));
particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv"));
particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX);
particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT);
vkcv::DescriptorSetConfig setConfig({
vkcv::DescriptorBinding(vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT),
});
vkcv::ResourcesHandle set = core.createResourceDescription({setConfig});
const vkcv::PipelineConfig particlePipelineDefinition(
particleShaderProgram,
windowWidth,
windowHeight,
particlePass,
{},
{core.getDescriptorSetLayout(set, 0)});
vkcv::PipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition);
vkcv::Buffer<glm_vec4> color = core.createBuffer<glm_vec4>(
vkcv::BufferType::UNIFORM,
1
);
vkcv::DescriptorWrites setWrites;
setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle())};
core.writeResourceDescription(set,0,setWrites);
if (!particlePipeline)
{
std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl;
return EXIT_FAILURE;
}
std::vector<vkcv::VertexBufferBinding> vertexBufferBindings;
auto start = std::chrono::system_clock::now();
while (window.isWindowOpen())
{
core.beginFrame();
window.pollEvents();
auto end = std::chrono::system_clock::now();
auto deltatime = end - start;
start = end;
cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count());
const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
core.renderMesh(
particlePass,
particlePipeline,
sizeof(mvp),
&mvp,
vertexBufferBindings,
triangleIndexBuffer.getHandle(),
3,
vkcv::ResourcesHandle(),
0);
core.endFrame();
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment