I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
I am in the process of converting the codebase from using vulkan.h to vulkan.hpp, as well as utilising RAII. Here is the new header for the BufferUtils class:
BufferUtils.h:
#pragma once
#include <vulkan/vulkan.hpp>
namespace BufferUtils { void CreateBuffer( vk::Device device, vk::PhysicalDevice physicalDevice, vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties, vk::Buffer& buffer, vk::DeviceMemory& bufferMemory);
uint32_t FindMemoryType(vk::PhysicalDevice physicalDevice, uint32_t typeFilter, vk::MemoryPropertyFlags properties); void CopyBuffer( vk::Device device, vk::CommandPool commandPool, vk::Queue graphicsQueue, vk::Buffer srcBuffer, vk::Buffer dstBuffer, vk::DeviceSize size);}
Can you update the following code of the Mesh class to utilise vulkan.hpp, RAII and align with the BufferUtils class?
Mesh.h:
#pragma once
#include #include <vulkan/vulkan.h> #include <glm/glm.hpp> #include "BufferUtils.h"
struct Vertex { glm::vec3 position; glm::vec3 color; glm::vec2 texCoord; };
class Mesh { public: Mesh(); ~Mesh();
void Initialize(std::vector<Vertex> vertices, std::vector<uint32_t> indices, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Cleanup(); const std::vector<Vertex>& GetVertices() const; const std::vector<uint32_t>& GetIndices() const; VkBuffer GetVertexBuffer() const; VkBuffer GetIndexBuffer() const; void SetVertices(const std::vector<Vertex>& vertices); void SetIndices(const std::vector<uint32_t>& indices); std::vector<VkVertexInputBindingDescription> GetVertexInputBindingDescriptions() const; std::vector<VkVertexInputAttributeDescription> GetVertexInputAttributeDescriptions() const;private: VkDevice device; std::vector vertices; std::vector<uint32_t> indices;
VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; VkBuffer indexBuffer; VkDeviceMemory indexBufferMemory;};
Mesh.cpp:
#include "Mesh.h"
Mesh::Mesh() : device(VK_NULL_HANDLE), vertexBuffer(VK_NULL_HANDLE), vertexBufferMemory(VK_NULL_HANDLE), indexBuffer(VK_NULL_HANDLE), indexBufferMemory(VK_NULL_HANDLE) { }
Mesh::~Mesh() { Cleanup(); }
- Assistant
- User
I am getting errors in the Mesh::Initialize method. It says it "cannot deduce 'auto' type". Can you identify and fix the issue?
- Assistant
- User
The BufferUtils class is utilised by other parts of the code that I haven't provided. Making such changes would require several more changes across the codebase. Is there a way to modify the Mesh class to address the error while aligning with the BufferUtils class as provided?
- Assistant
I apologize for the confusion. In that case, let's revert the changes to the
BufferUtils.hppandBufferUtils.cppfiles and adjust theMesh::Initializemethod to work with the originalBufferUtilsclass.You can keep the original versions of
BufferUtils.hppandBufferUtils.cppand then update theMesh::Initializemethod in theMesh.cppfile as follows:Mesh.cpp:
// ... other parts of the file void Mesh::Initialize(vk::Device device, vk::PhysicalDevice physicalDevice, vk::CommandPool commandPool, vk::Queue graphicsQueue) { this->device = device; vk::DeviceSize bufferSize = sizeof(vertices[0]) * vertices.size(); // Staging buffer vk::Buffer stagingBuffer; vk::DeviceMemory stagingBufferMemory; BufferUtils::CreateBuffer(device, physicalDevice, bufferSize, vk::BufferUsageFlagBits::eTransferSrc, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, stagingBuffer, stagingBufferMemory); // ... rest of the function } // ... rest of the fileThis way, you won't need to make changes in other parts of the code that utilize the class. The method should now work without any errors.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.