add event profiler, json profile dump, cleanup

This commit is contained in:
Irlan
2017-02-24 20:11:49 -03:00
parent e1038cfb31
commit 689425d9ef
64 changed files with 16135 additions and 531 deletions

View File

@ -22,10 +22,12 @@
// Include this file header in your project to directly access Bounce objects.
#include <bounce/common/settings.h>
#include <bounce/common/math/math.h>
#include <bounce/common/time.h>
#include <bounce/common/profiler.h>
#include <bounce/common/draw.h>
#include <bounce/common/math/math.h>
#include <bounce/collision/gjk/gjk.h>
#include <bounce/collision/gjk/gjk_cache.h>
#include <bounce/collision/sat/sat.h>

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef B3_PROFILER_H
#define B3_PROFILER_H
#include <bounce/common/settings.h>
#define B3_PROFILE(name) b3ProfileScope scope(name)
// You should implement this function to use your own profiler.
bool b3PushProfileScope(const char* name);
// You should implement this function to use your own profiler.
void b3PopProfileScope();
struct b3ProfileScope
{
b3ProfileScope(const char* name)
{
b = b3PushProfileScope(name);
}
~b3ProfileScope()
{
if (b)
{
b3PopProfileScope();
}
}
private:
bool b;
};
#endif

View File

@ -1,283 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef B3_OBJECT_ARRAY_H
#define B3_OBJECT_ARRAY_H
#include <bounce/common/settings.h>
// An array for objects.
template <typename T>
class b3ObjectArray
{
public:
const T& operator[](u32 i) const
{
B3_ASSERT(i < m_count);
return m_elements[i];
}
T& operator[](u32 i)
{
B3_ASSERT(i < m_count);
return m_elements[i];
}
const T* Get(u32 i) const
{
B3_ASSERT(i < m_count);
return m_elements + i;
}
T* Get(u32 i)
{
B3_ASSERT(i < m_count);
return m_elements + i;
}
const T* Elements() const
{
return m_elements;
}
T* Elements()
{
return m_elements;
}
void PushBack(const T& ele)
{
if (m_count == m_capacity)
{
// There is no capacity for one more element.
T* oldElements = m_elements;
m_capacity *= 2;
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
for (u32 i = 0; i < m_count; ++i)
{
T* old = oldElements + i;
T* e = m_elements + i;
new (e) T(*old);
old->~T();
}
if (oldElements != m_localElements)
{
b3Free(oldElements);
}
}
B3_ASSERT(m_count < m_capacity);
T* e = m_elements + m_count;
new(e) T(ele);
++m_count;
}
void PopBack()
{
B3_ASSERT(m_count > 0);
--m_count;
}
const T& Back() const
{
B3_ASSERT(m_count > 0);
return m_elements[m_count - 1];
}
T& Back()
{
B3_ASSERT(m_count > 0);
return m_elements[m_count - 1];
}
u32 Count() const
{
return m_count;
}
bool Empty() const
{
return m_count == 0;
}
void Resize(u32 size)
{
if (m_capacity < size)
{
// There is no capacity for the requested size.
T* oldElements = m_elements;
m_capacity = 2 * size;
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
// Construct and copy objects.
for (u32 i = 0; i < m_count; ++i)
{
T* old = oldElements + i;
T* e = m_elements + i;
new (e) T(*old);
old->~T();
}
// Construct objects up to the requested size.
for (u32 i = m_count; i < size; ++i)
{
T* e = m_elements + i;
new (e) T();
}
if (oldElements != m_localElements)
{
b3Free(oldElements);
}
m_count = size;
return;
}
B3_ASSERT(m_capacity >= size);
if (size < m_count)
{
// Destroy objects beyond the requested size.
for (u32 i = size; i < m_count; ++i)
{
T* e = m_elements + i;
e->~T();
}
}
else
{
// Construct objects up to the requested size.
for (u32 i = m_count; i < size; ++i)
{
T* e = m_elements + i;
new (e) T();
}
}
m_count = size;
}
void Swap(const b3ObjectArray<T>& other)
{
if (m_elements == other.m_elements)
{
return;
}
// Destroy all objects.
for (u32 i = 0; i < m_count; ++i)
{
T* e = m_elements + i;
e->~T();
}
// Ensure sufficient capacity for a copy.
if (m_capacity < other.m_count)
{
if (m_elements != m_localElements)
{
b3Free(m_elements);
}
m_capacity = 2 * other.m_count;
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
}
// Copy.
B3_ASSERT(m_capacity >= other.m_count);
for (u32 i = 0; i < other.m_count; ++i)
{
T* e2 = other.m_elements + i;
T* e1 = m_elements + i;
new (e1) T(*e2);
}
m_count = other.m_count;
}
protected:
b3ObjectArray(T* elements, u32 N)
{
B3_ASSERT(N > 0);
m_localElements = elements;
m_capacity = N;
m_elements = m_localElements;
m_count = 0;
}
b3ObjectArray(const b3ObjectArray<T>& other)
{
Swap(other);
}
~b3ObjectArray()
{
if (m_elements != m_localElements)
{
for (u32 i = 0; i < m_count; ++i)
{
T* e = m_elements + i;
e->~T();
}
b3Free(m_elements);
}
}
void operator=(const b3ObjectArray<T>& other)
{
Swap(other);
}
u32 m_capacity;
T* m_elements;
u32 m_count;
T* m_localElements;
};
template <typename T, u32 N>
class b3StackObjectArray : public b3ObjectArray<T>
{
public:
b3StackObjectArray<T, N>() : b3ObjectArray<T>(m_stackElements, N)
{
}
b3StackObjectArray<T, N>(const b3StackObjectArray<T, N>& other) : b3ObjectArray<T>(other)
{
}
b3StackObjectArray<T, N>(const b3ObjectArray<T>& other) : b3ObjectArray<T>(other)
{
}
void operator=(const b3StackObjectArray<T, N>& other)
{
Swap(other);
}
void operator=(const b3ObjectArray<T>& other)
{
Swap(other);
}
protected:
//@todo
// u8 m_bytes[N * sizeof(T)];
T m_stackElements[N];
};
#endif

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef B3_QUEUE_H
#define B3_QUEUE_H
#include <bounce/common/settings.h>
// A bounded FIFO queue with a fixed capacity of N.
// The capacity must be greater than one.
// The elements are allocated on the stack.
template <typename T, u32 N>
class b3BoundedQueue
{
public:
b3BoundedQueue()
{
B3_ASSERT(N > 1);
m_count = 0;
m_back = 0;
m_front = 0;
}
~b3BoundedQueue()
{
}
T* Push(const T& ele)
{
if ((m_count + 1) == N)
{
return NULL;
}
B3_ASSERT(m_back < N);
T* e = m_elements + m_back;
*e = ele;
m_back = (m_back + 1) % N;
B3_ASSERT(m_back != m_front);
++m_count;
return e;
}
bool Pop()
{
if (m_front == m_back)
{
return false;
}
B3_ASSERT(m_front != m_back);
m_front = (m_front + 1) % N;
B3_ASSERT(m_count > 0);
--m_count;
return true;
}
const T& Front() const
{
B3_ASSERT(m_count > 0);
return m_elements[m_front];
}
T& Front()
{
B3_ASSERT(m_count > 0);
return m_elements[m_front];
}
const T& Back() const
{
B3_ASSERT(m_count > 0);
return m_elements[m_back > 0 ? m_back - 1 : N - 1];
}
T& Back()
{
B3_ASSERT(m_count > 0);
return m_elements[m_back > 0 ? m_back - 1 : N - 1];
}
u32 Count() const
{
return m_count;
}
bool IsEmpty() const
{
return m_back == m_front;
}
private:
T m_elements[N];
u32 m_count;
u32 m_back;
u32 m_front;
};
#endif

View File

@ -21,7 +21,9 @@
#include <bounce/common/settings.h>
// A growable stack for plain-old-data (POD).
// A growable LIFO stack with an initial capacity of N.
// If the stack capacity exceeds the initial capacity, the heap
// is used to increase the capacity of the stack.
template <typename T, u32 N>
class b3Stack
{

View File

@ -30,6 +30,7 @@ class b3World;
class b3Shape;
struct b3ShapeDef;
struct b3MassData;
struct b3JointEdge;
// Static bodies have zero mass and velocity, and therefore they can't move.
@ -178,6 +179,12 @@ public:
// Get the rotational inertia of the body about the center of mass. Typically in kg/m^3.
const b3Mat33& GetInertia() const;
// Get this body mass data.
void GetMassData(b3MassData* data) const;
// Set this body mass data.
void SetMassData(const b3MassData* data);
// Get the linear kinetic energy of the body in Joules (kilogram-meters squared per second squared).
float32 GetLinearEnergy() const;

View File

@ -41,7 +41,7 @@ public :
void Add(b3Contact* contact);
void Add(b3Joint* joint);
void Solve(b3Profile* profile, const b3Vec3& gravity, float32 dt, u32 velocityIterations, u32 positionIterations, u32 flags);
void Solve(const b3Vec3& gravity, float32 dt, u32 velocityIterations, u32 positionIterations, u32 flags);
private :
enum b3IslandFlags
{

View File

@ -122,27 +122,4 @@ inline b3Mat33 b3Integrate(const b3Mat33& orientation, const b3Vec3& velocity, f
return orientation + integral;
}
// These structures contain the elapsed time it took
// to run particular method.
struct b3CollideProfile
{
float64 broadphase;
float64 narrowphase;
};
struct b3SolverProfile
{
float64 initializeContacts;
float64 initializeJoints;
float64 solveVelocity;
float64 solvePosition;
};
struct b3Profile
{
float64 total;
b3CollideProfile collide;
b3SolverProfile solver;
};
#endif

View File

@ -32,7 +32,6 @@ class b3QueryListener;
class b3RayCastListener;
class b3ContactListener;
class b3ContactFilter;
class b3Draw;
struct b3RayCastSingleOutput
{
@ -49,14 +48,6 @@ public:
b3World();
~b3World();
// Set the debug draw interface.
// The user must implement this interface to tell the world to
// draw the physics entities.
void SetDebugDraw(b3Draw* draw);
// Get the debug draw interface.
b3Draw* GetDebugDraw();
// The filter passed can tell the world to disallow the contact creation between
// two shapes.
void SetContactFilter(b3ContactFilter* filter);
@ -124,10 +115,9 @@ public:
const b3List2<b3Contact>& GetContactList() const;
b3List2<b3Contact>& GetContactList();
// Get the time spent to finish executing each simulation module of the last physics step.
const b3Profile& GetProfile() const;
// Tell the world to draw the entities that belong to this world.
// Debug draw the physics entities that belong to this world.
// The user must implement the debug draw interface b3Draw and b3_debugDraw must have been
// set to the user implementation.
void DebugDraw() const;
void DrawShape(const b3Transform& xf, const b3Shape* shape) const;
void DrawJoint(const b3Joint* joint) const;
@ -153,7 +143,6 @@ private :
u32 m_flags;
b3Vec3 m_gravity;
b3Draw* m_debugDraw;
b3Profile m_profile;
b3StackAllocator m_stackAllocator;
b3BlockPool m_bodyBlocks;
@ -168,11 +157,6 @@ private :
b3ContactManager m_contactMan;
};
inline b3Draw* b3World::GetDebugDraw()
{
return m_debugDraw;
}
inline void b3World::SetContactListener(b3ContactListener* listener)
{
m_contactMan.m_contactListener = listener;
@ -193,11 +177,6 @@ inline void b3World::SetWarmStart(bool flag)
m_warmStarting = flag;
}
inline const b3Profile& b3World::GetProfile() const
{
return m_profile;
}
inline const b3List2<b3Body>& b3World::GetBodyList() const
{
return m_bodyList;

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef PROFILER_H
#define PROFILER_H
#include <bounce/common/math/math.h>
#include <bounce/common/template/array.h>
enum ProfileType
{
e_begin,
e_end
};
void ProfileBeginEvents();
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 time, ProfileType type);
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 elapsed);
void ProfileEndEvents();
void ProfileBegin();
void ProfileEnd();
struct ProfileRecord
{
float64 elapsed;
float64 maxElapsed;
const char* name;
};
class Profiler
{
public:
void Clear()
{
for (u32 i = 0; i < m_records.Count(); ++i)
{
m_records[i].elapsed = 0;
}
}
void Add(const char* name, float64 elapsed)
{
for (u32 i = 0; i < m_records.Count(); ++i)
{
ProfileRecord& r = m_records[i];
if (r.name == name)
{
r.elapsed += elapsed;
r.maxElapsed = b3Max(r.maxElapsed, r.elapsed);
return;
}
}
ProfileRecord r;
r.elapsed = elapsed;
r.maxElapsed = 0;
r.name = name;
m_records.PushBack(r);
}
b3StackArray<ProfileRecord, 256> m_records;
};
#endif

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef ANGULAR_MOTION_H
#define ANGULAR_MOTION_H
class AngularMotion : public Test
{
public:
AngularMotion()
{
b3BodyDef bdef;
bdef.type = e_dynamicBody;
bdef.position.Set(0.0f, 0.0f, 0.0f);
m_body = m_world.CreateBody(bdef);
b3CapsuleShape shape;
shape.m_centers[0].Set(0.0f, 1.0f, 0.0f);
shape.m_centers[1].Set(0.0f, -1.0f, 0.0f);
shape.m_radius = 1.0f;
b3ShapeDef sdef;
sdef.shape = &shape;
sdef.density = 1.0f;
m_body->CreateShape(sdef);
b3MassData data;
m_body->GetMassData(&data);
m_body->SetMassData(&data);
b3Vec3 g(0.0f, 0.0f, 0.0f);
m_world.SetGravity(g);
}
void Step()
{
Test::Step();
b3Vec3 v(0.0f, 0.0f, 0.0f);
m_body->SetLinearVelocity(v);
b3Vec3 p = m_body->GetSweep().worldCenter;
b3Quat quat = m_body->GetSweep().orientation;
b3Vec3 axis;
float32 angle;
quat.GetAxisAngle(&axis, &angle);
b3Vec3 q(0.0f, 0.0f, 0.0f);
m_body->SetTransform(q, axis, angle);
}
static Test* Create()
{
return new AngularMotion();
}
b3Body* m_body;
};
#endif

View File

@ -32,10 +32,10 @@ public:
b3ClothDef def;
def.mesh = m_meshes + e_clothMesh;
def.density = 0.4f;
def.density = 0.2f;
def.gravity.Set(-10.0f, 1.0f, 0.0f);
def.k1 = 0.9f;
def.k2 = 0.2f;
def.k1 = 0.5f;
def.k2 = 0.05f;
def.kd = 0.1f;
def.r = 1.0f;

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef LINEAR_MOTION_H
#define LINEAR_MOTION_H
class LinearMotion : public Test
{
public:
LinearMotion()
{
b3BodyDef bdef;
bdef.type = e_dynamicBody;
bdef.position.Set(0.0f, 0.0f, 0.0f);
m_body = m_world.CreateBody(bdef);
b3CapsuleShape shape;
shape.m_centers[0].Set(0.0f, 1.0f, 0.0f);
shape.m_centers[1].Set(0.0f, -1.0f, 0.0f);
shape.m_radius = 1.0f;
b3ShapeDef sdef;
sdef.shape = &shape;
sdef.density = 1.0f;
m_body->CreateShape(sdef);
b3MassData data;
m_body->GetMassData(&data);
data.I.SetZero();
m_body->SetMassData(&data);
b3Vec3 g(0.0f, 0.0f, 0.0f);
m_world.SetGravity(g);
b3Vec3 f(0.0f, 0.0f, -10000.0f);
m_body->ApplyForceToCenter(f, true);
}
void Step()
{
Test::Step();
b3Vec3 q(0.0f, 0.0f, 0.0f);
b3Vec3 p = m_body->GetTransform().position;
if (b3Distance(p, q) > 50.0f)
{
b3Quat quat = m_body->GetSweep().orientation;
b3Vec3 axis;
float32 angle;
quat.GetAxisAngle(&axis, &angle);
m_body->SetTransform(q, axis, angle);
}
}
static Test* Create()
{
return new LinearMotion();
}
b3Body* m_body;
};
#endif

View File

@ -19,11 +19,13 @@
#ifndef TEST_H
#define TEST_H
#include <bounce/bounce.h>
#include <glfw/glfw3.h>
#include <imgui/imgui.h>
#include "../framework/debug_draw.h"
#include <bounce/bounce.h>
#include <testbed/framework/debug_draw.h>
#include <testbed/framework/profiler.h>
struct Settings
{
@ -45,7 +47,7 @@ struct Settings
drawContactTangents = false;
drawStats = true;
drawProfile = true;
drawGrid = false;;
drawGrid = true;
pause = false;
singleStep = false;
lastTestID = -1;
@ -131,8 +133,6 @@ public:
virtual void KeyUp(int button) { }
b3World m_world;
b3Profile m_profile;
b3Profile m_maxProfile;
b3RayCastSingleOutput m_rayHit;
b3BoxHull m_groundHull;