Skip to content

Commit 14dcb35

Browse files
committed
Add instancing support, remove unused STB dependency, and fetch Bistro assets
- Introduced instance data structure for instanced rendering. - Updated Vulkan pipeline to include instancing with vertex and instance attribute descriptions. - Removed STB library from dependencies and scripts. - Included Linux/macOS and Windows scripts to fetch Bistro example assets.
1 parent 4b8198d commit 14dcb35

29 files changed

+1972
-1200
lines changed

attachments/simple_engine/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ find_package (glm REQUIRED)
1414
find_package (Vulkan REQUIRED)
1515
find_package (tinygltf REQUIRED)
1616
find_package (KTX REQUIRED)
17-
find_package (stb REQUIRED)
1817
find_package (OpenAL REQUIRED)
1918

2019
# set up Vulkan C++ module
@@ -133,7 +132,6 @@ target_link_libraries(SimpleEngine PRIVATE
133132
glm::glm
134133
tinygltf::tinygltf
135134
KTX::ktx
136-
stb::stb
137135
OpenAL::OpenAL
138136
)
139137

attachments/simple_engine/camera_component.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ class CameraComponent : public Component {
198198
return up;
199199
}
200200

201+
/**
202+
* @brief Force view matrix recalculation without modifying camera orientation.
203+
* This is used when the camera's transform position changes externally (e.g., from GLTF loading).
204+
*/
205+
void ForceViewMatrixUpdate() {
206+
viewMatrixDirty = true;
207+
}
208+
201209
private:
202210
/**
203211
* @brief Update the view matrix based on the camera position and target.

attachments/simple_engine/engine.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Engine::~Engine() {
2525

2626
bool Engine::Initialize(const std::string& appName, int width, int height, bool enableValidationLayers) {
2727
// Create platform
28-
#if PLATFORM_ANDROID
28+
#if defined(PLATFORM_ANDROID)
2929
// For Android, the platform is created with the android_app
3030
// This will be handled in the android_main function
3131
return false;
@@ -213,14 +213,17 @@ void Engine::Run() {
213213
frameCount++;
214214
fpsUpdateTimer += deltaTime;
215215

216-
// Update window title with FPS and frame count every second
216+
// Update window title with FPS and frame time every second
217217
if (fpsUpdateTimer >= 1.0f) {
218218
uint64_t framesSinceLastUpdate = frameCount - lastFPSUpdateFrame;
219219
currentFPS = framesSinceLastUpdate / fpsUpdateTimer;
220+
// Average frame time in milliseconds over the last interval
221+
double avgMs = (fpsUpdateTimer / static_cast<double>(framesSinceLastUpdate)) * 1000.0;
220222

221-
// Update window title with frame count and FPS
223+
// Update window title with frame count, FPS, and frame time
222224
std::string title = "Simple Engine - Frame: " + std::to_string(frameCount) +
223-
" | FPS: " + std::to_string(static_cast<int>(currentFPS));
225+
" | FPS: " + std::to_string(static_cast<int>(currentFPS)) +
226+
" | ms: " + std::to_string(static_cast<int>(avgMs));
224227
platform->SetWindowTitle(title);
225228

226229
// Reset timer and frame counter for next update
@@ -415,29 +418,26 @@ void Engine::Render() {
415418
}
416419

417420
float Engine::CalculateDeltaTime() {
418-
// Get current time
419-
auto currentTime = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
420-
std::chrono::high_resolution_clock::now().time_since_epoch()
421-
).count());
421+
// Get current time using a steady clock to avoid system time jumps
422+
uint64_t currentTime = static_cast<uint64_t>(
423+
std::chrono::duration_cast<std::chrono::milliseconds>(
424+
std::chrono::steady_clock::now().time_since_epoch()
425+
).count()
426+
);
422427

423428
// Initialize lastFrameTime on first call
424429
if (lastFrameTime == 0) {
425430
lastFrameTime = currentTime;
426-
return 0.016f; // Return ~16ms (60 FPS) for first frame
431+
return 0.016f; // ~16ms as a sane initial guess
427432
}
428433

429-
// Calculate delta time
434+
// Calculate delta time in milliseconds
430435
uint64_t delta = currentTime - lastFrameTime;
431436

432437
// Update last frame time
433438
lastFrameTime = currentTime;
434439

435-
// Clamp delta time to reasonable values (prevent huge jumps)
436-
if (delta > 10) { // Cap at 100ms (10 FPS minimum)
437-
delta = 16; // Use 16ms instead
438-
}
439-
440-
return delta / 1000.0f; // Convert to seconds
440+
return static_cast<float>(delta) / 1000.0f;
441441
}
442442

443443
void Engine::HandleResize(int width, int height) const {
@@ -754,7 +754,7 @@ void Engine::HandleMouseHover(float mouseX, float mouseY) {
754754
}
755755

756756

757-
#if PLATFORM_ANDROID
757+
#if defined(PLATFORM_ANDROID)
758758
// Android-specific implementation
759759
bool Engine::InitializeAndroid(android_app* app, const std::string& appName, bool enableValidationLayers) {
760760
// Create platform

attachments/simple_engine/engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Engine {
141141
*/
142142
ImGuiSystem* GetImGuiSystem() const;
143143

144-
#if PLATFORM_ANDROID
144+
#if defined(PLATFORM_ANDROID)
145145
/**
146146
* @brief Initialize the engine for Android.
147147
* @param app The Android app.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
REM Fetch the Bistro example assets into the desired assets directory.
5+
REM Default target: assets\bistro at the repository root.
6+
REM Usage:
7+
REM fetch_bistro_assets.bat [target-dir]
8+
REM Examples:
9+
REM fetch_bistro_assets.bat
10+
REM fetch_bistro_assets.bat attachments\simple_engine\Assets\bistro
11+
12+
set REPO_SSH=[email protected]:gpx1000/bistro.git
13+
set REPO_HTTPS=https://github.com/gpx1000/bistro.git
14+
15+
if "%~1"=="" (
16+
set TARGET_DIR=assets\bistro
17+
) else (
18+
set TARGET_DIR=%~1
19+
)
20+
21+
REM Ensure parent directory exists
22+
for %%I in ("%TARGET_DIR%") do set PARENT=%%~dpI
23+
if not exist "%PARENT%" mkdir "%PARENT%"
24+
25+
REM If directory exists and is a git repo, update it; otherwise clone it
26+
if exist "%TARGET_DIR%\.git" (
27+
echo Updating existing bistro assets in %TARGET_DIR%
28+
pushd "%TARGET_DIR%"
29+
git pull --ff-only
30+
popd
31+
) else (
32+
echo Cloning bistro assets into %TARGET_DIR%
33+
REM Try SSH first; fall back to HTTPS on failure
34+
git clone --depth 1 "%REPO_SSH%" "%TARGET_DIR%" 2>nul
35+
if %ERRORLEVEL% neq 0 (
36+
echo SSH clone failed, trying HTTPS
37+
git clone --depth 1 "%REPO_HTTPS%" "%TARGET_DIR%"
38+
)
39+
)
40+
41+
REM If git-lfs is available, ensure LFS content is pulled
42+
where git >nul 2>nul
43+
if %ERRORLEVEL%==0 (
44+
pushd "%TARGET_DIR%"
45+
git lfs version >nul 2>nul
46+
if %ERRORLEVEL%==0 (
47+
git lfs install --local >nul 2>nul
48+
git lfs pull
49+
)
50+
popd
51+
)
52+
53+
echo Bistro assets ready at: %TARGET_DIR%
54+
endlocal
55+
exit /b 0
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Fetch the Bistro example assets into the desired assets directory.
5+
# Default target: assets/bistro at the repository root.
6+
# Usage:
7+
# ./fetch_bistro_assets.sh [target-dir]
8+
# Example:
9+
# ./fetch_bistro_assets.sh # clones to assets/bistro
10+
11+
REPO_SSH="[email protected]:gpx1000/bistro.git"
12+
REPO_HTTPS="https://github.com/gpx1000/bistro.git"
13+
TARGET_DIR="${1:-assets/bistro}"
14+
15+
mkdir -p "$(dirname "${TARGET_DIR}")"
16+
17+
# If directory exists and is a git repo, update it; otherwise clone it.
18+
if [ -d "${TARGET_DIR}/.git" ]; then
19+
echo "Updating existing bistro assets in ${TARGET_DIR}"
20+
git -C "${TARGET_DIR}" pull --ff-only
21+
else
22+
echo "Cloning bistro assets into ${TARGET_DIR}"
23+
# Try SSH first; if it fails (e.g., no SSH key), fall back to HTTPS.
24+
if git clone --depth 1 "${REPO_SSH}" "${TARGET_DIR}" 2>/dev/null; then
25+
:
26+
else
27+
echo "SSH clone failed, trying HTTPS"
28+
git clone --depth 1 "${REPO_HTTPS}" "${TARGET_DIR}"
29+
fi
30+
fi
31+
32+
# If git-lfs is available, ensure LFS content is pulled
33+
if command -v git >/dev/null 2>&1 && git -C "${TARGET_DIR}" lfs version >/dev/null 2>&1; then
34+
git -C "${TARGET_DIR}" lfs install --local >/dev/null 2>&1 || true
35+
git -C "${TARGET_DIR}" lfs pull || true
36+
fi
37+
38+
echo "Bistro assets ready at: ${TARGET_DIR}"

attachments/simple_engine/install_dependencies_linux.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ install_ubuntu_debian() {
5151
libglfw3-dev \
5252
libglm-dev \
5353
libopenal-dev \
54-
libktx-dev \
55-
libstb-dev
54+
libktx-dev
5655

5756
# Install Slang compiler (for shader compilation)
5857
echo "Installing Slang compiler..."
@@ -84,7 +83,7 @@ install_fedora_rhel() {
8483
openal-soft-devel
8584

8685
# Note: Some packages might need to be built from source on RHEL/CentOS
87-
echo "Note: Some dependencies (libktx, libstb, tinygltf) may need to be built from source"
86+
echo "Note: Some dependencies (libktx, tinygltf) may need to be built from source"
8887
echo "Please refer to the project documentation for manual installation instructions"
8988
}
9089

@@ -107,9 +106,9 @@ install_arch() {
107106

108107
# Install AUR packages (requires yay or another AUR helper)
109108
if command -v yay &> /dev/null; then
110-
yay -S --noconfirm libktx stb
109+
yay -S --noconfirm libktx
111110
else
112-
echo "Note: Please install yay or another AUR helper to install libktx and stb packages"
111+
echo "Note: Please install yay or another AUR helper to install libktx packages"
113112
echo "Alternatively, build these dependencies from source"
114113
fi
115114
}

attachments/simple_engine/install_dependencies_windows.bat

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,6 @@ if %errorLevel% neq 0 (
132132
echo Warning: Failed to install KTX via vcpkg
133133
)
134134

135-
REM Install STB
136-
echo Installing STB...
137-
vcpkg install stb:x64-windows
138-
if %errorLevel% neq 0 (
139-
echo Warning: Failed to install STB via vcpkg
140-
)
141-
142135
REM Install tinygltf
143136
echo Installing tinygltf...
144137
vcpkg install tinygltf:x64-windows

attachments/simple_engine/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void SetupScene(Engine* engine) {
3838
LoadGLTFModel(engine, "../Assets/bistro/bistro.gltf");
3939
}
4040

41-
#if PLATFORM_ANDROID
41+
#if defined(PLATFORM_ANDROID)
4242
/**
4343
* @brief Android entry point.
4444
* @param app The Android app.

attachments/simple_engine/memory_pool.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ bool MemoryPool::initialize() {
3737
);
3838

3939
// Uniform buffer pool: Small allocations, host-visible
40+
// Use 64-byte alignment to match nonCoherentAtomSize and prevent validation errors
4041
configurePool(
4142
PoolType::UNIFORM_BUFFER,
4243
4 * 1024 * 1024, // 4MB blocks
43-
256, // 256B allocation units
44+
64, // 64B allocation units (aligned to nonCoherentAtomSize)
4445
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
4546
4 // Max 4 blocks (16MB total)
4647
);
4748

4849
// Staging buffer pool: Variable allocations, host-visible
50+
// Use 64-byte alignment to match nonCoherentAtomSize and prevent validation errors
4951
configurePool(
5052
PoolType::STAGING_BUFFER,
5153
16 * 1024 * 1024, // 16MB blocks
52-
1024, // 1KB allocation units
54+
64, // 64B allocation units (aligned to nonCoherentAtomSize)
5355
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
5456
4 // Max 4 blocks (64MB total)
5557
);
@@ -311,14 +313,16 @@ std::pair<vk::raii::Buffer, std::unique_ptr<MemoryPool::Allocation>> MemoryPool:
311313

312314
// Determine a pool type based on usage and properties
313315
PoolType poolType = PoolType::VERTEX_BUFFER;
314-
if (usage & vk::BufferUsageFlagBits::eVertexBuffer) {
316+
317+
// Check for host-visible requirements first (for instance buffers and staging)
318+
if (properties & vk::MemoryPropertyFlagBits::eHostVisible) {
319+
poolType = PoolType::STAGING_BUFFER;
320+
} else if (usage & vk::BufferUsageFlagBits::eVertexBuffer) {
315321
poolType = PoolType::VERTEX_BUFFER;
316322
} else if (usage & vk::BufferUsageFlagBits::eIndexBuffer) {
317323
poolType = PoolType::INDEX_BUFFER;
318324
} else if (usage & vk::BufferUsageFlagBits::eUniformBuffer) {
319325
poolType = PoolType::UNIFORM_BUFFER;
320-
} else if (properties & vk::MemoryPropertyFlagBits::eHostVisible) {
321-
poolType = PoolType::STAGING_BUFFER;
322326
}
323327

324328
// Create the buffer

0 commit comments

Comments
 (0)