Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
VulkanMesh.cpp
1/*
2Copyright (C) 2017-2019,2021,2023,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include <fstream>
18#include <sstream>
19#include <exception>
20#include <utility>
21#include <vector>
22#include <cassert>
23#include <cstring>
25#include "aeongames/AeonEngine.hpp"
26#include "aeongames/Utilities.hpp"
27#include "aeongames/CRC.hpp"
29#include "VulkanRenderer.hpp"
30#include "VulkanMesh.hpp"
31#include "VulkanUtilities.hpp"
32
33namespace AeonGames
34{
35 VulkanMesh::VulkanMesh ( const VulkanRenderer& aVulkanRenderer, const Mesh& aMesh ) :
36 mVulkanRenderer {aVulkanRenderer}, mMesh{&aMesh}, mMeshBuffer { mVulkanRenderer }
37 {
38 const VkDeviceSize buffer_size{ aMesh.GetVertexBuffer().size() + ( aMesh.GetIndexBuffer().size() * ( aMesh.GetIndexSize() == 1 ? 2 : 1 ) ) };
39 const VkBufferUsageFlags buffer_usage {static_cast<VkBufferUsageFlags> ( ( aMesh.GetVertexCount() ? VK_BUFFER_USAGE_VERTEX_BUFFER_BIT : 0 ) | ( aMesh.GetIndexCount() ? VK_BUFFER_USAGE_INDEX_BUFFER_BIT : 0 ) ) };
40
41 mMeshBuffer.Initialize ( buffer_size, buffer_usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT );
42
43 if ( aMesh.GetVertexCount() )
44 {
45 mMeshBuffer.WriteMemory ( 0, aMesh.GetVertexBuffer().size(), aMesh.GetVertexBuffer().data() );
46 }
47 if ( aMesh.GetIndexCount() )
48 {
49 if ( aMesh.GetIndexSize() != 1 )
50 {
51 mMeshBuffer.WriteMemory ( aMesh.GetVertexBuffer().size(), aMesh.GetIndexBuffer().size(), aMesh.GetIndexBuffer().data() );
52 }
53 else
54 {
55 std::vector<uint16_t> buffer{};
56 buffer.resize ( aMesh.GetIndexCount() );
57 for ( size_t i = 0; i < aMesh.GetIndexCount(); ++i )
58 {
59 buffer[i] = aMesh.GetIndexBuffer() [i];
60 }
61 mMeshBuffer.WriteMemory ( aMesh.GetVertexBuffer().size(), buffer.size() * sizeof ( uint16_t ), buffer.data() );
62 }
63 }
64 }
65
66 VulkanMesh::~VulkanMesh()
67 {
68 mMeshBuffer.Finalize();
69 }
70
72 mVulkanRenderer{aVulkanMesh.mVulkanRenderer},
73 mMesh{aVulkanMesh.mMesh}, mMeshBuffer{std::move ( aVulkanMesh.mMeshBuffer ) } {}
74
75 static VkIndexType GetIndexType ( const Mesh* aMesh )
76 {
77 switch ( aMesh->GetIndexSize() )
78 {
79 case 1:
80 case 2:
81 return VK_INDEX_TYPE_UINT16;
82 case 4:
83 return VK_INDEX_TYPE_UINT32;
84 default:
85 break;
86 };
87 std::cout << LogLevel::Error << "Invalid Index Size." << std::endl;
88 throw std::runtime_error ( "Invalid Index Size." );
89 }
90
91 void VulkanMesh::Bind ( VkCommandBuffer aVkCommandBuffer ) const
92 {
93 const VkDeviceSize offset = 0;
94 vkCmdBindVertexBuffers ( aVkCommandBuffer, 0, 1, &mMeshBuffer.GetBuffer(), &offset );
95 if ( mMesh->GetIndexCount() )
96 {
97 vkCmdBindIndexBuffer ( aVkCommandBuffer,
98 mMeshBuffer.GetBuffer(), mMesh->GetVertexBuffer().size(),
99 GetIndexType ( mMesh ) );
100 }
101 }
102}
Defines log severity levels and stream output for the AeonGames engine.
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
Represents a polygon mesh with vertex attributes and index data.
Definition Mesh.hpp:31
DLL uint32_t GetIndexSize() const
Get the size in bytes of a single index.
Definition Mesh.cpp:43
DLL uint32_t GetIndexCount() const
Get the total number of indices.
Definition Mesh.cpp:48
DLL const std::vector< uint8_t > & GetIndexBuffer() const
Get the raw index data buffer.
Definition Mesh.cpp:63
DLL const std::vector< uint8_t > & GetVertexBuffer() const
Get the raw vertex data buffer.
Definition Mesh.cpp:58
DLL uint32_t GetVertexCount() const
Get the total number of vertices.
Definition Mesh.cpp:53
void Finalize()
Release the Vulkan buffer and its device memory.
void Bind(VkCommandBuffer aVkCommandBuffer) const
Bind the mesh vertex and index buffers to a command buffer.
VulkanMesh(const VulkanRenderer &aVulkanRenderer, const Mesh &aMesh)
Construct from a renderer and mesh resource.
Vulkan rendering backend implementing the Renderer interface.
<- This is here just for the literals
Definition AABB.hpp:31
@ Error
Error conditions.
Definition LogLevel.hpp:33
STL namespace.