Skip to content

Commit

Permalink
UPBGE: Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
youle31 committed Mar 7, 2025
1 parent 0c0c0cf commit 1542aa8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 93 deletions.
6 changes: 1 addition & 5 deletions source/blender/draw/intern/DRW_render.hh
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,8 @@ void DRW_game_render_loop(struct bContext *C,
struct GPUViewport *viewport,
struct Depsgraph *depsgraph,
const struct rcti *window,
bool is_overlay_pass,
bool called_from_constructor);
bool is_overlay_pass);

void DRW_game_render_loop_end(void);
void DRW_game_python_loop_end(struct ViewLayer *view_layer);
void DRW_game_viewport_render_loop_end(Scene *scene);
void DRW_transform_to_display(struct GPUViewport *viewport,
Expand All @@ -300,8 +298,6 @@ void DRW_transform_to_display(struct GPUViewport *viewport,
struct Scene *scene,
struct rcti *rect);
void DRW_transform_to_display_image_render(struct GPUTexture *tex);
void DRW_game_gpu_viewport_set(struct GPUViewport *viewport);
struct GPUViewport *DRW_game_gpu_viewport_get(void);


/* Viewport render debug */
Expand Down
101 changes: 37 additions & 64 deletions source/blender/draw/intern/draw_manager_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1200,15 +1200,6 @@ static bool drw_gpencil_engine_needed(Depsgraph *depsgraph, View3D *v3d)

/* -------------------------------------------------------------------- */

/* UPBGE */
if (scene->flag & SCE_INTERACTIVE) {
/* Hack to allow bge to use depsgraph to detect
* all scene changes and notify drw_engine for redraw. */
viewport = DRW_game_gpu_viewport_get();
}
/* End of UPBGE */


/** \name Callbacks
* \{ */

Expand Down Expand Up @@ -1259,7 +1250,7 @@ void DRW_draw_callbacks_post_scene()
drw_debug_draw();

/* UPBGE */
drw_debug_draw_bge(DST.draw_ctx.scene);
drw_debug_draw_bge(drw_get().draw_ctx.scene);
GPU_matrix_projection_set(rv3d->winmat);
GPU_matrix_set(rv3d->viewmat);
/**************************/
Expand Down Expand Up @@ -1486,7 +1477,7 @@ static void DRW_draw_render_loop_3d(Depsgraph *depsgraph,
}

/* UPBGE */
update_lods(depsgraph, ob, DST.draw_ctx.rv3d->viewinv[3]);
update_lods(depsgraph, ob, drw_get().draw_ctx.rv3d->viewinv[3]);
/* End of UPBGE */

blender::draw::ObjectRef ob_ref(data_, ob);
Expand Down Expand Up @@ -3081,7 +3072,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(DST.debug_bge.lines, line);
BLI_LINKS_PREPEND(drw_get().debug_bge.lines, line);
}

void DRW_debug_box_2D_bge(const float xco, const float yco, const float xsize, const float ysize)
Expand All @@ -3091,7 +3082,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(DST.debug_bge.boxes, box);
BLI_LINKS_PREPEND(drw_get().debug_bge.boxes, box);
}

void DRW_debug_text_2D_bge(const float xco, const float yco, const char *str)
Expand All @@ -3100,12 +3091,12 @@ 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(DST.debug_bge.texts, text);
BLI_LINKS_PREPEND(drw_get().debug_bge.texts, text);
}

static void drw_debug_draw_lines_bge(void)
{
int count = BLI_linklist_count((LinkNode *)DST.debug_bge.lines);
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.lines);

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

immBegin(GPU_PRIM_LINES, count * 2);

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

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

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

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

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

static void drw_debug_draw_boxes_bge(void)
{
int count = BLI_linklist_count((LinkNode *)DST.debug_bge.boxes);
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.boxes);

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

immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

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

static void drw_debug_draw_text_bge(Scene *scene)
{
int count = BLI_linklist_count((LinkNode *)DST.debug_bge.texts);
int count = BLI_linklist_count((LinkNode *)drw_get().debug_bge.texts);

if (count == 0) {
return;
Expand Down Expand Up @@ -3216,14 +3207,14 @@ 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 (DST.debug_bge.texts) {
void *next = DST.debug_bge.texts->next;
DRWDebugText2D *t = DST.debug_bge.texts;
while (drw_get().debug_bge.texts) {
void *next = drw_get().debug_bge.texts->next;
DRWDebugText2D *t = drw_get().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(DST.debug_bge.texts);
DST.debug_bge.texts = (DRWDebugText2D *)next;
MEM_freeN(drw_get().debug_bge.texts);
drw_get().debug_bge.texts = (DRWDebugText2D *)next;
}
BLF_disable(blf_mono_font, BLF_SHADOW);
}
Expand All @@ -3242,12 +3233,9 @@ void DRW_game_render_loop(bContext *C,
GPUViewport *viewport,
Depsgraph *depsgraph,
const rcti *window,
bool is_overlay_pass,
bool called_from_constructor)
bool is_overlay_pass)
{
using namespace blender::draw;
/* Reset before using it. */
DST.prepare_clean_for_draw();

Scene *scene = DEG_get_evaluated_scene(depsgraph);
ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
Expand All @@ -3263,6 +3251,10 @@ void DRW_game_render_loop(bContext *C,
/* Resize viewport if needed and set active view */
GPU_viewport_bind(viewport, 0, window);

g_context->state_ensure_not_reused();
DRWContext draw_ctx;
drw_set(draw_ctx);

drw_get().draw_ctx = {};
drw_get().draw_ctx.region = ar;
drw_get().draw_ctx.rv3d = rv3d;
Expand All @@ -3276,15 +3268,15 @@ void DRW_game_render_loop(bContext *C,
/* reuse if caller sets */
drw_get().draw_ctx.evil_C = C;

DST.options.draw_background = ((scene->r.alphamode == R_ADDSKY) ||
drw_get().options.draw_background = ((scene->r.alphamode == R_ADDSKY) ||
(v3d->shading.type != OB_RENDER)) &&
!is_overlay_pass;

drw_task_graph_init();
drw_context_state_init();

/* No need to pass size as argument since it is set in GPU_viewport_bind above */
drw_manager_init(&DST, viewport, NULL);
drw_manager_init(g_context, viewport, nullptr);

bool gpencil_engine_needed = drw_gpencil_engine_needed(depsgraph, v3d);

Expand All @@ -3306,7 +3298,7 @@ void DRW_game_render_loop(bContext *C,
drw_engines_init();

drw_engines_cache_init();
drw_engines_world_update(DST.draw_ctx.scene);
drw_engines_world_update(scene);

DupliCacheManager dupli_handler;

Expand Down Expand Up @@ -3384,16 +3376,9 @@ void DRW_game_render_loop(bContext *C,

drw_engines_disable();

if (!called_from_constructor) {
drw_manager_exit(&DST);
}

GPU_viewport_unbind(DST.viewport);
}
drw_manager_exit(&draw_ctx);

void DRW_game_render_loop_end()
{
GPU_viewport_free(DRW_game_gpu_viewport_get());
GPU_viewport_unbind(viewport);
}

void DRW_game_viewport_render_loop_end(Scene *scene)
Expand All @@ -3410,7 +3395,7 @@ void DRW_game_python_loop_end(ViewLayer * /*view_layer*/)
*/
// GPU_viewport_free(DST.viewport);

DST.prepare_clean_for_draw();
drw_get().prepare_clean_for_draw();

/*use_drw_engine(&draw_engine_eevee_type);
Expand Down Expand Up @@ -3565,18 +3550,6 @@ void DRW_transform_to_display(GPUViewport *viewport,
}
}

static GPUViewport *current_game_viewport = NULL;

void DRW_game_gpu_viewport_set(GPUViewport *viewport)
{
current_game_viewport = viewport;
}

GPUViewport *DRW_game_gpu_viewport_get()
{
return current_game_viewport;
}

bool is_eevee_next(const Scene *scene)
{
return RE_engines_find(scene->r.engine) == &DRW_engine_viewport_eevee_next_type;
Expand Down
14 changes: 9 additions & 5 deletions source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ extern "C" void StartKetsjiShell(struct bContext *C,
*/
RefreshContextAndScreen(C, wm, win, bfd->curscene);

/* ED_screen_init can change bContext then we need to restore it again after...
* b9907cb60b3c37e55cc8ea186e6cca26e333a039 */
InitBlenderContextVariables(C, wm, bfd->curscene);

if (blenderdata) {
BLI_strncpy(pathname, blenderdata->filepath, sizeof(pathname));
// Change G_MAIN path to ensure loading of data using relative paths.
Expand Down Expand Up @@ -396,21 +400,21 @@ extern "C" void StartKetsjiShell(struct bContext *C,
G_MAIN = G.main = maggie1;
CTX_data_main_set(C, maggie1);
CTX_wm_manager_set(C, wm_backup);
CTX_wm_window_set(C, win_backup);
win_backup->ghostwin = ghostwin_backup;
win_backup->gpuctx = gpuctx_backup;
wm_backup->message_bus = (wmMsgBus *)msgbus_backup;
}
else {
CTX_wm_window_set(C,
win_backup); // Fix for crash at exit when we have preferences window open
}
CTX_wm_window_set(C, win_backup); // Fix for crash at exit when we have preferences window open

RefreshContextAndScreen(C, wm_backup, win_backup, startscene);

/* ED_screen_init must be called to fix https://github.com/UPBGE/upbge/issues/1388 */
ED_screens_init(C,maggie1, wm_backup);

/* ED_screen_init can change bContext then we need to restore it again after...
* b9907cb60b3c37e55cc8ea186e6cca26e333a039 */
InitBlenderContextVariables(C, wm_backup, startscene);

/* Restore shading type we had before game start */
CTX_wm_view3d(C)->shading.type = shadingTypeBackup;

Expand Down
3 changes: 3 additions & 0 deletions source/gameengine/GamePlayer/GPG_ghost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,9 @@ int main(int argc,
if ((wm->init_flag & WM_INIT_FLAG_WINDOW) == 0) {
ED_screens_init(C, G_MAIN, wm);
wm->init_flag |= WM_INIT_FLAG_WINDOW;
/* ED_screen_init can change bContext then we need to restore it again
* after... b9907cb60b3c37e55cc8ea186e6cca26e333a039 */
InitBlenderContextVariables(C, wm, bfd->curscene);
}

WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
Expand Down
2 changes: 1 addition & 1 deletion source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ void KX_KetsjiEngine::Render()
GPU_matrix_ortho_set(0, width, 0, height, -100, 100);
GPU_matrix_identity_set();

DRW_transform_to_display(DRW_game_gpu_viewport_get(),
DRW_transform_to_display(m_scenes->GetFront()->GetCurrentGPUViewport(),
GPU_framebuffer_color_texture(background_fb->GetFrameBuffer()),
CTX_wm_view3d(m_context),
CTX_data_scene(m_context),
Expand Down
19 changes: 5 additions & 14 deletions source/gameengine/Ketsji/KX_Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ KX_Scene::~KX_Scene()

if (!KX_GetActiveEngine()->UseViewportRender()) {
if (!m_isPythonMainLoop) {
/* This will free m_gpuViewport and m_gpuOffScreen */
DRW_game_render_loop_end();
/* This will free m_currentGPUViewport */
GPU_viewport_free(m_currentGPUViewport);
}
else {
/* If we are in python loop and we called render code */
if (!m_initMaterialsGPUViewport) {
DRW_game_render_loop_end();
GPU_viewport_free(m_currentGPUViewport);
}
else {
/* It has not been freed before because the main Render loop
Expand All @@ -353,7 +353,6 @@ KX_Scene::~KX_Scene()
//DRW_game_python_loop_end(DEG_get_evaluated_view_layer(depsgraph));
}
}
DRW_game_gpu_viewport_set(nullptr);
}
else {
// Free the allocated profile a last time
Expand Down Expand Up @@ -627,13 +626,6 @@ void KX_Scene::OverlayPassDisableEffects(Depsgraph *depsgraph,
void KX_Scene::SetCurrentGPUViewport(GPUViewport *viewport)
{
m_currentGPUViewport = viewport;

/* We set a GPUViewport as soon as possible
* to be able to call DRW_notify_view_update.
* The GPUViewport set doesn't really matter
* (as far I understood) but we need to have
* one set when we use custom bge render loop. */
DRW_game_gpu_viewport_set(viewport);
}

GPUViewport *KX_Scene::GetCurrentGPUViewport()
Expand Down Expand Up @@ -713,7 +705,6 @@ void KX_Scene::RenderAfterCameraSetup(KX_Camera *cam,
bool calledFromConstructor = cam == nullptr;
if (calledFromConstructor) {
m_currentGPUViewport = GPU_viewport_create();
DRW_game_gpu_viewport_set(m_currentGPUViewport);
SetInitMaterialsGPUViewport(m_currentGPUViewport);
}
else {
Expand Down Expand Up @@ -867,7 +858,7 @@ void KX_Scene::RenderAfterCameraSetup(KX_Camera *cam,
}
/* Draw custom viewport render loop into its own GPUViewport */
DRW_game_render_loop(
C, m_currentGPUViewport, depsgraph, &window, is_overlay_pass, cam == nullptr);
C, m_currentGPUViewport, depsgraph, &window, is_overlay_pass);
}

RAS_FrameBuffer *input = rasty->GetFrameBuffer(rasty->NextFilterFrameBuffer(r));
Expand Down Expand Up @@ -931,7 +922,7 @@ void KX_Scene::RenderAfterCameraSetupImageRender(KX_Camera *cam, const rcti *win
winmat,
NULL);

DRW_game_render_loop(C, m_currentGPUViewport, depsgraph, window, false, false);
DRW_game_render_loop(C, m_currentGPUViewport, depsgraph, window, false);
}

void KX_Scene::SetBlenderSceneConverter(BL_SceneConverter *sc_converter)
Expand Down
Loading

0 comments on commit 1542aa8

Please sign in to comment.