Changeset View
Standalone View
source/blender/draw/engines/external/external_engine.c
| Show All 26 Lines | ||||||||||
| #include "DNA_modifier_types.h" | #include "DNA_modifier_types.h" | |||||||||
| #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_image.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; | |||||||||
| } | } | |||||||||
| } | } | |||||||||
| /* Add shading group call which will take care of writing to the depth buffer, so that the | ||||||||||
| * alpha-under overlay will happen for the render buffer. */ | ||||||||||
| static void external_cache_image_add(DRWShadingGroup *grp) | ||||||||||
| { | ||||||||||
| float obmat[4][4]; | ||||||||||
| unit_m4(obmat); | ||||||||||
| scale_m4_fl(obmat, 0.5f); | ||||||||||
| /* NOTE: Use the same Z-depth value as in the regular image drawing engine. */ | ||||||||||
| 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 View3D *v3d = draw_ctx->v3d; | |||||||||
| Show All 9 Lines | static void external_cache_init(void *vedata) | |||||||||
| /* 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 (v3d != NULL) { | ||||||||||
| /* Do not draw depth pass when overlays are turned off. */ | /* Do not draw depth pass when overlays are turned off. */ | |||||||||
| stl->g_data->need_depth = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0; | stl->g_data->need_depth = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0; | |||||||||
| } | } | |||||||||
| else if (draw_ctx->space_data != NULL) { | ||||||||||
| const eSpace_Type space_type = draw_ctx->space_data->spacetype; | ||||||||||
| if (space_type == SPACE_IMAGE) { | ||||||||||
| external_cache_image_add(stl->g_data->depth_shgrp); | ||||||||||
| stl->g_data->need_depth = true; | ||||||||||
| stl->g_data->update_depth = true; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| 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; | |||||||||
| if (draw_ctx->space_data != NULL) { | ||||||||||
| 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. */ | |||||||||
| stl->g_data->need_depth = true; | stl->g_data->need_depth = true; | |||||||||
| Show All 24 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(); | |||||||||
| RegionView3D *rv3d = draw_ctx->rv3d; | RegionView3D *rv3d = draw_ctx->rv3d; | |||||||||
| ARegion *region = draw_ctx->region; | ARegion *region = draw_ctx->region; | |||||||||
| const RenderEngineType *type; | ||||||||||
| 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_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'; | |||||||||
| } | } | |||||||||
| } | } | |||||||||
| /* Get render engine of the current scene. | ||||||||||
| * | ||||||||||
| * Note that existence of the engine does not correlate with the rendering scene of the scene: | ||||||||||
| * the engine could be non-NULL if the scene used persistent data. Or, if the scene is just | ||||||||||
| * beginning to render the engine might not be existing yet. */ | ||||||||||
| static RenderEngine *external_engine_get(void) | ||||||||||
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… | ||||||||||
| { | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| Scene *scene = draw_ctx->scene; | ||||||||||
| Render *re = RE_GetSceneRender(scene); | ||||||||||
| if (re == NULL) { | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
| return RE_engine_get(re); | ||||||||||
| } | ||||||||||
| /* Configure current matrix stack so that the external engine can use the same drawing code for | ||||||||||
| * both viewport and image editor drawing. | ||||||||||
| * | ||||||||||
| * The engine draws result in the pixel space, and is applying render offset. For image editor we | ||||||||||
| * need to switch from normalized space to pixel space, and "un-apply" offset. */ | ||||||||||
| static void external_draw_init_image_space(const RenderEngine *engine) | ||||||||||
| { | ||||||||||
| BLI_assert(engine != NULL); | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| const DRWView *view = DRW_view_get_active(); | ||||||||||
| struct SpaceImage *space_image = (struct SpaceImage *)draw_ctx->space_data; | ||||||||||
| /* Apply current view as transformation matrix. | ||||||||||
| * This will configure drawing for normalized space with current zoom and pan applied. */ | ||||||||||
| 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_projection_set(projection_matrix); | ||||||||||
| GPU_matrix_set(view_matrix); | ||||||||||
| /* Switch from normalized space to pixel space. */ | ||||||||||
| { | ||||||||||
| int width, height; | ||||||||||
| ED_space_image_get_size(space_image, &width, &height); | ||||||||||
| const float width_inv = width ? 1.0f / width : 0.0f; | ||||||||||
| const float height_inv = height ? 1.0f / height : 0.0f; | ||||||||||
| GPU_matrix_scale_2f(width_inv, height_inv); | ||||||||||
| } | ||||||||||
| /* Un-apply render offset. */ | ||||||||||
| { | ||||||||||
| Render *render = engine->re; | ||||||||||
| rctf view_rect; | ||||||||||
| rcti render_rect; | ||||||||||
| RE_GetViewPlane(render, &view_rect, &render_rect); | ||||||||||
| GPU_matrix_translate_2f(-render_rect.xmin, -render_rect.ymin); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| static void external_draw_scene_do_image(void *UNUSED(vedata)) | ||||||||||
| { | ||||||||||
| RenderEngine *engine = external_engine_get(); | ||||||||||
| if (engine == NULL) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | ||||||||||
| /* Clear the depth buffer to the value used bu the background overlay so that the overlay is not | ||||||||||
fclemUnsubmitted Not Done Inline Actions
fclem: | ||||||||||
| * happening outside of the drawn image. | ||||||||||
| * | ||||||||||
| * NOTE: The external engine only draws color. The depth is taken care of using the depth pass | ||||||||||
| * which initialized the depth to the values expected by the background overlay. */ | ||||||||||
| GPU_framebuffer_clear_depth(dfbl->default_fb, 1.0f); | ||||||||||
| GPU_matrix_push_projection(); | ||||||||||
| GPU_matrix_push(); | ||||||||||
| external_draw_init_image_space(engine); | ||||||||||
fclemUnsubmitted 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… | ||||||||||
| GPU_debug_group_begin("External Engine"); | ||||||||||
| const RenderEngineType *engine_type = engine->type; | ||||||||||
| BLI_assert(engine_type != NULL); | ||||||||||
| BLI_assert(engine_type->draw != NULL); | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| engine_type->draw(engine, draw_ctx->evil_C, draw_ctx->depsgraph); | ||||||||||
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. | ||||||||||
| GPU_debug_group_end(); | ||||||||||
| GPU_matrix_pop(); | ||||||||||
| GPU_matrix_pop_projection(); | ||||||||||
| GPU_bgl_end(); | ||||||||||
| } | ||||||||||
| static void external_draw_scene_do(void *vedata) | ||||||||||
| { | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| if (draw_ctx->v3d != NULL) { | ||||||||||
| external_draw_scene_do_v3d(vedata); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| if (draw_ctx->space_data == NULL) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| const eSpace_Type space_type = draw_ctx->space_data->spacetype; | ||||||||||
| if (space_type == SPACE_IMAGE) { | ||||||||||
| external_draw_scene_do_image(vedata); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| static void external_draw_scene(void *vedata) | static void external_draw_scene(void *vedata) | |||||||||
| { | { | |||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | |||||||||
| EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl; | |||||||||
| 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(); | const DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get(); | |||||||||
| Show All 22 Lines | ||||||||||
| 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}, | |||||||||
| }; | }; | |||||||||
| bool DRW_engine_external_use_for_image_editor(void) | ||||||||||
| { | ||||||||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||||||||
| const SpaceLink *space_data = draw_ctx->space_data; | ||||||||||
| if (space_data == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| const eSpace_Type space_type = draw_ctx->space_data->spacetype; | ||||||||||
| if (space_type != SPACE_IMAGE) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| struct SpaceImage *space_image = (struct SpaceImage *)space_data; | ||||||||||
| const Image *image = ED_space_image(space_image); | ||||||||||
| if (image == NULL || image->type != IMA_TYPE_R_RESULT) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| RenderEngine *engine = external_engine_get(); | ||||||||||
| if (engine == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| if (!RE_engine_is_rendering(engine->re)) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| const RenderEngineType *engine_type = engine->type; | ||||||||||
| BLI_assert(engine_type != NULL); | ||||||||||
| if (engine_type->draw == NULL) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
| #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.