Commit eab9f823 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Merge branch '69-partikelsystem' into 'develop'

Resolve "Partikelsystem"

Closes #69

See merge request !56
parents 87f98dec 2e4b1b45
Pipeline #26256 passed with stages
in 3 minutes and 58 seconds
......@@ -37,11 +37,12 @@ namespace vkcv {
};
struct DrawcallInfo {
inline DrawcallInfo(const Mesh& mesh, const std::vector<DescriptorSetUsage>& descriptorSets)
: mesh(mesh), descriptorSets(descriptorSets) {}
inline DrawcallInfo(const Mesh& mesh, const std::vector<DescriptorSetUsage>& descriptorSets, const uint32_t instanceCount = 1)
: mesh(mesh), descriptorSets(descriptorSets), instanceCount(instanceCount){}
Mesh mesh;
std::vector<DescriptorSetUsage> descriptorSets;
uint32_t instanceCount;
};
void recordDrawcall(
......
......@@ -18,16 +18,21 @@ namespace vkcv {
enum class CullMode{ None, Front, Back };
enum class DepthTest { None, Less, LessEqual, Greater, GreatherEqual, Equal };
// add more as needed
// alternatively we could expose the blend factors directly
enum class BlendMode{ None, Additive };
struct PipelineConfig {
ShaderProgram m_ShaderProgram;
uint32_t m_Width;
uint32_t m_Height;
PassHandle m_PassHandle;
VertexLayout m_VertexLayout;
std::vector<vk::DescriptorSetLayout> m_DescriptorLayouts;
bool m_UseDynamicViewport;
bool m_UseConservativeRasterization = false;
PrimitiveTopology m_PrimitiveTopology = PrimitiveTopology::TriangleList;
ShaderProgram m_ShaderProgram;
uint32_t m_Width;
uint32_t m_Height;
PassHandle m_PassHandle;
VertexLayout m_VertexLayout;
std::vector<vk::DescriptorSetLayout> m_DescriptorLayouts;
bool m_UseDynamicViewport;
bool m_UseConservativeRasterization = false;
PrimitiveTopology m_PrimitiveTopology = PrimitiveTopology::TriangleList;
BlendMode m_blendMode = BlendMode::None;
bool m_EnableDepthClamping = false;
Multisampling m_multisampling = Multisampling::None;
CullMode m_culling = CullMode::None;
......
......@@ -75,7 +75,7 @@ namespace vkcv::camera {
* @brief Gets the current projection of the camera
* @return The current projection matrix
*/
const glm::mat4& getProjection() const;
glm::mat4 getProjection() const;
/**
* @brief Gets the model-view-projection matrix of the camera with y-axis-correction applied
......
......@@ -37,22 +37,22 @@ namespace vkcv::camera {
m_view = view;
}
const glm::mat4& Camera::getProjection() const {
return m_projection;
const glm::mat4 y_correction(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
glm::mat4 Camera::getProjection() const {
return y_correction * m_projection;
}
void Camera::setProjection(const glm::mat4& projection) {
m_projection = projection;
m_projection = glm::inverse(y_correction) * projection;
}
glm::mat4 Camera::getMVP() const {
const glm::mat4 y_correction (
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
return y_correction * m_projection * m_view;
}
......
......@@ -3,5 +3,6 @@
add_subdirectory(bloom)
add_subdirectory(first_triangle)
add_subdirectory(first_mesh)
add_subdirectory(particle_simulation)
add_subdirectory(first_scene)
add_subdirectory(voxelization)
\ No newline at end of file
add_subdirectory(voxelization)
......@@ -175,8 +175,8 @@ int main(int argc, const char** argv) {
std::vector<vkcv::DrawcallInfo> shadowDrawcalls;
for (const auto& position : instancePositions) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), position));
drawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, { descriptorUsage }));
shadowDrawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, {}));
drawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, { descriptorUsage },1));
shadowDrawcalls.push_back(vkcv::DrawcallInfo(loadedMesh, {},1));
}
modelMatrices.back() *= glm::scale(glm::mat4(1.f), glm::vec3(10.f, 1.f, 10.f));
......
......@@ -83,6 +83,7 @@ int main(int argc, const char** argv) {
firstMeshProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("resources/shaders/frag.spv"));
auto& attributes = mesh.vertexGroups[0].vertexBuffer.attributes;
std::sort(attributes.begin(), attributes.end(), [](const vkcv::asset::VertexAttribute& x, const vkcv::asset::VertexAttribute& y) {
return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type);
......@@ -149,7 +150,7 @@ int main(int argc, const char** argv) {
const vkcv::Mesh renderMesh(vertexBufferBindings, indexBuffer.getVulkanHandle(), mesh.vertexGroups[0].numIndices);
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle);
vkcv::DrawcallInfo drawcall(renderMesh, { descriptorUsage });
vkcv::DrawcallInfo drawcall(renderMesh, { descriptorUsage },1);
vkcv::camera::CameraManager cameraManager(window);
uint32_t camIndex0 = cameraManager.addCamera(vkcv::camera::ControllerType::PILOT);
......
......@@ -199,7 +199,7 @@ int main(int argc, const char** argv) {
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSets[i]).vulkanHandle);
drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage}));
drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage},1));
}
std::vector<glm::mat4> modelMatrices;
......
......@@ -167,7 +167,7 @@ int main(int argc, const char** argv) {
vkcv::ImageHandle swapchainImageHandle = vkcv::ImageHandle::createSwapchainImageHandle();
const vkcv::Mesh renderMesh({}, triangleIndexBuffer.getVulkanHandle(), 3);
vkcv::DrawcallInfo drawcall(renderMesh, {});
vkcv::DrawcallInfo drawcall(renderMesh, {},1);
const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle();
......
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
src/ParticleSystem.hpp
src/ParticleSystem.cpp
src/Particle.hpp
src/Particle.cpp
src/BloomAndFlares.hpp
src/BloomAndFlares.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} ${vkcv_shader_compiler_include})
# linking with libraries from all dependencies and the VkCV framework
target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera vkcv_shader_compiler)
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(set=0, binding=0) uniform texture2D blurImage;
layout(set=0, binding=1) uniform texture2D lensImage;
layout(set=0, binding=2) uniform sampler linearSampler;
layout(set=0, binding=3, r11f_g11f_b10f) uniform image2D colorBuffer;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main()
{
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(colorBuffer)))){
return;
}
ivec2 pixel_coord = ivec2(gl_GlobalInvocationID.xy);
vec2 pixel_size = vec2(1.0f) / textureSize(sampler2D(blurImage, linearSampler), 0);
vec2 UV = pixel_coord.xy * pixel_size;
vec4 composite_color = vec4(0.0f);
vec3 blur_color = texture(sampler2D(blurImage, linearSampler), UV).rgb;
vec3 lens_color = texture(sampler2D(lensImage, linearSampler), UV).rgb;
vec3 main_color = imageLoad(colorBuffer, pixel_coord).rgb;
// composite blur and lens features
float bloom_weight = 0.01f;
float lens_weight = 0.f;
float main_weight = 1 - (bloom_weight + lens_weight);
composite_color.rgb = blur_color * bloom_weight +
lens_color * lens_weight +
main_color * main_weight;
imageStore(colorBuffer, pixel_coord, composite_color);
}
\ No newline at end of file
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(set=0, binding=0) uniform texture2D inBlurImage;
layout(set=0, binding=1) uniform sampler inImageSampler;
layout(set=0, binding=2, r11f_g11f_b10f) uniform writeonly image2D outBlurImage;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main()
{
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(outBlurImage)))){
return;
}
ivec2 pixel_coord = ivec2(gl_GlobalInvocationID.xy);
vec2 pixel_size = vec2(1.0f) / imageSize(outBlurImage);
vec2 UV = pixel_coord.xy * pixel_size;
vec2 UV_offset = UV + 0.5f * pixel_size;
vec2 color_fetches[13] = {
// center neighbourhood (RED)
vec2(-1, 1), // LT
vec2(-1, -1), // LB
vec2( 1, -1), // RB
vec2( 1, 1), // RT
vec2(-2, 2), // LT
vec2( 0, 2), // CT
vec2( 2, 2), // RT
vec2(0 ,-2), // LC
vec2(0 , 0), // CC
vec2(2, 0), // CR
vec2(-2, -2), // LB
vec2(0 , -2), // CB
vec2(2 , -2) // RB
};
float color_weights[13] = {
// 0.5f
1.f/8.f,
1.f/8.f,
1.f/8.f,
1.f/8.f,
// 0.125f
1.f/32.f,
1.f/16.f,
1.f/32.f,
// 0.25f
1.f/16.f,
1.f/8.f,
1.f/16.f,
// 0.125f
1.f/32.f,
1.f/16.f,
1.f/32.f
};
vec3 sampled_color = vec3(0.0f);
for(uint i = 0; i < 13; i++)
{
vec2 color_fetch = UV_offset + color_fetches[i] * pixel_size;
vec3 color = texture(sampler2D(inBlurImage, inImageSampler), color_fetch).rgb;
color *= color_weights[i];
sampled_color += color;
}
imageStore(outBlurImage, pixel_coord, vec4(sampled_color, 1.f));
}
\ No newline at end of file
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(set=0, binding=0) uniform texture2D blurBuffer;
layout(set=0, binding=1) uniform sampler linearSampler;
layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D lensBuffer;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
vec3 sampleColorChromaticAberration(vec2 _uv)
{
vec2 toCenter = (vec2(0.5) - _uv);
vec3 colorScales = vec3(-1, 0, 1);
float aberrationScale = 0.1;
vec3 scaleFactors = colorScales * aberrationScale;
float r = texture(sampler2D(blurBuffer, linearSampler), _uv + toCenter * scaleFactors.r).r;
float g = texture(sampler2D(blurBuffer, linearSampler), _uv + toCenter * scaleFactors.g).g;
float b = texture(sampler2D(blurBuffer, linearSampler), _uv + toCenter * scaleFactors.b).b;
return vec3(r, g, b);
}
// _uv assumed to be flipped UV coordinates!
vec3 ghost_vectors(vec2 _uv)
{
vec2 ghost_vec = (vec2(0.5f) - _uv);
const uint c_ghost_count = 64;
const float c_ghost_spacing = length(ghost_vec) / c_ghost_count;
ghost_vec *= c_ghost_spacing;
vec3 ret_color = vec3(0.0f);
for (uint i = 0; i < c_ghost_count; ++i)
{
// sample scene color
vec2 s_uv = fract(_uv + ghost_vec * vec2(i));
vec3 s = sampleColorChromaticAberration(s_uv);
// tint/weight
float d = distance(s_uv, vec2(0.5));
float weight = 1.0f - smoothstep(0.0f, 0.75f, d);
s *= weight;
ret_color += s;
}
ret_color /= c_ghost_count;
return ret_color;
}
vec3 halo(vec2 _uv)
{
const float c_aspect_ratio = float(imageSize(lensBuffer).x) / float(imageSize(lensBuffer).y);
const float c_radius = 0.6f;
const float c_halo_thickness = 0.1f;
vec2 halo_vec = vec2(0.5) - _uv;
//halo_vec.x /= c_aspect_ratio;
halo_vec = normalize(halo_vec);
//halo_vec.x *= c_aspect_ratio;
//vec2 w_uv = (_uv - vec2(0.5, 0.0)) * vec2(c_aspect_ratio, 1.0) + vec2(0.5, 0.0);
vec2 w_uv = _uv;
float d = distance(w_uv, vec2(0.5)); // distance to center
float distance_to_halo = abs(d - c_radius);
float halo_weight = 0.0f;
if(abs(d - c_radius) <= c_halo_thickness)
{
float distance_to_border = c_halo_thickness - distance_to_halo;
halo_weight = distance_to_border / c_halo_thickness;
//halo_weight = clamp((halo_weight / 0.4f), 0.0f, 1.0f);
halo_weight = pow(halo_weight, 2.0f);
//halo_weight = 1.0f;
}
return sampleColorChromaticAberration(_uv + halo_vec) * halo_weight;
}
void main()
{
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(lensBuffer)))){
return;
}
ivec2 pixel_coord = ivec2(gl_GlobalInvocationID.xy);
vec2 pixel_size = vec2(1.0f) / imageSize(lensBuffer);
vec2 UV = pixel_coord.xy * pixel_size;
vec2 flipped_UV = vec2(1.0f) - UV;
vec3 color = vec3(0.0f);
color += ghost_vectors(flipped_UV);
color += halo(UV);
color *= 0.5f;
imageStore(lensBuffer, pixel_coord, vec4(color, 0.0f));
}
\ No newline at end of file
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(set=0, binding=0) uniform texture2D inUpsampleImage;
layout(set=0, binding=1) uniform sampler inImageSampler;
layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D outUpsampleImage;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main()
{
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(outUpsampleImage)))){
return;
}
ivec2 pixel_coord = ivec2(gl_GlobalInvocationID.xy);
vec2 pixel_size = vec2(1.0f) / imageSize(outUpsampleImage);
vec2 UV = pixel_coord.xy * pixel_size;
const float gauss_kernel[3] = {1.f, 2.f, 1.f};
const float gauss_weight = 16.f;
vec3 sampled_color = vec3(0.f);
for(int i = -1; i <= 1; i++)
{
for(int j = -1; j <= 1; j++)
{
vec2 sample_location = UV + vec2(j, i) * pixel_size;
vec3 color = texture(sampler2D(inUpsampleImage, inImageSampler), sample_location).rgb;
color *= gauss_kernel[j+1];
color *= gauss_kernel[i+1];
color /= gauss_weight;
sampled_color += color;
}
}
//vec3 prev_color = imageLoad(outUpsampleImage, pixel_coord).rgb;
//float bloomRimStrength = 0.75f; // adjust this to change strength of bloom
//sampled_color = mix(prev_color, sampled_color, bloomRimStrength);
imageStore(outUpsampleImage, pixel_coord, vec4(sampled_color, 1.f));
}
\ No newline at end of file
float circleFactor(vec2 triangleCoordinates){
// percentage of distance from center to circle edge
float p = clamp((0.4 - length(triangleCoordinates)) / 0.4, 0, 1);
// remapping for nice falloff
return sqrt(p);
}
\ No newline at end of file
#version 460 core
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 particle;
struct Particle
{
vec3 position;
float lifeTime;
vec3 velocity;
float padding_2;
vec3 reset_velocity;
float padding_3;
};
layout(std430, binding = 2) coherent buffer buffer_inParticle
{
Particle inParticle[];
};
layout( push_constant ) uniform constants{
mat4 view;
mat4 projection;
};
layout(location = 0) out vec2 passTriangleCoordinates;
layout(location = 1) out vec3 passVelocity;
layout(location = 2) out float passlifeTime;
void main()
{
int id = gl_InstanceIndex;
passVelocity = inParticle[id].velocity;
passlifeTime = inParticle[id].lifeTime;
// particle position in view space
vec4 positionView = view * vec4(inParticle[id].position, 1);
// by adding the triangle position in view space the mesh is always camera facing
positionView.xyz += particle;
// multiply with projection matrix for final position
gl_Position = projection * positionView;
// 0.01 corresponds to vertex position size in main
float normalizationDivider = 0.012;
passTriangleCoordinates = particle.xy / normalizationDivider;
}
\ No newline at end of file
#version 450 core
#extension GL_ARB_separate_shader_objects : enable
layout(local_size_x = 256) in;
struct Particle
{
vec3 position;
float lifeTime;
vec3 velocity;
float mass;
vec3 reset_velocity;
float _padding;
};
layout(std430, binding = 0) coherent buffer buffer_inParticle
{
Particle inParticle[];
};
layout( push_constant ) uniform constants{
float deltaTime;
float rand;
};
const int n = 4;
vec4 gravityPoint[n] = vec4[n](
vec4(-0.8, -0.5, 0.0, 3),
vec4(-0.4, 0.5, 0.8, 2),
vec4( 0.8, 0.8, -0.3, 4),
vec4( 0.5, -0.7, -0.5, 1)
);
const float G = 6.6743015e-11;
const float sim_d_factor = 10e11;
const float sim_g_factor = 10e30;
const float sim_t_factor = 5;
const float c = 299792458;
void main() {
uint id = gl_GlobalInvocationID.x;
inParticle[id].lifeTime -= deltaTime;
vec3 pos = inParticle[id].position;
vec3 vel = inParticle[id].velocity;
float mass = inParticle[id].mass;
if(inParticle[id].lifeTime < 0.f)
{
inParticle[id].lifeTime = 5.f * rand;
inParticle[id].mass *= rand;
pos = vec3(0);
vel *= rand;
}
for(int i = 0; i < n; i++)
{
vec3 d = (gravityPoint[i].xyz - pos) * sim_d_factor;
float r = length(d);
float g = G * (gravityPoint[i].w * sim_g_factor) / (r * r);
if (r > 0) {
vec3 dvel = (deltaTime * sim_t_factor) * g * (d / r);
vel = (vel + dvel) / (1.0 + dot(vel, dvel) / (c*c));
}
}
pos += vel * (deltaTime * sim_t_factor);
vec3 a_pos = abs(pos);
if ((a_pos.x > 2.0) || (a_pos.y > 2.0) || (a_pos.z > 2.0))
{
inParticle[id].lifeTime *= 0.9;
}
inParticle[id].position = pos;
inParticle[id].velocity = vel;
}
#version 450 core
#extension GL_ARB_separate_shader_objects : enable
layout(local_size_x = 256) in;
struct Particle
{