Skip to content

Commit

Permalink
Improve debug render
Browse files Browse the repository at this point in the history
  • Loading branch information
Hozar2002 authored and Shtrecker committed Nov 30, 2024
1 parent 4584c1a commit 010de06
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/Layers/xrRender/DetailManager_Decompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ IC bool InterpolateAndDither(float* alpha255, u32 x, u32 y, u32 sx, u32 sy, u32
}

#ifndef _EDITOR
#ifdef DEBUG
#if 0 //def DEBUG
//#include "../../Include/xrRender/DebugRender.h"
#include "dxDebugRender.h"
static void draw_obb ( const Fmatrix &matrix, const u32 &color )
Expand Down Expand Up @@ -287,7 +287,7 @@ RDEVICE.Statistic->TEST0.End ();
Bounds.merge (ItemBB);

#ifndef _EDITOR
#ifdef DEBUG
#if 0 //def DEBUG
if(det_render_debug)
draw_obb( mXform, color_rgba (255,0,0,255) );//Fmatrix().mul_43( mXform, Fmatrix().scale(5,5,5) )
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRender/HOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ CHOM::CHOM()
bEnabled = FALSE;
m_pModel = 0;
m_pTris = 0;
#ifdef DEBUG
#ifdef DEBUG_DRAW
Device.seqRender.Add(this,REG_PRIORITY_LOW-1000);
#endif
}

CHOM::~CHOM()
{
#ifdef DEBUG
#ifdef DEBUG_DRAW
Device.seqRender.Remove(this);
#endif
}
Expand Down
233 changes: 121 additions & 112 deletions src/Layers/xrRender/dxDebugRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include "dxUIShader.h"

dxDebugRender DebugRenderImpl;
dxDebugRender DebugRenderImpl_1;

dxDebugRender::dxDebugRender()
{
m_lines.reserve (line_vertex_limit);
m_lines.reserve(line_vertex_limit);
}

void dxDebugRender::Render()
Expand Down Expand Up @@ -44,48 +44,55 @@ void _add_lines ( xr_vector<FVF::L> &vertices, xr_vector<u32>& indices, Fvecto
}
#endif

void dxDebugRender::add_lines (Fvector const *vertices, u32 const &vertex_count, u32 const *pairs, u32 const &pair_count, u32 const &color)
{
for (size_t i = 0; i < pair_count * 2; i += 2) {
void dxDebugRender::add_lines(Fvector const* vertices, u32 const& vertex_count, u32 const* pairs, u32 const& pair_count, u32 const& color) {
for(size_t i = 0; i < pair_count * 2; i += 2) {
u32 i0 = pairs[i];
u32 i1 = pairs[i + 1];
FVF::L v0 = {};
FVF::L v1 = {};
v0.p = vertices[i0];
v1.p = vertices[i1];
v0.color = color;
v1.color = color;

Device.mView_saved.transform(v0.p);
Device.mView_saved.transform(v1.p);

if (v0.p.z > VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
Fvector fp = v1.p;
fp.lerp(v0.p, v1.p, (v0.p.z - VIEWPORT_NEAR) / (v0.p.z - v1.p.z));
v1.p = fp;
}

if (v0.p.z <= VIEWPORT_NEAR && v1.p.z > VIEWPORT_NEAR) {
Fvector fp = v0.p;
fp.lerp(v1.p, v0.p, (v1.p.z - VIEWPORT_NEAR) / (v1.p.z - v0.p.z));
v0.p = fp;
}
auto v0p = vertices[i0];
auto v1p = vertices[i1];

Device.mView_saved.transform(v0p);
Device.mView_saved.transform(v1p);

static auto viewport_near = VIEWPORT_NEAR * 0.1f;

if (v0.p.z <= VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
if(v0p.z <= viewport_near && v1p.z <= viewport_near) {
continue;
}

Device.mProject_saved.transform(v0.p);
Device.mProject_saved.transform(v1.p);
v0.p.x = (v0.p.x * 0.5f + 0.5f) * Device.TargetWidth;
v0.p.y = (-v0.p.y * 0.5f + 0.5f) * Device.TargetHeight;
static auto NearClipping = [](Fvector& v0p, Fvector& v1p) {
if(v0p.z > viewport_near && v1p.z <= viewport_near) {
Fvector fp = v1p;
fp.lerp(v0p, v1p, (v0p.z - viewport_near) / (v0p.z - v1p.z));
v1p = fp;
}
};

v1.p.x = (v1.p.x * 0.5f + 0.5f) * Device.TargetWidth;
v1.p.y = (-v1.p.y * 0.5f + 0.5f) * Device.TargetHeight;
NearClipping(v0p, v1p);
NearClipping(v1p, v0p);

m_lines.emplace_back(v0, v1);
}
if(!RImplementation.get_HUD()) {
Device.mProject_saved.transform(v0p);
Device.mProject_saved.transform(v1p);
}
else {
Device.mProject_hud.transform(v0p);
Device.mProject_hud.transform(v1p);
}

static LineRenderPack LineData;

LineData.x1 = (0.5f + 0.5f * v0p.x) * Device.TargetWidth;
LineData.y1 = (0.5f - 0.5f * v0p.y) * Device.TargetHeight;

LineData.x2 = (0.5f + 0.5f * v1p.x) * Device.TargetWidth;
LineData.y2 = (0.5f - 0.5f * v1p.y) * Device.TargetHeight;

LineData.color = color;

m_lines.push_back(LineData);
}
}

void dxDebugRender::NextSceneMode()
Expand Down Expand Up @@ -162,83 +169,85 @@ void dxDebugRender::dbg_DrawTRI(Fmatrix& T, Fvector& p1, Fvector& p2, Fvector& p
RCache.dbg_DrawTRI(T, p1, p2, p3, C);
}


struct RDebugRender:
public dxDebugRender,
public pureRender
{
private:
xr_vector<std::pair<FVF::L, FVF::L>> _lines;

// Vertices _line_vertices;
// Indices _line_indices;
public:
RDebugRender()
{
#ifndef _EDITOR
Device.seqRender.Add (this,REG_PRIORITY_LOW-100);
#endif
}

virtual ~RDebugRender()
{
#ifndef _EDITOR
Device.seqRender.Remove (this);
#endif
}

void OnRender()
{

m_lines = _lines;
Render();


}
virtual void add_lines (Fvector const *vertices, u32 const &vertex_count, u32 const *pairs, u32 const &pair_count, u32 const &color)
{
_lines.resize(0);
for (size_t i = 0; i < pair_count * 2; i += 2) {
u32 i0 = pairs[i];
u32 i1 = pairs[i + 1];
FVF::L v0 = {};
FVF::L v1 = {};
v0.p = vertices[i0];
v1.p = vertices[i1];
v0.color = color;
v1.color = color;

Device.mView_saved.transform(v0.p);
Device.mView_saved.transform(v1.p);

if (v0.p.z > VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
Fvector fp = v1.p;
fp.lerp(v0.p, v1.p, (v0.p.z - VIEWPORT_NEAR) / (v0.p.z - v1.p.z));
v1.p = fp;
}

if (v0.p.z <= VIEWPORT_NEAR && v1.p.z > VIEWPORT_NEAR) {
Fvector fp = v0.p;
fp.lerp(v1.p, v0.p, (v1.p.z - VIEWPORT_NEAR) / (v1.p.z - v0.p.z));
v0.p = fp;
}

if (v0.p.z <= VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
continue;
}

Device.mProject_saved.transform(v0.p);
Device.mProject_saved.transform(v1.p);
v0.p.x = (v0.p.x * 0.5f + 0.5f) * Device.TargetWidth;
v0.p.y = (-v0.p.y * 0.5f + 0.5f) * Device.TargetHeight;

v1.p.x = (v1.p.x * 0.5f + 0.5f) * Device.TargetWidth;
v1.p.y = (-v1.p.y * 0.5f + 0.5f) * Device.TargetHeight;

_lines.emplace_back(v0, v1);
}
}
} rdebug_render_impl;
dxDebugRender *rdebug_render = &rdebug_render_impl;
//
//struct RDebugRender :
// public dxDebugRender,
// public pureRender {
//private:
// xr_vector<std::pair<FVF::L, FVF::L>> _lines;
//
// // Vertices _line_vertices;
// // Indices _line_indices;
//public:
// RDebugRender() {
//#ifndef _EDITOR
// Device.seqRender.Add(this, REG_PRIORITY_LOW - 100);
//#endif
// }
//
// virtual ~RDebugRender() {
//#ifndef _EDITOR
// Device.seqRender.Remove(this);
//#endif
// }
//
// void OnRender() {
//
// m_lines = _lines;
// Render();
//
//
// }
// virtual void add_lines(Fvector const* vertices, u32 const& vertex_count, u32 const* pairs, u32 const& pair_count, u32 const& color) {
// _lines.resize(0);
// for(size_t i = 0; i < pair_count * 2; i += 2) {
// u32 i0 = pairs[i];
// u32 i1 = pairs[i + 1];
// FVF::L v0 = {};
// FVF::L v1 = {};
// v0.p = vertices[i0];
// v1.p = vertices[i1];
// v0.color = color;
// v1.color = color;
//
// Device.mView_saved.transform(v0.p);
// Device.mView_saved.transform(v1.p);
//
// if(v0.p.z > VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
// Fvector fp = v1.p;
// fp.lerp(v0.p, v1.p, (v0.p.z - VIEWPORT_NEAR) / (v0.p.z - v1.p.z));
// v1.p = fp;
// }
//
// if(v0.p.z <= VIEWPORT_NEAR && v1.p.z > VIEWPORT_NEAR) {
// Fvector fp = v0.p;
// fp.lerp(v1.p, v0.p, (v1.p.z - VIEWPORT_NEAR) / (v1.p.z - v0.p.z));
// v0.p = fp;
// }
//
// if(v0.p.z <= VIEWPORT_NEAR && v1.p.z <= VIEWPORT_NEAR) {
// continue;
// }
//
// if(!RImplementation.get_HUD()) {
// Device.mProject_saved.transform(v0.p);
// Device.mProject_saved.transform(v1.p);
// }
// else {
// Device.mProject_hud.transform(v0.p);
// Device.mProject_hud.transform(v1.p);
// }
//
// v0.p.x = (v0.p.x * 0.5f + 0.5f) * Device.TargetWidth;
// v0.p.y = (-v0.p.y * 0.5f + 0.5f) * Device.TargetHeight;
//
// v1.p.x = (v1.p.x * 0.5f + 0.5f) * Device.TargetWidth;
// v1.p.y = (-v1.p.y * 0.5f + 0.5f) * Device.TargetHeight;
//
// _lines.emplace_back(v0, v1);
// }
// }
//} rdebug_render_impl;
//dxDebugRender *rdebug_render = &rdebug_render_impl;

#endif // DEBUG
12 changes: 9 additions & 3 deletions src/Layers/xrRender/dxDebugRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ class dxDebugRender : public IDebugRender
};

public:
xr_vector<std::pair<FVF::L, FVF::L>> m_lines;
struct LineRenderPack {
float x1 = 0.0f, y1 = 0.0f;
float x2 = 0.0f, y2 = 0.0f;
u32 color = 0xffffffff;
};

xr_vector<LineRenderPack> m_lines;

private:
ref_shader m_dbgShaders[dbgShaderCount];
ref_shader m_dbgShaders[dbgShaderCount];
};

extern dxDebugRender DebugRenderImpl;
extern dxDebugRender* rdebug_render;
//extern dxDebugRender* rdebug_render;
#endif // DEBUG
8 changes: 4 additions & 4 deletions src/Layers/xrRender/dxRenderDeviceRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ void dxRenderDeviceRender::Create(SDL_Window* window, u32 &dwWidth, u32 &dwHeigh
for (const auto& Line : DebugRenderImpl.m_lines)
{
CmdList.AddLine(
ImVec2(Line.first.p.x + ViewPort->WorkPos.x, Line.first.p.y + ViewPort->WorkPos.y),
ImVec2(Line.second.p.x + ViewPort->WorkPos.x, Line.second.p.y + ViewPort->WorkPos.y),
Line.first.color
ImVec2(Line.x1, Line.y1),
ImVec2(Line.x2, Line.y2),
Line.color
);
}
DebugRenderImpl.m_lines.clear();
DebugRenderImpl.m_lines.resize(0);
}

ImGui::End();
Expand Down
2 changes: 1 addition & 1 deletion src/xrEngine/xr_ioc_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ void CCC_Register()
CMD3(CCC_Mask, "r_actor_shadow", &psGameFlags, rsActorShadow );

CMD3(CCC_Mask, "rs_cam_pos", &psDeviceFlags, rsCameraPos );
#ifdef DEBUG
#ifdef DEBUG_DRAW
CMD3(CCC_Mask, "rs_occ_draw", &psDeviceFlags, rsOcclusionDraw );
CMD3(CCC_Mask, "rs_occ_stats", &psDeviceFlags, rsOcclusionStats );
#endif // DEBUG
Expand Down

0 comments on commit 010de06

Please sign in to comment.