Skip to content

Commit 4da0cca

Browse files
committed
*Improved loading times
*Better resource loading parallelization *LibRenderer's ResourceManager is now thread-safe *Added light direction as a tunable artist parameter *Tuned depth bias and slope scaled depth bias for shadowmap
1 parent 9db78c0 commit 4da0cca

26 files changed

+979
-396
lines changed

GITechDemo/Code/AppMain/Framework/App.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using namespace gmtl;
1414
CLASS (); \
1515
~ CLASS (); \
1616
bool Init(void* hWnd); \
17+
void Release(); \
1718
void LoadResources(unsigned int thId, unsigned int thCount); \
1819
void Update(const float fDeltaTime); \
1920
void Draw();
@@ -32,6 +33,7 @@ namespace AppFramework
3233
virtual ~App() { if (m_pInputManager) delete m_pInputManager; }
3334

3435
virtual bool Init(void* hWnd) = 0;
36+
virtual void Release() = 0;
3537
virtual void LoadResources(unsigned int thId, unsigned int thCount) = 0;
3638
virtual void Update(const float fDeltaTime) = 0;
3739
virtual void Draw() = 0;

GITechDemo/Code/AppMain/Framework/Framework.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ namespace AppFramework
1919
virtual void GetClientArea(int& left, int& top, int& right, int& bottom) = 0;
2020
virtual void GetWindowArea(int& left, int& top, int& right, int& bottom) = 0;
2121

22+
virtual unsigned int GetTicks() = 0; // in microseconds
23+
virtual void Sleep(const unsigned int miliseconds) = 0;
24+
2225
protected:
23-
virtual unsigned int GetTicks() = 0;
2426
virtual float CalculateDeltaTime() = 0; // in miliseconds
2527

2628
static Framework* m_pInstance;

GITechDemo/Code/AppMain/Framework/Windows/FrameworkWin.cpp

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using namespace AppFramework;
1212
#include <fcntl.h>
1313
#include <mmsystem.h>
1414
#include <strsafe.h>
15+
using namespace std;
1516

1617
namespace AppFramework
1718
{
@@ -42,7 +43,22 @@ void FrameworkWin::Init(HINSTANCE& hInstance, int& nCmdShow)
4243

4344
int FrameworkWin::Run()
4445
{
45-
#ifdef _DEBUG
46+
// TODO: Place code here.
47+
MSG msg; memset(&msg, 0, sizeof(msg));
48+
HACCEL hAccelTable;
49+
50+
// Initialize global strings
51+
LoadString(m_hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
52+
LoadString(m_hInstance, IDC_FRAMEWORK, m_szWindowClass, MAX_LOADSTRING);
53+
MyRegisterClass(m_hInstance);
54+
55+
// Perform application initialization:
56+
if (!InitInstance(m_hInstance, m_nCmdShow))
57+
{
58+
return FALSE;
59+
}
60+
61+
#if (1) //_DEBUG
4662
// Allocate the console
4763
AllocConsole();
4864

@@ -60,21 +76,6 @@ int FrameworkWin::Run()
6076
*stdin = *hf_in;
6177
#endif
6278

63-
// TODO: Place code here.
64-
MSG msg; memset(&msg, 0, sizeof(msg));
65-
HACCEL hAccelTable;
66-
67-
// Initialize global strings
68-
LoadString(m_hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
69-
LoadString(m_hInstance, IDC_FRAMEWORK, m_szWindowClass, MAX_LOADSTRING);
70-
MyRegisterClass(m_hInstance);
71-
72-
// Perform application initialization:
73-
if (!InitInstance(m_hInstance, m_nCmdShow))
74-
{
75-
return FALSE;
76-
}
77-
7879
hAccelTable = LoadAccelerators(m_hInstance, MAKEINTRESOURCE(IDC_FRAMEWORK));
7980

8081
// Force display and system to be in a working state by resetting
@@ -85,13 +86,14 @@ int FrameworkWin::Run()
8586
bool bAppRdy = false; // App can be updated
8687
PHANDLE hThread = nullptr; // Array of thread handles
8788
unsigned int* nThArgs = nullptr; // Array of thread arguments
89+
unsigned int startLoadingTicks = 0; // Timestamp for timing resource loading
8890

8991
// Acquire system information
9092
SYSTEM_INFO sysinfo;
9193
GetSystemInfo(&sysinfo);
9294

93-
// Create one thread per CPU (minus the one for the UI)
94-
DWORD dwCPUCount = sysinfo.dwNumberOfProcessors - 1;
95+
// Create one thread per CPU
96+
DWORD dwCPUCount = sysinfo.dwNumberOfProcessors;
9597
if (dwCPUCount < 1)
9698
dwCPUCount = 1;
9799

@@ -104,6 +106,9 @@ int FrameworkWin::Run()
104106
// Initialize app
105107
if (AppMain->Init(m_hWnd))
106108
{
109+
cout << "Starting " << dwCPUCount << " threads for loading resources." << endl << endl;
110+
startLoadingTicks = GetTicks();
111+
107112
// Create some threads for loading data. Applications can decide how to schedule
108113
// their tasks based on provided thread index and total thread count.
109114
DWORD(WINAPI* pThread)(LPVOID) = &AppMainLoadResources_wrapper;
@@ -119,7 +124,6 @@ int FrameworkWin::Run()
119124
ErrorExit(TEXT("CreateThread()"));
120125
bExit = true;
121126
}
122-
/*
123127
else
124128
{
125129
// Set the loading threads' priorities to above normal
@@ -129,8 +133,10 @@ int FrameworkWin::Run()
129133
bExit = true;
130134
}
131135
}
132-
*/
133136
}
137+
138+
// Reduce this thread's priority since it isn't doing anything important
139+
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
134140
}
135141
else
136142
{
@@ -153,12 +159,12 @@ int FrameworkWin::Run()
153159
DispatchMessage(&msg);
154160
}
155161

156-
if (pInputMgr)
162+
if (pInputMgr && bAppRdy)
157163
pInputMgr->HandleMessage(msg);
158164
}
159165

160166
// Update input
161-
if (pInputMgr)
167+
if (pInputMgr && bAppRdy)
162168
{
163169
RECT rc;
164170
RECT wRect;
@@ -200,34 +206,61 @@ int FrameworkWin::Run()
200206
if (lpExitCode == STILL_ACTIVE)
201207
{
202208
bAppRdy = false;
203-
}
204-
else
205-
{
206-
CloseHandle(hThread[i]);
207-
hThread[i] = nullptr;
209+
break;
208210
}
209211
}
210212
else
211213
{
212214
ErrorExit(TEXT("GetExitCodeThread()"));
213215
bExit = true;
216+
break;
214217
}
215218
}
216219
}
220+
221+
// Restore thread priority if resources are loaded
222+
if (bAppRdy)
223+
{
224+
cout << endl << "Resources successfully loaded in " << (float)(GetTicks() - startLoadingTicks) / 1000000.f << " seconds." << endl;
225+
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
226+
#if !(_DEBUG)
227+
Sleep(2000);
228+
// Free the console
229+
FreeConsole();
230+
#endif
231+
BringWindowToTop(m_hWnd);
232+
SetFocus(m_hWnd);
233+
}
234+
235+
// Give some CPU time to other threads
236+
Sleep(1);
217237
}
218238
else
219239
bExit = true;
220240
}
221241
}
222242

243+
WaitForMultipleObjects(dwCPUCount, hThread, TRUE, INFINITE);
244+
for (unsigned int i = 0; i < dwCPUCount; i++)
245+
{
246+
if (hThread[i])
247+
CloseHandle(hThread[i]);
248+
hThread[i] = nullptr;
249+
}
250+
223251
// Reset execution state
224252
SetThreadExecutionState(ES_CONTINUOUS);
225253

254+
// Release app resources
255+
AppMain->Release();
256+
226257
// Release previously allocated memory
227258
delete[] nThArgs;
228259
delete[] hThread;
229260

230-
#ifdef _DEBUG
261+
DestroyWindow(m_hWnd);
262+
263+
#if _DEBUG
231264
// Free the console
232265
FreeConsole();
233266
#endif
@@ -452,7 +485,7 @@ unsigned int FrameworkWin::GetTicks()
452485
now = timeGetTime();
453486
}
454487

455-
return (now - m_nStartTicks);
488+
return (now - m_nStartTicks) * 1000;
456489
}
457490

458491
float FrameworkWin::CalculateDeltaTime()
@@ -464,6 +497,11 @@ float FrameworkWin::CalculateDeltaTime()
464497
return (float)deltaTicks / 1000.f;
465498
}
466499

500+
void FrameworkWin::Sleep(const unsigned int miliseconds)
501+
{
502+
::Sleep(miliseconds);
503+
}
504+
467505
void FrameworkWin::ErrorExit(LPTSTR lpszFunction)
468506
{
469507
// Retrieve the system error message for the last-error code

GITechDemo/Code/AppMain/Framework/Windows/FrameworkWin.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace AppFramework
2626
void GetClientArea(int& left, int& top, int& right, int& bottom);
2727
void GetWindowArea(int& left, int& top, int& right, int& bottom);
2828

29+
unsigned int GetTicks(); // in microseconds
30+
void Sleep(const unsigned int miliseconds);
31+
2932
private:
3033
// Windows specific stuff
3134
ATOM MyRegisterClass(HINSTANCE hInstance);
@@ -39,17 +42,16 @@ namespace AppFramework
3942
HWND m_hWnd;
4043
int m_nCmdShow;
4144

42-
// High resolution timer, adapted from SDL
43-
unsigned int GetTicks();
44-
float CalculateDeltaTime(); // in miliseconds
45+
float CalculateDeltaTime(); // in miliseconds
4546

4647
// Rendering pause
4748
void PauseRendering(const bool pauseEnable) { m_bPauseRendering = pauseEnable; }
4849
const bool IsRenderingPaused() { return m_bPauseRendering; }
4950

5051
// Error handling
5152
void ErrorExit(LPTSTR lpszFunction);
52-
53+
54+
// High resolution timer, adapted from SDL
5355
// The first (low-resolution) ticks value of the application
5456
unsigned int m_nStartTicks;
5557
bool m_bTicksStarted;

GITechDemo/Code/AppMain/GITechDemo.vcxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
<WarningLevel>Level3</WarningLevel>
125125
<Optimization>Disabled</Optimization>
126126
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
127+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
128128
</ClCompile>
129129
<Link>
130130
<SubSystem>Windows</SubSystem>
@@ -145,7 +145,7 @@
145145
<WarningLevel>Level3</WarningLevel>
146146
<Optimization>Disabled</Optimization>
147147
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
148-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
148+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
149149
</ClCompile>
150150
<Link>
151151
<SubSystem>Windows</SubSystem>
@@ -168,7 +168,7 @@
168168
<FunctionLevelLinking>true</FunctionLevelLinking>
169169
<IntrinsicFunctions>true</IntrinsicFunctions>
170170
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
171-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
171+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
172172
</ClCompile>
173173
<Link>
174174
<SubSystem>Windows</SubSystem>
@@ -191,7 +191,7 @@
191191
<FunctionLevelLinking>true</FunctionLevelLinking>
192192
<IntrinsicFunctions>true</IntrinsicFunctions>
193193
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
194-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
194+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
195195
</ClCompile>
196196
<Link>
197197
<SubSystem>Windows</SubSystem>
@@ -214,7 +214,7 @@
214214
<FunctionLevelLinking>true</FunctionLevelLinking>
215215
<IntrinsicFunctions>true</IntrinsicFunctions>
216216
<PreprocessorDefinitions>WIN32;_PROFILE;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
217-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
217+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
218218
</ClCompile>
219219
<Link>
220220
<SubSystem>Windows</SubSystem>
@@ -238,7 +238,7 @@
238238
<FunctionLevelLinking>true</FunctionLevelLinking>
239239
<IntrinsicFunctions>true</IntrinsicFunctions>
240240
<PreprocessorDefinitions>WIN32;_PROFILE;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
241-
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
241+
<AdditionalIncludeDirectories>$(SolutionDir)..\Libraries\LibRenderer\SDK\gmtl\include;$(SolutionDir)..\Libraries\LibRenderer\Common;$(SolutionDir)..\Libraries\LibRenderer;$(ProjectDir)Framework;$(ProjectDir)Framework\Windows;$(ProjectDir)GITechDemo;$(ProjectDir)GITechDemo\RenderScheme;$(ProjectDir)GITechDemo\Resources;$(ProjectDir)GITechDemo\Utilities;$(SolutionDir)..\Libraries\freetype-2.6\include;$(SolutionDir)..\Libraries\gainput-0.10.0-beta\include</AdditionalIncludeDirectories>
242242
</ClCompile>
243243
<Link>
244244
<SubSystem>Windows</SubSystem>

0 commit comments

Comments
 (0)