Skip to content
Snippets Groups Projects
Commit dca0b5cc authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#69] added instance rendering and added GPU computation

parent ccbe042e
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #25916 failed
......@@ -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(
......
No preview for this file type
No preview for this file type
#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
#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
......@@ -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);
}
No preview for this file type
......@@ -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;
}
......@@ -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 );
......
......@@ -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);
......
......@@ -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
}
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