diff --git a/include/vkcv/Buffer.hpp b/include/vkcv/Buffer.hpp index 5c21b1ce1d2d84afad6816fd8bf5f66a82b80941..7cace9324e6b1d885974c1c8906db63d5d8b095d 100644 --- a/include/vkcv/Buffer.hpp +++ b/include/vkcv/Buffer.hpp @@ -10,6 +10,8 @@ namespace vkcv { template<typename T> class Buffer { + friend Core; + public: // explicit destruction of default constructor Buffer<T>() = delete; @@ -56,6 +58,9 @@ namespace vkcv { m_count(count), m_memoryType(memoryType) {} - + + vk::Buffer getVulkanHandle() const { + return m_manager->getBuffer(m_handle_id); + } }; } diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index 66db4776d053352d8ccb2eea5e09c3fb77b68561..ec09e325efe2cecdc559756b2e72a07717cad121 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -187,7 +187,8 @@ namespace vkcv * @brief render a beautiful triangle */ void renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle, - const int width, const int height, const size_t pushConstantSize, const void* pushConstantData); + const int width, const int height, const size_t pushConstantSize, const void* pushConstantData, + const Buffer<float> &vertexBuffer); /** * @brief end recording and present image diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index e7664abd3f2bd74e0ad04629391d5f3ad108d4c2..6ed43eaa2a1e1c3f80029875502741835fdb7027 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -40,14 +40,16 @@ int main(int argc, const char** argv) { const size_t n = 5027; - auto buffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); - vec3 vec_data [n]; + auto vertexBuffer = core.createBuffer<float>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); + float vec_data [n*3]; - for (size_t i = 0; i < n; i++) { - vec_data[i] = { 42, static_cast<float>(i), 7 }; - } + for (int i = 0; i < n; i += 3) { + vec_data[i] = 42; + vec_data[i + 1] = static_cast<float>(i); + vec_data[i + 2] = 7; + }; - buffer.fill(vec_data); + vertexBuffer.fill(vec_data); /*vec3* m = buffer.map(); m[0] = { 0, 0, 0 }; @@ -142,7 +144,7 @@ int main(int argc, const char** argv) { cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); - core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp); + core.renderTriangle(trianglePass, trianglePipeline, windowWidth, windowHeight, sizeof(mvp), &mvp, vertexBuffer); core.endFrame(); } return 0; diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index 0ee414fb974676b1d2df820e69218f5bb247341b..155b500e867774823fd41789e6b47f9eb900cb91 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -163,7 +163,8 @@ namespace vkcv } void Core::renderTriangle(const PassHandle renderpassHandle, const PipelineHandle pipelineHandle, - const int width, const int height, const size_t pushConstantSize, const void *pushConstantData) { + const int width, const int height, const size_t pushConstantSize, const void *pushConstantData, + const Buffer<float> &vertexBuffer) { if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) { return; @@ -174,6 +175,7 @@ namespace vkcv const vk::Pipeline pipeline = m_PipelineManager->getVkPipeline(pipelineHandle); const vk::PipelineLayout pipelineLayout = m_PipelineManager->getVkPipelineLayout(pipelineHandle); const vk::Rect2D renderArea(vk::Offset2D(0, 0), vk::Extent2D(width, height)); + const vk::Buffer vulkanVertexBuffer = vertexBuffer.getVulkanHandle(); const vk::Framebuffer framebuffer = createFramebuffer(m_Context.getDevice(), renderpass, width, height, imageView); m_TemporaryFramebuffers.push_back(framebuffer); @@ -181,7 +183,8 @@ namespace vkcv SubmitInfo submitInfo; submitInfo.queueType = QueueType::Graphics; submitInfo.signalSemaphores = { m_SyncResources.renderFinished }; - submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline, pipelineLayout, pushConstantSize, pushConstantData](const vk::CommandBuffer& cmdBuffer) { + submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline, pipelineLayout, + pushConstantSize, pushConstantData, vulkanVertexBuffer](const vk::CommandBuffer& cmdBuffer) { const std::array<float, 4> clearColor = { 0.f, 0.f, 0.f, 1.f }; const vk::ClearValue clearValues(clearColor); @@ -191,6 +194,8 @@ namespace vkcv cmdBuffer.beginRenderPass(beginInfo, subpassContents, {}); cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline, {}); + + cmdBuffer.bindVertexBuffers(0, (vulkanVertexBuffer), { 0 }); cmdBuffer.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eAll, 0, pushConstantSize, pushConstantData); cmdBuffer.draw(3, 1, 0, 0, {}); cmdBuffer.endRenderPass();