Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/basic/basic_engine.c
| Show All 31 Lines | |||||
| #include "GPU_shader.h" | #include "GPU_shader.h" | ||||
| #include "basic_engine.h" | #include "basic_engine.h" | ||||
| /* Shaders */ | /* Shaders */ | ||||
| #define BASIC_ENGINE "BLENDER_BASIC" | #define BASIC_ENGINE "BLENDER_BASIC" | ||||
| extern char datatoc_conservative_depth_frag_glsl[]; | |||||
| extern char datatoc_conservative_depth_vert_glsl[]; | |||||
| extern char datatoc_conservative_depth_geom_glsl[]; | |||||
| extern char datatoc_common_view_lib_glsl[]; | |||||
| /* *********** LISTS *********** */ | /* *********** LISTS *********** */ | ||||
| /* GPUViewport.storage | /* GPUViewport.storage | ||||
| * Is freed everytime the viewport engine changes */ | * Is freed everytime the viewport engine changes */ | ||||
| typedef struct BASIC_StorageList { | typedef struct BASIC_StorageList { | ||||
| struct BASIC_PrivateData *g_data; | struct BASIC_PrivateData *g_data; | ||||
| } BASIC_StorageList; | } BASIC_StorageList; | ||||
| typedef struct BASIC_PassList { | typedef struct BASIC_PassList { | ||||
| struct DRWPass *depth_pass[2]; | struct DRWPass *depth_pass[2]; | ||||
| struct DRWPass *depth_pass_cull[2]; | struct DRWPass *depth_pass_cull[2]; | ||||
| } BASIC_PassList; | } BASIC_PassList; | ||||
| typedef struct BASIC_Data { | typedef struct BASIC_Data { | ||||
| void *engine_type; | void *engine_type; | ||||
| DRWViewportEmptyList *fbl; | DRWViewportEmptyList *fbl; | ||||
| DRWViewportEmptyList *txl; | DRWViewportEmptyList *txl; | ||||
| BASIC_PassList *psl; | BASIC_PassList *psl; | ||||
| BASIC_StorageList *stl; | BASIC_StorageList *stl; | ||||
| } BASIC_Data; | } BASIC_Data; | ||||
| typedef struct BASIC_Shaders { | typedef struct BASIC_Shaders { | ||||
| /* Depth Pre Pass */ | /* Depth Pre Pass */ | ||||
| struct GPUShader *depth; | struct GPUShader *depth; | ||||
| struct GPUShader *depth_conservative; | |||||
| } BASIC_Shaders; | } BASIC_Shaders; | ||||
| /* *********** STATIC *********** */ | /* *********** STATIC *********** */ | ||||
| static struct { | static struct { | ||||
| BASIC_Shaders sh_data[GPU_SHADER_CFG_LEN]; | BASIC_Shaders sh_data[GPU_SHADER_CFG_LEN]; | ||||
| } e_data = {{{NULL}}}; /* Engine data */ | } e_data = {{{NULL}}}; /* Engine data */ | ||||
| typedef struct BASIC_PrivateData { | typedef struct BASIC_PrivateData { | ||||
| DRWShadingGroup *depth_shgrp[2]; | DRWShadingGroup *depth_shgrp[2]; | ||||
| DRWShadingGroup *depth_shgrp_cull[2]; | DRWShadingGroup *depth_shgrp_cull[2]; | ||||
| } BASIC_PrivateData; /* Transient data */ | } BASIC_PrivateData; /* Transient data */ | ||||
| /* Functions */ | /* Functions */ | ||||
| static void basic_engine_init(void *UNUSED(vedata)) | static void basic_engine_init(void *UNUSED(vedata)) | ||||
| { | { | ||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||
| BASIC_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg]; | BASIC_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg]; | ||||
| /* Depth prepass */ | /* Depth prepass */ | ||||
| if (!sh_data->depth) { | if (!sh_data->depth) { | ||||
| sh_data->depth = DRW_shader_create_3d_depth_only(draw_ctx->sh_cfg); | sh_data->depth = DRW_shader_create_3d_depth_only(draw_ctx->sh_cfg); | ||||
| const GPUShaderConfigData *sh_cfg = &GPU_shader_cfg_data[draw_ctx->sh_cfg]; | |||||
| sh_data->depth_conservative = GPU_shader_create_from_arrays({ | |||||
| .vert = (const char *[]){sh_cfg->lib, | |||||
| datatoc_common_view_lib_glsl, | |||||
| datatoc_conservative_depth_vert_glsl, | |||||
| NULL}, | |||||
| .geom = (const char *[]){sh_cfg->lib, | |||||
| datatoc_common_view_lib_glsl, | |||||
| datatoc_conservative_depth_geom_glsl, | |||||
| NULL}, | |||||
| .frag = (const char *[]){datatoc_common_view_lib_glsl, | |||||
| datatoc_conservative_depth_frag_glsl, | |||||
| NULL}, | |||||
| .defs = (const char *[]){sh_cfg->def, NULL}, | |||||
| }); | |||||
| } | } | ||||
| } | } | ||||
| static void basic_cache_init(void *vedata) | static void basic_cache_init(void *vedata) | ||||
| { | { | ||||
| BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl; | BASIC_PassList *psl = ((BASIC_Data *)vedata)->psl; | ||||
| BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl; | BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl; | ||||
| DRWShadingGroup *grp; | |||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||
| BASIC_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg]; | BASIC_Shaders *sh_data = &e_data.sh_data[draw_ctx->sh_cfg]; | ||||
| if (!stl->g_data) { | if (!stl->g_data) { | ||||
| /* Alloc transient pointers */ | /* Alloc transient pointers */ | ||||
| stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__); | stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__); | ||||
| } | } | ||||
| /* Twice for normal and infront objects. */ | /* Twice for normal and infront objects. */ | ||||
| for (int i = 0; i < 2; i++) { | for (int i = 0; i < 2; i++) { | ||||
| DRWState clip_state = (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) ? DRW_STATE_CLIP_PLANES : 0; | DRWState clip_state = (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) ? DRW_STATE_CLIP_PLANES : 0; | ||||
| DRWState infront_state = (DRW_state_is_select() && (i == 1)) ? DRW_STATE_IN_FRONT_SELECT : 0; | DRWState infront_state = (DRW_state_is_select() && (i == 1)) ? DRW_STATE_IN_FRONT_SELECT : 0; | ||||
| DRWState state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL; | DRWState state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL; | ||||
| GPUShader *sh = DRW_state_is_select() ? sh_data->depth_conservative : sh_data->depth; | |||||
| DRW_PASS_CREATE(psl->depth_pass[i], state | clip_state | infront_state); | DRW_PASS_CREATE(psl->depth_pass[i], state | clip_state | infront_state); | ||||
| stl->g_data->depth_shgrp[i] = DRW_shgroup_create(sh_data->depth, psl->depth_pass[i]); | stl->g_data->depth_shgrp[i] = grp = DRW_shgroup_create(sh, psl->depth_pass[i]); | ||||
| DRW_shgroup_uniform_vec2(grp, "sizeViewport", DRW_viewport_size_get(), 1); | |||||
| DRW_shgroup_uniform_vec2(grp, "sizeViewportInv", DRW_viewport_invert_size_get(), 1); | |||||
| state |= DRW_STATE_CULL_BACK; | state |= DRW_STATE_CULL_BACK; | ||||
| DRW_PASS_CREATE(psl->depth_pass_cull[i], state | clip_state | infront_state); | DRW_PASS_CREATE(psl->depth_pass_cull[i], state | clip_state | infront_state); | ||||
| stl->g_data->depth_shgrp_cull[i] = DRW_shgroup_create(sh_data->depth, psl->depth_pass_cull[i]); | stl->g_data->depth_shgrp_cull[i] = grp = DRW_shgroup_create(sh, psl->depth_pass_cull[i]); | ||||
| DRW_shgroup_uniform_vec2(grp, "sizeViewport", DRW_viewport_size_get(), 1); | |||||
| DRW_shgroup_uniform_vec2(grp, "sizeViewportInv", DRW_viewport_invert_size_get(), 1); | |||||
| } | } | ||||
| } | } | ||||
| static void basic_cache_populate(void *vedata, Object *ob) | static void basic_cache_populate(void *vedata, Object *ob) | ||||
| { | { | ||||
| BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl; | BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl; | ||||
| /* TODO(fclem) fix selection of smoke domains. */ | /* TODO(fclem) fix selection of smoke domains. */ | ||||
| ▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | static void basic_draw_scene(void *vedata) | ||||
| DRW_draw_pass(psl->depth_pass[0]); | DRW_draw_pass(psl->depth_pass[0]); | ||||
| DRW_draw_pass(psl->depth_pass_cull[0]); | DRW_draw_pass(psl->depth_pass_cull[0]); | ||||
| DRW_draw_pass(psl->depth_pass[1]); | DRW_draw_pass(psl->depth_pass[1]); | ||||
| DRW_draw_pass(psl->depth_pass_cull[1]); | DRW_draw_pass(psl->depth_pass_cull[1]); | ||||
| } | } | ||||
| static void basic_engine_free(void) | static void basic_engine_free(void) | ||||
| { | { | ||||
| /* all shaders are builtin */ | for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) { | ||||
| BASIC_Shaders *sh_data = &e_data.sh_data[i]; | |||||
| DRW_SHADER_FREE_SAFE(sh_data->depth_conservative); | |||||
| } | |||||
| } | } | ||||
| static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data); | static const DrawEngineDataSize basic_data_size = DRW_VIEWPORT_DATA_SIZE(BASIC_Data); | ||||
| DrawEngineType draw_engine_basic_type = { | DrawEngineType draw_engine_basic_type = { | ||||
| NULL, | NULL, | ||||
| NULL, | NULL, | ||||
| N_("Basic"), | N_("Basic"), | ||||
| &basic_data_size, | &basic_data_size, | ||||
| &basic_engine_init, | &basic_engine_init, | ||||
| &basic_engine_free, | &basic_engine_free, | ||||
| &basic_cache_init, | &basic_cache_init, | ||||
| &basic_cache_populate, | &basic_cache_populate, | ||||
| &basic_cache_finish, | &basic_cache_finish, | ||||
| NULL, | NULL, | ||||
| &basic_draw_scene, | &basic_draw_scene, | ||||
| NULL, | NULL, | ||||
| NULL, | NULL, | ||||
| NULL, | NULL, | ||||
| }; | }; | ||||
| /* Note: currently unused, we may want to register so we can see this when debugging the view. */ | |||||
| RenderEngineType DRW_engine_viewport_basic_type = { | |||||
| NULL, | |||||
| NULL, | |||||
| BASIC_ENGINE, | |||||
| N_("Basic"), | |||||
| RE_INTERNAL, | |||||
| NULL, | |||||
| NULL, | |||||
| NULL, | |||||
| NULL, | |||||
| NULL, | |||||
| NULL, | |||||
| NULL, | |||||
| &draw_engine_basic_type, | |||||
| {NULL, NULL, NULL}, | |||||
| }; | |||||
| #undef BASIC_ENGINE | #undef BASIC_ENGINE | ||||