Changeset View
Standalone View
source/blender/draw/engines/external/external_engine.c
| Show All 28 Lines | ||||||||||
| #include "DNA_screen_types.h" | #include "DNA_screen_types.h" | |||||||||
| #include "DNA_view3d_types.h" | #include "DNA_view3d_types.h" | |||||||||
| #include "BKE_object.h" | #include "BKE_object.h" | |||||||||
| #include "BKE_particle.h" | #include "BKE_particle.h" | |||||||||
| #include "ED_screen.h" | #include "ED_screen.h" | |||||||||
| #include "GPU_batch.h" | ||||||||||
| #include "GPU_debug.h" | ||||||||||
| #include "GPU_matrix.h" | #include "GPU_matrix.h" | |||||||||
| #include "GPU_shader.h" | #include "GPU_shader.h" | |||||||||
| #include "GPU_state.h" | #include "GPU_state.h" | |||||||||
| #include "GPU_viewport.h" | #include "GPU_viewport.h" | |||||||||
| #include "RE_engine.h" | ||||||||||
| #include "RE_pipeline.h" | ||||||||||
| #include "external_engine.h" /* own include */ | #include "external_engine.h" /* own include */ | |||||||||
| /* Shaders */ | /* Shaders */ | |||||||||
| #define EXTERNAL_ENGINE "BLENDER_EXTERNAL" | #define EXTERNAL_ENGINE "BLENDER_EXTERNAL" | |||||||||
| extern char datatoc_depth_frag_glsl[]; | extern char datatoc_depth_frag_glsl[]; | |||||||||
| extern char datatoc_depth_vert_glsl[]; | extern char datatoc_depth_vert_glsl[]; | |||||||||
| ▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | static void external_engine_init(void *vedata) | |||||||||
| /* Progressive render samples are tagged with no rebuild, in that case we | /* Progressive render samples are tagged with no rebuild, in that case we | |||||||||
| * can skip updating the depth buffer */ | * can skip updating the depth buffer */ | |||||||||
| if (region && (region->do_draw & RGN_DRAW_NO_REBUILD)) { | if (region && (region->do_draw & RGN_DRAW_NO_REBUILD)) { | |||||||||
| stl->g_data->update_depth = false; | stl->g_data->update_depth = false; | |||||||||
| } | } | |||||||||
| } | } | |||||||||
| static void image_cache_image_add(DRWShadingGroup *grp) | ||||||||||
| { | ||||||||||
| float obmat[4][4]; | ||||||||||
| unit_m4(obmat); | ||||||||||
| scale_m4_fl(obmat, 0.5f); | ||||||||||
| translate_m4(obmat, 1.0f, 1.0f, 0.75f); | ||||||||||
| GPUBatch *geom = DRW_cache_quad_get(); | ||||||||||
| DRW_shgroup_call_obmat(grp, geom, obmat); | ||||||||||
| } | ||||||||||
| static void external_cache_init(void *vedata) | static void external_cache_init(void *vedata) | |||||||||
| { | { | |||||||||
| EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl; | EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl; | |||||||||
| EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | |||||||||
| EXTERNAL_TextureList *txl = ((EXTERNAL_Data *)vedata)->txl; | EXTERNAL_TextureList *txl = ((EXTERNAL_Data *)vedata)->txl; | |||||||||
| EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl; | EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl; | |||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | |||||||||
| const View3D *v3d = draw_ctx->v3d; | const eSpace_Type space_type = draw_ctx->space_data->spacetype; | |||||||||
| { | { | |||||||||
| DRW_texture_ensure_fullscreen_2d(&txl->depth_buffer_tx, GPU_DEPTH24_STENCIL8, 0); | DRW_texture_ensure_fullscreen_2d(&txl->depth_buffer_tx, GPU_DEPTH24_STENCIL8, 0); | |||||||||
| GPU_framebuffer_ensure_config(&fbl->depth_buffer_fb, | GPU_framebuffer_ensure_config(&fbl->depth_buffer_fb, | |||||||||
| { | { | |||||||||
| GPU_ATTACHMENT_TEXTURE(txl->depth_buffer_tx), | GPU_ATTACHMENT_TEXTURE(txl->depth_buffer_tx), | |||||||||
| }); | }); | |||||||||
| } | } | |||||||||
| /* Depth Pass */ | /* Depth Pass */ | |||||||||
| { | { | |||||||||
| psl->depth_pass = DRW_pass_create("Depth Pass", | psl->depth_pass = DRW_pass_create("Depth Pass", | |||||||||
| DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL); | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL); | |||||||||
| stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass); | stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass); | |||||||||
| } | } | |||||||||
| if (space_type == SPACE_VIEW3D) { | ||||||||||
| /* Do not draw depth pass when overlays are turned off. */ | /* Do not draw depth pass when overlays are turned off. */ | |||||||||
| const View3D *v3d = draw_ctx->v3d; | ||||||||||
| stl->g_data->need_depth = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0; | stl->g_data->need_depth = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0; | |||||||||
| } | } | |||||||||
| else if (space_type == SPACE_IMAGE) { | ||||||||||
| image_cache_image_add(stl->g_data->depth_shgrp); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| static void external_cache_populate(void *vedata, Object *ob) | static void external_cache_populate(void *vedata, Object *ob) | |||||||||
| { | { | |||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
jbakker: Additional speedup can be get when modify-ing `DRW_draw_render_loop_2d_ex#do_populate_loop`.
I… | ||||||||||
Done Inline ActionsSeems that the optimization you're mentioning can be performed in the master branch by checking the image type. There is even a TODO there ;) sergey: Seems that the optimization you're mentioning can be performed in the master branch by checking… | ||||||||||
| EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | |||||||||
| const eSpace_Type space_type = draw_ctx->space_data->spacetype; | ||||||||||
| if (space_type == SPACE_IMAGE) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| if (!(DRW_object_is_renderable(ob) && | if (!(DRW_object_is_renderable(ob) && | |||||||||
| DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) { | DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) { | |||||||||
| return; | return; | |||||||||
| } | } | |||||||||
| if (ob->type == OB_GPENCIL) { | if (ob->type == OB_GPENCIL) { | |||||||||
| /* Grease Pencil objects need correct depth to do the blending. */ | /* Grease Pencil objects need correct depth to do the blending. */ | |||||||||
| Show All 25 Lines | if (geom) { | |||||||||
| DRW_shgroup_call(stl->g_data->depth_shgrp, geom, ob); | DRW_shgroup_call(stl->g_data->depth_shgrp, geom, ob); | |||||||||
| } | } | |||||||||
| } | } | |||||||||
| static void external_cache_finish(void *UNUSED(vedata)) | static void external_cache_finish(void *UNUSED(vedata)) | |||||||||
| { | { | |||||||||
| } | } | |||||||||
| static void external_draw_scene_do(void *vedata) | static void external_draw_scene_do_v3d(void *vedata) | |||||||||
| { | { | |||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | |||||||||
| const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | ||||||||||
| Scene *scene = draw_ctx->scene; | Scene *scene = draw_ctx->scene; | |||||||||
| RegionView3D *rv3d = draw_ctx->rv3d; | RegionView3D *rv3d = draw_ctx->rv3d; | |||||||||
| ARegion *region = draw_ctx->region; | ARegion *region = draw_ctx->region; | |||||||||
| const RenderEngineType *type; | ||||||||||
| const float clear_col[4] = {0, 0, 0, 0}; | ||||||||||
| /* This is to keep compatibility with external engine. */ | ||||||||||
| /* TODO(fclem): remove it eventually. */ | ||||||||||
| GPU_framebuffer_bind(dfbl->default_fb); | ||||||||||
| GPU_framebuffer_clear_color(dfbl->default_fb, clear_col); | ||||||||||
| DRW_state_reset_ex(DRW_STATE_DEFAULT & ~DRW_STATE_DEPTH_LESS_EQUAL); | DRW_state_reset_ex(DRW_STATE_DEFAULT & ~DRW_STATE_DEPTH_LESS_EQUAL); | |||||||||
| /* Create render engine. */ | /* Create render engine. */ | |||||||||
| if (!rv3d->render_engine) { | if (!rv3d->render_engine) { | |||||||||
| RenderEngineType *engine_type = draw_ctx->engine_type; | RenderEngineType *engine_type = draw_ctx->engine_type; | |||||||||
| if (!(engine_type->view_update && engine_type->view_draw)) { | if (!(engine_type->view_update && engine_type->view_draw)) { | |||||||||
| return; | return; | |||||||||
| } | } | |||||||||
| RenderEngine *engine = RE_engine_create(engine_type); | RenderEngine *engine = RE_engine_create(engine_type); | |||||||||
| engine->tile_x = scene->r.tilex; | engine->tile_x = scene->r.tilex; | |||||||||
| engine->tile_y = scene->r.tiley; | engine->tile_y = scene->r.tiley; | |||||||||
| engine_type->view_update(engine, draw_ctx->evil_C, draw_ctx->depsgraph); | engine_type->view_update(engine, draw_ctx->evil_C, draw_ctx->depsgraph); | |||||||||
| rv3d->render_engine = engine; | rv3d->render_engine = engine; | |||||||||
| } | } | |||||||||
| /* Rendered draw. */ | /* Rendered draw. */ | |||||||||
| GPU_matrix_push_projection(); | GPU_matrix_push_projection(); | |||||||||
| GPU_matrix_push(); | GPU_matrix_push(); | |||||||||
| ED_region_pixelspace(region); | ED_region_pixelspace(region); | |||||||||
| /* Render result draw. */ | /* Render result draw. */ | |||||||||
| type = rv3d->render_engine->type; | const RenderEngineType *type = rv3d->render_engine->type; | |||||||||
| type->view_draw(rv3d->render_engine, draw_ctx->evil_C, draw_ctx->depsgraph); | type->view_draw(rv3d->render_engine, draw_ctx->evil_C, draw_ctx->depsgraph); | |||||||||
| GPU_bgl_end(); | GPU_bgl_end(); | |||||||||
| GPU_matrix_pop(); | GPU_matrix_pop(); | |||||||||
| GPU_matrix_pop_projection(); | GPU_matrix_pop_projection(); | |||||||||
| /* Set render info. */ | /* Set render info. */ | |||||||||
| EXTERNAL_Data *data = vedata; | EXTERNAL_Data *data = vedata; | |||||||||
| if (rv3d->render_engine->text[0] != '\0') { | if (rv3d->render_engine->text[0] != '\0') { | |||||||||
| BLI_strncpy(data->info, rv3d->render_engine->text, sizeof(data->info)); | BLI_strncpy(data->info, rv3d->render_engine->text, sizeof(data->info)); | |||||||||
| } | } | |||||||||
| else { | else { | |||||||||
| data->info[0] = '\0'; | data->info[0] = '\0'; | |||||||||
| } | } | |||||||||
| } | } | |||||||||
| static void external_draw_scene(void *vedata) | // XXX: Somehow fit into more common routine of drawing. | |||||||||
| static bool draw_render_engine(const bContext *C) | ||||||||||
| { | { | |||||||||
| Scene *scene = CTX_data_scene(C); | ||||||||||
| Render *re = RE_GetSceneRender(scene); | ||||||||||
| if (re == NULL) { | ||||||||||
Not Done Inline ActionsHmm... current structure would still allocate shader passes and shader groups. We could not register the external engine when the prerequisites have not met (draw_manager.c) or invoke this method in the populate_init and don't construct the passes/groups in this case. The second option is mostly how we solve this. Note: DRW_engine_external_use_for_image_editor could also be a good location to block the external engine registration. jbakker: Hmm...
current structure would still allocate shader passes and shader groups. We could not… | ||||||||||
Done Inline ActionsCan you please rephrase the comment? DRW_engine_external_use_for_image_editor will actually enforce use of image_engine if either scene's Render or render's engine is missing. sergey: Can you please rephrase the comment?
`DRW_engine_external_use_for_image_editor` will actually… | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| RenderEngine *engine = RE_engine_get(re); | ||||||||||
| if (re == NULL || engine == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| if (scene->r.engine == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| const RenderEngineType *type = RE_engines_find(scene->r.engine); | ||||||||||
| if (type == NULL || type->draw == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C); | ||||||||||
| type->draw(engine, C, depsgraph); | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
| static void external_draw_scene_do_image(void *vedata) | ||||||||||
| { | ||||||||||
| const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | |||||||||
| EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | const DRWView *view = DRW_view_get_active(); | |||||||||
| EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl; | EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl; | |||||||||
| EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl; | EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl; | |||||||||
| const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | ||||||||||
| float view_matrix[4][4]; | ||||||||||
| DRW_view_viewmat_get(view, view_matrix, false); | ||||||||||
| float projection_matrix[4][4]; | ||||||||||
| DRW_view_winmat_get(view, projection_matrix, false); | ||||||||||
| GPU_matrix_push_projection(); | ||||||||||
| GPU_matrix_push(); | ||||||||||
| GPU_matrix_projection_set(projection_matrix); | ||||||||||
| GPU_matrix_set(view_matrix); | ||||||||||
| GPU_framebuffer_bind(dfbl->color_only_fb); | ||||||||||
| static float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; | ||||||||||
| GPU_framebuffer_clear_color(dfbl->color_only_fb, clear_col); | ||||||||||
| GPU_debug_group_begin("External Engine"); | ||||||||||
| draw_render_engine(draw_ctx->evil_C); | ||||||||||
| GPU_debug_group_end(); | ||||||||||
| GPU_framebuffer_bind(dfbl->depth_only_fb); | ||||||||||
| GPU_framebuffer_clear_depth(dfbl->depth_only_fb, 1.0f); | ||||||||||
| DRW_draw_pass(psl->depth_pass); | ||||||||||
| GPU_framebuffer_blit(dfbl->default_fb, 0, dfbl->depth_only_fb, 0, GPU_DEPTH_BIT); | ||||||||||
| DRW_state_reset(); | ||||||||||
| GPU_matrix_pop(); | ||||||||||
| GPU_matrix_pop_projection(); | ||||||||||
| } | ||||||||||
| static void external_draw_scene_do(void *vedata) | ||||||||||
| { | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| const char space_type = draw_ctx->space_data->spacetype; | ||||||||||
Not Done Inline Actions
fclem: | ||||||||||
| if (draw_ctx->v3d != NULL) { | ||||||||||
| external_draw_scene_do_v3d(vedata); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| if (space_type == SPACE_IMAGE) { | ||||||||||
| external_draw_scene_do_image(vedata); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
Not Done Inline ActionsI would suggest renaming the function to external_image_space_matrix_set to make it clear it sets the matrices and to not confuse it with external_cache_init (was my first thought). fclem: I would suggest renaming the function to `external_image_space_matrix_set` to make it clear it… | ||||||||||
| } | ||||||||||
| static void external_draw_scene(void *vedata) | ||||||||||
| { | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| /* Will be NULL during OpenGL render. | /* Will be NULL during OpenGL render. | |||||||||
| * OpenGL render is used for quick preview (thumbnails or sequencer preview) | * OpenGL render is used for quick preview (thumbnails or sequencer preview) | |||||||||
| * where using the rendering engine to preview doesn't make so much sense. */ | * where using the rendering engine to preview doesn't make so much sense. */ | |||||||||
Not Done Inline Actionsexternal engines could alter the state of the GPU. Internal flags could be different than we expect. We might need a DRW_state_reset to make sure we can trust the internal and GPU state. In the viewport we added fixes when using BGL render engines, but I don't see the same when render engines call GL directly. Would add the GPU_bgl_end() here also, for case users want to use BGL to do similar tricks. jbakker: external engines could alter the state of the GPU. Internal flags could be different than we… | ||||||||||
Done Inline ActionsAm i understanding it correctly that I simply need to add DRW_state_reset(); and GPU_bgl_end(); at the end of this function? sergey: Am i understanding it correctly that I simply need to add `DRW_state_reset();` and `GPU_bgl_end… | ||||||||||
Not Done Inline ActionsGPU_bgl_end: yes, jbakker: GPU_bgl_end: yes,
DRW_state_reset: not sure, need feedback from Clement. | ||||||||||
Not Done Inline ActionsThrow a DRW_state_reset in there for good measure. But this is being a little bit paranoid. fclem: Throw a `DRW_state_reset` in there for good measure. But this is being a little bit paranoid. | ||||||||||
| if (draw_ctx->evil_C) { | if (draw_ctx->evil_C) { | |||||||||
| const float clear_col[4] = {0, 0, 0, 0}; | ||||||||||
| /* This is to keep compatibility with external engine. */ | ||||||||||
| /* TODO(fclem): remove it eventually. */ | ||||||||||
| GPU_framebuffer_bind(dfbl->default_fb); | ||||||||||
| GPU_framebuffer_clear_color(dfbl->default_fb, clear_col); | ||||||||||
| external_draw_scene_do(vedata); | external_draw_scene_do(vedata); | |||||||||
| } | } | |||||||||
| #if 0 | ||||||||||
| { | ||||||||||
| EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | ||||||||||
| EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl; | ||||||||||
| EXTERNAL_FramebufferList *fbl = ((EXTERNAL_Data *)vedata)->fbl; | ||||||||||
| const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | ||||||||||
| if (stl->g_data->update_depth && stl->g_data->need_depth) { | if (stl->g_data->update_depth && stl->g_data->need_depth) { | |||||||||
| DRW_draw_pass(psl->depth_pass); | DRW_draw_pass(psl->depth_pass); | |||||||||
| /* Copy main depth buffer to cached framebuffer. */ | /* Copy main depth buffer to cached framebuffer. */ | |||||||||
| GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->depth_buffer_fb, 0, GPU_DEPTH_BIT); | GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->depth_buffer_fb, 0, GPU_DEPTH_BIT); | |||||||||
| } | } | |||||||||
| /* Copy cached depth buffer to main framebuffer. */ | /* Copy cached depth buffer to main framebuffer. */ | |||||||||
| GPU_framebuffer_blit(fbl->depth_buffer_fb, 0, dfbl->depth_only_fb, 0, GPU_DEPTH_BIT); | GPU_framebuffer_blit(fbl->depth_buffer_fb, 0, dfbl->depth_only_fb, 0, GPU_DEPTH_BIT); | |||||||||
| } | } | |||||||||
| #endif | ||||||||||
| } | ||||||||||
| static void external_engine_free(void) | static void external_engine_free(void) | |||||||||
| { | { | |||||||||
| DRW_SHADER_FREE_SAFE(e_data.depth_sh); | DRW_SHADER_FREE_SAFE(e_data.depth_sh); | |||||||||
| } | } | |||||||||
| static const DrawEngineDataSize external_data_size = DRW_VIEWPORT_DATA_SIZE(EXTERNAL_Data); | static const DrawEngineDataSize external_data_size = DRW_VIEWPORT_DATA_SIZE(EXTERNAL_Data); | |||||||||
| static DrawEngineType draw_engine_external_type = { | DrawEngineType draw_engine_external_type = { | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| N_("External"), | N_("External"), | |||||||||
| &external_data_size, | &external_data_size, | |||||||||
| &external_engine_init, | &external_engine_init, | |||||||||
| &external_engine_free, | &external_engine_free, | |||||||||
| &external_cache_init, | &external_cache_init, | |||||||||
| &external_cache_populate, | &external_cache_populate, | |||||||||
| Show All 16 Lines | RenderEngineType DRW_engine_viewport_external_type = { | |||||||||
| RE_INTERNAL | RE_USE_STEREO_VIEWPORT, | RE_INTERNAL | RE_USE_STEREO_VIEWPORT, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | NULL, | |||||||||
| NULL, | ||||||||||
| &draw_engine_external_type, | &draw_engine_external_type, | |||||||||
| {NULL, NULL, NULL}, | {NULL, NULL, NULL}, | |||||||||
| }; | }; | |||||||||
| #undef EXTERNAL_ENGINE | #undef EXTERNAL_ENGINE | |||||||||
Additional speedup can be get when modify-ing DRW_draw_render_loop_2d_ex#do_populate_loop.
I don't see this part of this patch, but would improve performance in heavier scenes.