Skip to content

Commit 3ddf1ed

Browse files
authored
Added FrameTime. (#3521)
1 parent 9d63c39 commit 3ddf1ed

File tree

47 files changed

+608
-590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+608
-590
lines changed

examples/01-cubes/cubes.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class ExampleCubes : public entry::AppI
209209
// Create program from shaders.
210210
m_program = loadProgram("vs_cubes", "fs_cubes");
211211

212-
m_timeOffset = bx::getHPCounter();
212+
m_frameTime.reset();
213213

214214
imguiCreate();
215215
}
@@ -237,6 +237,9 @@ class ExampleCubes : public entry::AppI
237237
{
238238
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
239239
{
240+
m_frameTime.frame();
241+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
242+
240243
imguiBeginFrame(m_mouseState.m_mx
241244
, m_mouseState.m_my
242245
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -274,8 +277,6 @@ class ExampleCubes : public entry::AppI
274277

275278
imguiEndFrame();
276279

277-
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
278-
279280
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
280281
const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
281282

@@ -354,7 +355,9 @@ class ExampleCubes : public entry::AppI
354355
bgfx::VertexBufferHandle m_vbh;
355356
bgfx::IndexBufferHandle m_ibh[BX_COUNTOF(s_ptState)];
356357
bgfx::ProgramHandle m_program;
357-
int64_t m_timeOffset;
358+
359+
FrameTime m_frameTime;
360+
358361
int32_t m_pt;
359362

360363
bool m_r;

examples/02-metaballs/metaballs.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ class ExampleMetaballs : public entry::AppI
537537
m_program = bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */);
538538

539539
m_grid = new Grid[kMaxDims*kMaxDims*kMaxDims];
540-
m_timeOffset = bx::getHPCounter();
540+
m_frameTime.reset();
541541

542542
imguiCreate();
543543
}
@@ -564,6 +564,9 @@ class ExampleMetaballs : public entry::AppI
564564

565565
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
566566
{
567+
m_frameTime.frame();
568+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
569+
567570
imguiBeginFrame(m_mouseState.m_mx
568571
, m_mouseState.m_my
569572
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -606,14 +609,6 @@ class ExampleMetaballs : public entry::AppI
606609
// if no other draw calls are submitted to view 0.
607610
bgfx::touch(0);
608611

609-
int64_t now = bx::getHPCounter();
610-
static int64_t last = now;
611-
const int64_t frameTime = now - last;
612-
last = now;
613-
const double freq = double(bx::getHPFrequency() );
614-
const double toMs = 1000.0/freq;
615-
float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
616-
617612
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
618613
const bx::Vec3 eye = { 0.0f, 0.0f, -50.0f };
619614

@@ -632,9 +627,9 @@ class ExampleMetaballs : public entry::AppI
632627

633628
// Stats.
634629
uint32_t numVertices = 0;
635-
int64_t profUpdate = 0;
636-
int64_t profNormal = 0;
637-
int64_t profTriangulate = 0;
630+
bx::Ticks profUpdate = bx::InitZero;
631+
bx::Ticks profNormal = bx::InitZero;
632+
bx::Ticks profTriangulate = bx::InitZero;
638633

639634
// Allocate 32K vertices in transient vertex buffer.
640635
uint32_t maxVertices = (32<<10);
@@ -651,7 +646,7 @@ class ExampleMetaballs : public entry::AppI
651646
sphere[ii][3] = 1.0f/(3.0f + (bx::sin(time*(ii*0.13f) )*0.5f+0.5f)*0.9f );
652647
}
653648

654-
profUpdate = bx::getHPCounter();
649+
profUpdate = bx::getNow();
655650

656651
for (uint32_t zz = 0; zz < numDims; ++zz)
657652
{
@@ -685,9 +680,9 @@ class ExampleMetaballs : public entry::AppI
685680
}
686681
}
687682

688-
profUpdate = bx::getHPCounter() - profUpdate;
683+
profUpdate = bx::getNow() - profUpdate;
689684

690-
profNormal = bx::getHPCounter();
685+
profNormal = bx::getNow();
691686

692687
for (uint32_t zz = 1; zz < numDims-1; ++zz)
693688
{
@@ -712,9 +707,9 @@ class ExampleMetaballs : public entry::AppI
712707
}
713708
}
714709

715-
profNormal = bx::getHPCounter() - profNormal;
710+
profNormal = bx::getNow() - profNormal;
716711

717-
profTriangulate = bx::getHPCounter();
712+
profTriangulate = bx::getNow();
718713

719714
PosNormalColorVertex* vertex = (PosNormalColorVertex*)tvb.data;
720715

@@ -772,7 +767,7 @@ class ExampleMetaballs : public entry::AppI
772767
}
773768
}
774769

775-
profTriangulate = bx::getHPCounter() - profTriangulate;
770+
profTriangulate = bx::getNow() - profTriangulate;
776771

777772
float mtx[16];
778773
bx::mtxRotateXY(mtx, time*0.67f, time);
@@ -804,10 +799,10 @@ class ExampleMetaballs : public entry::AppI
804799
);
805800

806801
ImGui::Text("Num vertices:"); ImGui::SameLine(100); ImGui::Text("%5d (%6.4f%%)", numVertices, float(numVertices)/maxVertices * 100);
807-
ImGui::Text("Update:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", double(profUpdate)*toMs);
808-
ImGui::Text("Calc normals:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", double(profNormal)*toMs);
809-
ImGui::Text("Triangulate:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", double(profTriangulate)*toMs);
810-
ImGui::Text("Frame:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", double(frameTime)*toMs);
802+
ImGui::Text("Update:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", bx::toMilliseconds<double>(profUpdate) );
803+
ImGui::Text("Calc normals:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", bx::toMilliseconds<double>(profNormal) );
804+
ImGui::Text("Triangulate:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", bx::toMilliseconds<double>(profTriangulate) );
805+
ImGui::Text("Frame:"); ImGui::SameLine(100); ImGui::Text("% 7.3f[ms]", bx::toMilliseconds<double>(m_frameTime.getDeltaTime() ) );
811806

812807
ImGui::End();
813808

@@ -832,7 +827,8 @@ class ExampleMetaballs : public entry::AppI
832827
bgfx::ProgramHandle m_program;
833828

834829
Grid* m_grid;
835-
int64_t m_timeOffset;
830+
831+
FrameTime m_frameTime;
836832
};
837833

838834
} // namespace

examples/03-raymarch/raymarch.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ class ExampleRaymarch : public entry::AppI
147147
// Create program from shaders.
148148
m_program = loadProgram("vs_raymarching", "fs_raymarching");
149149

150-
m_timeOffset = bx::getHPCounter();
151-
152150
imguiCreate();
151+
152+
m_frameTime.reset();
153153
}
154154

155155
int shutdown() override
@@ -172,6 +172,9 @@ class ExampleRaymarch : public entry::AppI
172172
{
173173
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
174174
{
175+
m_frameTime.frame();
176+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
177+
175178
imguiBeginFrame(m_mouseState.m_mx
176179
, m_mouseState.m_my
177180
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -214,8 +217,6 @@ class ExampleRaymarch : public entry::AppI
214217
// Set view and projection matrix for view 0.
215218
bgfx::setViewTransform(1, NULL, ortho);
216219

217-
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
218-
219220
float vp[16];
220221
bx::mtxMul(vp, view, proj);
221222

@@ -259,7 +260,8 @@ class ExampleRaymarch : public entry::AppI
259260
uint32_t m_debug;
260261
uint32_t m_reset;
261262

262-
int64_t m_timeOffset;
263+
FrameTime m_frameTime;
264+
263265
bgfx::UniformHandle u_mtx;
264266
bgfx::UniformHandle u_lightDirTime;
265267
bgfx::ProgramHandle m_program;

examples/04-mesh/mesh.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class ExampleMesh : public entry::AppI
5656

5757
m_mesh = meshLoad("meshes/bunny.bin");
5858

59-
m_timeOffset = bx::getHPCounter();
60-
6159
imguiCreate();
60+
61+
m_frameTime.reset();
6262
}
6363

6464
int shutdown() override
@@ -82,6 +82,10 @@ class ExampleMesh : public entry::AppI
8282
{
8383
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
8484
{
85+
m_frameTime.frame();
86+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
87+
bgfx::setFrameUniform(u_time, &time);
88+
8589
imguiBeginFrame(m_mouseState.m_mx
8690
, m_mouseState.m_my
8791
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -103,9 +107,6 @@ class ExampleMesh : public entry::AppI
103107
// if no other draw calls are submitted to view 0.
104108
bgfx::touch(0);
105109

106-
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
107-
bgfx::setFrameUniform(u_time, &time);
108-
109110
const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
110111
const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };
111112

@@ -147,7 +148,8 @@ class ExampleMesh : public entry::AppI
147148
uint32_t m_debug;
148149
uint32_t m_reset;
149150

150-
int64_t m_timeOffset;
151+
FrameTime m_frameTime;
152+
151153
Mesh* m_mesh;
152154
bgfx::ProgramHandle m_program;
153155
bgfx::UniformHandle u_time;

examples/05-instancing/instancing.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class ExampleInstancing : public entry::AppI
119119
m_program = loadProgram("vs_instancing", "fs_instancing");
120120
m_program_non_instanced = loadProgram("vs_cubes", "fs_cubes");
121121

122-
m_timeOffset = bx::getHPCounter();
123-
124122
imguiCreate();
123+
124+
m_frameTime.reset();
125125
}
126126

127127
int shutdown() override
@@ -144,6 +144,9 @@ class ExampleInstancing : public entry::AppI
144144
{
145145
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
146146
{
147+
m_frameTime.frame();
148+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
149+
147150
imguiBeginFrame(m_mouseState.m_mx
148151
, m_mouseState.m_my
149152
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -206,8 +209,6 @@ class ExampleInstancing : public entry::AppI
206209
// if no other draw calls are submitted to view 0.
207210
bgfx::touch(0);
208211

209-
float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) );
210-
211212
if (!instancingSupported)
212213
{
213214
// When instancing is not supported by GPU, implement alternative
@@ -341,7 +342,7 @@ class ExampleInstancing : public entry::AppI
341342
bgfx::ProgramHandle m_program;
342343
bgfx::ProgramHandle m_program_non_instanced;
343344

344-
int64_t m_timeOffset;
345+
FrameTime m_frameTime;
345346
};
346347

347348
} // namespace

examples/06-bump/bump.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class ExampleBump : public entry::AppI
161161
// Load normal texture.
162162
m_textureNormal = loadTexture("textures/fieldstone-n.dds");
163163

164-
m_timeOffset = bx::getHPCounter();
165-
166164
imguiCreate();
165+
166+
m_frameTime.reset();
167167
}
168168

169169
virtual int shutdown() override
@@ -191,6 +191,9 @@ class ExampleBump : public entry::AppI
191191
{
192192
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
193193
{
194+
m_frameTime.frame();
195+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
196+
194197
imguiBeginFrame(m_mouseState.m_mx
195198
, m_mouseState.m_my
196199
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -212,8 +215,6 @@ class ExampleBump : public entry::AppI
212215
// if no other draw calls are submitted to view 0.
213216
bgfx::touch(0);
214217

215-
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ));
216-
217218
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
218219
const bx::Vec3 eye = { 0.0f, 0.0f, -7.0f };
219220

@@ -368,7 +369,8 @@ class ExampleBump : public entry::AppI
368369
uint32_t m_height;
369370
uint32_t m_debug;
370371
uint32_t m_reset;
371-
int64_t m_timeOffset;
372+
373+
FrameTime m_frameTime;
372374
};
373375

374376
} // namespace

examples/08-update/update.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,9 @@ class ExampleUpdate : public entry::AppI
443443
m_hit = 0;
444444
m_miss = 0;
445445

446-
m_updateTime = 0;
447-
m_timeOffset = bx::getHPCounter();
448-
449446
imguiCreate();
447+
448+
m_frameTime.reset();
450449
}
451450

452451
virtual int shutdown() override
@@ -545,6 +544,10 @@ class ExampleUpdate : public entry::AppI
545544
{
546545
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
547546
{
547+
m_frameTime.frame();
548+
const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
549+
bgfx::setFrameUniform(u_time, &time);
550+
548551
imguiBeginFrame(m_mouseState.m_mx
549552
, m_mouseState.m_my
550553
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
@@ -590,11 +593,7 @@ class ExampleUpdate : public entry::AppI
590593
// if no other draw calls are submitted to view 0.
591594
bgfx::touch(0);
592595

593-
int64_t now = bx::getHPCounter();
594-
float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
595-
bgfx::setFrameUniform(u_time, &time);
596-
597-
if (now > m_updateTime)
596+
if (bx::getNow() > m_updateTime)
598597
{
599598
PackCube face;
600599

@@ -978,8 +977,8 @@ class ExampleUpdate : public entry::AppI
978977

979978
std::list<PackCube> m_quads;
980979
RectPackCubeT<256> m_cube;
981-
int64_t m_updateTime;
982-
int64_t m_timeOffset;
980+
bx::Ticks m_updateTime = bx::InitZero;
981+
983982
bx::RngMwc m_rng;
984983

985984
uint32_t m_hit;
@@ -1007,6 +1006,7 @@ class ExampleUpdate : public entry::AppI
10071006
bgfx::UniformHandle s_texColor;
10081007
bgfx::UniformHandle s_texCube;
10091008

1009+
FrameTime m_frameTime;
10101010
};
10111011

10121012
} // namespace

0 commit comments

Comments
 (0)