Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/intern/draw_cache_impl_volume.c
| Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | typedef struct VolumeBatchCache { | ||||
| ListBase grids; | ListBase grids; | ||||
| /* Wireframe */ | /* Wireframe */ | ||||
| struct { | struct { | ||||
| GPUVertBuf *pos_nor_in_order; | GPUVertBuf *pos_nor_in_order; | ||||
| GPUBatch *batch; | GPUBatch *batch; | ||||
| } face_wire; | } face_wire; | ||||
| /* Surface for selection */ | |||||
| GPUBatch *selection_surface; | |||||
| /* settings to determine if cache is invalid */ | /* settings to determine if cache is invalid */ | ||||
| bool is_dirty; | bool is_dirty; | ||||
| } VolumeBatchCache; | } VolumeBatchCache; | ||||
| /* GPUBatch cache management. */ | /* GPUBatch cache management. */ | ||||
| static bool volume_batch_cache_valid(Volume *volume) | static bool volume_batch_cache_valid(Volume *volume) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | static void volume_batch_cache_clear(Volume *volume) | ||||
| LISTBASE_FOREACH (DRWVolumeGrid *, grid, &cache->grids) { | LISTBASE_FOREACH (DRWVolumeGrid *, grid, &cache->grids) { | ||||
| MEM_SAFE_FREE(grid->name); | MEM_SAFE_FREE(grid->name); | ||||
| DRW_TEXTURE_FREE_SAFE(grid->texture); | DRW_TEXTURE_FREE_SAFE(grid->texture); | ||||
| } | } | ||||
| BLI_freelistN(&cache->grids); | BLI_freelistN(&cache->grids); | ||||
| GPU_VERTBUF_DISCARD_SAFE(cache->face_wire.pos_nor_in_order); | GPU_VERTBUF_DISCARD_SAFE(cache->face_wire.pos_nor_in_order); | ||||
| GPU_BATCH_DISCARD_SAFE(cache->face_wire.batch); | GPU_BATCH_DISCARD_SAFE(cache->face_wire.batch); | ||||
| GPU_BATCH_DISCARD_SAFE(cache->selection_surface); | |||||
| } | } | ||||
| void DRW_volume_batch_cache_free(Volume *volume) | void DRW_volume_batch_cache_free(Volume *volume) | ||||
| { | { | ||||
| volume_batch_cache_clear(volume); | volume_batch_cache_clear(volume); | ||||
| MEM_SAFE_FREE(volume->batch_cache); | MEM_SAFE_FREE(volume->batch_cache); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | if (cache->face_wire.batch == NULL) { | ||||
| /* Create wireframe from OpenVDB tree. */ | /* Create wireframe from OpenVDB tree. */ | ||||
| BKE_volume_grid_wireframe(volume, volume_grid, drw_volume_wireframe_cb, volume); | BKE_volume_grid_wireframe(volume, volume_grid, drw_volume_wireframe_cb, volume); | ||||
| } | } | ||||
| return cache->face_wire.batch; | return cache->face_wire.batch; | ||||
| } | } | ||||
| static void drw_volume_selection_surface_cb( | |||||
| void *userdata, float (*verts)[3], int (*tris)[3], int totvert, int tottris) | |||||
| { | |||||
| Volume *volume = userdata; | |||||
| VolumeBatchCache *cache = volume->batch_cache; | |||||
| static GPUVertFormat format = {0}; | |||||
| static uint pos_id; | |||||
| if (format.attr_len == 0) { | |||||
| pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); | |||||
| } | |||||
| /* Create vertex buffer. */ | |||||
| GPUVertBuf *vbo_surface = GPU_vertbuf_create_with_format(&format); | |||||
| GPU_vertbuf_data_alloc(vbo_surface, totvert); | |||||
| GPU_vertbuf_attr_fill(vbo_surface, pos_id, verts); | |||||
| /* Create index buffer. */ | |||||
| GPUIndexBufBuilder elb; | |||||
| GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tottris, totvert); | |||||
| for (int i = 0; i < tottris; i++) { | |||||
| GPU_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i])); | |||||
| } | |||||
| GPUIndexBuf *ibo_surface = GPU_indexbuf_build(&elb); | |||||
| cache->selection_surface = GPU_batch_create_ex( | |||||
| GPU_PRIM_TRIS, vbo_surface, ibo_surface, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); | |||||
| } | |||||
| GPUBatch *DRW_volume_batch_cache_get_selection_surface(Volume *volume) | |||||
| { | |||||
| VolumeBatchCache *cache = volume_batch_cache_get(volume); | |||||
| if (cache->selection_surface == NULL) { | |||||
| VolumeGrid *volume_grid = BKE_volume_grid_active_get(volume); | |||||
| if (volume_grid == NULL) { | |||||
| return NULL; | |||||
| } | |||||
| BKE_volume_grid_selection_surface( | |||||
| volume, volume_grid, drw_volume_selection_surface_cb, volume); | |||||
| } | |||||
| return cache->selection_surface; | |||||
| } | |||||
| static DRWVolumeGrid *volume_grid_cache_get(Volume *volume, | static DRWVolumeGrid *volume_grid_cache_get(Volume *volume, | ||||
| VolumeGrid *grid, | VolumeGrid *grid, | ||||
| VolumeBatchCache *cache) | VolumeBatchCache *cache) | ||||
| { | { | ||||
| const char *name = BKE_volume_grid_name(grid); | const char *name = BKE_volume_grid_name(grid); | ||||
| /* Return cached grid. */ | /* Return cached grid. */ | ||||
| DRWVolumeGrid *cache_grid; | DRWVolumeGrid *cache_grid; | ||||
| ▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines | |||||