Skip to content

Commit 5edca50

Browse files
committed
GITechDemo:
*Fixed a bug where the WM_QUIT message would sometimes not be processed, causing the process to remain running in the background after the window was closed *Made the HUD texture triple buffered so as to minimize stalls (why was it ever single buffered if it's updated on the CPU?!?)
1 parent 68fcb7c commit 5edca50

File tree

3 files changed

+64
-39
lines changed

3 files changed

+64
-39
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ int FrameworkWin::Run()
178178

179179
if (pInputMgr && bAppRdy)
180180
pInputMgr->HandleMessage(msg);
181+
182+
// If the message is WM_QUIT, exit the while loop
183+
if (msg.message == WM_QUIT)
184+
bExit = true;
181185
}
182186

183187
// Update input
@@ -192,10 +196,6 @@ int FrameworkWin::Run()
192196
pInputMgr->Update();
193197
}
194198

195-
// If the message is WM_QUIT, exit the while loop
196-
if (msg.message == WM_QUIT)
197-
break;
198-
199199
if (bAppRdy)
200200
{
201201
// Calculate delta time and update app

GITechDemo/Code/AppMain/GITechDemo/RenderScheme/HUDPass.cpp

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ namespace GITechDemoApp
4747

4848
HUDPass::HUDPass(const char* const passName, RenderPass* const parentPass)
4949
: RenderPass(passName, parentPass)
50-
, m_pHUDTexture(nullptr)
51-
, m_nHUDTextureIdx(-1)
50+
, m_nCurrUpdateTexIdx(0)
51+
, m_nCurrRenderTexIdx(0)
5252
{
53+
assert(HUD_TEXTURE_BUFFER_COUNT > 0);
54+
55+
for (unsigned int idx = 0; idx < HUD_TEXTURE_BUFFER_COUNT; idx++)
56+
{
57+
m_pHUDTexture[idx] = nullptr;
58+
m_nHUDTextureIdx[idx] = -1;
59+
}
60+
5361
FT_Library fontLibrary = nullptr;
5462
FT_Face face = nullptr;
5563
FT_GlyphSlot slot = nullptr;
@@ -115,25 +123,33 @@ void HUDPass::Update(const float fDeltaTime)
115123
if (!ResourceMgr)
116124
return;
117125

118-
if (!m_pHUDTexture ||
119-
RenderContext->GetDisplayResolution()[0] * HUD_TEX_WIDTH_RATIO != m_pHUDTexture->GetWidth() ||
120-
RenderContext->GetDisplayResolution()[1] * HUD_TEX_HEIGHT_RATIO != m_pHUDTexture->GetHeight())
126+
for (unsigned int idx = 0; idx < HUD_TEXTURE_BUFFER_COUNT; idx++)
121127
{
122-
if (m_pHUDTexture)
128+
if (!m_pHUDTexture[idx] ||
129+
RenderContext->GetDisplayResolution()[0] * HUD_TEX_WIDTH_RATIO != m_pHUDTexture[idx]->GetWidth() ||
130+
RenderContext->GetDisplayResolution()[1] * HUD_TEX_HEIGHT_RATIO != m_pHUDTexture[idx]->GetHeight())
123131
{
124-
ResourceMgr->ReleaseTexture(m_nHUDTextureIdx);
125-
m_pHUDTexture = nullptr;
126-
}
132+
if (m_pHUDTexture[idx])
133+
{
134+
ResourceMgr->ReleaseTexture(m_nHUDTextureIdx[idx]);
135+
m_pHUDTexture[idx] = nullptr;
136+
}
127137

128-
m_nHUDTextureIdx = ResourceMgr->CreateTexture(PF_L8, TT_2D,
129-
(unsigned int)(RenderContext->GetDisplayResolution()[0] * HUD_TEX_WIDTH_RATIO),
130-
(unsigned int)(RenderContext->GetDisplayResolution()[1] * HUD_TEX_HEIGHT_RATIO),
131-
1, 1, BU_DYNAMIC);
132-
m_pHUDTexture = ResourceMgr->GetTexture(m_nHUDTextureIdx);
138+
m_nHUDTextureIdx[idx] = ResourceMgr->CreateTexture(PF_L8, TT_2D,
139+
(unsigned int)(RenderContext->GetDisplayResolution()[0] * HUD_TEX_WIDTH_RATIO),
140+
(unsigned int)(RenderContext->GetDisplayResolution()[1] * HUD_TEX_HEIGHT_RATIO),
141+
1, 1, BU_DYNAMIC);
142+
m_pHUDTexture[idx] = ResourceMgr->GetTexture(m_nHUDTextureIdx[idx]);
143+
}
133144
}
134145

135-
f2HalfTexelOffset = Vec2f(0.5f / m_pHUDTexture->GetWidth(), 0.5f / m_pHUDTexture->GetHeight());
136-
texSource = m_nHUDTextureIdx;
146+
// Calculate update and render texture indices
147+
//m_nCurrRenderTexIdx = m_nCurrUpdateTexIdx;
148+
m_nCurrUpdateTexIdx = (m_nCurrUpdateTexIdx + 1) % HUD_TEXTURE_BUFFER_COUNT;
149+
m_nCurrRenderTexIdx = (m_nCurrUpdateTexIdx + 1) % HUD_TEXTURE_BUFFER_COUNT;
150+
151+
f2HalfTexelOffset = Vec2f(0.5f / m_pHUDTexture[m_nCurrRenderTexIdx]->GetWidth(), 0.5f / m_pHUDTexture[m_nCurrRenderTexIdx]->GetHeight());
152+
texSource = m_nHUDTextureIdx[m_nCurrRenderTexIdx];
137153
}
138154

139155
void HUDPass::Draw()
@@ -149,17 +165,17 @@ void HUDPass::Draw()
149165
if (m_szTextBuf.length() == 0)
150166
return;
151167

152-
if (m_pHUDTexture->Lock(0, BL_WRITE_ONLY))
168+
if (m_pHUDTexture[m_nCurrUpdateTexIdx]->Lock(0, BL_WRITE_ONLY))
153169
{
154-
memset(m_pHUDTexture->GetMipData(0), 0, m_pHUDTexture->GetMipSizeBytes(0));
170+
memset(m_pHUDTexture[m_nCurrUpdateTexIdx]->GetMipData(0), 0, m_pHUDTexture[m_nCurrUpdateTexIdx]->GetMipSizeBytes(0));
155171

156172
int pen_x = HUD_TEXT_LEFT_MARGIN;
157173
int pen_y = HUD_TEXT_TOP_MARGIN;
158174

159175
for (unsigned int n = 0; n < m_szTextBuf.length(); n++)
160176
{
161177
char ch = m_szTextBuf[n];
162-
if (ch == '\n' || (unsigned int)(pen_x + m_pGlyphCache[ch].left + m_pGlyphCache[ch].width) > m_pHUDTexture->GetWidth())
178+
if (ch == '\n' || (unsigned int)(pen_x + m_pGlyphCache[ch].left + m_pGlyphCache[ch].width) > m_pHUDTexture[m_nCurrUpdateTexIdx]->GetWidth())
163179
{
164180
pen_x = HUD_TEXT_LEFT_MARGIN;
165181
pen_y += m_pGlyphCache[ch].advance_y >> 6;
@@ -177,12 +193,12 @@ void HUDPass::Draw()
177193
for (int j = y, q = 0; j < y_max; j++, q++)
178194
{
179195
if (x < 0 || j < 0 ||
180-
x_max >= (FT_Int)m_pHUDTexture->GetWidth() ||
181-
j >= (FT_Int)m_pHUDTexture->GetHeight())
196+
x_max >= (FT_Int)m_pHUDTexture[m_nCurrUpdateTexIdx]->GetWidth() ||
197+
j >= (FT_Int)m_pHUDTexture[m_nCurrUpdateTexIdx]->GetHeight())
182198
continue;
183199

184200
memcpy(
185-
m_pHUDTexture->GetMipData(0) + j * m_pHUDTexture->GetWidth() + x,
201+
m_pHUDTexture[m_nCurrUpdateTexIdx]->GetMipData(0) + j * m_pHUDTexture[m_nCurrUpdateTexIdx]->GetWidth() + x,
186202
m_pGlyphCache[ch].buffer + q * m_pGlyphCache[ch].width,
187203
m_pGlyphCache[ch].width
188204
);
@@ -192,8 +208,8 @@ void HUDPass::Draw()
192208
pen_x += m_pGlyphCache[ch].advance_x >> 6;
193209
}
194210

195-
m_pHUDTexture->Update();
196-
m_pHUDTexture->Unlock();
211+
m_pHUDTexture[m_nCurrUpdateTexIdx]->Update();
212+
m_pHUDTexture[m_nCurrUpdateTexIdx]->Unlock();
197213
}
198214

199215
const bool sRGBEnabled = RSMgr->GetSRGBWriteEnabled();
@@ -208,8 +224,8 @@ void HUDPass::Draw()
208224
RSMgr->SetZFunc(CMP_ALWAYS);
209225
RSMgr->SetScissorEnabled(true);
210226

211-
RenderContext->SetViewport(Vec2i(m_pHUDTexture->GetWidth(), m_pHUDTexture->GetHeight()));
212-
RSMgr->SetScissor(Vec2i(m_pHUDTexture->GetWidth(), m_pHUDTexture->GetHeight()));
227+
RenderContext->SetViewport(Vec2i(m_pHUDTexture[m_nCurrRenderTexIdx]->GetWidth(), m_pHUDTexture[m_nCurrRenderTexIdx]->GetHeight()));
228+
RSMgr->SetScissor(Vec2i(m_pHUDTexture[m_nCurrRenderTexIdx]->GetWidth(), m_pHUDTexture[m_nCurrRenderTexIdx]->GetHeight()));
213229

214230
HUDTextShader.Enable();
215231
RenderContext->DrawVertexBuffer(FullScreenTri);
@@ -229,13 +245,17 @@ void HUDPass::Draw()
229245
void HUDPass::ReleaseHUDTexture()
230246
{
231247
Renderer* RenderContext = Renderer::GetInstance();
232-
if (RenderContext)
248+
249+
for (unsigned int idx = 0; idx < HUD_TEXTURE_BUFFER_COUNT; idx++)
233250
{
234-
ResourceManager* ResourceMgr = RenderContext->GetResourceManager();
235-
if (ResourceMgr && m_nHUDTextureIdx != ~0u)
236-
ResourceMgr->ReleaseTexture(m_nHUDTextureIdx);
237-
}
251+
if (RenderContext)
252+
{
253+
ResourceManager* ResourceMgr = RenderContext->GetResourceManager();
254+
if (ResourceMgr && m_nHUDTextureIdx[idx] != ~0u)
255+
ResourceMgr->ReleaseTexture(m_nHUDTextureIdx[idx]);
256+
}
238257

239-
m_nHUDTextureIdx = ~0u;
240-
m_pHUDTexture = nullptr;
258+
m_nHUDTextureIdx[idx] = ~0u;
259+
m_pHUDTexture[idx] = nullptr;
260+
}
241261
}

GITechDemo/Code/AppMain/GITechDemo/RenderScheme/HUDPass.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ namespace Synesthesia3D
3030
class Texture;
3131
}
3232

33+
// Use triple buffering to minimize stalls (one for the application update loop, one for the driver thread and one for the GPU)
34+
#define HUD_TEXTURE_BUFFER_COUNT (3)
35+
3336
namespace GITechDemoApp
3437
{
3538
class HUDPass : public RenderPass
@@ -55,8 +58,10 @@ namespace GITechDemoApp
5558

5659
std::string m_szTextBuf;
5760

58-
Synesthesia3D::Texture* m_pHUDTexture;
59-
unsigned int m_nHUDTextureIdx;
61+
Synesthesia3D::Texture* m_pHUDTexture[HUD_TEXTURE_BUFFER_COUNT];
62+
unsigned int m_nHUDTextureIdx[HUD_TEXTURE_BUFFER_COUNT];
63+
unsigned int m_nCurrUpdateTexIdx;
64+
unsigned int m_nCurrRenderTexIdx;
6065

6166
public:
6267
void Clear() { m_szTextBuf.clear(); }

0 commit comments

Comments
 (0)