diff options
Diffstat (limited to 'source/blender/gpu/intern')
-rw-r--r-- | source/blender/gpu/intern/gpu_buffers.c | 138 |
1 files changed, 63 insertions, 75 deletions
diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c index 3043b3ab686..3367a568bc5 100644 --- a/source/blender/gpu/intern/gpu_buffers.c +++ b/source/blender/gpu/intern/gpu_buffers.c @@ -986,15 +986,37 @@ struct GPU_PBVH_Buffers { float diffuse_color[4]; }; -typedef struct { +static struct { uint pos, nor, col; -} VertexBufferAttrID; +} g_vbo_id = {0}; -static void gpu_pbvh_vert_format_init__gwn(Gwn_VertFormat *format, VertexBufferAttrID *vbo_id) +/* Allocates a non-initialized buffer to be sent to GPU. + * Return is false it indicates that the memory map failed. */ +static bool gpu_pbvh_vert_buf_data_set(GPU_PBVH_Buffers *buffers, unsigned int vert_ct) { - vbo_id->pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); - vbo_id->nor = GWN_vertformat_attr_add(format, "nor", GWN_COMP_I16, 3, GWN_FETCH_INT_TO_FLOAT_UNIT); - vbo_id->col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 3, GWN_FETCH_INT_TO_FLOAT_UNIT); + if (buffers->vert_buf == NULL) { + /* Initialize vertex buffer */ + /* match 'VertexBufferFormat' */ + + static Gwn_VertFormat format = {0}; + if (format.attrib_ct == 0) { + g_vbo_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + g_vbo_id.nor = GWN_vertformat_attr_add(&format, "nor", GWN_COMP_I16, 3, GWN_FETCH_INT_TO_FLOAT_UNIT); + g_vbo_id.col = GWN_vertformat_attr_add(&format, "color", GWN_COMP_U8, 3, GWN_FETCH_INT_TO_FLOAT_UNIT); + } +#if 0 + buffers->vert_buf = GWN_vertbuf_create_with_format_ex(&format, GWN_USAGE_DYNAMIC); + GWN_vertbuf_data_alloc(buffers->vert_buf, vert_ct); + } + else if (vert_ct != buffers->vert_buf->vertex_ct) { + GWN_vertbuf_data_resize(buffers->vert_buf, vert_ct); + } +#else + buffers->vert_buf = GWN_vertbuf_create_with_format_ex(&format, GWN_USAGE_STATIC); + } + GWN_vertbuf_data_alloc(buffers->vert_buf, vert_ct); +#endif + return buffers->vert_buf->data != NULL; } static void gpu_pbvh_batch_init(GPU_PBVH_Buffers *buffers) @@ -1004,14 +1026,14 @@ static void gpu_pbvh_batch_init(GPU_PBVH_Buffers *buffers) GWN_vertbuf_use(buffers->vert_buf); } - GWN_BATCH_DISCARD_SAFE(buffers->triangles); - buffers->triangles = GWN_batch_create( - GWN_PRIM_TRIS, buffers->vert_buf, - /* can be NULL */ - buffers->index_buf); + if (buffers->triangles == NULL) { + buffers->triangles = GWN_batch_create( + GWN_PRIM_TRIS, buffers->vert_buf, + /* can be NULL */ + buffers->index_buf); + } - GWN_BATCH_DISCARD_SAFE(buffers->triangles_fast); - if (buffers->index_buf_fast) { + if ((buffers->triangles_fast == NULL) && buffers->index_buf_fast) { buffers->triangles_fast = GWN_batch_create( GWN_PRIM_TRIS, buffers->vert_buf, /* can be NULL */ @@ -1085,25 +1107,15 @@ void GPU_pbvh_mesh_buffers_update( rgba_float_to_uchar(diffuse_color_ub, diffuse_color); /* Build VBO */ - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); - - /* match 'VertexBufferFormat' */ - Gwn_VertFormat format = {0}; - VertexBufferAttrID vbo_id; - gpu_pbvh_vert_format_init__gwn(&format, &vbo_id); - - buffers->vert_buf = GWN_vertbuf_create_with_format(&format); - GWN_vertbuf_data_alloc(buffers->vert_buf, totelem); - - if (buffers->vert_buf->data) { + if (gpu_pbvh_vert_buf_data_set(buffers, totelem)) { /* Vertex data is shared if smooth-shaded, but separate * copies are made for flat shading because normals * shouldn't be shared. */ if (buffers->smooth) { for (uint i = 0; i < totvert; ++i) { const MVert *v = &mvert[vert_indices[i]]; - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, i, v->co); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, i, v->no); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, i, v->co); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, i, v->no); } for (uint i = 0; i < buffers->face_indices_len; i++) { @@ -1114,10 +1126,10 @@ void GPU_pbvh_mesh_buffers_update( int v_index = buffers->mloop[lt->tri[j]].v; uchar color_ub[3]; gpu_color_from_mask_copy(vmask[v_index], diffuse_color, color_ub); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vidx, color_ub); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vidx, color_ub); } else { - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vidx, diffuse_color_ub); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vidx, diffuse_color_ub); } } } @@ -1160,9 +1172,9 @@ void GPU_pbvh_mesh_buffers_update( for (uint j = 0; j < 3; j++) { const MVert *v = &mvert[vtri[j]]; - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, vbo_index, v->co); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, vbo_index, v->co); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub); vbo_index++; } @@ -1171,9 +1183,6 @@ void GPU_pbvh_mesh_buffers_update( gpu_pbvh_batch_init(buffers); } - else { - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); - } } buffers->mvert = mvert; @@ -1293,17 +1302,9 @@ void GPU_pbvh_grid_buffers_update( copy_v4_v4(buffers->diffuse_color, diffuse_color); - Gwn_VertFormat format = {0}; - VertexBufferAttrID vbo_id; - gpu_pbvh_vert_format_init__gwn(&format, &vbo_id); - - /* Build coord/normal VBO */ - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); - buffers->vert_buf = GWN_vertbuf_create_with_format(&format); - GWN_vertbuf_data_alloc(buffers->vert_buf, totgrid * key->grid_area); - uint vbo_index_offset = 0; - if (buffers->vert_buf->data) { + /* Build VBO */ + if (gpu_pbvh_vert_buf_data_set(buffers, totgrid * key->grid_area)) { for (i = 0; i < totgrid; ++i) { CCGElem *grid = grids[grid_indices[i]]; int vbo_index = vbo_index_offset; @@ -1311,12 +1312,12 @@ void GPU_pbvh_grid_buffers_update( for (y = 0; y < key->grid_size; y++) { for (x = 0; x < key->grid_size; x++) { CCGElem *elem = CCG_grid_elem(key, grid, x, y); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, vbo_index, CCG_elem_co(key, elem)); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, vbo_index, CCG_elem_co(key, elem)); if (buffers->smooth) { short no_short[3]; normal_float_to_short_v3(no_short, CCG_elem_no(key, elem)); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no_short); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no_short); if (has_mask) { uchar color_ub[3]; @@ -1327,7 +1328,7 @@ void GPU_pbvh_grid_buffers_update( else { F3TOCHAR3(diffuse_color, color_ub); } - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub); } } vbo_index += 1; @@ -1354,7 +1355,7 @@ void GPU_pbvh_grid_buffers_update( vbo_index = vbo_index_offset + ((j + 1) * key->grid_size + k); short no_short[3]; normal_float_to_short_v3(no_short, fno); - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no_short); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no_short); if (has_mask) { uchar color_ub[3]; @@ -1370,7 +1371,7 @@ void GPU_pbvh_grid_buffers_update( else { F3TOCHAR3(diffuse_color, color_ub); } - GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub); + GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub); } } } @@ -1381,9 +1382,6 @@ void GPU_pbvh_grid_buffers_update( gpu_pbvh_batch_init(buffers); } - else { - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); - } } buffers->grids = grids; @@ -1562,7 +1560,6 @@ GPU_PBVH_Buffers *GPU_pbvh_grid_buffers_build( static void gpu_bmesh_vert_to_buffer_copy__gwn( BMVert *v, Gwn_VertBuf *vert_buf, - const VertexBufferAttrID *vbo_id, int *v_index, const float fno[3], const float *fmask, @@ -1573,12 +1570,12 @@ static void gpu_bmesh_vert_to_buffer_copy__gwn( if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN)) { /* Set coord, normal, and mask */ - GWN_vertbuf_attr_set(vert_buf, vbo_id->pos, *v_index, v->co); + GWN_vertbuf_attr_set(vert_buf, g_vbo_id.pos, *v_index, v->co); { short no_short[3]; normal_float_to_short_v3(no_short, fno ? fno : v->no); - GWN_vertbuf_attr_set(vert_buf, vbo_id->nor, *v_index, no_short); + GWN_vertbuf_attr_set(vert_buf, g_vbo_id.nor, *v_index, no_short); } { @@ -1596,7 +1593,7 @@ static void gpu_bmesh_vert_to_buffer_copy__gwn( effective_mask, diffuse_color, color_ub); - GWN_vertbuf_attr_set(vert_buf, vbo_id->col, *v_index, color_ub); + GWN_vertbuf_attr_set(vert_buf, g_vbo_id.col, *v_index, color_ub); } /* Assign index for use in the triangle index buffer */ @@ -1694,18 +1691,8 @@ void GPU_pbvh_bmesh_buffers_update( copy_v4_v4(buffers->diffuse_color, diffuse_color); - /* Initialize vertex buffer */ - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); - /* match 'VertexBufferFormat' */ - Gwn_VertFormat format = {0}; - VertexBufferAttrID vbo_id; - gpu_pbvh_vert_format_init__gwn(&format, &vbo_id); - - buffers->vert_buf = GWN_vertbuf_create_with_format(&format); - GWN_vertbuf_data_alloc(buffers->vert_buf, totvert); - /* Fill vertex buffer */ - if (buffers->vert_buf->data) { + if (gpu_pbvh_vert_buf_data_set(buffers, totvert)) { int v_index = 0; if (buffers->smooth) { @@ -1718,7 +1705,7 @@ void GPU_pbvh_bmesh_buffers_update( GSET_ITER (gs_iter, bm_unique_verts) { gpu_bmesh_vert_to_buffer_copy__gwn( BLI_gsetIterator_getKey(&gs_iter), - buffers->vert_buf, &vbo_id, &v_index, NULL, NULL, + buffers->vert_buf, &v_index, NULL, NULL, cd_vert_mask_offset, diffuse_color, show_mask); } @@ -1726,7 +1713,7 @@ void GPU_pbvh_bmesh_buffers_update( GSET_ITER (gs_iter, bm_other_verts) { gpu_bmesh_vert_to_buffer_copy__gwn( BLI_gsetIterator_getKey(&gs_iter), - buffers->vert_buf, &vbo_id, &v_index, NULL, NULL, + buffers->vert_buf, &v_index, NULL, NULL, cd_vert_mask_offset, diffuse_color, show_mask); } @@ -1759,7 +1746,7 @@ void GPU_pbvh_bmesh_buffers_update( for (i = 0; i < 3; i++) { gpu_bmesh_vert_to_buffer_copy__gwn( - v[i], buffers->vert_buf, &vbo_id, + v[i], buffers->vert_buf, &v_index, f->no, &fmask, cd_vert_mask_offset, diffuse_color, show_mask); @@ -1774,7 +1761,6 @@ void GPU_pbvh_bmesh_buffers_update( bm->elem_index_dirty |= BM_VERT; } else { - GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf); /* Memory map failed */ return; } @@ -1786,9 +1772,6 @@ void GPU_pbvh_bmesh_buffers_update( GWN_indexbuf_init(&elb, GWN_PRIM_TRIS, tottri, maxvert); /* Initialize triangle index buffer */ - if (buffers->triangles && !buffers->is_index_buf_global) { - GWN_BATCH_DISCARD_SAFE(buffers->triangles); - } buffers->is_index_buf_global = false; /* Fill triangle index buffer */ @@ -1812,7 +1795,12 @@ void GPU_pbvh_bmesh_buffers_update( buffers->tot_tri = tottri; - buffers->index_buf = GWN_indexbuf_build(&elb); + if (buffers->index_buf == NULL) { + buffers->index_buf = GWN_indexbuf_build(&elb); + } + else { + GWN_indexbuf_build_in_place(&elb, buffers->index_buf); + } } } else if (buffers->index_buf) { |