Skip to content

Commit

Permalink
UPBGE: Use debug draw directly in KX_Scene and KX_KetsjiEngine.
Browse files Browse the repository at this point in the history
Previously the debug draw (RAS_DebugDraw) was get from the rasterizer with
a scene as identifier, but the scene could simply store an instance of
RAS_DebugDraw and avoid lookup in rasterizer.
In the same time the ketsji engine request a debug draw only for profiling
render.
  • Loading branch information
panzergame committed Jun 17, 2018
1 parent 822bd35 commit e613636
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ const std::string& KX_GetOrigPath()

void KX_RasterizerDrawDebugLine(const mt::vec3& from, const mt::vec3& to, const mt::vec4& color)
{
g_engine->GetRasterizer()->GetDebugDraw(g_scene).DrawLine(from, to, color);
g_scene->GetDebugDraw().DrawLine(from, to, color);
}
44 changes: 21 additions & 23 deletions source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,11 @@ void KX_KetsjiEngine::RenderCamera(KX_Scene *scene, const CameraRenderData& came

m_logger.StartLog(tc_rasterizer, m_kxsystem->GetTimeInSeconds());

RAS_DebugDraw& debugDraw = m_rasterizer->GetDebugDraw(scene);
// Draw debug infos like bouding box, armature ect.. if enabled.
scene->DrawDebug(debugDraw, objects, m_showBoundingBox, m_showArmature);
scene->DrawDebug(objects, m_showBoundingBox, m_showArmature);
// Draw debug camera frustum.
DrawDebugCameraFrustum(scene, debugDraw, cameraFrameData);
DrawDebugShadowFrustum(scene, debugDraw);
DrawDebugCameraFrustum(scene, cameraFrameData);
DrawDebugShadowFrustum(scene);

#ifdef WITH_PYTHON
// Run any pre-drawing python callbacks
Expand All @@ -1026,7 +1025,7 @@ RAS_OffScreen *KX_KetsjiEngine::PostRenderScene(KX_Scene *scene, RAS_OffScreen *
{
KX_SetActiveScene(scene);

m_rasterizer->FlushDebugDraw(scene, m_canvas);
scene->FlushDebugDraw(m_rasterizer, m_canvas);

// We need to first make sure our viewport is correct (enabling multiple viewports can mess this up), only for filters.
const int width = m_canvas->GetWidth();
Expand All @@ -1043,7 +1042,7 @@ RAS_OffScreen *KX_KetsjiEngine::PostRenderScene(KX_Scene *scene, RAS_OffScreen *
scene->RunDrawingCallbacks(KX_Scene::POST_DRAW, nullptr);

// Python draw callback can also call debug draw functions, so we have to clear debug shapes.
m_rasterizer->FlushDebugDraw(scene, m_canvas);
scene->FlushDebugDraw(m_rasterizer, m_canvas);
#endif

return offScreen;
Expand Down Expand Up @@ -1134,13 +1133,10 @@ void KX_KetsjiEngine::RenderDebugProperties()

static const mt::vec4 white(1.0f, 1.0f, 1.0f, 1.0f);

// Use nullptrfor no scene.
RAS_DebugDraw& debugDraw = m_rasterizer->GetDebugDraw(nullptr);

if (m_flags & (SHOW_FRAMERATE | SHOW_PROFILE)) {
// Title for profiling("Profile")
// Adds the constant x indent (0 for now) to the title x margin
debugDraw.RenderText2d("Profile", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);
m_debugDraw.RenderText2d("Profile", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);

// Increase the indent by default increase
ycoord += const_ysize;
Expand All @@ -1150,38 +1146,38 @@ void KX_KetsjiEngine::RenderDebugProperties()

// Framerate display
if (m_flags & SHOW_FRAMERATE) {
debugDraw.RenderText2d("Frametime :",
m_debugDraw.RenderText2d("Frametime :",
mt::vec2(xcoord + const_xindent,
ycoord), white);

debugtxt = (boost::format("%5.2fms (%.1ffps)") % (tottime * 1000.0f) % (1.0f / tottime)).str();
debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);
m_debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);
// Increase the indent by default increase
ycoord += const_ysize;
}

// Profile display
if (m_flags & SHOW_PROFILE) {
for (int j = tc_first; j < tc_numCategories; j++) {
debugDraw.RenderText2d(m_profileLabels[j], mt::vec2(xcoord + const_xindent, ycoord), white);
m_debugDraw.RenderText2d(m_profileLabels[j], mt::vec2(xcoord + const_xindent, ycoord), white);

double time = m_logger.GetAverage((KX_TimeCategory)j);

debugtxt = (boost::format("%5.2fms | %d%%") % (time * 1000.f) % (int)(time / tottime * 100.f)).str();
debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);
m_debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);

const mt::vec2 boxSize(50 * (time / tottime), 9);
debugDraw.RenderBox2d(mt::vec2(xcoord + (int)(2.2 * profile_indent), ycoord), boxSize, white);
m_debugDraw.RenderBox2d(mt::vec2(xcoord + (int)(2.2 * profile_indent), ycoord), boxSize, white);
ycoord += const_ysize;
}
}

if (m_flags & SHOW_RENDER_QUERIES) {
debugDraw.RenderText2d("Render Queries :", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);
m_debugDraw.RenderText2d("Render Queries :", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);
ycoord += const_ysize;

for (unsigned short i = 0; i < QUERY_MAX; ++i) {
debugDraw.RenderText2d(m_renderQueriesLabels[i], mt::vec2(xcoord + const_xindent, ycoord), white);
m_debugDraw.RenderText2d(m_renderQueriesLabels[i], mt::vec2(xcoord + const_xindent, ycoord), white);

if (i == QUERY_TIME) {
debugtxt = (boost::format("%.2fms") % (((float)m_renderQueries[i].Result()) / 1e6)).str();
Expand All @@ -1190,7 +1186,7 @@ void KX_KetsjiEngine::RenderDebugProperties()
debugtxt = (boost::format("%i") % m_renderQueries[i].Result()).str();
}

debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);
m_debugDraw.RenderText2d(debugtxt, mt::vec2(xcoord + const_xindent + profile_indent, ycoord), white);
ycoord += const_ysize;
}
}
Expand All @@ -1202,7 +1198,7 @@ void KX_KetsjiEngine::RenderDebugProperties()
if (m_flags & SHOW_DEBUG_PROPERTIES) {
// Title for debugging("Debug properties")
// Adds the constant x indent (0 for now) to the title x margin
debugDraw.RenderText2d("Debug Properties", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);
m_debugDraw.RenderText2d("Debug Properties", mt::vec2(xcoord + const_xindent + title_xmargin, ycoord), white);

// Increase the indent by default increase
ycoord += const_ysize;
Expand All @@ -1213,19 +1209,20 @@ void KX_KetsjiEngine::RenderDebugProperties()
const unsigned short propsMax = (m_canvas->GetHeight() - ycoord) / const_ysize;

for (KX_Scene *scene : m_scenes) {
scene->RenderDebugProperties(debugDraw, const_xindent, const_ysize, xcoord, ycoord, propsMax);
scene->RenderDebugProperties(m_debugDraw, const_xindent, const_ysize, xcoord, ycoord, propsMax);
}
}

m_rasterizer->FlushDebugDraw(nullptr, m_canvas);
m_debugDraw.Flush(m_rasterizer, m_canvas);
}

void KX_KetsjiEngine::DrawDebugCameraFrustum(KX_Scene *scene, RAS_DebugDraw& debugDraw, const CameraRenderData& cameraFrameData)
void KX_KetsjiEngine::DrawDebugCameraFrustum(KX_Scene *scene, const CameraRenderData& cameraFrameData)
{
if (m_showCameraFrustum == KX_DebugOption::DISABLE) {
return;
}

RAS_DebugDraw& debugDraw = scene->GetDebugDraw();
for (KX_Camera *cam : scene->GetCameraList()) {
if (cam != cameraFrameData.m_renderCamera && (m_showCameraFrustum == KX_DebugOption::FORCE || cam->GetShowCameraFrustum())) {
const mt::mat4 viewmat = m_rasterizer->GetViewMatrix(cameraFrameData.m_stereoMode, cameraFrameData.m_eye,
Expand All @@ -1237,12 +1234,13 @@ void KX_KetsjiEngine::DrawDebugCameraFrustum(KX_Scene *scene, RAS_DebugDraw& deb
}
}

void KX_KetsjiEngine::DrawDebugShadowFrustum(KX_Scene *scene, RAS_DebugDraw& debugDraw)
void KX_KetsjiEngine::DrawDebugShadowFrustum(KX_Scene *scene)
{
if (m_showShadowFrustum == KX_DebugOption::DISABLE) {
return;
}

RAS_DebugDraw& debugDraw = scene->GetDebugDraw();
for (KX_LightObject *light : scene->GetLightList()) {
RAS_ILightObject *raslight = light->GetLightData();
if (m_showShadowFrustum == KX_DebugOption::FORCE || light->GetShowShadowFrustum()) {
Expand Down
7 changes: 5 additions & 2 deletions source/gameengine/Ketsji/KX_KetsjiEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "KX_WorldInfo.h"
#include "RAS_CameraData.h"
#include "RAS_Rasterizer.h"
#include "RAS_DebugDraw.h"
#include <vector>

struct TaskScheduler;
Expand Down Expand Up @@ -154,6 +155,8 @@ class KX_KetsjiEngine : public mt::SimdClassAllocator
RAS_ICanvas *m_canvas;
/// 3D Rasterizer (3D Rendering)
RAS_Rasterizer *m_rasterizer;
/// Global debug draw, mainly used for profiling texts.
RAS_DebugDraw m_debugDraw;
KX_ISystem *m_kxsystem;
BL_Converter *m_converter;
KX_NetworkMessageManager *m_networkMessageManager;
Expand Down Expand Up @@ -280,9 +283,9 @@ class KX_KetsjiEngine : public mt::SimdClassAllocator
RAS_OffScreen *PostRenderScene(KX_Scene *scene, RAS_OffScreen *inputofs, RAS_OffScreen *targetofs);
void RenderDebugProperties();
/// Debug draw cameras frustum of a scene.
void DrawDebugCameraFrustum(KX_Scene *scene, RAS_DebugDraw& debugDraw, const CameraRenderData& cameraFrameData);
void DrawDebugCameraFrustum(KX_Scene *scene, const CameraRenderData& cameraFrameData);
/// Debug draw lights shadow frustum of a scene.
void DrawDebugShadowFrustum(KX_Scene *scene, RAS_DebugDraw& debugDraw);
void DrawDebugShadowFrustum(KX_Scene *scene);

/**
* Processes all scheduled scene activity.
Expand Down
31 changes: 19 additions & 12 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,12 @@ std::vector<KX_GameObject *> KX_Scene::CalculateVisibleMeshes(const SG_Frustum&
return handler.Process();
}

void KX_Scene::DrawDebug(RAS_DebugDraw& debugDraw, const std::vector<KX_GameObject *>& objects,
RAS_DebugDraw& KX_Scene::GetDebugDraw()
{
return m_debugDraw;
}

void KX_Scene::DrawDebug(const std::vector<KX_GameObject *>& objects,
KX_DebugOption showBoundingBox, KX_DebugOption showArmatures)
{
if (showBoundingBox != KX_DebugOption::DISABLE) {
Expand All @@ -1148,19 +1153,16 @@ void KX_Scene::DrawDebug(RAS_DebugDraw& debugDraw, const std::vector<KX_GameObje
const SG_BBox& box = gameobj->GetCullingNode().GetAabb();
const mt::vec3& center = box.GetCenter();

debugDraw.DrawAabb(position, orientation, box.GetMin() * scale, box.GetMax() * scale,
m_debugDraw.DrawAabb(position, orientation, box.GetMin() * scale, box.GetMax() * scale,
mt::vec4(1.0f, 0.0f, 1.0f, 1.0f));

static const mt::vec3 axes[] = {mt::axisX3, mt::axisY3, mt::axisZ3};
static const mt::vec4 colors[] = {mt::vec4(1.0f, 0.0f, 0.0f, 1.0f), mt::vec4(0.0f, 1.0f, 0.0f, 1.0f), mt::vec4(0.0f, 0.0f, 1.0f, 1.0f)};
// Render center in red, green and blue.
debugDraw.DrawLine(orientation * (center * scale) + position,
orientation * ((center + mt::axisX3) * scale) + position,
mt::vec4(1.0f, 0.0f, 0.0f, 1.0f));
debugDraw.DrawLine(orientation * (center * scale) + position,
orientation * ((center + mt::axisY3) * scale) + position,
mt::vec4(0.0f, 1.0f, 0.0f, 1.0f));
debugDraw.DrawLine(orientation * (center * scale) + position,
orientation * ((center + mt::axisZ3) * scale) + position,
mt::vec4(0.0f, 0.0f, 1.0f, 1.0f));
for (unsigned short i = 0; i < 3; ++i) {
m_debugDraw.DrawLine(orientation * (center * scale) + position,
orientation * ((center + axes[i]) * scale) + position, colors[i]);
}
}
}

Expand All @@ -1170,7 +1172,7 @@ void KX_Scene::DrawDebug(RAS_DebugDraw& debugDraw, const std::vector<KX_GameObje
if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
BL_ArmatureObject *armature = static_cast<BL_ArmatureObject *>(gameobj);
if (showArmatures == KX_DebugOption::FORCE || armature->GetDrawDebug()) {
armature->DrawDebug(debugDraw);
armature->DrawDebug(m_debugDraw);
}
}
}
Expand Down Expand Up @@ -1223,6 +1225,11 @@ void KX_Scene::RenderDebugProperties(RAS_DebugDraw& debugDraw, int xindent, int
}
}

void KX_Scene::FlushDebugDraw(RAS_Rasterizer *rasty, RAS_ICanvas *canvas)
{
m_debugDraw.Flush(rasty, canvas);
}

void KX_Scene::LogicBeginFrame(double curtime, double framestep)
{
// Have a look at temp objects.
Expand Down
9 changes: 7 additions & 2 deletions source/gameengine/Ketsji/KX_Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "SCA_IScene.h"

#include "RAS_Rasterizer.h" // For RAS_Rasterizer::DrawType.
#include "RAS_DebugDraw.h"
#include "RAS_FramingManager.h"
#include "RAS_Rect.h"

Expand Down Expand Up @@ -79,7 +80,6 @@ class RAS_BucketManager;
class RAS_MaterialBucket;
class RAS_IPolyMaterial;
class RAS_Rasterizer;
class RAS_DebugDraw;
class RAS_OffScreen;
class RAS_2DFilterManager;

Expand Down Expand Up @@ -238,6 +238,9 @@ class KX_Scene : public EXP_Value, public SCA_IScene
*/
RAS_Rect m_viewport;

/// Debug drawing registering.
RAS_DebugDraw m_debugDraw;

/// Visibility testing functions.
static void PhysicsCullingCallback(KX_ClientObjectInfo *objectInfo, void *cullingInfo);

Expand Down Expand Up @@ -354,10 +357,12 @@ class KX_Scene : public EXP_Value, public SCA_IScene
std::vector<KX_GameObject *> CalculateVisibleMeshes(KX_Camera *cam, int layer);
std::vector<KX_GameObject *> CalculateVisibleMeshes(const SG_Frustum& frustum, int layer);

RAS_DebugDraw& GetDebugDraw();
/// \section Debug draw.
void DrawDebug(RAS_DebugDraw& debugDraw, const std::vector<KX_GameObject *>& objects,
void DrawDebug(const std::vector<KX_GameObject *>& objects,
KX_DebugOption showBoundingBox, KX_DebugOption showArmatures);
void RenderDebugProperties(RAS_DebugDraw& debugDraw, int xindent, int ysize, int& xcoord, int& ycoord, unsigned short propsMax);
void FlushDebugDraw(RAS_Rasterizer *rasty, RAS_ICanvas *canvas);

/// Replicate the logic bricks associated to this object.
void ReplicateLogic(KX_GameObject *newobj);
Expand Down
1 change: 1 addition & 0 deletions source/gameengine/Rasterizer/RAS_IPolygonMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <map>

class RAS_Rasterizer;
class SCA_IScene;
struct Material;
struct Scene;
struct GameSettings;
Expand Down
10 changes: 0 additions & 10 deletions source/gameengine/Rasterizer/RAS_Rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,6 @@ void RAS_Rasterizer::DrawOverlayPlane()
m_impl->DrawOverlayPlane();
}

RAS_DebugDraw& RAS_Rasterizer::GetDebugDraw(SCA_IScene *scene)
{
return m_debugDraws[scene];
}

void RAS_Rasterizer::FlushDebugDraw(SCA_IScene *scene, RAS_ICanvas *canvas)
{
m_debugDraws[scene].Flush(this, canvas);
}

void RAS_Rasterizer::UpdateOffScreens(RAS_ICanvas *canvas)
{
m_offScreens.Update(canvas);
Expand Down
8 changes: 0 additions & 8 deletions source/gameengine/Rasterizer/RAS_Rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include "mathfu.h"

#include "RAS_DebugDraw.h"
#include "RAS_Rect.h"

#include <string>
Expand All @@ -54,7 +53,6 @@ class RAS_OffScreen;
class RAS_MeshSlot;
class RAS_IDisplayArray;
class RAS_ILightObject;
class SCA_IScene;
class RAS_ISync;
struct KX_ClientObjectInfo;
class KX_RayCast;
Expand Down Expand Up @@ -283,9 +281,6 @@ class RAS_Rasterizer : public mt::SimdClassAllocator
int rightEyeTexLoc;
};

// We store each debug shape by scene.
std::map<SCA_IScene *, RAS_DebugDraw> m_debugDraws;

double m_time;
mt::vec3 m_ambient;
mt::mat4 m_viewmatrix;
Expand Down Expand Up @@ -630,9 +625,6 @@ class RAS_Rasterizer : public mt::SimdClassAllocator
*/
void SetPolygonOffset(DrawType drawingMode, float mult, float add);

RAS_DebugDraw& GetDebugDraw(SCA_IScene *scene);
void FlushDebugDraw(SCA_IScene *scene, RAS_ICanvas *canvas);

const mt::mat4 &GetViewMatrix() const;
const mt::mat4 &GetViewInvMatrix() const;

Expand Down

0 comments on commit e613636

Please sign in to comment.