diff --git a/include/vkcv/DrawcallRecording.hpp b/include/vkcv/DrawcallRecording.hpp index 0929ad038fb95ec1573e7c76e5ce13adb84ab760..7f653d21a70f90cf92bcc50de9a2eec31c26b3e2 100644 --- a/include/vkcv/DrawcallRecording.hpp +++ b/include/vkcv/DrawcallRecording.hpp @@ -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) + : mesh(mesh), descriptorSets(descriptorSets), instanceCount(instanceCount){} Mesh mesh; std::vector<DescriptorSetUsage> descriptorSets; + uint32_t instanceCount; }; void recordDrawcall( diff --git a/projects/particle_simulation/shaders/comp1.spv b/projects/particle_simulation/shaders/comp1.spv index aa72785c8955dbb67fd1d1f5d37274e8789ddb40..c82562d6735289dd7f7ba2dbeb2243817034d47d 100644 Binary files a/projects/particle_simulation/shaders/comp1.spv and b/projects/particle_simulation/shaders/comp1.spv differ diff --git a/projects/particle_simulation/shaders/frag.spv b/projects/particle_simulation/shaders/frag.spv index 1b3a6f369887d3cad66e2f93316261c9c94202ac..a37f971964481b32a05bafc7efcf0c7b4b25671e 100644 Binary files a/projects/particle_simulation/shaders/frag.spv and b/projects/particle_simulation/shaders/frag.spv differ diff --git a/projects/particle_simulation/shaders/shader.frag b/projects/particle_simulation/shaders/shader.frag index f7a6c9bc5e9bddfef9dd48ebf2555ea18f488112..1c228be035b77c2eeaec55c328b333576fa41810 100644 --- a/projects/particle_simulation/shaders/shader.frag +++ b/projects/particle_simulation/shaders/shader.frag @@ -1,6 +1,8 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable +layout(location = 1) in vec3 passVelocity; + layout(location = 0) out vec4 outColor; layout(set=0, binding=0) uniform uColor { @@ -14,7 +16,7 @@ layout(set=0,binding=1) uniform uPosition{ void main() { vec2 mouse = vec2(Position.position.x, Position.position.y); - outColor = vec4(0,1,0,0); + outColor = vec4(1,0,0,0); //outColor = float(distance(gl_FragCoord.xy, mouse) < 100) * vec4(0,0,1,0) + // float(distance(gl_FragCoord.xy, mouse) >= 100) * Color.color; } \ No newline at end of file diff --git a/projects/particle_simulation/shaders/shader.vert b/projects/particle_simulation/shaders/shader.vert index 9e3a8249b113adad1563627aa8c16ffba5c2d0d0..d68f087f90f6dd56589b71060a102bf42c2b9c2a 100644 --- a/projects/particle_simulation/shaders/shader.vert +++ b/projects/particle_simulation/shaders/shader.vert @@ -1,4 +1,4 @@ -#version 450 core +#version 460 core #extension GL_ARB_separate_shader_objects : enable layout(location = 0) in vec3 particle; @@ -20,7 +20,12 @@ layout( push_constant ) uniform constants{ mat4 mvp; }; +layout(location = 1) out vec3 passVelocity; + void main() { - gl_Position = mvp * vec4(particle, 1.0); + int id = gl_InstanceIndex; + passVelocity = inParticle[id].velocity; + vec3 moved_particle = particle + inParticle[id].position; + gl_Position = mvp * vec4(moved_particle, 1.0); } \ No newline at end of file diff --git a/projects/particle_simulation/shaders/shader1.comp b/projects/particle_simulation/shaders/shader1.comp index ee84397b2d1efcd6cf4bcd6426234519fdc56d13..eea7fe7beb1c6796ce8b0accc2f634ea5174f423 100644 --- a/projects/particle_simulation/shaders/shader1.comp +++ b/projects/particle_simulation/shaders/shader1.comp @@ -16,7 +16,19 @@ layout(std430, binding = 0) coherent buffer buffer_inParticle Particle inParticle[]; }; +layout( push_constant ) uniform constants{ + vec3 respawnPos; + float deltaTime; +}; + void main() { uint id = gl_GlobalInvocationID.x; - inParticle[id].position = inParticle[id].position + vec3(0.f,0.01f,0.f); + inParticle[id].lifeTime -= deltaTime; + if(inParticle[id].lifeTime < 0.f) + { + inParticle[id].lifeTime = 3.f; + inParticle[id].position = respawnPos; + } + + inParticle[id].position = (inParticle[id].position + deltaTime * inParticle[id].velocity); } diff --git a/projects/particle_simulation/shaders/vert.spv b/projects/particle_simulation/shaders/vert.spv index 841133cd5ef81d8fed6fdf94b598f82627eed2f5..bdfc6c77e59f2b2a73ea0c4ec9a44086a438d15e 100644 Binary files a/projects/particle_simulation/shaders/vert.spv and b/projects/particle_simulation/shaders/vert.spv differ diff --git a/projects/particle_simulation/src/ParticleSystem.cpp b/projects/particle_simulation/src/ParticleSystem.cpp index daf9a16dbb5e98b07f55a8c967046c2328324ee6..b3162d6bea685640d3949577271affc8b2080407 100644 --- a/projects/particle_simulation/src/ParticleSystem.cpp +++ b/projects/particle_simulation/src/ParticleSystem.cpp @@ -54,3 +54,7 @@ void ParticleSystem::setRdmVelocity( glm::vec3 minVelocity, glm::vec3 maxVelocit m_rdmVel[1] = std::uniform_real_distribution<float> (minVelocity.y,maxVelocity.y); m_rdmVel[2] = std::uniform_real_distribution<float> (minVelocity.z,maxVelocity.z); } + +const glm::vec3 ParticleSystem::getRespawnPos() const{ + return m_respawnPos; +} diff --git a/projects/particle_simulation/src/ParticleSystem.hpp b/projects/particle_simulation/src/ParticleSystem.hpp index 2afb825392dcb4fdc36b6e52096589f19ef0ecb6..fe5c99f9b407b9dbdfd414e265e7cd91bbe790b9 100644 --- a/projects/particle_simulation/src/ParticleSystem.hpp +++ b/projects/particle_simulation/src/ParticleSystem.hpp @@ -14,6 +14,8 @@ public: void setRespawnPos( const glm::vec3 respawnPos ); void setRdmLifeTime( const glm::vec2 lifeTime ); void setRdmVelocity( glm::vec3 minVelocity, glm::vec3 maxVelocity ); + const glm::vec3 getRespawnPos() const; + private: void addParticle( const Particle particle ); diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index a9c3e25907a69e486b331625578be449ca74df36..5305be8431aee2a274d0aa6f5bec305609a3f09b 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -120,9 +120,9 @@ int main(int argc, const char **argv) { 1 ); - glm::vec3 minVelocity = glm::vec3(-0.0f,-0.0f,0.f); - glm::vec3 maxVelocity = glm::vec3(0.0f,0.0f,0.f); - glm::vec2 lifeTime = glm::vec2(0.f,5.f); + glm::vec3 minVelocity = glm::vec3(-0.1f,-0.1f,0.f); + glm::vec3 maxVelocity = glm::vec3(0.1f,0.1f,0.f); + glm::vec2 lifeTime = glm::vec2(0.f,2.f); ParticleSystem particleSystem = ParticleSystem( 100 , minVelocity, maxVelocity, lifeTime); vkcv::Buffer<Particle> particleBuffer = core.createBuffer<Particle>( @@ -164,6 +164,7 @@ int main(int argc, const char **argv) { window.e_mouseMove.add([&](double offsetX, double offsetY) { pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); +// std::cout << offsetX << " , " << offsetY << std::endl; // borders are assumed to be 0.5 //pos = glm::vec2((pos.x -0.5f * static_cast<float>(window.getWidth()))/static_cast<float>(window.getWidth()), (pos.y -0.5f * static_cast<float>(window.getHeight()))/static_cast<float>(window.getHeight())); //borders are assumed to be 1 @@ -173,11 +174,11 @@ int main(int argc, const char **argv) { glm::vec4 row2 = glm::row(cameraManager.getCamera(0).getView(), 1); glm::vec4 row3 = glm::row(cameraManager.getCamera(0).getView(), 2); glm::vec4 camera_pos = glm::column(cameraManager.getCamera(0).getView(), 3); - std::cout << "row1: " << row1.x << ", " << row1.y << ", " << row1.z << std::endl; - std::cout << "row2: " << row2.x << ", " << row2.y << ", " << row2.z << std::endl; - std::cout << "row3: " << row3.x << ", " << row3.y << ", " << row3.z << std::endl; - std::cout << "camerapos: " << camera_pos.x << ", " << camera_pos.y << ", " << camera_pos.z << std::endl; - std::cout << "camerapos: " << camera_pos.x << ", " << camera_pos.y << ", " << camera_pos.z << std::endl; +// std::cout << "row1: " << row1.x << ", " << row1.y << ", " << row1.z << std::endl; +// std::cout << "row2: " << row2.x << ", " << row2.y << ", " << row2.z << std::endl; +// std::cout << "row3: " << row3.x << ", " << row3.y << ", " << row3.z << std::endl; +// std::cout << "camerapos: " << camera_pos.x << ", " << camera_pos.y << ", " << camera_pos.z << std::endl; +// std::cout << "camerapos: " << camera_pos.x << ", " << camera_pos.y << ", " << camera_pos.z << std::endl; //glm::vec4 view_axis = glm::row(cameraManager.getCamera().getView(), 2); // std::cout << "view_axis: " << view_axis.x << ", " << view_axis.y << ", " << view_axis.z << std::endl; //std::cout << "Front: " << cameraManager.getCamera().getFront().x << ", " << cameraManager.getCamera().getFront().z << ", " << cameraManager.getCamera().getFront().z << std::endl; @@ -186,15 +187,12 @@ int main(int argc, const char **argv) { tempPosition = viewmat * glm::vec4(spawnPosition, 1.0f); spawnPosition = glm::vec3(tempPosition.x, tempPosition.y, tempPosition.z); particleSystem.setRespawnPos(glm::vec3(-spawnPosition.x, -spawnPosition.y, spawnPosition.z)); - std::cout << "respawn pos: " << spawnPosition.x << ", " << spawnPosition.y << ", " << spawnPosition.z << std::endl; +// std::cout << "respawn pos: " << spawnPosition.x << ", " << spawnPosition.y << ", " << spawnPosition.z << std::endl; }); std::vector<glm::mat4> modelMatrices; std::vector<vkcv::DrawcallInfo> drawcalls; - for (auto particle : particleSystem.getParticles()) { - modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition())); - drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage})); - } + drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage}, particleSystem.getParticles().size())); auto start = std::chrono::system_clock::now(); @@ -215,41 +213,36 @@ int main(int argc, const char **argv) { } color.fill(&colorData); - - position.fill(&pos); + auto end = std::chrono::system_clock::now(); - auto deltatime = std::chrono::duration_cast<std::chrono::microseconds>(end - start); + float deltatime = 0.000001 * static_cast<float>( std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() ); start = end; - particleSystem.updateParticles(0.000001 * static_cast<float>(deltatime.count())); + particleSystem.updateParticles(deltatime); - modelMatrices.clear(); - for (Particle particle : particleSystem.getParticles()) { - modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition())); - } - - cameraManager.update(0.000001 * static_cast<double>(deltatime.count())); + cameraManager.update(deltatime); std::vector<glm::mat4> mvp; mvp.clear(); - for (const auto &m : modelMatrices) { - mvp.push_back(m * cameraManager.getActiveCamera().getMVP()); - } + mvp.push_back( cameraManager.getCamera(0).getMVP()); - vkcv::PushConstantData pushConstantData((void *) mvp.data(), sizeof(glm::mat4)); auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); + glm::vec4 pushData = glm::vec4(particleSystem.getRespawnPos(),deltatime); + + vkcv::PushConstantData pushConstantDataCompute( &pushData, sizeof(glm::vec4)); uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil(particleSystem.getParticles().size()/64.f)),1,1}; core.recordComputeDispatchToCmdStream(cmdStream, computePipeline, computeDispatchCount, {vkcv::DescriptorSetUsage(0,core.getDescriptorSet(computeDescriptorSet).vulkanHandle)}, - vkcv::PushConstantData(nullptr, 0)); + pushConstantDataCompute); + vkcv::PushConstantData pushConstantDataDraw((void *) mvp.data(), sizeof(glm::mat4)); core.recordDrawcallsToCmdStream( cmdStream, particlePass, particlePipeline, - pushConstantData, + pushConstantDataDraw, {drawcalls}, {swapchainInput}); core.prepareSwapchainImageForPresent(cmdStream); diff --git a/src/vkcv/DrawcallRecording.cpp b/src/vkcv/DrawcallRecording.cpp index df7b7bbcb3fe278622cd160593eb750db00ec7b1..5e0ef6ba100e68dea18d25ed1ea05358cc9f59fa 100644 --- a/src/vkcv/DrawcallRecording.cpp +++ b/src/vkcv/DrawcallRecording.cpp @@ -36,10 +36,10 @@ namespace vkcv { if (drawcall.mesh.indexBuffer) { cmdBuffer.bindIndexBuffer(drawcall.mesh.indexBuffer, 0, vk::IndexType::eUint16); //FIXME: choose proper size - cmdBuffer.drawIndexed(drawcall.mesh.indexCount, 1, 0, 0, {}); + cmdBuffer.drawIndexed(drawcall.mesh.indexCount, drawcall.instanceCount, 0, 0, {}); } else { cmdBuffer.draw(drawcall.mesh.indexCount, 1, 0, 0, {}); } } -} \ No newline at end of file +}