Skip to content
Snippets Groups Projects
Verified Commit 1712d145 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Added shader to update deformation and forces

parent 0386c845
No related branches found
No related tags found
1 merge request!103Added project wobble_bobble and refactored some parts of the framework
#version 450
#extension GL_GOOGLE_include_directive : enable
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
#include "particle.inc"
layout(set=0, binding=0, rgba32f) uniform image3D gridImage;
layout( push_constant ) uniform constants {
float t;
float dt;
};
void main() {
vec4 gridSample = imageLoad(
gridImage,
ivec3(gl_GlobalInvocationID)
);
vec3 velocity = gridSample.xyz;
float mass = gridSample.w;
velocity += vec3(0.0f, 0.0f, 0.0f) * dt;
bvec3 lowerID = lessThanEqual(gl_GlobalInvocationID, ivec3(0));
bvec3 negativeVelocity = lessThan(velocity, vec3(0.0f));
bvec3 greaterID = greaterThanEqual(gl_GlobalInvocationID + ivec3(1), imageSize(gridImage));
bvec3 positiveVelocity = greaterThan(velocity, vec3(0.0f));
bvec3 invert = bvec3(
(lowerID.x && negativeVelocity.x) || (greaterID.x && positiveVelocity.x),
(lowerID.y && negativeVelocity.y) || (greaterID.y && positiveVelocity.y),
(lowerID.z && negativeVelocity.z) || (greaterID.z && positiveVelocity.z)
);
velocity = mix(velocity, -velocity, invert);
imageStore(
gridImage,
ivec3(gl_GlobalInvocationID),
vec4(velocity, mass)
);
}
\ No newline at end of file
#version 450
#extension GL_GOOGLE_include_directive : enable
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
#include "particle.inc"
layout(set=0, binding=0, std430) buffer particleBuffer {
Particle particles [];
};
layout(set=0, binding=1) uniform texture3D gridTextureIn;
layout(set=0, binding=2) uniform sampler gridSampler;
layout( push_constant ) uniform constants {
float t;
float dt;
};
void main() {
ParticleMinimal particle = particles[gl_GlobalInvocationID.x].minimal;
mat3 deformation = mat3(particles[gl_GlobalInvocationID.x].deformation);
mat3 grad_velocity = mat3(
textureGrad(sampler3D(gridTextureIn, gridSampler), particle.position, vec3(1.0f, 0.0f, 0.0f), vec3(0.0f)).xyz,
textureGrad(sampler3D(gridTextureIn, gridSampler), particle.position, vec3(0.0f, 1.0f, 0.0f), vec3(0.0f)).xyz,
textureGrad(sampler3D(gridTextureIn, gridSampler), particle.position, vec3(0.0f, 0.0f, 1.0f), vec3(0.0f)).xyz
);
mat3 deformation_elastic = (mat3(1.0f) + grad_velocity * dt) * deformation;
particles[gl_GlobalInvocationID.x].deformation = mat4(deformation_elastic);
}
\ No newline at end of file
......@@ -47,6 +47,7 @@ void distributeParticles(Particle *particles, size_t count, const glm::vec3& cen
for (size_t i = 0; i < count; i++) {
particles[i].mass = (mass * particles[i].size / volume);
particles[i].deformation = glm::mat4(1.0f);
}
}
......@@ -210,6 +211,12 @@ int main(int argc, const char **argv) {
updateGridForcesSets
);
{
vkcv::DescriptorWrites writes;
writes.storageImageWrites.push_back(vkcv::StorageImageDescriptorWrite(0, grid.getHandle()));
core.writeDescriptorSet(updateGridForcesSets[0], writes);
}
std::vector<vkcv::DescriptorSetHandle> updateGridVelocitiesSets;
vkcv::ComputePipelineHandle updateGridVelocitiesPipeline = createComputePipeline(
core, compiler,
......@@ -232,6 +239,14 @@ int main(int argc, const char **argv) {
updateParticleDeformationSets
);
{
vkcv::DescriptorWrites writes;
writes.storageBufferWrites.push_back(vkcv::BufferDescriptorWrite(0, particles.getHandle()));
writes.sampledImageWrites.push_back(vkcv::SampledImageDescriptorWrite(1, tmpGrid.getHandle()));
writes.samplerWrites.push_back(vkcv::SamplerDescriptorWrite(2, gridSampler));
core.writeDescriptorSet(updateParticleDeformationSets[0], writes);
}
std::vector<vkcv::DescriptorSetHandle> updateParticleVelocitiesSets;
vkcv::ComputePipelineHandle updateParticleVelocitiesPipeline = createComputePipeline(
core, compiler,
......@@ -501,8 +516,10 @@ int main(int argc, const char **argv) {
cmdStream,
updateGridForcesPipeline,
dispatchSizeGrid,
{},
vkcv::PushConstants(0)
{ vkcv::DescriptorSetUsage(
0, core.getDescriptorSet(updateGridForcesSets[0]).vulkanHandle
) },
timePushConstants
);
core.recordEndDebugLabel(cmdStream);
......@@ -524,18 +541,22 @@ int main(int argc, const char **argv) {
core.recordEndDebugLabel(cmdStream);
core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE DEFORMATION", { 0.78f, 0.89f, 0.94f, 1.0f });
core.prepareImageForSampling(cmdStream, tmpGrid.getHandle());
core.recordComputeDispatchToCmdStream(
cmdStream,
updateParticleDeformationPipeline,
dispatchSizeParticles,
{},
vkcv::PushConstants(0)
{ vkcv::DescriptorSetUsage(
0, core.getDescriptorSet(updateParticleDeformationSets[0]).vulkanHandle
) },
timePushConstants
);
core.recordBufferMemoryBarrier(cmdStream, particles.getHandle());
core.recordEndDebugLabel(cmdStream);
core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE VELOCITIES", { 0.78f, 0.89f, 0.94f, 1.0f });
core.prepareImageForSampling(cmdStream, tmpGrid.getHandle());
core.recordComputeDispatchToCmdStream(
cmdStream,
updateParticleVelocitiesPipeline,
......
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