diff --git a/projects/first_mesh/src/main.cpp b/projects/first_mesh/src/main.cpp index 599eae46ca0243d7eaacc06ec59c713edd5f1e72..95c81ba0f3a44e1fc965e20e808c95b6b8b3f3e1 100644 --- a/projects/first_mesh/src/main.cpp +++ b/projects/first_mesh/src/main.cpp @@ -89,6 +89,7 @@ int main(int argc, const char** argv) { triangleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); auto& attributes = mesh.vertexGroups[0].vertexBuffer.attributes; + std::sort(attributes.begin(), attributes.end(), [](const vkcv::VertexAttribute& x, const vkcv::VertexAttribute& y) { return static_cast<uint32_t>(x.type) < static_cast<uint32_t>(y.type); @@ -108,7 +109,8 @@ int main(int argc, const char** argv) { { core.getDescriptorSet(descriptorSet).layout }, true); vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); - + + if (!trianglePipeline) { std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; return EXIT_FAILURE; diff --git a/projects/particle_simulation/shaders/frag.spv b/projects/particle_simulation/shaders/frag.spv index 2bda54b389e0b6a615e819f05c1ac07bbd714f86..fdf98514955a63d0e54791970d2f1867ca3c5375 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 87ae41a7546b304b14d4be21e62e7ceb2a9759d1..540281d9a743017fb835e28ae211972ba2b4a025 100644 --- a/projects/particle_simulation/shaders/shader.frag +++ b/projects/particle_simulation/shaders/shader.frag @@ -13,5 +13,8 @@ layout(set=0,binding=1) uniform uPosition{ void main() { - outColor = Color.color; + vec2 mouse = vec2(Position.position.x, Position.position.y); + + 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 6c78c446355f87d007e24daa3bc11ca6fea068fa..97376d3df46525e49aa5fd0f0f1b6f84b15665c1 100644 --- a/projects/particle_simulation/shaders/shader.vert +++ b/projects/particle_simulation/shaders/shader.vert @@ -1,17 +1,13 @@ #version 450 core #extension GL_ARB_separate_shader_objects : enable +layout(location = 0) in vec3 position; + layout( push_constant ) uniform constants{ mat4 mvp; }; -vec3 positions[3] = { -vec3(-0.5, 0.5, -1), -vec3( 0.5, 0.5, -1), -vec3(0, -0.5, -1) -}; - void main() { - gl_Position = mvp * vec4(positions[gl_VertexIndex], 1.0); + gl_Position = mvp * vec4(position, 1.0); } \ No newline at end of file diff --git a/projects/particle_simulation/shaders/vert.spv b/projects/particle_simulation/shaders/vert.spv index 9e89006811ae50bbd74619623632a0d5e4423ddb..1d8d780bcb1a3dff229389902f9d624e83500380 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/main.cpp b/projects/particle_simulation/src/main.cpp index 1d5f4de5d4596fde46a972848d0e84c585446ee1..5c48c52fca1ffda541377ec7aec9ddb09044993c 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -4,25 +4,6 @@ #include <vkcv/camera/CameraManager.hpp> #include <chrono> -uint32_t findMemoryType( vk::PhysicalDeviceMemoryProperties const & memoryProperties, - uint32_t typeBits, - vk::MemoryPropertyFlags requirementsMask ) -{ - uint32_t typeIndex = uint32_t( ~0 ); - for ( uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++ ) - { - if ( ( typeBits & 1 ) && - ( ( memoryProperties.memoryTypes[i].propertyFlags & requirementsMask ) == requirementsMask ) ) - { - typeIndex = i; - break; - } - typeBits >>= 1; - } - assert( typeIndex != uint32_t( ~0 ) ); - return typeIndex; -} - int main(int argc, const char** argv) { const char* applicationName = "Particlesystem"; @@ -48,31 +29,10 @@ int main(int argc, const char** argv) { { "VK_KHR_swapchain" } ); - const auto& context = core.getContext(); - const vk::Instance& instance = context.getInstance(); - const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); - const vk::Device& device = context.getDevice(); - - struct vec3 { - float x, y, z; - }; - - const size_t n = 5027; - - auto testBuffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); - vec3 vec_data[n]; - - for (size_t i = 0; i < n; i++) { - vec_data[i] = { 42, static_cast<float>(i), 7 }; - } - - testBuffer.fill(vec_data); - - auto particleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); + auto particleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3, vkcv::BufferMemoryType::DEVICE_LOCAL); uint16_t indices[3] = { 0, 1, 2 }; particleIndexBuffer.fill(&indices[0], sizeof(indices)); - std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl; // an example attachment for passes that output to the window const vkcv::AttachmentDescription present_color_attachment( @@ -96,6 +56,27 @@ int main(int argc, const char** argv) { particleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); particleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); + vkcv::Buffer<glm::vec3> vertexbuffer = core.createBuffer<glm::vec3>( + vkcv::BufferType::VERTEX, + 3 + ); + + const std::vector<glm::vec3> vertices = {glm::vec3(-0.5, 0.5, -1), + glm::vec3( 0.5, 0.5, -1), + glm::vec3(0, -0.5, -1)}; + + vertexbuffer.fill(vertices); + + + vkcv::VertexAttribute attrib = vkcv::VertexAttribute{ + vkcv::PrimitiveType::POSITION, + 0, + sizeof(glm::vec3) * vertices.size(), + 0, + 5126, + 3}; + + std::vector<vkcv::DescriptorBinding> descriptorBindings = { vkcv::DescriptorBinding(vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT), vkcv::DescriptorBinding(vkcv::DescriptorType::UNIFORM_BUFFER, 1, vkcv::ShaderStage::FRAGMENT)}; @@ -106,7 +87,7 @@ int main(int argc, const char** argv) { UINT32_MAX, UINT32_MAX, particlePass, - {}, + {attrib}, { core.getDescriptorSet(descriptorSet).layout }, true); @@ -121,6 +102,10 @@ int main(int argc, const char** argv) { 1 ); + const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { + vkcv::VertexBufferBinding(0, vertexbuffer.getVulkanHandle()) + }; + vkcv::DescriptorWrites setWrites; setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle()), vkcv::UniformBufferDescriptorWrite(1,position.getHandle())}; @@ -134,7 +119,8 @@ int main(int argc, const char** argv) { const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); - const vkcv::Mesh renderMesh({}, particleIndexBuffer.getVulkanHandle(), 3); + const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), 3); + vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); vkcv::DrawcallInfo drawcalls(renderMesh, {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle)}); auto start = std::chrono::system_clock::now(); @@ -147,6 +133,33 @@ int main(int argc, const char** argv) { pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); }); + + struct Particle{ + glm::vec2 Position; + glm::vec2 Velocity; + float Rotation = 0.0f; + float SizeBegin, SizeEnd; + + float LifeTime = 1.0f; + float LifeRemaining = 0.0f; + + bool Active = true; + }; + + std::vector<Particle> m_ParticlePool; + uint32_t poolIndex = 999; + + m_ParticlePool.resize(1000); + + //float angle = 0.0005; + glm::mat4 modelmatrix = glm::mat4(1.0); + + for(auto& particle : m_ParticlePool){ + if(!particle.Active){ + continue; + } + } + while (window.isWindowOpen()) { window.pollEvents(); @@ -159,12 +172,28 @@ int main(int argc, const char** argv) { color.fill(&colorData); position.fill(&pos); - auto end = std::chrono::system_clock::now(); - auto deltatime = end - start; + float deltatime = std::chrono::duration<float>(end - start).count(); start = end; - cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); - const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); + + //modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(0,0,1)); + for(auto& particle : m_ParticlePool){ + if (!particle.Active){ + continue; + } + if (particle.LifeRemaining <= 0.0f){ + particle.Active = false; + continue; + } + + particle.LifeRemaining -= deltatime; + particle.Position += particle.Velocity * deltatime; + particle.Rotation += 0.01f * deltatime; + + } + + cameraManager.getCamera().updateView(deltatime); + const glm::mat4 mvp = modelmatrix * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); vkcv::PushConstantData pushConstantData((void*)&mvp, sizeof(glm::mat4)); auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics);