Skip to content

Commit

Permalink
tmp fix for profile drawing when "use viewport render"
Browse files Browse the repository at this point in the history
  • Loading branch information
youle31 committed Mar 7, 2025
1 parent 1542aa8 commit e524366
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 63 deletions.
4 changes: 3 additions & 1 deletion source/blender/draw/intern/DRW_render.hh
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void DRW_game_render_loop(struct bContext *C,
bool is_overlay_pass);

void DRW_game_python_loop_end(struct ViewLayer *view_layer);
void DRW_game_viewport_render_loop_end(Scene *scene);
void DRW_game_viewport_render_loop_end();
void DRW_transform_to_display(struct GPUViewport *viewport,
struct GPUTexture *tex,
struct View3D *v3d,
Expand All @@ -301,6 +301,8 @@ void DRW_transform_to_display_image_render(struct GPUTexture *tex);


/* Viewport render debug */
void DRW_start_debug_bge_viewport();
void DRW_end_debug_bge_viewport();
void DRW_debug_line_bge(const float v1[3], const float v2[3], const float color[4]);
void DRW_debug_box_2D_bge(const float xco, const float yco, const float xsize, const float ysize);
void DRW_debug_text_2D_bge(const float xco, const float yco, const char *str);
Expand Down
124 changes: 98 additions & 26 deletions source/blender/draw/intern/draw_manager_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3063,7 +3063,50 @@ void DRW_gpu_context_activate(bool drw_state)

/*--UPBGE Viewport Debug Drawing --*/

/* ------------- DRAW DEBUG - UPBGE ------------ */

typedef struct DRWDebugLine {
struct DRWDebugLine *next; /* linked list */
float pos[2][3];
float color[4];
} DRWDebugLine;

typedef struct DRWDebugText2D {
struct DRWDebugText2D *next; /* linked list */
char text[64];
float xco;
float yco;
} DRWDebugText2D;

typedef struct DRWDebugBox2D {
struct DRWDebugBox2D *next; /* linked list */
float xco;
float yco;
float xsize;
float ysize;
} DRWDebugBox2D;

typedef struct DRWDebugBge {
DRWDebugLine *lines;
DRWDebugBox2D *boxes;
DRWDebugText2D *texts;
} DRWDebugBge;

/* End of UPBGE */

static float g_modelmat[4][4];
thread_local DRWDebugBge *debug_bge = nullptr;

void DRW_start_debug_bge_viewport()
{
debug_bge = MEM_new<DRWDebugBge>("debug_bge");
}

void DRW_end_debug_bge_viewport()
{
MEM_delete(debug_bge);
debug_bge = nullptr;
}

void DRW_debug_line_bge(const float v1[3], const float v2[3], const float color[4])
{
Expand All @@ -3072,7 +3115,7 @@ void DRW_debug_line_bge(const float v1[3], const float v2[3], const float color[
mul_v3_m4v3(line->pos[0], g_modelmat, v1);
mul_v3_m4v3(line->pos[1], g_modelmat, v2);
copy_v4_v4(line->color, color);
BLI_LINKS_PREPEND(drw_get().debug_bge.lines, line);
BLI_LINKS_PREPEND(debug_bge->lines, line);
}

void DRW_debug_box_2D_bge(const float xco, const float yco, const float xsize, const float ysize)
Expand All @@ -3082,7 +3125,7 @@ void DRW_debug_box_2D_bge(const float xco, const float yco, const float xsize, c
box->yco = yco;
box->xsize = xsize;
box->ysize = ysize;
BLI_LINKS_PREPEND(drw_get().debug_bge.boxes, box);
BLI_LINKS_PREPEND(debug_bge->boxes, box);
}

void DRW_debug_text_2D_bge(const float xco, const float yco, const char *str)
Expand All @@ -3091,12 +3134,15 @@ void DRW_debug_text_2D_bge(const float xco, const float yco, const char *str)
text->xco = xco;
text->yco = yco;
strncpy(text->text, str, 64);
BLI_LINKS_PREPEND(drw_get().debug_bge.texts, text);
BLI_LINKS_PREPEND(debug_bge->texts, text);
}

static void drw_debug_draw_lines_bge(void)
{
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.lines);
if (!debug_bge) {
return;
}
int count = BLI_linklist_count((LinkNode *)debug_bge->lines);

if (count == 0) {
return;
Expand All @@ -3113,17 +3159,17 @@ static void drw_debug_draw_lines_bge(void)

immBegin(GPU_PRIM_LINES, count * 2);

while (drw_get().debug_bge.lines) {
void *next = drw_get().debug_bge.lines->next;
while (debug_bge->lines) {
void *next = debug_bge->lines->next;

immAttr4fv(col, drw_get().debug_bge.lines->color);
immVertex3fv(pos, drw_get().debug_bge.lines->pos[0]);
immAttr4fv(col, debug_bge->lines->color);
immVertex3fv(pos, debug_bge->lines->pos[0]);

immAttr4fv(col, drw_get().debug_bge.lines->color);
immVertex3fv(pos, drw_get().debug_bge.lines->pos[1]);
immAttr4fv(col, debug_bge->lines->color);
immVertex3fv(pos, debug_bge->lines->pos[1]);

MEM_freeN(drw_get().debug_bge.lines);
drw_get().debug_bge.lines = (DRWDebugLine *)next;
MEM_freeN(debug_bge->lines);
debug_bge->lines = (DRWDebugLine *)next;
}
immEnd();

Expand All @@ -3134,7 +3180,10 @@ static void drw_debug_draw_lines_bge(void)

static void drw_debug_draw_boxes_bge(void)
{
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.boxes);
if (!debug_bge) {
return;
}
int count = BLI_linklist_count((LinkNode *)debug_bge->boxes);

if (count == 0) {
return;
Expand All @@ -3152,20 +3201,23 @@ static void drw_debug_draw_boxes_bge(void)

immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

while (drw_get().debug_bge.boxes) {
void *next = drw_get().debug_bge.boxes->next;
DRWDebugBox2D *b = drw_get().debug_bge.boxes;
while (debug_bge->boxes) {
void *next = debug_bge->boxes->next;
DRWDebugBox2D *b = debug_bge->boxes;
immUniformColor4fv(white);
immRectf(pos, b->xco + 1 + b->xsize, b->yco + b->ysize, b->xco, b->yco);
MEM_freeN(drw_get().debug_bge.boxes);
drw_get().debug_bge.boxes = (DRWDebugBox2D *)next;
MEM_freeN(debug_bge->boxes);
debug_bge->boxes = (DRWDebugBox2D *)next;
}
immUnbindProgram();
}

static void drw_debug_draw_text_bge(Scene *scene)
{
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.texts);
if (!debug_bge) {
return;
}
int count = BLI_linklist_count((LinkNode *)debug_bge->texts);

if (count == 0) {
return;
Expand Down Expand Up @@ -3207,24 +3259,27 @@ static void drw_debug_draw_text_bge(Scene *scene)
BLF_shadow(blf_mono_font, FontShadowType::Blur3x3, black);
BLF_shadow_offset(blf_mono_font, 1, 1);

while (drw_get().debug_bge.texts) {
void *next = drw_get().debug_bge.texts->next;
DRWDebugText2D *t = drw_get().debug_bge.texts;
while (debug_bge->texts) {
void *next = debug_bge->texts->next;
DRWDebugText2D *t = debug_bge->texts;
BLF_color4fv(blf_mono_font, white);
BLF_position(blf_mono_font, t->xco, t->yco, 0.0f);
BLF_draw(blf_mono_font, t->text, BLF_DRAW_STR_DUMMY_MAX);
MEM_freeN(drw_get().debug_bge.texts);
drw_get().debug_bge.texts = (DRWDebugText2D *)next;
MEM_freeN(debug_bge->texts);
debug_bge->texts = (DRWDebugText2D *)next;
}
BLF_disable(blf_mono_font, BLF_SHADOW);
}

void drw_debug_draw_bge(Scene *scene)
{
blender::draw::command::StateSet::set(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);

drw_debug_draw_lines_bge();
drw_debug_draw_boxes_bge();
drw_debug_draw_text_bge(scene);

DRW_end_debug_bge_viewport();
}

/*--End of UPBGE Viewport Debug Drawing--*/
Expand Down Expand Up @@ -3381,9 +3436,26 @@ void DRW_game_render_loop(bContext *C,
GPU_viewport_unbind(viewport);
}

void DRW_game_viewport_render_loop_end(Scene *scene)
void DRW_game_viewport_render_loop_end()
{
drw_debug_draw_bge(scene);
while (debug_bge->lines) {
void *next = debug_bge->lines->next;

MEM_freeN(debug_bge->lines);
debug_bge->lines = (DRWDebugLine *)next;
}
while (debug_bge->boxes) {
void *next = debug_bge->boxes->next;

MEM_freeN(debug_bge->boxes);
debug_bge->boxes = (DRWDebugBox2D *)next;
}
while (debug_bge->texts) {
void *next = debug_bge->texts->next;

MEM_freeN(debug_bge->texts);
debug_bge->texts = (DRWDebugText2D *)next;
}
}

void DRW_game_python_loop_end(ViewLayer * /*view_layer*/)
Expand Down
35 changes: 0 additions & 35 deletions source/blender/draw/intern/draw_manager_c.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,6 @@ struct DRWData {
void modules_exit();
};

/* ------------- DRAW DEBUG - UPBGE ------------ */

typedef struct DRWDebugLine {
struct DRWDebugLine *next; /* linked list */
float pos[2][3];
float color[4];
} DRWDebugLine;

/* UPBGE */
typedef struct DRWDebugText2D {
struct DRWDebugText2D *next; /* linked list */
char text[64];
float xco;
float yco;
} DRWDebugText2D;

typedef struct DRWDebugBox2D {
struct DRWDebugBox2D *next; /* linked list */
float xco;
float yco;
float xsize;
float ysize;
} DRWDebugBox2D;

typedef struct DRWDebugBge {
DRWDebugLine *lines;
DRWDebugBox2D *boxes;
DRWDebugText2D *texts;
} DRWDebugBge;

/* End of UPBGE */

/** \} */

/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -142,9 +110,6 @@ struct DRWContext {
/* Contains list of objects that needs to be extracted from other objects. */
GSet *delayed_extraction = nullptr;

DRWDebugBge debug_bge;


/* Reset all members before drawing in order to avoid undefined state. */
void prepare_clean_for_draw();
/* Poison all members to detect missing `prepare_clean_for_draw()`. */
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ KX_Scene::~KX_Scene()
}
else {
// Free the allocated profile a last time
DRW_game_viewport_render_loop_end(GetBlenderScene());
DRW_game_viewport_render_loop_end();
}

/* Fixes issue when switching .blend erm...*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void RAS_OpenGLDebugDraw::Flush(RAS_Rasterizer *rasty,
RAS_DebugDraw *debugDraw)
{
if (KX_GetActiveEngine()->UseViewportRender()) {
DRW_start_debug_bge_viewport();

/* Draw Debug lines */
if (!debugDraw->m_lines.empty()) {
for (int i = 0; i < debugDraw->m_lines.size(); i++) {
Expand Down

0 comments on commit e524366

Please sign in to comment.