textures almost work?
This commit is contained in:
4
src/dependency/.ignore
Normal file
4
src/dependency/.ignore
Normal file
@@ -0,0 +1,4 @@
|
||||
SDL/*
|
||||
# !SDL/src
|
||||
# bgfx.cmake/*
|
||||
tinygltf/*
|
||||
@@ -6,7 +6,10 @@ struct SharedWindowData
|
||||
void* Handle = nullptr;
|
||||
int32_t WindowWidth = 1920;
|
||||
int32_t WindowHeight = 1080;
|
||||
bool HeldScanCodes[512]{0};
|
||||
bool HeldScanCodes[512]{false};
|
||||
bool LastHeldScanCodes[512]{false};
|
||||
bool HeldMouseButtons[8]{false};
|
||||
bool LastHeldMouseButtons[8]{false};
|
||||
float MouseDeltaX = 0.0f;
|
||||
float MouseDeltaY = 0.0f;
|
||||
};
|
||||
@@ -29,6 +32,9 @@ struct GameData
|
||||
|
||||
void* EntityStorage = nullptr;
|
||||
uint64_t EntityStorageSize = 0;
|
||||
|
||||
void* TransientStorage = nullptr;
|
||||
uint64_t TransientStorageSize = 0;
|
||||
};
|
||||
|
||||
struct SharedData
|
||||
|
||||
@@ -61,6 +61,16 @@ void EngineWindow::Update(SharedWindowData& shared)
|
||||
shared.MouseDeltaY += evt.motion.yrel;
|
||||
break;
|
||||
}
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
{
|
||||
shared.HeldMouseButtons[evt.button.button] = true;
|
||||
break;
|
||||
}
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
{
|
||||
shared.HeldMouseButtons[evt.button.button] = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
#include <cstdio>
|
||||
#include <bx/string.h>
|
||||
#include <bx/mpscqueue.h>
|
||||
#include <bx/allocator.h>
|
||||
#include <bx/mpscqueue.h>
|
||||
#include <bx/string.h>
|
||||
#include <cstdio>
|
||||
|
||||
#include "Shared.h"
|
||||
#include "Window.h"
|
||||
@@ -74,7 +74,7 @@ namespace
|
||||
Startup StartupFunc;
|
||||
Update UpdateFunc;
|
||||
Shutdown ShutdownFunc;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t* compName = nullptr)
|
||||
{
|
||||
@@ -83,13 +83,21 @@ void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t*
|
||||
uint8_t fileChangeBuffer[1024]{0};
|
||||
bx::memSet(fileChangeBuffer, 0, sizeof(fileChangeBuffer));
|
||||
DWORD bytesReturned = 0;
|
||||
if (ReadDirectoryChangesW(dirHandle, fileChangeBuffer, sizeof(fileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL))
|
||||
if (ReadDirectoryChangesW(dirHandle,
|
||||
fileChangeBuffer,
|
||||
sizeof(fileChangeBuffer),
|
||||
true,
|
||||
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE,
|
||||
&bytesReturned,
|
||||
NULL,
|
||||
NULL))
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
while (true)
|
||||
{
|
||||
FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&fileChangeBuffer[offset]);
|
||||
if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME)
|
||||
if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED ||
|
||||
notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME)
|
||||
{
|
||||
if (compName == nullptr || compName[0] == 0 || wcscmp(notifyData->FileName, compName) == 0)
|
||||
{
|
||||
@@ -247,20 +255,31 @@ int main()
|
||||
DevData.FileWatcher.CompiledShaderWatcher.ChangeType = FileChangeType::CompiledShader;
|
||||
|
||||
bx::strCopy(DevData.FileWatcher.DLLWatcher.DirPath, sizeof(DevData.FileWatcher.DLLWatcher.DirPath), "cmake-build");
|
||||
bx::strCopy(DevData.FileWatcher.ShaderWatcher.DirPath, sizeof(DevData.FileWatcher.ShaderWatcher.DirPath), "game/shaders");
|
||||
bx::strCopy(DevData.FileWatcher.CompiledShaderWatcher.DirPath, sizeof(DevData.FileWatcher.CompiledShaderWatcher.DirPath), "game/compiled-shaders/dx11");
|
||||
bx::strCopy(
|
||||
DevData.FileWatcher.ShaderWatcher.DirPath, sizeof(DevData.FileWatcher.ShaderWatcher.DirPath), "game/shaders");
|
||||
bx::strCopy(DevData.FileWatcher.CompiledShaderWatcher.DirPath,
|
||||
sizeof(DevData.FileWatcher.CompiledShaderWatcher.DirPath),
|
||||
"game/compiled-shaders/dx11");
|
||||
|
||||
wcscpy(DevData.FileWatcher.DLLWatcher.CompName, DLLWatch);
|
||||
|
||||
DWORD fileWatcherThreadId = 0;
|
||||
HANDLE dllThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.DLLWatcher, 0, &fileWatcherThreadId);
|
||||
HANDLE shaderThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.ShaderWatcher, 0, &fileWatcherThreadId);
|
||||
HANDLE compiledShaderThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId);
|
||||
HANDLE dllThread =
|
||||
CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.DLLWatcher, 0, &fileWatcherThreadId);
|
||||
HANDLE shaderThread =
|
||||
CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.ShaderWatcher, 0, &fileWatcherThreadId);
|
||||
HANDLE compiledShaderThread =
|
||||
CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId);
|
||||
|
||||
Shared.Game.PermanentStorageSize = 1024 * 1024;
|
||||
Shared.Game.PermanentStorage = VirtualAllocEx(GetCurrentProcess(), NULL, Shared.Game.PermanentStorageSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
||||
Shared.Game.PermanentStorage = VirtualAllocEx(
|
||||
GetCurrentProcess(), NULL, Shared.Game.PermanentStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
Shared.Game.EntityStorageSize = 1024 * 1024;
|
||||
Shared.Game.EntityStorage = VirtualAllocEx(GetCurrentProcess(), NULL, Shared.Game.EntityStorageSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
||||
Shared.Game.EntityStorage = VirtualAllocEx(
|
||||
GetCurrentProcess(), NULL, Shared.Game.EntityStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
Shared.Game.TransientStorageSize = 1024 * 1024 * 100;
|
||||
Shared.Game.TransientStorage = VirtualAllocEx(
|
||||
GetCurrentProcess(), NULL, Shared.Game.TransientStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
StartupFunc(Shared);
|
||||
|
||||
bool isRunning = true;
|
||||
@@ -275,7 +294,9 @@ int main()
|
||||
if (DevData.FileWatcher.DLLQueue.pop())
|
||||
{
|
||||
// Empty queue to avoid multiple reloads
|
||||
while (DevData.FileWatcher.DLLQueue.pop()) {}
|
||||
while (DevData.FileWatcher.DLLQueue.pop())
|
||||
{
|
||||
}
|
||||
ShutdownFunc();
|
||||
ReloadDLL();
|
||||
StartupFunc(Shared);
|
||||
@@ -286,7 +307,8 @@ int main()
|
||||
{
|
||||
if (Shared.Dev.ChangedShaderCount < BX_COUNTOF(Shared.Dev.ChangedShaders))
|
||||
{
|
||||
wcscpy(Shared.Dev.ChangedShaders[Shared.Dev.ChangedShaderCount].FileName, CompiledShaderChange->FileName);
|
||||
wcscpy(Shared.Dev.ChangedShaders[Shared.Dev.ChangedShaderCount].FileName,
|
||||
CompiledShaderChange->FileName);
|
||||
++Shared.Dev.ChangedShaderCount;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "../engine/Shared.h"
|
||||
#include "Global.h"
|
||||
#include "Instance.h"
|
||||
#include "bx/bx.h"
|
||||
#include "bx/math.h"
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -105,4 +108,15 @@ namespace Game
|
||||
{
|
||||
GameInst = &instance;
|
||||
}
|
||||
|
||||
void* AllocateScratch(size_t byteCount, size_t align = 16)
|
||||
{
|
||||
size_t offset = GetInstance().UsedScratchAmount;
|
||||
uint8_t* base = static_cast<uint8_t*>(GetShared().Game.TransientStorage);
|
||||
uint8_t* current = base + offset;
|
||||
uintptr_t ptrAligned = ((reinterpret_cast<uintptr_t>(current) + align - 1) / align) * align;
|
||||
uintptr_t newOffset = ptrAligned - reinterpret_cast<uintptr_t>(base) + byteCount;
|
||||
if (newOffset > GetShared().Game.TransientStorageSize) return nullptr;
|
||||
return reinterpret_cast<void*>(ptrAligned);
|
||||
}
|
||||
} // namespace Game
|
||||
|
||||
@@ -2,26 +2,48 @@
|
||||
|
||||
#include "bx/math.h"
|
||||
|
||||
struct Vec2
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
};
|
||||
|
||||
struct Vec3
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
};
|
||||
|
||||
struct Vec4
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
float w = 0.0f;
|
||||
};
|
||||
|
||||
struct Mat3
|
||||
{
|
||||
// clang-format off
|
||||
float M[9]{
|
||||
1.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
struct Mat4
|
||||
{
|
||||
// clang-format off
|
||||
float M[16]{
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0,
|
||||
};
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
struct Transform
|
||||
@@ -54,4 +76,5 @@ namespace Game
|
||||
void SetShared(SharedData& instance);
|
||||
GameInstance& GetInstance();
|
||||
void SetInstance(GameInstance& instance);
|
||||
void* AllocateScratch(size_t byteCount);
|
||||
} // namespace Game
|
||||
|
||||
@@ -8,4 +8,32 @@ namespace Game
|
||||
{
|
||||
return GetShared().Window.HeldScanCodes[(int32_t)key];
|
||||
}
|
||||
bool GetKeyPressedNow(ScanCode key)
|
||||
{
|
||||
auto& win = GetShared().Window;
|
||||
return win.HeldScanCodes[(int32_t)key] && !win.LastHeldScanCodes[(int32_t)key];
|
||||
}
|
||||
bool GetKeyReleasedNow(ScanCode key)
|
||||
{
|
||||
auto& win = GetShared().Window;
|
||||
return !win.HeldScanCodes[(int32_t)key] && win.LastHeldScanCodes[(int32_t)key];
|
||||
}
|
||||
bool GetMouseButton(MouseButton button)
|
||||
{
|
||||
return GetShared().Window.HeldMouseButtons[(int32_t)button];
|
||||
}
|
||||
bool GetMouseButtonPressedNow(MouseButton button)
|
||||
{
|
||||
auto& win = GetShared().Window;
|
||||
return win.HeldMouseButtons[(int32_t)button] && !win.LastHeldMouseButtons[(int32_t)button];
|
||||
}
|
||||
bool GetMouseButtonReleasedNow(MouseButton button)
|
||||
{
|
||||
auto& win = GetShared().Window;
|
||||
return !win.HeldMouseButtons[(int32_t)button] && win.LastHeldMouseButtons[(int32_t)button];
|
||||
}
|
||||
Vec2 GetMouseMovement()
|
||||
{
|
||||
return {GetShared().Window.MouseDeltaX, GetShared().Window.MouseDeltaY};
|
||||
}
|
||||
} // namespace Game
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
#pragma once
|
||||
#include "Global.h"
|
||||
|
||||
enum class MouseButton
|
||||
{
|
||||
Left = 1,
|
||||
Middle = 2,
|
||||
Right = 3,
|
||||
Four = 4,
|
||||
Five = 5,
|
||||
};
|
||||
|
||||
enum class ScanCode
|
||||
{
|
||||
@@ -378,4 +388,10 @@ enum class ScanCode
|
||||
namespace Game
|
||||
{
|
||||
bool GetKey(ScanCode key);
|
||||
}
|
||||
bool GetKeyPressedNow(ScanCode key);
|
||||
bool GetKeyReleasedNow(ScanCode key);
|
||||
bool GetMouseButton(MouseButton button);
|
||||
bool GetMouseButtonPressedNow(MouseButton button);
|
||||
bool GetMouseButtonReleasedNow(MouseButton button);
|
||||
Vec2 GetMouseMovement();
|
||||
} // namespace Game
|
||||
|
||||
@@ -5,14 +5,37 @@
|
||||
|
||||
namespace Game
|
||||
{
|
||||
enum class PlayerMode
|
||||
{
|
||||
Walk,
|
||||
Freefly,
|
||||
};
|
||||
|
||||
struct Time
|
||||
{
|
||||
double Now = 0.0;
|
||||
double Delta = 0.0;
|
||||
uint32_t FrameCounter = 0;
|
||||
int64_t StartTime = 0;
|
||||
};
|
||||
|
||||
struct PlayerData
|
||||
{
|
||||
Transform FreeflyCamTransform;
|
||||
float FreeflyXRot = 0.0f;
|
||||
float FreeflyYRot = 0.0f;
|
||||
Transform PlayerTransform;
|
||||
Transform PlayerCamTransform;
|
||||
PlayerMode Mode = PlayerMode::Freefly;
|
||||
};
|
||||
|
||||
struct GameInstance
|
||||
{
|
||||
bool IsInitialized = false;
|
||||
uint64_t Size = sizeof(GameInstance);
|
||||
uint32_t FrameCounter = 0;
|
||||
int64_t StartTime = 0;
|
||||
double Now = 0.0;
|
||||
double Delta = 0.0;
|
||||
uint8_t UsedScratchAmount = 0;
|
||||
Time Time;
|
||||
PlayerData Player;
|
||||
Level GameLevel;
|
||||
};
|
||||
} // namespace Game
|
||||
|
||||
@@ -27,7 +27,8 @@ namespace Game
|
||||
bgfx::setState(currentMaterial.State);
|
||||
|
||||
float TimeValues[4]{0.0f};
|
||||
TimeValues[0] = GetInstance().Now;
|
||||
TimeValues[0] = GetInstance().Time.Now;
|
||||
bgfx::setTexture(0, currentMaterial.Textures[0].SamplerHandle, currentMaterial.Textures[0].Handle);
|
||||
bgfx::setUniform(currentMaterial.Uniforms[Material::UTime], TimeValues);
|
||||
bgfx::setUniform(currentMaterial.Uniforms[Material::UDotColor], TestColor);
|
||||
|
||||
@@ -66,13 +67,40 @@ namespace Game
|
||||
|
||||
void Level::Update()
|
||||
{
|
||||
if (GetKey(ScanCode::R))
|
||||
PlayerData& player = GetInstance().Player;
|
||||
if (GetKeyPressedNow(ScanCode::R))
|
||||
{
|
||||
Cubes.Count = 0;
|
||||
Tests.Count = 0;
|
||||
Setup(GetShared().Game);
|
||||
}
|
||||
|
||||
float delta = GetInstance().Time.Delta;
|
||||
constexpr float moveSpeed = 10.0f;
|
||||
constexpr float rotSpeed = 0.6f;
|
||||
|
||||
float forwardInput = (GetKey(ScanCode::W) ? 1.0f : 0.0f) + (GetKey(ScanCode::S) ? -1.0f : 0.0f);
|
||||
float rightInput = (GetKey(ScanCode::D) ? 1.0f : 0.0f) + (GetKey(ScanCode::A) ? -1.0f : 0.0f);
|
||||
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
||||
moveInput = bx::normalize(moveInput);
|
||||
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
||||
|
||||
bx::Vec3 camForward = player.FreeflyCamTransform.Forward();
|
||||
bx::Vec3 camRight = player.FreeflyCamTransform.Right();
|
||||
|
||||
if (GetMouseButton(MouseButton::Left))
|
||||
{
|
||||
Vec2 mouseMovement = GetMouseMovement();
|
||||
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f};
|
||||
player.FreeflyXRot += rotInput.x;
|
||||
player.FreeflyYRot += rotInput.y;
|
||||
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot);
|
||||
player.FreeflyCamTransform.RotateLocal({player.FreeflyXRot, 0.0f, 0.0f});
|
||||
}
|
||||
|
||||
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||
|
||||
Cubes.Update();
|
||||
Tests.Update();
|
||||
}
|
||||
@@ -87,7 +115,7 @@ namespace Game
|
||||
{
|
||||
if (TestX >= 0 && TestY >= 0)
|
||||
{
|
||||
double globalTime = GetInstance().Now;
|
||||
double globalTime = GetInstance().Time.Now;
|
||||
double time = TestY <= 5 ? globalTime * 1.0f : 0.0f;
|
||||
float scale = 1.0f + TestX * 0.4f;
|
||||
EData.Transform.Position = bx::Vec3{TestX * 2.0f, TestY * 2.0f, 0.0f};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Instance.h"
|
||||
#include "Log.h"
|
||||
#include "Setup.h"
|
||||
#include "bx/bx.h"
|
||||
#include "bx/timer.h"
|
||||
#include "rendering/Rendering.h"
|
||||
|
||||
@@ -52,13 +53,18 @@ namespace Game
|
||||
|
||||
void Update()
|
||||
{
|
||||
++GetInstance().FrameCounter;
|
||||
double newNow = (bx::getHPCounter() - GetInstance().StartTime) / (double)(bx::getHPFrequency());
|
||||
GetInstance().Delta = newNow - GetInstance().Now;
|
||||
GetInstance().Now = newNow;
|
||||
++GetInstance().Time.FrameCounter;
|
||||
double newNow = (bx::getHPCounter() - GetInstance().Time.StartTime) / (double)(bx::getHPFrequency());
|
||||
GetInstance().Time.Delta = newNow - GetInstance().Time.Now;
|
||||
GetInstance().Time.Now = newNow;
|
||||
|
||||
SetupInstance.Rendering.Update();
|
||||
GetShared().Window.MouseDeltaX = 0.0f;
|
||||
GetShared().Window.MouseDeltaY = 0.0f;
|
||||
|
||||
auto& win = GetShared().Window;
|
||||
win.MouseDeltaX = 0.0f;
|
||||
win.MouseDeltaY = 0.0f;
|
||||
bx::memCopy(win.LastHeldScanCodes, win.HeldScanCodes, sizeof(win.HeldScanCodes));
|
||||
bx::memCopy(win.LastHeldMouseButtons, win.HeldMouseButtons, sizeof(win.HeldMouseButtons));
|
||||
}
|
||||
|
||||
void Shutdown()
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,15 +1,18 @@
|
||||
#include "../../engine/Shared.h"
|
||||
#include "../Global.h"
|
||||
#include "../Input.h"
|
||||
#include "../Instance.h"
|
||||
#include "../Log.h"
|
||||
#include "../Mesh.h"
|
||||
#include "Rendering.h"
|
||||
|
||||
#include "bgfx/defines.h"
|
||||
#include "bx/filepath.h"
|
||||
#include "bx/math.h"
|
||||
#include "bx/timer.h"
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <bimg/bimg.h>
|
||||
#include <bx/file.h>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
@@ -18,20 +21,34 @@ namespace Game
|
||||
{
|
||||
namespace
|
||||
{
|
||||
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
|
||||
const bgfx::Memory* loadFile(const char* path, bool appendZero = false, int32_t retryCount = 1)
|
||||
{
|
||||
if (bx::open(_reader, _filePath))
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < retryCount; ++i)
|
||||
{
|
||||
file = fopen(path, "rb");
|
||||
if (file == nullptr && i < retryCount - 1)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
break;
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
if (appendZero)
|
||||
{
|
||||
uint32_t size = (uint32_t)bx::getSize(_reader);
|
||||
const bgfx::Memory* mem = bgfx::alloc(size + 1);
|
||||
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
|
||||
bx::close(_reader);
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
Log("Failed to load %s.", _filePath.getCPtr());
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bgfx::ShaderHandle loadShader(const char* FILENAME)
|
||||
@@ -72,31 +89,98 @@ namespace Game
|
||||
bx::strCat(buffer, sizeof(buffer), ".bin");
|
||||
|
||||
Log("Loading shader at %s", buffer);
|
||||
const bgfx::Memory* mem = loadFile(buffer, true, 3);
|
||||
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < 3; ++i)
|
||||
if (mem == nullptr)
|
||||
{
|
||||
file = fopen(buffer, "rb");
|
||||
if (file == nullptr)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
break;
|
||||
Log("Failed to load shader %s", FILENAME);
|
||||
return {};
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
fclose(file);
|
||||
|
||||
return bgfx::createShader(mem);
|
||||
}
|
||||
|
||||
Log("Failed to load shader %s", FILENAME);
|
||||
return {};
|
||||
bgfx::TextureHandle loadTexture(const bx::FilePath& _filePath,
|
||||
uint64_t _flags,
|
||||
uint8_t _skip,
|
||||
bgfx::TextureInfo* _info,
|
||||
bimg::Orientation::Enum* _orientation)
|
||||
{
|
||||
BX_UNUSED(_skip);
|
||||
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
|
||||
bx::Error err;
|
||||
|
||||
const bgfx::Memory* data = loadFile(_filePath.getCPtr());
|
||||
if (data == nullptr)
|
||||
{
|
||||
Log("Failed to find image %s", _filePath.getCPtr());
|
||||
return handle;
|
||||
}
|
||||
bimg::ImageContainer imageContainer;
|
||||
if (!bimg::imageParse(imageContainer, data->data, data->size, &err))
|
||||
{
|
||||
Log("Failed to load image %s", _filePath.getCPtr());
|
||||
return handle;
|
||||
}
|
||||
if (NULL != _orientation)
|
||||
{
|
||||
*_orientation = imageContainer.m_orientation;
|
||||
}
|
||||
|
||||
const bgfx::Memory* mem = bgfx::makeRef(imageContainer.m_data, imageContainer.m_size);
|
||||
|
||||
if (NULL != _info)
|
||||
{
|
||||
bgfx::calcTextureSize(*_info,
|
||||
uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
uint16_t(imageContainer.m_depth),
|
||||
imageContainer.m_cubeMap,
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format));
|
||||
}
|
||||
|
||||
if (imageContainer.m_cubeMap)
|
||||
{
|
||||
handle = bgfx::createTextureCube(uint16_t(imageContainer.m_width),
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
else if (1 < imageContainer.m_depth)
|
||||
{
|
||||
handle = bgfx::createTexture3D(uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
uint16_t(imageContainer.m_depth),
|
||||
1 < imageContainer.m_numMips,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
else if (bgfx::isTextureValid(0,
|
||||
false,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags))
|
||||
{
|
||||
handle = bgfx::createTexture2D(uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
|
||||
if (bgfx::isValid(handle))
|
||||
{
|
||||
const bx::StringView name(_filePath);
|
||||
bgfx::setName(handle, name.getPtr(), name.getLength());
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -108,7 +192,6 @@ namespace Game
|
||||
bgfx::Init init;
|
||||
init.type = bgfx::RendererType::Direct3D12;
|
||||
init.debug = true;
|
||||
init.callback = &Callback;
|
||||
init.platformData.nwh = shared.Window.Handle;
|
||||
init.platformData.ndt = nullptr;
|
||||
init.platformData.type = bgfx::NativeWindowHandleType::Default;
|
||||
@@ -129,14 +212,18 @@ namespace Game
|
||||
bgfx::setDebug(BGFX_DEBUG_TEXT);
|
||||
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
|
||||
|
||||
DefaultSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
|
||||
Textures[0].Handle =
|
||||
loadTexture(bx::FilePath{"models/body.dds"}, BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE, 0, nullptr, nullptr);
|
||||
Textures[0].SamplerHandle = DefaultSampler;
|
||||
LoadMesh(Models[0], "models/cube.gltf");
|
||||
LoadMesh(Models[1], "models/zurg.gltf");
|
||||
|
||||
Materials[0] = Material::LoadFromShader("vert", "frag");
|
||||
Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle);
|
||||
|
||||
if (!GetInstance().IsInitialized)
|
||||
{
|
||||
GetInstance().StartTime = bx::getHPCounter();
|
||||
GetInstance().Time.StartTime = bx::getHPCounter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,30 +257,6 @@ namespace Game
|
||||
|
||||
{
|
||||
GetInstance().GameLevel.Update();
|
||||
|
||||
float delta = GetInstance().Delta;
|
||||
constexpr float moveSpeed = 10.0f;
|
||||
constexpr float rotSpeed = 0.6f;
|
||||
|
||||
float forwardInput = (GetKey(ScanCode::W) ? 1.0f : 0.0f) + (GetKey(ScanCode::S) ? -1.0f : 0.0f);
|
||||
float rightInput = (GetKey(ScanCode::D) ? 1.0f : 0.0f) + (GetKey(ScanCode::A) ? -1.0f : 0.0f);
|
||||
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
||||
moveInput = bx::normalize(moveInput);
|
||||
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
||||
|
||||
bx::Vec3 camForward = Cam.Transform.Forward();
|
||||
bx::Vec3 camRight = Cam.Transform.Right();
|
||||
|
||||
bx::Vec3 rotInput = {
|
||||
shared.Window.MouseDeltaY * delta * rotSpeed, shared.Window.MouseDeltaX * delta * rotSpeed, 0.0f};
|
||||
Cam.FreelookXRot += rotInput.x;
|
||||
Cam.FreelookYRot += rotInput.y;
|
||||
bx::mtxRotateY(Cam.Transform.Rotation.M, Cam.FreelookYRot);
|
||||
Cam.Transform.RotateLocal({Cam.FreelookXRot, 0.0f, 0.0f});
|
||||
|
||||
Cam.Transform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||
Cam.Transform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||
bgfx::dbgTextPrintf(1, 4, 0x0f, "Cam forward: %.2f %.2f %.2f", camForward.x, camForward.y, camForward.z);
|
||||
}
|
||||
|
||||
// Set view and projection matrix for view 0.
|
||||
@@ -205,8 +268,17 @@ namespace Game
|
||||
0.1f,
|
||||
1000.0f,
|
||||
bgfx::getCaps()->homogeneousDepth);
|
||||
Cam.Transform.UpdateMatrix();
|
||||
bgfx::setViewTransform(0, Cam.Transform.M.M, proj);
|
||||
|
||||
auto& player = GetInstance().Player;
|
||||
if (player.Mode == PlayerMode::Freefly)
|
||||
{
|
||||
player.FreeflyCamTransform.UpdateMatrix();
|
||||
bgfx::setViewTransform(0, player.FreeflyCamTransform.M.M, proj);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Set view 0 default viewport.
|
||||
bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
|
||||
@@ -215,9 +287,10 @@ namespace Game
|
||||
GetInstance().GameLevel.Cubes.Render(Models, Materials);
|
||||
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
||||
|
||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", GetInstance().Now);
|
||||
bgfx::dbgTextPrintf(1, 2, 0x0F, "Frame: %u", GetInstance().FrameCounter);
|
||||
bgfx::dbgTextPrintf(1, 3, 0x0F, "Delta: %.3fms / %.0ffps", GetInstance().Delta, 1.0 / GetInstance().Delta);
|
||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", GetInstance().Time.Now);
|
||||
bgfx::dbgTextPrintf(1, 2, 0x0F, "Frame: %u", GetInstance().Time.FrameCounter);
|
||||
bgfx::dbgTextPrintf(
|
||||
1, 3, 0x0F, "Delta: %.3fms / %.0ffps", GetInstance().Time.Delta, 1.0 / GetInstance().Time.Delta);
|
||||
|
||||
bgfx::frame();
|
||||
}
|
||||
@@ -227,10 +300,14 @@ namespace Game
|
||||
bgfx::shutdown();
|
||||
}
|
||||
|
||||
Material Material::LoadFromShader(const char* vertPath, const char* fragPath)
|
||||
Material Material::LoadFromShader(const char* vertPath,
|
||||
const char* fragPath,
|
||||
bgfx::TextureHandle tex,
|
||||
bgfx::UniformHandle sampler)
|
||||
{
|
||||
bgfx::ShaderHandle vertexShader = loadShader("vert");
|
||||
bgfx::ShaderHandle fragmentShader = loadShader("frag");
|
||||
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
|
||||
bgfx::ShaderHandle vertexShader = loadShader(vertPath);
|
||||
bgfx::ShaderHandle fragmentShader = loadShader(fragPath);
|
||||
|
||||
Material mat;
|
||||
mat.Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
|
||||
@@ -238,6 +315,8 @@ namespace Game
|
||||
BGFX_STATE_CULL_CCW | BGFX_STATE_MSAA;
|
||||
mat.Uniforms[Material::UTime] = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
|
||||
mat.Uniforms[Material::UDotColor] = bgfx::createUniform("u_testColor", bgfx::UniformType::Vec4);
|
||||
mat.Textures[0].Handle = tex;
|
||||
mat.Textures[0].SamplerHandle = sampler;
|
||||
return mat;
|
||||
}
|
||||
} // namespace Game
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#pragma once
|
||||
#include "../Global.h"
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <bx/string.h>
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Game
|
||||
{
|
||||
@@ -19,84 +18,10 @@ namespace Game
|
||||
float uv_y;
|
||||
};
|
||||
|
||||
struct BgfxCallback : public bgfx::CallbackI
|
||||
struct Texture
|
||||
{
|
||||
virtual ~BgfxCallback()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) override
|
||||
{
|
||||
// Something unexpected happened, inform user and bail out.
|
||||
printf("Fatal error: 0x%08x: %s", _code, _str);
|
||||
|
||||
// Must terminate, continuing will cause crash anyway.
|
||||
abort();
|
||||
}
|
||||
|
||||
virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
|
||||
{
|
||||
printf("%s (%d): ", _filePath, _line);
|
||||
vprintf(_format, _argList);
|
||||
}
|
||||
|
||||
virtual void profilerBegin(const char* /*_name*/,
|
||||
uint32_t /*_abgr*/,
|
||||
const char* /*_filePath*/,
|
||||
uint16_t /*_line*/) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void profilerBeginLiteral(const char* /*_name*/,
|
||||
uint32_t /*_abgr*/,
|
||||
const char* /*_filePath*/,
|
||||
uint16_t /*_line*/) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void profilerEnd() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual uint32_t cacheReadSize(uint64_t _id) override
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void screenShot(const char* _filePath,
|
||||
uint32_t _width,
|
||||
uint32_t _height,
|
||||
uint32_t _pitch,
|
||||
const void* _data,
|
||||
uint32_t /*_size*/,
|
||||
bool _yflip) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void captureBegin(uint32_t _width,
|
||||
uint32_t _height,
|
||||
uint32_t /*_pitch*/,
|
||||
bgfx::TextureFormat::Enum /*_format*/,
|
||||
bool _yflip) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void captureEnd() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void captureFrame(const void* _data, uint32_t /*_size*/) override
|
||||
{
|
||||
}
|
||||
bgfx::UniformHandle SamplerHandle;
|
||||
bgfx::TextureHandle Handle;
|
||||
};
|
||||
|
||||
struct Model
|
||||
@@ -116,24 +41,21 @@ namespace Game
|
||||
|
||||
bgfx::ProgramHandle Shader;
|
||||
bgfx::UniformHandle Uniforms[8];
|
||||
Texture Textures[4];
|
||||
uint64_t State = 0;
|
||||
static Material LoadFromShader(const char* vertPath, const char* fragPath);
|
||||
};
|
||||
|
||||
struct Camera
|
||||
{
|
||||
float FreelookXRot = 0.0f;
|
||||
float FreelookYRot = 0.0f;
|
||||
Transform Transform;
|
||||
static Material LoadFromShader(const char* vertPath,
|
||||
const char* fragPath,
|
||||
bgfx::TextureHandle = BGFX_INVALID_HANDLE,
|
||||
bgfx::UniformHandle sampler = BGFX_INVALID_HANDLE);
|
||||
};
|
||||
|
||||
class GameRendering
|
||||
{
|
||||
private:
|
||||
bgfx::UniformHandle DefaultSampler;
|
||||
Texture Textures[8];
|
||||
Material Materials[8];
|
||||
Model Models[8];
|
||||
BgfxCallback Callback;
|
||||
Camera Cam;
|
||||
|
||||
public:
|
||||
void Setup();
|
||||
|
||||
@@ -3,11 +3,12 @@ $input v_normal
|
||||
$input v_uv0
|
||||
$input v_wpos
|
||||
|
||||
#include "common.sh"
|
||||
|
||||
SAMPLER2D(s_texColor, 0);
|
||||
uniform vec4 u_time;
|
||||
uniform vec4 u_testColor;
|
||||
|
||||
#include "common.sh"
|
||||
|
||||
float circle(vec2 uv, float radius)
|
||||
{
|
||||
float distSq = uv.x * uv.x + uv.y * uv.y;
|
||||
@@ -86,4 +87,5 @@ void main()
|
||||
// gl_FragColor = brightness;
|
||||
// gl_FragColor = dither;
|
||||
// gl_FragColor = u_testColor;
|
||||
gl_FragColor = texture2D(s_texColor, v_uv0);
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ void main()
|
||||
v_uv0 = a_texcoord0;
|
||||
v_wpos = mul(u_model[0], vec4(a_position, 1.0)).xyz;
|
||||
|
||||
v_normal = normalize(mul((mat3)u_model[0], a_normal));
|
||||
v_normal = normalize(mul(mat3(u_model[0]), a_normal));
|
||||
}
|
||||
|
||||
4
src/setup-release.bat
Normal file
4
src/setup-release.bat
Normal file
@@ -0,0 +1,4 @@
|
||||
cd dependency/bgfx.cmake
|
||||
cmake -G "Ninja" -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="clang.exe" -DCMAKE_CXX_COMPILER="clang++.exe" -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF
|
||||
cd ..\..
|
||||
cmake -G "Ninja" -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="clang.exe" -DCMAKE_CXX_COMPILER="clang++.exe" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
||||
Reference in New Issue
Block a user