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

Updating velocities with data from grid

parent 3428a701
No related branches found
No related tags found
1 merge request!103Added project wobble_bobble and refactored some parts of the framework
#version 450 #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 = 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 gridImage;
layout(set=0, binding=2) uniform sampler gridSampler;
void main() { void main() {
if (gl_GlobalInvocationID.x < particles.length()) {
vec3 position = particles[gl_GlobalInvocationID.x].minimal.position;
float mass = particles[gl_GlobalInvocationID.x].minimal.mass;
vec3 offset = position;
vec4 gridSample = texture(sampler3D(gridImage, gridSampler), offset);
vec3 gridVelocity = gridSample.xyz;
float gridMass = gridSample.w;
particles[gl_GlobalInvocationID.x].minimal.velocity = gridVelocity * mass / gridMass;
}
} }
\ No newline at end of file
...@@ -35,7 +35,7 @@ void distributeParticles(Particle *particles, size_t count, const glm::vec3& cen ...@@ -35,7 +35,7 @@ void distributeParticles(Particle *particles, size_t count, const glm::vec3& cen
particles[i].position = center + offset; particles[i].position = center + offset;
particles[i].size = size; particles[i].size = size;
particles[i].velocity = glm::vec3(0.1f); particles[i].velocity = glm::vec3(0.0f);
volume += size; volume += size;
} }
...@@ -154,7 +154,12 @@ int main(int argc, const char **argv) { ...@@ -154,7 +154,12 @@ int main(int argc, const char **argv) {
grid.fill(grid_vec.data()); // FIXME: gets limited by staging buffer size... grid.fill(grid_vec.data()); // FIXME: gets limited by staging buffer size...
*/ */
grid.switchLayout(vk::ImageLayout::eGeneral); vkcv::SamplerHandle gridSampler = core.createSampler(
vkcv::SamplerFilterType::LINEAR,
vkcv::SamplerFilterType::LINEAR,
vkcv::SamplerMipmapMode::NEAREST,
vkcv::SamplerAddressMode::REPEAT
);
vkcv::shader::GLSLCompiler compiler; vkcv::shader::GLSLCompiler compiler;
...@@ -207,6 +212,14 @@ int main(int argc, const char **argv) { ...@@ -207,6 +212,14 @@ int main(int argc, const char **argv) {
updateParticleVelocitiesSets updateParticleVelocitiesSets
); );
{
vkcv::DescriptorWrites writes;
writes.storageBufferWrites.push_back(vkcv::BufferDescriptorWrite(0, particles.getHandle()));
writes.sampledImageWrites.push_back(vkcv::SampledImageDescriptorWrite(1, grid.getHandle()));
writes.samplerWrites.push_back(vkcv::SamplerDescriptorWrite(2, gridSampler));
core.writeDescriptorSet(updateParticleVelocitiesSets[0], writes);
}
std::vector<vkcv::DescriptorSetHandle> updateParticlePositionsSets; std::vector<vkcv::DescriptorSetHandle> updateParticlePositionsSets;
vkcv::ComputePipelineHandle updateParticlePositionsPipeline = createComputePipeline( vkcv::ComputePipelineHandle updateParticlePositionsPipeline = createComputePipeline(
core, compiler, core, compiler,
...@@ -357,6 +370,8 @@ int main(int argc, const char **argv) { ...@@ -357,6 +370,8 @@ int main(int argc, const char **argv) {
const uint32_t dispatchSize [3] = { 1, 1, 1 }; const uint32_t dispatchSize [3] = { 1, 1, 1 };
core.recordBeginDebugLabel(cmdStream, "TRANSFORM PARTICLES TO GRID", { 0.47f, 0.77f, 0.85f, 1.0f }); core.recordBeginDebugLabel(cmdStream, "TRANSFORM PARTICLES TO GRID", { 0.47f, 0.77f, 0.85f, 1.0f });
core.prepareImageForStorage(cmdStream, grid.getHandle());
core.recordComputeDispatchToCmdStream( core.recordComputeDispatchToCmdStream(
cmdStream, cmdStream,
transformParticlesToGridPipeline, transformParticlesToGridPipeline,
...@@ -414,13 +429,19 @@ int main(int argc, const char **argv) { ...@@ -414,13 +429,19 @@ int main(int argc, const char **argv) {
core.recordEndDebugLabel(cmdStream); core.recordEndDebugLabel(cmdStream);
core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE VELOCITIES", { 0.78f, 0.89f, 0.94f, 1.0f }); core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE VELOCITIES", { 0.78f, 0.89f, 0.94f, 1.0f });
core.prepareImageForSampling(cmdStream, grid.getHandle());
core.recordComputeDispatchToCmdStream( core.recordComputeDispatchToCmdStream(
cmdStream, cmdStream,
updateParticleVelocitiesPipeline, updateParticleVelocitiesPipeline,
dispatchSizeUpdateParticles, dispatchSizeUpdateParticles,
{}, { vkcv::DescriptorSetUsage(
0, core.getDescriptorSet(updateParticleVelocitiesSets[0]).vulkanHandle
) },
vkcv::PushConstants(0) vkcv::PushConstants(0)
); );
core.recordBufferMemoryBarrier(cmdStream, particles.getHandle());
core.recordEndDebugLabel(cmdStream); core.recordEndDebugLabel(cmdStream);
core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE POSITIONS", { 0.78f, 0.89f, 0.94f, 1.0f }); core.recordBeginDebugLabel(cmdStream, "UPDATE PARTICLE POSITIONS", { 0.78f, 0.89f, 0.94f, 1.0f });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment