From c4ca7e6bc087f74bffb7f76dc25055efefd74460 Mon Sep 17 00:00:00 2001 From: CAMERON SOAL Date: Thu, 10 Mar 2022 00:27:54 +0000 Subject: [PATCH 1/4] Removed step Vertices are now integers instead of floats --- VulkanGraphicsAPI/Cells.hpp | 10 ++-- VulkanGraphicsAPI/GameOfLifeSystem.cpp | 26 +++++----- VulkanGraphicsAPI/GridHelpers.hpp | 56 ++++++++++------------ VulkanGraphicsAPI/GridSettings.hpp | 5 +- VulkanGraphicsAPI/MarchingCubesHelpers.hpp | 6 +-- VulkanGraphicsAPI/MarchingCubesSystem.cpp | 44 ++++++++--------- VulkanGraphicsAPI/MemoryHelpers.hpp | 4 +- VulkanGraphicsAPI/SimpleRenderSystem.cpp | 10 +++- VulkanGraphicsAPI/SolGrid.cpp | 50 +++++++------------ VulkanGraphicsAPI/SolGrid.hpp | 18 +++---- 10 files changed, 104 insertions(+), 125 deletions(-) diff --git a/VulkanGraphicsAPI/Cells.hpp b/VulkanGraphicsAPI/Cells.hpp index 0dda776..1d3fbc7 100644 --- a/VulkanGraphicsAPI/Cells.hpp +++ b/VulkanGraphicsAPI/Cells.hpp @@ -43,11 +43,11 @@ namespace SolEngine::DOD _wasFreed = true; } - float* pXVertices { nullptr }; // All cubes vertices along x-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] - float* pYVertices { nullptr }; // All cubes vertices along y-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] - float* pZVertices { nullptr }; // All cubes vertices along z-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] - bool* pCellStates { nullptr }; // Stores all cell states (false = dead, true = alive) - NeighbourCount_t* pLiveNeighbourCounts{ nullptr }; // Stores all live neighbours relative to the node + int* pXVertices { nullptr }; // All cubes vertices along x-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] + int* pYVertices { nullptr }; // All cubes vertices along y-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] + int* pZVertices { nullptr }; // All cubes vertices along z-axis [position_index * CUBE_VERTEX_COUNT + vertex_index] + bool* pCellStates { nullptr }; // Stores all cell states (false = dead, true = alive) + NeighbourCount_t* pLiveNeighbourCounts{ nullptr }; // Stores all live neighbours relative to the node private: bool _wasFreed{ false }; // Memory leak flag diff --git a/VulkanGraphicsAPI/GameOfLifeSystem.cpp b/VulkanGraphicsAPI/GameOfLifeSystem.cpp index c1d8218..16fe15b 100644 --- a/VulkanGraphicsAPI/GameOfLifeSystem.cpp +++ b/VulkanGraphicsAPI/GameOfLifeSystem.cpp @@ -10,10 +10,10 @@ namespace SolEngine::System void GameOfLifeSystem::CheckAllCellNeighbours() { - const uint32_t neighbourOffset = 1U; - const Cells& gridNodes = _rSolGrid.cells; - const glm::vec3 scaledDimensions = _rSolGrid.GetScaledDimensions(); - const glm::vec3 validNeighbourDimensions = scaledDimensions - glm::vec3(1); + const uint32_t neighbourOffset = 1U; + const Cells& gridNodes = _rSolGrid.cells; + const glm::uvec3 gridDimensions = _rSolGrid.GetDimensions(); + const glm::uvec3 validNeighbourDimensions = gridDimensions - glm::uvec3(1); uint32_t neighbourIndex(0); @@ -24,7 +24,7 @@ namespace SolEngine::System const uint32_t nodeIndex = _3DTo1DIndex(xIndex, yIndex, zIndex, - scaledDimensions); + gridDimensions); NeighbourCount_t& rLiveNeighbourCount = gridNodes.pLiveNeighbourCounts[nodeIndex]; @@ -37,7 +37,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex - neighbourOffset, yIndex, zIndex, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -48,7 +48,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex + neighbourOffset, yIndex, zIndex, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -59,7 +59,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex, yIndex - neighbourOffset, zIndex, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -70,7 +70,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex, yIndex + neighbourOffset, zIndex, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -81,7 +81,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex, yIndex, zIndex - neighbourOffset, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -92,7 +92,7 @@ namespace SolEngine::System CheckNeighbourState(xIndex, yIndex, zIndex + neighbourOffset, - scaledDimensions, + gridDimensions, gridNodes.pCellStates, rLiveNeighbourCount); } @@ -101,7 +101,7 @@ namespace SolEngine::System void GameOfLifeSystem::UpdateAllCellStates() { - const glm::vec3 scaledDimensions = _rSolGrid.GetScaledDimensions(); + const glm::uvec3 dimensions = _rSolGrid.GetDimensions(); _rSolGrid.TraverseAllGridCells([&](const uint32_t xIndex, const uint32_t yIndex, @@ -112,7 +112,7 @@ namespace SolEngine::System const size_t cellIndex = _3DTo1DIndex(xIndex, yIndex, zIndex, - scaledDimensions); + dimensions); bool& rCellState = rGridNodes.pCellStates[cellIndex]; const NeighbourCount_t cellNeighbourCount = rGridNodes.pLiveNeighbourCounts[cellIndex]; diff --git a/VulkanGraphicsAPI/GridHelpers.hpp b/VulkanGraphicsAPI/GridHelpers.hpp index 9c2c64c..b9da303 100644 --- a/VulkanGraphicsAPI/GridHelpers.hpp +++ b/VulkanGraphicsAPI/GridHelpers.hpp @@ -19,24 +19,22 @@ namespace Utility /// /// Bytes used. template - static size_t GenerateVertices(float *pOutPositions, - const float minValue, - const float maxValue, - const float step) = delete; + static size_t GenerateVertices(int *pOutPositions, + const int minValue, + const int maxValue) = delete; template<> - static size_t GenerateVertices(float *pOutXPositions, - const float minValue, - const float maxValue, - const float step) + static size_t GenerateVertices(int *pOutXPositions, + const int minValue, + const int maxValue) { - const size_t floatSizeBytes = sizeof(float); + const size_t intSizeBytes = sizeof(int); // TODO: Move to constants? size_t bytesInUse = 0; uint32_t rowIndex = 0; - for (float xPos = minValue; xPos < maxValue; xPos += step) + for (int xPos = minValue; xPos < maxValue; ++xPos) { - const float adjXPos = xPos + step; + const int adjXPos = xPos + 1; const uint32_t rowWidth = rowIndex * CUBE_VERTEX_COUNT; pOutXPositions[rowWidth] = xPos; @@ -49,7 +47,7 @@ namespace Utility pOutXPositions[rowWidth + 7] = xPos; ++rowIndex; - bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); } printf_s("Generated %u X-Positions.\n", rowIndex); @@ -58,18 +56,17 @@ namespace Utility } template<> - static size_t GenerateVertices(float *pOutYPositions, - const float minValue, - const float maxValue, - const float step) + static size_t GenerateVertices(int *pOutYPositions, + const int minValue, + const int maxValue) { - const size_t floatSizeBytes = sizeof(float); + const size_t intSizeBytes = sizeof(int); size_t bytesInUse = 0; uint32_t rowIndex = 0; - for (float yPos = minValue; yPos > maxValue; yPos -= step) + for (int yPos = minValue; yPos > maxValue; --yPos) { - const float adjYPos = yPos - step; + const int adjYPos = yPos - 1; const uint32_t rowWidth = rowIndex * CUBE_VERTEX_COUNT; pOutYPositions[rowWidth] = yPos; @@ -82,7 +79,7 @@ namespace Utility pOutYPositions[rowWidth + 7] = adjYPos; ++rowIndex; - bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); } printf_s("Generated %u Y-Positions.\n", rowIndex); @@ -91,18 +88,17 @@ namespace Utility } template<> - static size_t GenerateVertices(float *pOutZPositions, - const float minValue, - const float maxValue, - const float step) + static size_t GenerateVertices(int *pOutZPositions, + const int minValue, + const int maxValue) { - const size_t floatSizeBytes = sizeof(float); + const size_t intSizeBytes = sizeof(int); size_t bytesInUse = 0; uint32_t rowIndex = 0; - for (float zPos = minValue; zPos < maxValue; zPos += step) + for (int zPos = minValue; zPos < maxValue; ++zPos) { - const float adjZPos = zPos + step; + const int adjZPos = zPos + 1; const uint32_t rowWidth = rowIndex * CUBE_VERTEX_COUNT; pOutZPositions[rowWidth] = zPos; @@ -115,7 +111,7 @@ namespace Utility pOutZPositions[rowWidth + 7] = adjZPos; ++rowIndex; - bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); } printf_s("Generated %u Z-Positions.\n", rowIndex); @@ -142,8 +138,8 @@ namespace Utility static size_t DefaultLiveNeighbours(unsigned char* pOutLiveNeighbours, const size_t count) { - const size_t neighbourCountSizeBytes = sizeof(unsigned char); - size_t bytesInUse = 0; + const size_t neighbourCountSizeBytes = sizeof(NeighbourCount_t); + size_t bytesInUse = 0; for (size_t i = 0; i < count; ++i) { diff --git a/VulkanGraphicsAPI/GridSettings.hpp b/VulkanGraphicsAPI/GridSettings.hpp index 8399f83..d45e7f8 100644 --- a/VulkanGraphicsAPI/GridSettings.hpp +++ b/VulkanGraphicsAPI/GridSettings.hpp @@ -7,12 +7,9 @@ namespace SolEngine::Settings { size_t GetNodeCount() const { - const glm::vec3 scaledDimensions = (glm::vec3)dimensions / step; - - return (size_t)((double)scaledDimensions.x * scaledDimensions.y * scaledDimensions.z); + return (size_t)((double)dimensions.x * dimensions.y * dimensions.z); } glm::uvec3 dimensions{ 10 }; // Adjusts the size of the grid - float step { 1.f }; // Adjusts the resolution of the grid }; } diff --git a/VulkanGraphicsAPI/MarchingCubesHelpers.hpp b/VulkanGraphicsAPI/MarchingCubesHelpers.hpp index 7f670c9..d768910 100644 --- a/VulkanGraphicsAPI/MarchingCubesHelpers.hpp +++ b/VulkanGraphicsAPI/MarchingCubesHelpers.hpp @@ -14,7 +14,7 @@ namespace Utility static uint32_t _3DTo1DIndex(const uint32_t xIndex, const uint32_t yIndex, const uint32_t zIndex, - const glm::vec3 &scaledDimensions) + const glm::vec3 &dimensions) { // Always 0 if (xIndex == 0 && @@ -24,8 +24,8 @@ namespace Utility return 0; } - const glm::vec3 sqrDimensions = scaledDimensions * scaledDimensions; - const uint32_t returnIndex = (uint32_t)((zIndex * sqrDimensions.x) + (yIndex * scaledDimensions.y) + xIndex); + const glm::vec3 sqrDimensions = dimensions * dimensions; + const uint32_t returnIndex = (uint32_t)((zIndex * sqrDimensions.x) + (yIndex * dimensions.y) + xIndex); // Convert 3D array indexes into a 1D array index return returnIndex; diff --git a/VulkanGraphicsAPI/MarchingCubesSystem.cpp b/VulkanGraphicsAPI/MarchingCubesSystem.cpp index 2de4f0c..0311f6c 100644 --- a/VulkanGraphicsAPI/MarchingCubesSystem.cpp +++ b/VulkanGraphicsAPI/MarchingCubesSystem.cpp @@ -16,8 +16,8 @@ namespace SolEngine::System // Delete all previous vertices _vertices.clear(); - const glm::vec3 scaledGridDimensions = _rSolGrid.GetScaledDimensions(); - const bool* pGridCellStates = _rSolGrid.cells.pCellStates; + const glm::uvec3 gridDimensions = _rSolGrid.GetDimensions(); + const bool* pGridCellStates = _rSolGrid.cells.pCellStates; _rSolGrid.TraverseAllGridCells([&](const uint32_t xIndex, const uint32_t yIndex, @@ -31,7 +31,7 @@ namespace SolEngine::System xIndex, yIndex, zIndex, - scaledGridDimensions); + gridDimensions); // Calculate the cube index to pull from the Tri-table const uint32_t cubeIndex = GetCubeIndex(cubeIsoValues); @@ -81,34 +81,34 @@ namespace SolEngine::System } void MarchingCubesSystem::GetCubeIsoValues(bool* pOutCubeIsoValues, - const bool* pGridCellStates, - const uint32_t xIndex, - const uint32_t yIndex, - const uint32_t zIndex, - const glm::vec3& scaledGridDimensions) + const bool* pGridCellStates, + const uint32_t xIndex, + const uint32_t yIndex, + const uint32_t zIndex, + const glm::vec3& gridDimensions) { const uint32_t adjOffset = 1U; const uint32_t adjXIndex = xIndex + adjOffset; const uint32_t adjYIndex = yIndex + adjOffset; const uint32_t adjZIndex = zIndex + adjOffset; - if (!(adjXIndex < scaledGridDimensions.x) || - !(adjYIndex < scaledGridDimensions.y) || - !(adjZIndex < scaledGridDimensions.z)) + if (!(adjXIndex < gridDimensions.x) || + !(adjYIndex < gridDimensions.y) || + !(adjZIndex < gridDimensions.z)) { // Out-of-range return; } // Retrieve a "Cube" of cell states - pOutCubeIsoValues[0] = pGridCellStates[_3DTo1DIndex(xIndex, yIndex, zIndex, scaledGridDimensions)]; - pOutCubeIsoValues[1] = pGridCellStates[_3DTo1DIndex(adjXIndex, yIndex, zIndex, scaledGridDimensions)]; - pOutCubeIsoValues[2] = pGridCellStates[_3DTo1DIndex(adjXIndex, yIndex, adjZIndex, scaledGridDimensions)]; - pOutCubeIsoValues[3] = pGridCellStates[_3DTo1DIndex(xIndex, yIndex, adjZIndex, scaledGridDimensions)]; - pOutCubeIsoValues[4] = pGridCellStates[_3DTo1DIndex(xIndex, adjYIndex, zIndex, scaledGridDimensions)]; - pOutCubeIsoValues[5] = pGridCellStates[_3DTo1DIndex(adjXIndex, adjYIndex, zIndex, scaledGridDimensions)]; - pOutCubeIsoValues[6] = pGridCellStates[_3DTo1DIndex(adjXIndex, adjYIndex, adjZIndex, scaledGridDimensions)]; - pOutCubeIsoValues[7] = pGridCellStates[_3DTo1DIndex(xIndex, adjYIndex, adjZIndex, scaledGridDimensions)]; + pOutCubeIsoValues[0] = pGridCellStates[_3DTo1DIndex(xIndex, yIndex, zIndex, gridDimensions)]; + pOutCubeIsoValues[1] = pGridCellStates[_3DTo1DIndex(adjXIndex, yIndex, zIndex, gridDimensions)]; + pOutCubeIsoValues[2] = pGridCellStates[_3DTo1DIndex(adjXIndex, yIndex, adjZIndex, gridDimensions)]; + pOutCubeIsoValues[3] = pGridCellStates[_3DTo1DIndex(xIndex, yIndex, adjZIndex, gridDimensions)]; + pOutCubeIsoValues[4] = pGridCellStates[_3DTo1DIndex(xIndex, adjYIndex, zIndex, gridDimensions)]; + pOutCubeIsoValues[5] = pGridCellStates[_3DTo1DIndex(adjXIndex, adjYIndex, zIndex, gridDimensions)]; + pOutCubeIsoValues[6] = pGridCellStates[_3DTo1DIndex(adjXIndex, adjYIndex, adjZIndex, gridDimensions)]; + pOutCubeIsoValues[7] = pGridCellStates[_3DTo1DIndex(xIndex, adjYIndex, adjZIndex, gridDimensions)]; } void MarchingCubesSystem::CreateVertices(Cells& rNodes, @@ -159,9 +159,9 @@ namespace SolEngine::System const uint32_t yRowWidth = yIndex * CUBE_VERTEX_COUNT; const uint32_t zRowWidth = zIndex * CUBE_VERTEX_COUNT; - const float* pXVertices = &rNodes.pXVertices[xRowWidth]; - const float* pYVertices = &rNodes.pYVertices[yRowWidth]; - const float* pZVertices = &rNodes.pZVertices[zRowWidth]; + const int* pXVertices = &rNodes.pXVertices[xRowWidth]; + const int* pYVertices = &rNodes.pYVertices[yRowWidth]; + const int* pZVertices = &rNodes.pZVertices[zRowWidth]; const Index_t indexA = cornerIndices.first; const Index_t indexB = cornerIndices.second; diff --git a/VulkanGraphicsAPI/MemoryHelpers.hpp b/VulkanGraphicsAPI/MemoryHelpers.hpp index 5d7020d..5ac42b5 100644 --- a/VulkanGraphicsAPI/MemoryHelpers.hpp +++ b/VulkanGraphicsAPI/MemoryHelpers.hpp @@ -24,9 +24,9 @@ namespace Utility static size_t AlignedMallocContiguousArray(_Ty*& prArr, const size_t size) { - const size_t typeSizeBytes = sizeof(_Ty); + const size_t typeSizeBytes = sizeof(_Ty); const size_t arraySizeBytes = size * typeSizeBytes; - const size_t alignment = 16U; + const size_t alignment = 16U; prArr = (_Ty*)_aligned_malloc(arraySizeBytes, alignment); diff --git a/VulkanGraphicsAPI/SimpleRenderSystem.cpp b/VulkanGraphicsAPI/SimpleRenderSystem.cpp index 80397bd..4598722 100644 --- a/VulkanGraphicsAPI/SimpleRenderSystem.cpp +++ b/VulkanGraphicsAPI/SimpleRenderSystem.cpp @@ -8,10 +8,16 @@ namespace SolEngine::Rendering renderPass) {} - void SimpleRenderSystem::RenderGameObject(const SolCamera &solCamera, + void SimpleRenderSystem::RenderGameObject(const SolCamera& solCamera, const VkCommandBuffer commandBuffer, - const SolGameObject &gameObject) const + const SolGameObject& gameObject) const { + if (gameObject.GetModel() == nullptr) + { + // No model to bind/draw + return; + } + const glm::mat4 projectionView = solCamera.GetProjectionViewMatrix(); _pSolPipeline->Bind(commandBuffer); diff --git a/VulkanGraphicsAPI/SolGrid.cpp b/VulkanGraphicsAPI/SolGrid.cpp index 1f41f21..a3da5e6 100644 --- a/VulkanGraphicsAPI/SolGrid.cpp +++ b/VulkanGraphicsAPI/SolGrid.cpp @@ -17,8 +17,7 @@ namespace SolEngine void SolGrid::Initialise() { - _isGridDataValid = !AreCellLimitsExceeded(_rGridSettings.dimensions, - _rGridSettings.step); + _isGridDataValid = !AreCellLimitsExceeded(_rGridSettings.dimensions); // Bad grid data - will cause problems // so just back out... @@ -33,43 +32,32 @@ namespace SolEngine void SolGrid::TraverseAllGridCells(const TraverseCubesCallback_t& callback) { - const float gridStep = _rGridSettings.step; + //const float gridStep = _rGridSettings.step; + const glm::uvec3 gridDimensions = GetDimensions(); - // We have to index this way to account for resolution (step) - uint32_t zIndex(0); - for (float z(_minBounds.z); z < _maxBounds.z; z += gridStep) + for (uint32_t z(0U); z < gridDimensions.z; ++z) { - uint32_t yIndex(0); - for (float y(_minBounds.y); y > _maxBounds.y; y -= gridStep) + for (uint32_t y(0U); y < gridDimensions.y; ++y) { - uint32_t xIndex(0); - for (float x(_minBounds.x); x < _maxBounds.x; x += gridStep) + for (uint32_t x(0U); x < gridDimensions.x; ++x) { - callback(xIndex, - yIndex, - zIndex); - - ++xIndex; + callback(x, y, z); } - - ++yIndex; } - - ++zIndex; } + } void SolGrid::InitialiseNodes() { - const float gridStep = _rGridSettings.step; const size_t nodeCount = _rGridSettings.GetNodeCount(); size_t bytesInUse(0); _rDiagnosticData.memoryAllocatedBytes = cells.AllocateDataArrays(); - bytesInUse += GenerateVertices(cells.pXVertices, _minBounds.x, _maxBounds.x, gridStep); - bytesInUse += GenerateVertices(cells.pYVertices, _minBounds.y, _maxBounds.y, gridStep); - bytesInUse += GenerateVertices(cells.pZVertices, _minBounds.z, _maxBounds.z, gridStep); + bytesInUse += GenerateVertices(cells.pXVertices, _minBounds.x, _maxBounds.x); + bytesInUse += GenerateVertices(cells.pYVertices, _minBounds.y, _maxBounds.y); + bytesInUse += GenerateVertices(cells.pZVertices, _minBounds.z, _maxBounds.z); bytesInUse += DefaultLiveNeighbours(cells.pLiveNeighbourCounts, nodeCount); bytesInUse += GenerateRandomStates(cells.pCellStates, nodeCount); @@ -84,20 +72,16 @@ namespace SolEngine _maxBounds = glm::vec3(halfExtents.x, -halfExtents.y, halfExtents.z); } - bool SolGrid::AreCellLimitsExceeded(const glm::uvec3& dimensions, - const float step) + bool SolGrid::AreCellLimitsExceeded(const glm::uvec3& dimensions) { // If any of these fail, the whole check should fail - return IsMaxCellsPerAxisExceeded(dimensions.x, step) || - IsMaxCellsPerAxisExceeded(dimensions.y, step) || - IsMaxCellsPerAxisExceeded(dimensions.z, step); + return IsMaxCellsPerAxisExceeded(dimensions.x) || + IsMaxCellsPerAxisExceeded(dimensions.y) || + IsMaxCellsPerAxisExceeded(dimensions.z); } - bool SolGrid::IsMaxCellsPerAxisExceeded(const uint32_t axisSize, - const float step) + bool SolGrid::IsMaxCellsPerAxisExceeded(const uint32_t axisSize) { - const uint32_t scaledAxisSize = (uint32_t)(axisSize / step); - - return scaledAxisSize > MAX_CELLS_PER_AXIS_COUNT; + return axisSize > MAX_CELLS_PER_AXIS_COUNT; } } \ No newline at end of file diff --git a/VulkanGraphicsAPI/SolGrid.hpp b/VulkanGraphicsAPI/SolGrid.hpp index 53f8fa1..da7dcf9 100644 --- a/VulkanGraphicsAPI/SolGrid.hpp +++ b/VulkanGraphicsAPI/SolGrid.hpp @@ -25,14 +25,10 @@ namespace SolEngine SolGrid(GridSettings& rGridData, DiagnosticData& rDiagnosticData); ~SolGrid(); - void Initialise(); - - bool IsGridDataValid() const { return _isGridDataValid; } - - const glm::uvec3& GetDimensions() const { return _rGridSettings.dimensions; } - float GetStep() const { return _rGridSettings.step; } - glm::vec3 GetScaledDimensions() const { return (glm::vec3)GetDimensions() / GetStep(); } + bool IsGridDataValid() const { return _isGridDataValid; } + const glm::uvec3& GetDimensions() const { return _rGridSettings.dimensions; } + void Initialise(); void TraverseAllGridCells(const TraverseCubesCallback_t& callback); Cells cells; @@ -40,14 +36,14 @@ namespace SolEngine private: void InitialiseNodes(); void SetBoundsWithDimensions(const glm::uvec3& dimensions); - bool AreCellLimitsExceeded(const glm::uvec3& dimensions, const float step); - bool IsMaxCellsPerAxisExceeded(const uint32_t axisSize, const float step); + bool AreCellLimitsExceeded(const glm::uvec3& dimensions); + bool IsMaxCellsPerAxisExceeded(const uint32_t axisSize); GridSettings& _rGridSettings; DiagnosticData& _rDiagnosticData; - glm::vec3 _minBounds { 0 }; - glm::vec3 _maxBounds { 0 }; + glm::ivec3 _minBounds { 0 }; + glm::ivec3 _maxBounds { 0 }; bool _isGridDataValid{ false }; }; From daa94026a4abf2c15552cb2fd44f43786dd375a4 Mon Sep 17 00:00:00 2001 From: CAMERON SOAL Date: Thu, 10 Mar 2022 00:37:14 +0000 Subject: [PATCH 2/4] Moved typedefs to a Typedefs.hpp file --- VulkanGraphicsAPI/Cells.hpp | 3 --- VulkanGraphicsAPI/Constants.hpp | 4 ++-- VulkanGraphicsAPI/GridHelpers.hpp | 1 + VulkanGraphicsAPI/Typedefs.hpp | 8 ++++++++ VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj | 1 + VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj.filters | 3 +++ VulkanGraphicsAPI/main.cpp | 2 +- 7 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 VulkanGraphicsAPI/Typedefs.hpp diff --git a/VulkanGraphicsAPI/Cells.hpp b/VulkanGraphicsAPI/Cells.hpp index 1d3fbc7..57e8fde 100644 --- a/VulkanGraphicsAPI/Cells.hpp +++ b/VulkanGraphicsAPI/Cells.hpp @@ -3,14 +3,11 @@ #include "MemoryHelpers.hpp" #include "DiagnosticData.hpp" -using namespace Utility; using namespace SolEngine::Data; using namespace SolEngine::Enumeration; namespace SolEngine::DOD { - typedef unsigned char NeighbourCount_t; - struct Cells { ~Cells() diff --git a/VulkanGraphicsAPI/Constants.hpp b/VulkanGraphicsAPI/Constants.hpp index 514918c..0fa161e 100644 --- a/VulkanGraphicsAPI/Constants.hpp +++ b/VulkanGraphicsAPI/Constants.hpp @@ -1,8 +1,8 @@ #pragma once #include "Vertex.hpp" +#include "Typedefs.hpp" -typedef uint32_t UIndex_t; -typedef int32_t Index_t; +using namespace Utility; namespace SolEngine::Data { diff --git a/VulkanGraphicsAPI/GridHelpers.hpp b/VulkanGraphicsAPI/GridHelpers.hpp index b9da303..6c6da68 100644 --- a/VulkanGraphicsAPI/GridHelpers.hpp +++ b/VulkanGraphicsAPI/GridHelpers.hpp @@ -3,6 +3,7 @@ #include "Axis.hpp" #include "Constants.hpp" +#include "Typedefs.hpp" using namespace SolEngine::Data; using namespace SolEngine::Enumeration; diff --git a/VulkanGraphicsAPI/Typedefs.hpp b/VulkanGraphicsAPI/Typedefs.hpp new file mode 100644 index 0000000..39a9607 --- /dev/null +++ b/VulkanGraphicsAPI/Typedefs.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace Utility +{ + typedef uint32_t UIndex_t; + typedef int32_t Index_t; + typedef unsigned char NeighbourCount_t; +} diff --git a/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj b/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj index f574b47..b35e261 100644 --- a/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj +++ b/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj @@ -415,6 +415,7 @@ + diff --git a/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj.filters b/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj.filters index 328b461..782ec56 100644 --- a/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj.filters +++ b/VulkanGraphicsAPI/VulkanGraphicsAPI.vcxproj.filters @@ -314,6 +314,9 @@ SolEngine\Settings + + Utility + diff --git a/VulkanGraphicsAPI/main.cpp b/VulkanGraphicsAPI/main.cpp index e603626..7c1f693 100644 --- a/VulkanGraphicsAPI/main.cpp +++ b/VulkanGraphicsAPI/main.cpp @@ -9,7 +9,7 @@ int main() }; DiagnosticData diagnosticData {}; - GridSettings gridSettings{ .dimensions = glm::vec3{20} }; + GridSettings gridSettings { .dimensions = glm::uvec3{20} }; GameOfLifeSettings gameOfLifeSettings{}; Application application(appData, From 274f99ada736913bf652211ef454276a9eedc008 Mon Sep 17 00:00:00 2001 From: CAMERON SOAL Date: Thu, 10 Mar 2022 00:55:37 +0000 Subject: [PATCH 3/4] Added optional "align" param to Malloc helpers (default 16 bytes) --- VulkanGraphicsAPI/Cells.hpp | 26 ++++++++++++++++---------- VulkanGraphicsAPI/Constants.hpp | 5 +++-- VulkanGraphicsAPI/MemoryHelpers.hpp | 16 ++++++++-------- VulkanGraphicsAPI/SolModel.cpp | 2 +- VulkanGraphicsAPI/Typedefs.hpp | 4 ++-- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/VulkanGraphicsAPI/Cells.hpp b/VulkanGraphicsAPI/Cells.hpp index 57e8fde..ff95345 100644 --- a/VulkanGraphicsAPI/Cells.hpp +++ b/VulkanGraphicsAPI/Cells.hpp @@ -18,24 +18,30 @@ namespace SolEngine::DOD size_t AllocateDataArrays() { + // Malloc alignments + const size_t intAlign { 4U }; + const size_t boolAlign { 1U }; + const size_t neighbourStateAlign{ 1U }; + size_t memoryAllocatedBytes(0); - memoryAllocatedBytes += AlignedMallocContiguous2DArray(pXVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT); - memoryAllocatedBytes += AlignedMallocContiguous2DArray(pYVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT); - memoryAllocatedBytes += AlignedMallocContiguous2DArray(pZVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT); - memoryAllocatedBytes += AlignedMallocContiguousArray(pCellStates, MAX_CUBES_COUNT * CUBE_VERTEX_COUNT); - memoryAllocatedBytes += AlignedMallocContiguousArray(pLiveNeighbourCounts, MAX_CUBES_COUNT * CUBE_VERTEX_COUNT); + memoryAllocatedBytes += AlignedMallocContiguous2DArray(pXVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT, intAlign); + memoryAllocatedBytes += AlignedMallocContiguous2DArray(pYVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT, intAlign); + memoryAllocatedBytes += AlignedMallocContiguous2DArray(pZVertices, MAX_CELLS_PER_AXIS_COUNT, CUBE_VERTEX_COUNT, intAlign); + + memoryAllocatedBytes += AlignedMallocContiguousArray(pCellStates, MAX_CUBE_VERTEX_COUNT, boolAlign); + memoryAllocatedBytes += AlignedMallocContiguousArray(pLiveNeighbourCounts, MAX_CUBE_VERTEX_COUNT, neighbourStateAlign); return memoryAllocatedBytes; } void Free() { - FreeAlignedMallocArray(pXVertices); // X-Positions - FreeAlignedMallocArray(pYVertices); // Y-Positions - FreeAlignedMallocArray(pZVertices); // Z-Positions - FreeAlignedMallocArray(pCellStates); // Cell States - FreeAlignedMallocArray(pLiveNeighbourCounts); // Live Neighbours + FreeAlignedMallocArray(pXVertices); // X-Positions + FreeAlignedMallocArray(pYVertices); // Y-Positions + FreeAlignedMallocArray(pZVertices); // Z-Positions + FreeAlignedMallocArray(pCellStates); // Cell States + FreeAlignedMallocArray(pLiveNeighbourCounts); // Live Neighbours _wasFreed = true; } diff --git a/VulkanGraphicsAPI/Constants.hpp b/VulkanGraphicsAPI/Constants.hpp index 0fa161e..f20c8c1 100644 --- a/VulkanGraphicsAPI/Constants.hpp +++ b/VulkanGraphicsAPI/Constants.hpp @@ -15,8 +15,9 @@ namespace SolEngine::Data static constexpr float SPHERE_RADIUS{ 5.f }; static constexpr uint32_t MAX_CELLS_PER_AXIS_COUNT{ 1 << 8 }; // Currently can only go upto 9 (134,217,728 Cubes) - static constexpr uint32_t MAX_CUBES_COUNT{ MAX_CELLS_PER_AXIS_COUNT * MAX_CELLS_PER_AXIS_COUNT * MAX_CELLS_PER_AXIS_COUNT }; - static constexpr uint32_t CUBE_VERTEX_COUNT{ 8U }; + static constexpr uint32_t MAX_CUBES_COUNT { MAX_CELLS_PER_AXIS_COUNT * MAX_CELLS_PER_AXIS_COUNT * MAX_CELLS_PER_AXIS_COUNT }; + static constexpr uint32_t CUBE_VERTEX_COUNT { 8U }; + static constexpr uint32_t MAX_CUBE_VERTEX_COUNT { MAX_CUBES_COUNT * CUBE_VERTEX_COUNT }; static constexpr size_t TRI_TABLE_COUNT{ 256 }; static constexpr size_t TRI_TABLE_INDEX_COUNT{ 16 }; diff --git a/VulkanGraphicsAPI/MemoryHelpers.hpp b/VulkanGraphicsAPI/MemoryHelpers.hpp index 5ac42b5..239203f 100644 --- a/VulkanGraphicsAPI/MemoryHelpers.hpp +++ b/VulkanGraphicsAPI/MemoryHelpers.hpp @@ -22,13 +22,13 @@ namespace Utility template static size_t AlignedMallocContiguousArray(_Ty*& prArr, - const size_t size) + const size_t size, + const size_t align = 16U) { const size_t typeSizeBytes = sizeof(_Ty); - const size_t arraySizeBytes = size * typeSizeBytes; - const size_t alignment = 16U; + const size_t arraySizeBytes = size * align; - prArr = (_Ty*)_aligned_malloc(arraySizeBytes, alignment); + prArr = (_Ty*)_aligned_malloc(arraySizeBytes, align); DBG_ASSERT_MSG((prArr != nullptr), "_aligned_malloc Failed!"); printf_s("%s - Allocated: %zu bytes.\n", __FUNCTION__, arraySizeBytes); @@ -39,14 +39,14 @@ namespace Utility template static size_t AlignedMallocContiguous2DArray(_Ty *&prArr, const size_t rowCount, - const size_t columnCount) + const size_t columnCount, + const size_t align = 16U) { // Must be indexed via my_matrix[row_index * row_length + column_index]; const size_t typeSizeBytes = sizeof(_Ty); - const size_t arraySizeBytes = rowCount * columnCount * typeSizeBytes; - const size_t alignment = 16U; + const size_t arraySizeBytes = rowCount * columnCount * align; - prArr = (_Ty *)_aligned_malloc(arraySizeBytes, alignment); + prArr = (_Ty *)_aligned_malloc(arraySizeBytes, align); DBG_ASSERT_MSG((prArr != nullptr), "_aligned_malloc Failed!"); printf_s("%s - Allocated: %zu bytes.\n", __FUNCTION__, arraySizeBytes); diff --git a/VulkanGraphicsAPI/SolModel.cpp b/VulkanGraphicsAPI/SolModel.cpp index 4f70f3a..47da180 100644 --- a/VulkanGraphicsAPI/SolModel.cpp +++ b/VulkanGraphicsAPI/SolModel.cpp @@ -37,7 +37,7 @@ namespace SolEngine vkCmdBindIndexBuffer(commandBuffer, _pIndexBuffer->GetBuffer(), 0, - VK_INDEX_TYPE_UINT32); + VK_INDEX_TYPE_UINT8_EXT); } void SolModel::Draw(const VkCommandBuffer commandBuffer) diff --git a/VulkanGraphicsAPI/Typedefs.hpp b/VulkanGraphicsAPI/Typedefs.hpp index 39a9607..dc53e09 100644 --- a/VulkanGraphicsAPI/Typedefs.hpp +++ b/VulkanGraphicsAPI/Typedefs.hpp @@ -2,7 +2,7 @@ namespace Utility { - typedef uint32_t UIndex_t; - typedef int32_t Index_t; + typedef uint8_t UIndex_t; + typedef int8_t Index_t; typedef unsigned char NeighbourCount_t; } From 35bb77585cddbfa5f0fca2759c656acf00430ec2 Mon Sep 17 00:00:00 2001 From: CAMERON SOAL Date: Thu, 10 Mar 2022 00:58:11 +0000 Subject: [PATCH 4/4] Moved intSizeBytes to constants --- VulkanGraphicsAPI/Constants.hpp | 2 ++ VulkanGraphicsAPI/GridHelpers.hpp | 21 +++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/VulkanGraphicsAPI/Constants.hpp b/VulkanGraphicsAPI/Constants.hpp index f20c8c1..7f483ba 100644 --- a/VulkanGraphicsAPI/Constants.hpp +++ b/VulkanGraphicsAPI/Constants.hpp @@ -6,6 +6,8 @@ using namespace Utility; namespace SolEngine::Data { + static constexpr size_t INT_SIZE_BYTES{ sizeof(int) }; + static constexpr float SECONDS_TO_MILLISECONDS{ 1000.f }; static constexpr glm::vec3 VEC3_RIGHT { 1.f, 0.f, 0.f }; diff --git a/VulkanGraphicsAPI/GridHelpers.hpp b/VulkanGraphicsAPI/GridHelpers.hpp index 6c6da68..f42c6a7 100644 --- a/VulkanGraphicsAPI/GridHelpers.hpp +++ b/VulkanGraphicsAPI/GridHelpers.hpp @@ -29,9 +29,8 @@ namespace Utility const int minValue, const int maxValue) { - const size_t intSizeBytes = sizeof(int); // TODO: Move to constants? - size_t bytesInUse = 0; - uint32_t rowIndex = 0; + size_t bytesInUse = 0; + uint32_t rowIndex = 0; for (int xPos = minValue; xPos < maxValue; ++xPos) { @@ -48,7 +47,7 @@ namespace Utility pOutXPositions[rowWidth + 7] = xPos; ++rowIndex; - bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT); } printf_s("Generated %u X-Positions.\n", rowIndex); @@ -61,9 +60,8 @@ namespace Utility const int minValue, const int maxValue) { - const size_t intSizeBytes = sizeof(int); - size_t bytesInUse = 0; - uint32_t rowIndex = 0; + size_t bytesInUse = 0; + uint32_t rowIndex = 0; for (int yPos = minValue; yPos > maxValue; --yPos) { @@ -80,7 +78,7 @@ namespace Utility pOutYPositions[rowWidth + 7] = adjYPos; ++rowIndex; - bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT); } printf_s("Generated %u Y-Positions.\n", rowIndex); @@ -93,9 +91,8 @@ namespace Utility const int minValue, const int maxValue) { - const size_t intSizeBytes = sizeof(int); - size_t bytesInUse = 0; - uint32_t rowIndex = 0; + size_t bytesInUse = 0; + uint32_t rowIndex = 0; for (int zPos = minValue; zPos < maxValue; ++zPos) { @@ -112,7 +109,7 @@ namespace Utility pOutZPositions[rowWidth + 7] = adjZPos; ++rowIndex; - bytesInUse += (intSizeBytes * CUBE_VERTEX_COUNT); + bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT); } printf_s("Generated %u Z-Positions.\n", rowIndex);