Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions VulkanGraphicsAPI/Cells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -21,33 +18,39 @@ 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;
}

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
Expand Down
11 changes: 7 additions & 4 deletions VulkanGraphicsAPI/Constants.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once
#include "Vertex.hpp"
#include "Typedefs.hpp"

typedef uint32_t UIndex_t;
typedef int32_t Index_t;
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 };
Expand All @@ -15,8 +17,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 };
Expand Down
26 changes: 13 additions & 13 deletions VulkanGraphicsAPI/GameOfLifeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -24,7 +24,7 @@ namespace SolEngine::System
const uint32_t nodeIndex = _3DTo1DIndex(xIndex,
yIndex,
zIndex,
scaledDimensions);
gridDimensions);

NeighbourCount_t& rLiveNeighbourCount =
gridNodes.pLiveNeighbourCounts[nodeIndex];
Expand All @@ -37,7 +37,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex - neighbourOffset,
yIndex,
zIndex,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -48,7 +48,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex + neighbourOffset,
yIndex,
zIndex,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -59,7 +59,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex,
yIndex - neighbourOffset,
zIndex,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -70,7 +70,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex,
yIndex + neighbourOffset,
zIndex,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -81,7 +81,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex,
yIndex,
zIndex - neighbourOffset,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -92,7 +92,7 @@ namespace SolEngine::System
CheckNeighbourState(xIndex,
yIndex,
zIndex + neighbourOffset,
scaledDimensions,
gridDimensions,
gridNodes.pCellStates,
rLiveNeighbourCount);
}
Expand All @@ -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,
Expand All @@ -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];
Expand Down
66 changes: 30 additions & 36 deletions VulkanGraphicsAPI/GridHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Axis.hpp"
#include "Constants.hpp"
#include "Typedefs.hpp"

using namespace SolEngine::Data;
using namespace SolEngine::Enumeration;
Expand All @@ -19,24 +20,21 @@ namespace Utility
/// </summary>
/// <returns>Bytes used.</returns>
template<Axis>
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<Axis::X>(float *pOutXPositions,
const float minValue,
const float maxValue,
const float step)
static size_t GenerateVertices<Axis::X>(int *pOutXPositions,
const int minValue,
const int maxValue)
{
const size_t floatSizeBytes = sizeof(float);
size_t bytesInUse = 0;
uint32_t rowIndex = 0;
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;
Expand All @@ -49,7 +47,7 @@ namespace Utility
pOutXPositions[rowWidth + 7] = xPos;

++rowIndex;
bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT);
bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT);
}

printf_s("Generated %u X-Positions.\n", rowIndex);
Expand All @@ -58,18 +56,16 @@ namespace Utility
}

template<>
static size_t GenerateVertices<Axis::Y>(float *pOutYPositions,
const float minValue,
const float maxValue,
const float step)
static size_t GenerateVertices<Axis::Y>(int *pOutYPositions,
const int minValue,
const int maxValue)
{
const size_t floatSizeBytes = sizeof(float);
size_t bytesInUse = 0;
uint32_t rowIndex = 0;
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;
Expand All @@ -82,7 +78,7 @@ namespace Utility
pOutYPositions[rowWidth + 7] = adjYPos;

++rowIndex;
bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT);
bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT);
}

printf_s("Generated %u Y-Positions.\n", rowIndex);
Expand All @@ -91,18 +87,16 @@ namespace Utility
}

template<>
static size_t GenerateVertices<Axis::Z>(float *pOutZPositions,
const float minValue,
const float maxValue,
const float step)
static size_t GenerateVertices<Axis::Z>(int *pOutZPositions,
const int minValue,
const int maxValue)
{
const size_t floatSizeBytes = sizeof(float);
size_t bytesInUse = 0;
uint32_t rowIndex = 0;
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;
Expand All @@ -115,7 +109,7 @@ namespace Utility
pOutZPositions[rowWidth + 7] = adjZPos;

++rowIndex;
bytesInUse += (floatSizeBytes * CUBE_VERTEX_COUNT);
bytesInUse += (INT_SIZE_BYTES * CUBE_VERTEX_COUNT);
}

printf_s("Generated %u Z-Positions.\n", rowIndex);
Expand All @@ -142,8 +136,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)
{
Expand Down
5 changes: 1 addition & 4 deletions VulkanGraphicsAPI/GridSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
6 changes: 3 additions & 3 deletions VulkanGraphicsAPI/MarchingCubesHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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;
Expand Down
Loading