Mesh Batch Cache: Refactor + Multithread

For clarity sake, the batch cache now uses exclusively per Loop attributes.
While this is a bit of a waste of VRAM (for the few case where per vert
attribs are enough) it reduces the complexity and amount of overall VBO
to update in general situations.

This patch also makes the VertexBuffers filling multithreaded. This make
the update of dense meshes a bit faster. The main bottleneck is the
IndexBuffers update which cannot be multithreaded efficiently (have to
increment a counter and/or do a final sorting pass).

We introduce the concept of "extract" functions/step.
All extract functions are executed in one thread each and if possible,
using multiple thread for looping over all elements.

Reviewed By: brecht

Differential Revision: http://developer.blender.org/D5424
This commit is contained in:
Clément Foucault 2019-07-14 16:49:44 +02:00
parent 45a45f7d66
commit 9c010c44f4
Notes: blender-bot 2023-05-29 09:17:12 +02:00
Referenced by issue #72217, Crash when switching scene
Referenced by issue #70226, Blender crash when try to edit a mesh in devlook
Referenced by issue #70206, 2.81 - On Texture Paint the UV Map doesnt show up on the left viewport as it does on 2.80
Referenced by issue #69051, Vertex / Weight Paint. Blender does not show if vertex is selected.
Referenced by issue #68954, Auto Smooth option conflicts with uvs
Referenced by issue #68658, Text offset makes scale to fit not to work
Referenced by issue #66463, Speedup Mesh Batch Cache
23 changed files with 5157 additions and 4726 deletions

View File

@ -486,6 +486,7 @@ void BKE_mesh_calc_poly_center(const struct MPoly *mpoly,
float BKE_mesh_calc_poly_area(const struct MPoly *mpoly,
const struct MLoop *loopstart,
const struct MVert *mvarray);
float BKE_mesh_calc_poly_uv_area(const struct MPoly *mpoly, const struct MLoopUV *uv_array);
void BKE_mesh_calc_poly_angles(const struct MPoly *mpoly,
const struct MLoop *loopstart,
const struct MVert *mvarray,

View File

@ -2378,6 +2378,24 @@ float BKE_mesh_calc_poly_area(const MPoly *mpoly, const MLoop *loopstart, const
}
}
float BKE_mesh_calc_poly_uv_area(const MPoly *mpoly, const MLoopUV *uv_array)
{
int i, l_iter = mpoly->loopstart;
float area;
float(*vertexcos)[2] = BLI_array_alloca(vertexcos, (size_t)mpoly->totloop);
/* pack vertex cos into an array for area_poly_v2 */
for (i = 0; i < mpoly->totloop; i++, l_iter++) {
copy_v2_v2(vertexcos[i], uv_array[l_iter].uv);
}
/* finally calculate the area */
area = area_poly_v2((const float(*)[2])vertexcos, (unsigned int)mpoly->totloop);
return area;
}
/**
* Calculate the volume and volume-weighted centroid of the volume
* formed by the polygon and the origin.

View File

@ -92,6 +92,7 @@ float volume_tetrahedron_signed_v3(const float v1[3],
const float v3[3],
const float v4[3]);
bool is_edge_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]);
bool is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]);
bool is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2]);
bool is_poly_convex_v2(const float verts[][2], unsigned int nr);

View File

@ -5729,6 +5729,27 @@ float form_factor_hemi_poly(
return contrib;
}
/**
* Check if the edge is convex or concave
* (depends on face winding)
* Copied from BM_edge_is_convex().
*/
bool is_edge_convex_v3(const float v1[3],
const float v2[3],
const float f1_no[3],
const float f2_no[3])
{
if (!equals_v3v3(f1_no, f2_no)) {
float cross[3];
float l_dir[3];
cross_v3_v3v3(cross, f1_no, f2_no);
/* we assume contiguous normals, otherwise the result isn't meaningful */
sub_v3_v3v3(l_dir, v2, v1);
return (dot_v3v3(l_dir, cross) > 0.0f);
}
return false;
}
/**
* Evaluate if entire quad is a proper convex quad
*/

View File

@ -52,6 +52,7 @@ set(SRC
intern/draw_anim_viz.c
intern/draw_armature.c
intern/draw_cache.c
intern/draw_cache_extract_mesh.c
intern/draw_cache_impl_curve.c
intern/draw_cache_impl_displist.c
intern/draw_cache_impl_lattice.c
@ -135,6 +136,7 @@ set(SRC
DRW_select_buffer.h
intern/DRW_render.h
intern/draw_cache.h
intern/draw_cache_extract.h
intern/draw_cache_impl.h
intern/draw_cache_inline.h
intern/draw_common.h

View File

@ -4033,7 +4033,7 @@ void drw_batch_cache_validate(Object *ob)
void drw_batch_cache_generate_requested(Object *ob)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
const ToolSettings *ts = draw_ctx->scene->toolsettings;
const Scene *scene = draw_ctx->scene;
const enum eContextObjectMode mode = CTX_data_mode_enum_ex(
draw_ctx->object_edit, draw_ctx->obact, draw_ctx->object_mode);
const bool is_paint_mode = ELEM(
@ -4047,13 +4047,13 @@ void drw_batch_cache_generate_requested(Object *ob)
struct Mesh *mesh_eval = ob->runtime.mesh_eval;
switch (ob->type) {
case OB_MESH:
DRW_mesh_batch_cache_create_requested(ob, (Mesh *)ob->data, ts, is_paint_mode, use_hide);
DRW_mesh_batch_cache_create_requested(ob, (Mesh *)ob->data, scene, is_paint_mode, use_hide);
break;
case OB_CURVE:
case OB_FONT:
case OB_SURF:
if (mesh_eval) {
DRW_mesh_batch_cache_create_requested(ob, mesh_eval, ts, is_paint_mode, use_hide);
DRW_mesh_batch_cache_create_requested(ob, mesh_eval, scene, is_paint_mode, use_hide);
}
DRW_curve_batch_cache_create_requested(ob);
break;

View File

@ -0,0 +1,249 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2019, Blender Foundation.
*/
/** \file
* \ingroup draw
*/
#ifndef __DRAW_CACHE_EXTRACT_MESH_H__
#define __DRAW_CACHE_EXTRACT_MESH_H__
/* Vertex Group Selection and display options */
typedef struct DRW_MeshWeightState {
int defgroup_active;
int defgroup_len;
short flags;
char alert_mode;
/* Set of all selected bones for Multipaint. */
bool *defgroup_sel; /* [defgroup_len] */
int defgroup_sel_count;
} DRW_MeshWeightState;
/* DRW_MeshWeightState.flags */
enum {
DRW_MESH_WEIGHT_STATE_MULTIPAINT = (1 << 0),
DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE = (1 << 1),
};
typedef struct DRW_MeshCDMask {
uint32_t uv : 8;
uint32_t tan : 8;
uint32_t vcol : 8;
uint32_t orco : 1;
uint32_t tan_orco : 1;
} DRW_MeshCDMask;
typedef enum eMRIterType {
MR_ITER_LOOPTRI = 1 << 0,
MR_ITER_LOOP = 1 << 1,
MR_ITER_LEDGE = 1 << 2,
MR_ITER_LVERT = 1 << 3,
} eMRIterType;
typedef enum eMRDataType {
MR_DATA_POLY_NOR = 1 << 1,
MR_DATA_LOOP_NOR = 1 << 2,
MR_DATA_LOOPTRI = 1 << 3,
/** Force loop normals calculation. */
MR_DATA_TAN_LOOP_NOR = 1 << 4,
} eMRDataType;
typedef enum eMRExtractType {
MR_EXTRACT_BMESH,
MR_EXTRACT_MAPPED,
MR_EXTRACT_MESH,
} eMRExtractType;
BLI_INLINE int mesh_render_mat_len_get(Mesh *me)
{
return MAX2(1, me->totcol);
}
typedef struct MeshBufferCache {
/* Every VBO below contains at least enough
* data for every loops in the mesh (except fdots).
* For some VBOs, it extends to (in this exact order) :
* loops + loose_edges*2 + loose_verts */
struct {
GPUVertBuf *pos_nor; /* extend */
GPUVertBuf *lnor; /* extend */
GPUVertBuf *edge_fac; /* extend */
GPUVertBuf *weights; /* extend */
GPUVertBuf *uv_tan;
GPUVertBuf *vcol;
GPUVertBuf *orco;
/* Only for edit mode. */
GPUVertBuf *edit_data; /* extend */
GPUVertBuf *edituv_data;
GPUVertBuf *stretch_area;
GPUVertBuf *stretch_angle;
GPUVertBuf *mesh_analysis;
GPUVertBuf *fdots_pos;
GPUVertBuf *fdots_nor;
GPUVertBuf *fdots_uv;
// GPUVertBuf *fdots_edit_data; /* inside fdots_nor for now. */
GPUVertBuf *fdots_edituv_data;
/* Selection */
GPUVertBuf *vert_idx; /* extend */
GPUVertBuf *edge_idx; /* extend */
GPUVertBuf *poly_idx;
GPUVertBuf *fdot_idx;
} vbo;
/* Index Buffers:
* Only need to be updated when topology changes. */
struct {
/* Indices to vloops. */
GPUIndexBuf *tris; /* Ordered per material. */
GPUIndexBuf *lines; /* Loose edges last. */
GPUIndexBuf *points;
GPUIndexBuf *fdots;
/* 3D overlays. */
GPUIndexBuf *lines_paint_mask; /* no loose edges. */
GPUIndexBuf *lines_adjacency;
/* Uv overlays. (visibility can differ from 3D view) */
GPUIndexBuf *edituv_tris;
GPUIndexBuf *edituv_lines;
GPUIndexBuf *edituv_points;
GPUIndexBuf *edituv_fdots;
} ibo;
} MeshBufferCache;
typedef enum DRWBatchFlag {
MBC_SURFACE = (1 << 0),
MBC_SURFACE_WEIGHTS = (1 << 1),
MBC_EDIT_TRIANGLES = (1 << 2),
MBC_EDIT_VERTICES = (1 << 3),
MBC_EDIT_EDGES = (1 << 4),
MBC_EDIT_VNOR = (1 << 5),
MBC_EDIT_LNOR = (1 << 6),
MBC_EDIT_FACEDOTS = (1 << 7),
MBC_EDIT_MESH_ANALYSIS = (1 << 8),
MBC_EDITUV_FACES_STRECH_AREA = (1 << 9),
MBC_EDITUV_FACES_STRECH_ANGLE = (1 << 10),
MBC_EDITUV_FACES = (1 << 11),
MBC_EDITUV_EDGES = (1 << 12),
MBC_EDITUV_VERTS = (1 << 13),
MBC_EDITUV_FACEDOTS = (1 << 14),
MBC_EDIT_SELECTION_VERTS = (1 << 15),
MBC_EDIT_SELECTION_EDGES = (1 << 16),
MBC_EDIT_SELECTION_FACES = (1 << 17),
MBC_EDIT_SELECTION_FACEDOTS = (1 << 18),
MBC_ALL_VERTS = (1 << 19),
MBC_ALL_EDGES = (1 << 20),
MBC_LOOSE_EDGES = (1 << 21),
MBC_EDGE_DETECTION = (1 << 22),
MBC_WIRE_EDGES = (1 << 23),
MBC_WIRE_LOOPS = (1 << 24),
MBC_WIRE_LOOPS_UVS = (1 << 25),
MBC_SURF_PER_MAT = (1 << 26),
} DRWBatchFlag;
#define MBC_EDITUV \
(MBC_EDITUV_FACES_STRECH_AREA | MBC_EDITUV_FACES_STRECH_ANGLE | MBC_EDITUV_FACES | \
MBC_EDITUV_EDGES | MBC_EDITUV_VERTS | MBC_EDITUV_FACEDOTS | MBC_WIRE_LOOPS_UVS)
#define FOREACH_MESH_BUFFER_CACHE(batch_cache, mbc) \
for (MeshBufferCache *mbc = &batch_cache->final; \
mbc == &batch_cache->final || mbc == &batch_cache->cage || mbc == &batch_cache->uv_cage; \
mbc = (mbc == &batch_cache->final) ? \
&batch_cache->cage : \
((mbc == &batch_cache->cage) ? &batch_cache->uv_cage : NULL))
typedef struct MeshBatchCache {
MeshBufferCache final, cage, uv_cage;
struct {
/* Surfaces / Render */
GPUBatch *surface;
GPUBatch *surface_weights;
/* Edit mode */
GPUBatch *edit_triangles;
GPUBatch *edit_vertices;
GPUBatch *edit_edges;
GPUBatch *edit_vnor;
GPUBatch *edit_lnor;
GPUBatch *edit_fdots;
GPUBatch *edit_mesh_analysis;
/* Edit UVs */
GPUBatch *edituv_faces_strech_area;
GPUBatch *edituv_faces_strech_angle;
GPUBatch *edituv_faces;
GPUBatch *edituv_edges;
GPUBatch *edituv_verts;
GPUBatch *edituv_fdots;
/* Edit selection */
GPUBatch *edit_selection_verts;
GPUBatch *edit_selection_edges;
GPUBatch *edit_selection_faces;
GPUBatch *edit_selection_fdots;
/* Common display / Other */
GPUBatch *all_verts;
GPUBatch *all_edges;
GPUBatch *loose_edges;
GPUBatch *edge_detection;
GPUBatch *wire_edges; /* Individual edges with face normals. */
GPUBatch *wire_loops; /* Loops around faces. no edges between selected faces */
GPUBatch *wire_loops_uvs; /* Same as wire_loops but only has uvs. */
} batch;
GPUBatch **surface_per_mat;
/* arrays of bool uniform names (and value) that will be use to
* set srgb conversion for auto attributes.*/
char *auto_layer_names;
int *auto_layer_is_srgb;
int auto_layer_len;
DRWBatchFlag batch_requested;
DRWBatchFlag batch_ready;
/* settings to determine if cache is invalid */
int edge_len;
int tri_len;
int poly_len;
int vert_len;
int mat_len;
bool is_dirty; /* Instantly invalidates cache, skipping mesh check */
bool is_editmode;
bool is_uvsyncsel;
struct DRW_MeshWeightState weight_state;
DRW_MeshCDMask cd_used, cd_needed, cd_used_over_time;
int lastmatch;
/* Valid only if edge_detection is up to date. */
bool is_manifold;
bool no_loose_wire;
} MeshBatchCache;
void mesh_buffer_cache_create_requested(MeshBatchCache *cache,
MeshBufferCache mbc,
Mesh *me,
const bool do_final,
const bool do_uvedit,
const bool use_subsurf_fdots,
const DRW_MeshCDMask *cd_layer_used,
const ToolSettings *ts,
const bool use_hide);
#endif /* __DRAW_CACHE_EXTRACT_MESH_H__ */

File diff suppressed because it is too large Load Diff

View File

@ -119,7 +119,7 @@ struct GPUBatch *DRW_lattice_batch_cache_get_edit_verts(struct Lattice *lt);
/* Mesh */
void DRW_mesh_batch_cache_create_requested(struct Object *ob,
struct Mesh *me,
const struct ToolSettings *ts,
const struct Scene *scene,
const bool is_paint_mode,
const bool use_hide);
@ -143,6 +143,7 @@ struct GPUBatch *DRW_mesh_batch_cache_get_surface_weights(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_triangles(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_vertices(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_edges(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_vnors(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_lnors(struct Mesh *me);
struct GPUBatch *DRW_mesh_batch_cache_get_edit_facedots(struct Mesh *me);
/* edit-mesh selection */

File diff suppressed because it is too large Load Diff

View File

@ -128,8 +128,7 @@ typedef struct EDIT_MESH_Shaders {
GPUShader *depth;
/* Mesh analysis shader */
GPUShader *mesh_analysis_face;
GPUShader *mesh_analysis_vertex;
GPUShader *mesh_analysis;
} EDIT_MESH_Shaders;
/* *********** STATIC *********** */
@ -307,15 +306,9 @@ static void EDIT_MESH_engine_init(void *vedata)
});
/* Mesh Analysis */
sh_data->mesh_analysis_face = GPU_shader_create_from_arrays({
sh_data->mesh_analysis = GPU_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_mesh_analysis_vert_glsl, NULL},
.frag = (const char *[]){datatoc_edit_mesh_overlay_mesh_analysis_frag_glsl, NULL},
.defs = (const char *[]){sh_cfg_data->def, "#define FACE_COLOR\n", NULL},
});
sh_data->mesh_analysis_vertex = GPU_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_mesh_analysis_vert_glsl, NULL},
.frag = (const char *[]){datatoc_edit_mesh_overlay_mesh_analysis_frag_glsl, NULL},
.defs = (const char *[]){sh_cfg_data->def, "#define VERTEX_COLOR\n", NULL},
});
MEM_freeN(lib);
@ -548,10 +541,9 @@ static void EDIT_MESH_cache_init(void *vedata)
/* Mesh Analysis Pass */
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA;
psl->mesh_analysis_pass = DRW_pass_create("Mesh Analysis", state);
const bool is_vertex_color = scene->toolsettings->statvis.type == SCE_STATVIS_SHARP;
g_data->mesh_analysis_shgrp = DRW_shgroup_create(
is_vertex_color ? sh_data->mesh_analysis_vertex : sh_data->mesh_analysis_face,
psl->mesh_analysis_pass);
g_data->mesh_analysis_shgrp = DRW_shgroup_create(sh_data->mesh_analysis,
psl->mesh_analysis_pass);
DRW_shgroup_uniform_texture(g_data->mesh_analysis_shgrp, "weightTex", G_draw.weight_ramp);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_state_enable(g_data->mesh_analysis_shgrp, DRW_STATE_CLIP_PLANES);
}
@ -704,17 +696,10 @@ static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
geom = DRW_cache_mesh_surface_weights_get(ob);
DRW_shgroup_call_no_cull(g_data->fweights_shgrp, geom, ob);
}
if (do_show_mesh_analysis && !XRAY_ACTIVE(v3d)) {
Mesh *me = (Mesh *)ob->data;
BMEditMesh *embm = me->edit_mesh;
const bool is_original = embm->mesh_eval_final &&
(embm->mesh_eval_final->runtime.is_original == true);
if (is_original) {
geom = DRW_cache_mesh_surface_mesh_analysis_get(ob);
if (geom) {
DRW_shgroup_call_no_cull(g_data->mesh_analysis_shgrp, geom, ob);
}
else if (do_show_mesh_analysis && !XRAY_ACTIVE(v3d)) {
geom = DRW_cache_mesh_surface_mesh_analysis_get(ob);
if (geom) {
DRW_shgroup_call_no_cull(g_data->mesh_analysis_shgrp, geom, ob);
}
}
@ -727,7 +712,7 @@ static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
}
if (vnormals_do) {
geom = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
geom = DRW_mesh_batch_cache_get_edit_vnors(ob->data);
DRW_shgroup_call_no_cull(g_data->vnormals_shgrp, geom, ob);
}
if (lnormals_do) {

View File

@ -1,12 +1,6 @@
out vec4 fragColor;
#ifdef FACE_COLOR
flat in vec4 weightColor;
#endif
#ifdef VERTEX_COLOR
in vec4 weightColor;
#endif
void main()
{

View File

@ -1,14 +1,25 @@
in vec3 pos;
in vec4 weight_color;
in float weight;
#ifdef FACE_COLOR
flat out vec4 weightColor;
#endif
uniform sampler1D weightTex;
#ifdef VERTEX_COLOR
out vec4 weightColor;
#endif
vec3 weight_to_rgb(float t)
{
if (t < 0.0) {
/* Minimum color, grey */
return vec3(0.25, 0.25, 0.25);
}
else if (t > 1.0) {
/* Error color */
return vec3(1.0, 0.0, 1.0);
}
else {
return texture(weightTex, t).rgb;
}
}
void main()
{
@ -16,7 +27,7 @@ void main()
vec3 world_pos = point_object_to_world(pos);
gl_Position = point_world_to_ndc(world_pos);
weightColor = vec4(weight_color.rgb, 1.0);
weightColor = vec4(weight_to_rgb(weight), 1.0);
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance(world_pos);

View File

@ -163,13 +163,13 @@ void ED_image_draw_cursor(ARegion *ar, const float cursor[2])
static void uvedit_get_batches(Object *ob,
SpaceImage *sima,
const ToolSettings *ts,
const Scene *scene,
GPUBatch **faces,
GPUBatch **edges,
GPUBatch **verts,
GPUBatch **facedots)
{
int drawfaces = draw_uvs_face_check(ts);
int drawfaces = draw_uvs_face_check(scene->toolsettings);
const bool draw_stretch = (sima->flag & SI_DRAW_STRETCH) != 0;
const bool draw_faces = (sima->flag & SI_NO_DRAWFACES) == 0;
@ -197,7 +197,7 @@ static void uvedit_get_batches(Object *ob,
*faces = NULL;
}
DRW_mesh_batch_cache_create_requested(ob, ob->data, ts, false, false);
DRW_mesh_batch_cache_create_requested(ob, ob->data, scene, false, false);
}
static void draw_uvs_shadow(SpaceImage *UNUSED(sima),
@ -212,7 +212,7 @@ static void draw_uvs_shadow(SpaceImage *UNUSED(sima),
DRW_mesh_batch_cache_validate(me);
GPUBatch *edges = DRW_mesh_batch_cache_get_uv_edges(me);
DRW_mesh_batch_cache_create_requested(eval_ob, me, scene->toolsettings, false, false);
DRW_mesh_batch_cache_create_requested(eval_ob, me, scene, false, false);
if (edges) {
GPU_batch_program_set_builtin(edges, GPU_SHADER_2D_UV_UNIFORM_COLOR);
@ -235,7 +235,7 @@ static void draw_uvs_texpaint(Scene *scene, Object *ob, Depsgraph *depsgraph)
DRW_mesh_batch_cache_validate(me);
GPUBatch *geom = DRW_mesh_batch_cache_get_uv_edges(me);
DRW_mesh_batch_cache_create_requested(eval_ob, me, scene->toolsettings, false, false);
DRW_mesh_batch_cache_create_requested(eval_ob, me, scene, false, false);
GPU_batch_program_set_builtin(geom, GPU_SHADER_2D_UV_UNIFORM_COLOR);
GPU_batch_uniform_4fv(geom, "color", col);
@ -300,7 +300,7 @@ static void draw_uvs(SpaceImage *sima, Scene *scene, Object *obedit, Depsgraph *
}
}
uvedit_get_batches(eval_ob, sima, ts, &faces, &edges, &verts, &facedots);
uvedit_get_batches(eval_ob, sima, scene, &faces, &edges, &verts, &facedots);
bool interpedges;
bool draw_stretch = (sima->flag & SI_DRAW_STRETCH) != 0;

View File

@ -40,7 +40,7 @@ typedef enum {
GPU_BATCH_READY_TO_DRAW,
} GPUBatchPhase;
#define GPU_BATCH_VBO_MAX_LEN 4
#define GPU_BATCH_VBO_MAX_LEN 5
#define GPU_BATCH_VAO_STATIC_LEN 3
#define GPU_BATCH_VAO_DYN_ALLOC_COUNT 16
@ -115,6 +115,7 @@ void GPU_batch_vao_cache_clear(GPUBatch *);
void GPU_batch_callback_free_set(GPUBatch *, void (*callback)(GPUBatch *, void *), void *);
void GPU_batch_instbuf_set(GPUBatch *, GPUVertBuf *, bool own_vbo); /* Instancing */
void GPU_batch_elembuf_set(GPUBatch *batch, GPUIndexBuf *elem, bool own_ibo);
int GPU_batch_vertbuf_add_ex(GPUBatch *, GPUVertBuf *, bool own_vbo);

View File

@ -36,14 +36,19 @@ typedef enum {
} GPUIndexBufType;
typedef struct GPUIndexBuf {
uint index_start;
uint index_len;
bool is_subrange;
#if GPU_TRACK_INDEX_RANGE
GPUIndexBufType index_type;
uint32_t gl_index_type;
uint base_index;
#endif
uint32_t ibo_id; /* 0 indicates not yet sent to VRAM */
void *data; /* non-NULL indicates not yet sent to VRAM */
union {
void *data; /* non-NULL indicates not yet sent to VRAM */
struct GPUIndexBuf *src; /* if is_subrange is true, this is the source buffer. */
};
} GPUIndexBuf;
void GPU_indexbuf_use(GPUIndexBuf *);
@ -71,9 +76,21 @@ void GPU_indexbuf_add_line_verts(GPUIndexBufBuilder *, uint v1, uint v2);
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3);
void GPU_indexbuf_add_line_adj_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3, uint v4);
void GPU_indexbuf_set_point_vert(GPUIndexBufBuilder *builder, uint elem, uint v1);
void GPU_indexbuf_set_line_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2);
void GPU_indexbuf_set_tri_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2, uint v3);
/* Skip primitive rendering at the given index. */
void GPU_indexbuf_set_point_restart(GPUIndexBufBuilder *builder, uint elem);
void GPU_indexbuf_set_line_restart(GPUIndexBufBuilder *builder, uint elem);
void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem);
GPUIndexBuf *GPU_indexbuf_build(GPUIndexBufBuilder *);
void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *, GPUIndexBuf *);
/* Create a subrange of an existing indexbuffer. */
GPUIndexBuf *GPU_indexbuf_create_subrange(GPUIndexBuf *ibo, uint start, uint length);
void GPU_indexbuf_discard(GPUIndexBuf *);
int GPU_indexbuf_primitive_len(GPUPrimType prim_type);

View File

@ -31,7 +31,7 @@
#include "BLI_assert.h"
#define GPU_VERT_ATTR_MAX_LEN 16
#define GPU_VERT_ATTR_MAX_NAMES 5
#define GPU_VERT_ATTR_MAX_NAMES 6
#define GPU_VERT_ATTR_NAME_AVERAGE_LEN 11
#define GPU_VERT_ATTR_NAMES_BUF_LEN ((GPU_VERT_ATTR_NAME_AVERAGE_LEN + 1) * GPU_VERT_ATTR_MAX_LEN)
@ -88,6 +88,8 @@ typedef struct GPUVertFormat {
uint packed : 1;
/** Current offset in names[]. */
uint name_offset : 8;
/** Store each attrib in one contiguous buffer region. */
uint deinterleaved : 1;
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN];
char names[GPU_VERT_ATTR_NAMES_BUF_LEN];
@ -104,6 +106,8 @@ uint GPU_vertformat_attr_add(
GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode);
void GPU_vertformat_alias_add(GPUVertFormat *, const char *alias);
void GPU_vertformat_deinterleave(GPUVertFormat *format);
int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name);
BLI_INLINE const char *GPU_vertformat_attr_name_get(const GPUVertFormat *format,
@ -122,7 +126,59 @@ typedef struct GPUPackedNormal {
int w : 2; /* 0 by default, can manually set to { -2, -1, 0, 1 } */
} GPUPackedNormal;
GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3]);
GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3]);
/* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
* Of the code here, only struct GPUPackedNormal needs to change. */
#define SIGNED_INT_10_MAX 511
#define SIGNED_INT_10_MIN -512
BLI_INLINE int clampi(int x, int min_allowed, int max_allowed)
{
#if TRUST_NO_ONE
assert(min_allowed <= max_allowed);
#endif
if (x < min_allowed) {
return min_allowed;
}
else if (x > max_allowed) {
return max_allowed;
}
else {
return x;
}
}
BLI_INLINE int gpu_convert_normalized_f32_to_i10(float x)
{
int qx = x * 511.0f;
return clampi(qx, SIGNED_INT_10_MIN, SIGNED_INT_10_MAX);
}
BLI_INLINE int gpu_convert_i16_to_i10(short x)
{
/* 16-bit signed --> 10-bit signed */
/* TODO: round? */
return x >> 6;
}
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3])
{
GPUPackedNormal n = {
.x = gpu_convert_normalized_f32_to_i10(data[0]),
.y = gpu_convert_normalized_f32_to_i10(data[1]),
.z = gpu_convert_normalized_f32_to_i10(data[2]),
};
return n;
}
BLI_INLINE GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3])
{
GPUPackedNormal n = {
.x = gpu_convert_i16_to_i10(data[0]),
.y = gpu_convert_i16_to_i10(data[1]),
.z = gpu_convert_i16_to_i10(data[2]),
};
return n;
}
#endif /* __GPU_VERTEX_FORMAT_H__ */

View File

@ -182,6 +182,25 @@ void GPU_batch_instbuf_set(GPUBatch *batch, GPUVertBuf *inst, bool own_vbo)
}
}
void GPU_batch_elembuf_set(GPUBatch *batch, GPUIndexBuf *elem, bool own_ibo)
{
BLI_assert(elem != NULL);
/* redo the bindings */
GPU_batch_vao_cache_clear(batch);
if (batch->elem != NULL && (batch->owns_flag & GPU_BATCH_OWNS_INDEX)) {
GPU_indexbuf_discard(batch->elem);
}
batch->elem = elem;
if (own_ibo) {
batch->owns_flag |= GPU_BATCH_OWNS_INDEX;
}
else {
batch->owns_flag &= ~GPU_BATCH_OWNS_INDEX;
}
}
/* Returns the index of verts in the batch. */
int GPU_batch_vertbuf_add_ex(GPUBatch *batch, GPUVertBuf *verts, bool own_vbo)
{
@ -362,13 +381,23 @@ static void create_bindings(GPUVertBuf *verts,
const GPUVertFormat *format = &verts->format;
const uint attr_len = format->attr_len;
const uint stride = format->stride;
uint stride = format->stride;
uint offset = 0;
GPU_vertbuf_use(verts);
for (uint a_idx = 0; a_idx < attr_len; ++a_idx) {
const GPUVertAttr *a = &format->attrs[a_idx];
const GLvoid *pointer = (const GLubyte *)0 + a->offset + v_first * stride;
if (format->deinterleaved) {
offset += ((a_idx == 0) ? 0 : format->attrs[a_idx - 1].sz) * verts->vertex_len;
stride = a->sz;
}
else {
offset = a->offset;
}
const GLvoid *pointer = (const GLubyte *)0 + offset + v_first * stride;
for (uint n_idx = 0; n_idx < a->name_len; ++n_idx) {
const char *name = GPU_vertformat_attr_name_get(format, a, n_idx);
@ -419,8 +448,11 @@ static void create_bindings(GPUVertBuf *verts,
static void batch_update_program_bindings(GPUBatch *batch, uint v_first)
{
for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN && batch->verts[v] != NULL; ++v) {
create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false);
/* Reverse order so first vbos have more prevalence (in term of attrib override). */
for (int v = GPU_BATCH_VBO_MAX_LEN - 1; v > -1; --v) {
if (batch->verts[v] != NULL) {
create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false);
}
}
if (batch->inst) {
create_bindings(batch->inst, batch->interface, v_first, true);
@ -550,10 +582,10 @@ static void *elem_offset(const GPUIndexBuf *el, int v_first)
{
#if GPU_TRACK_INDEX_RANGE
if (el->index_type == GPU_INDEX_U16) {
return (GLushort *)0 + v_first;
return (GLushort *)0 + v_first + el->index_start;
}
#endif
return (GLuint *)0 + v_first;
return (GLuint *)0 + v_first + el->index_start;
}
/* Use when drawing with GPU_batch_draw_advanced */

View File

@ -162,6 +162,100 @@ void GPU_indexbuf_add_line_adj_verts(
GPU_indexbuf_add_generic_vert(builder, v4);
}
void GPU_indexbuf_set_point_vert(GPUIndexBufBuilder *builder, uint elem, uint v1)
{
BLI_assert(builder->prim_type == GPU_PRIM_POINTS);
BLI_assert(elem < builder->max_index_len);
builder->data[elem++] = v1;
if (builder->index_len < elem) {
builder->index_len = elem;
}
}
void GPU_indexbuf_set_line_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2)
{
BLI_assert(builder->prim_type == GPU_PRIM_LINES);
BLI_assert(v1 != v2);
BLI_assert(v1 <= builder->max_allowed_index);
BLI_assert(v2 <= builder->max_allowed_index);
BLI_assert((elem + 1) * 2 <= builder->max_index_len);
uint idx = elem * 2;
builder->data[idx++] = v1;
builder->data[idx++] = v2;
if (builder->index_len < idx) {
builder->index_len = idx;
}
}
void GPU_indexbuf_set_tri_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2, uint v3)
{
BLI_assert(builder->prim_type == GPU_PRIM_TRIS);
BLI_assert(v1 != v2 && v2 != v3 && v3 != v1);
BLI_assert(v1 <= builder->max_allowed_index);
BLI_assert(v2 <= builder->max_allowed_index);
BLI_assert(v3 <= builder->max_allowed_index);
BLI_assert((elem + 1) * 3 <= builder->max_index_len);
uint idx = elem * 3;
builder->data[idx++] = v1;
builder->data[idx++] = v2;
builder->data[idx++] = v3;
if (builder->index_len < idx) {
builder->index_len = idx;
}
}
void GPU_indexbuf_set_point_restart(GPUIndexBufBuilder *builder, uint elem)
{
BLI_assert(builder->prim_type == GPU_PRIM_POINTS);
BLI_assert(elem < builder->max_index_len);
builder->data[elem++] = RESTART_INDEX;
if (builder->index_len < elem) {
builder->index_len = elem;
}
}
void GPU_indexbuf_set_line_restart(GPUIndexBufBuilder *builder, uint elem)
{
BLI_assert(builder->prim_type == GPU_PRIM_LINES);
BLI_assert((elem + 1) * 2 <= builder->max_index_len);
uint idx = elem * 2;
builder->data[idx++] = RESTART_INDEX;
builder->data[idx++] = RESTART_INDEX;
if (builder->index_len < idx) {
builder->index_len = idx;
}
}
void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem)
{
BLI_assert(builder->prim_type == GPU_PRIM_TRIS);
BLI_assert((elem + 1) * 3 <= builder->max_index_len);
uint idx = elem * 3;
builder->data[idx++] = RESTART_INDEX;
builder->data[idx++] = RESTART_INDEX;
builder->data[idx++] = RESTART_INDEX;
if (builder->index_len < idx) {
builder->index_len = idx;
}
}
GPUIndexBuf *GPU_indexbuf_create_subrange(GPUIndexBuf *elem_src, uint start, uint length)
{
GPUIndexBuf *elem = MEM_callocN(sizeof(GPUIndexBuf), "GPUIndexBuf");
BLI_assert(elem_src && !elem_src->is_subrange);
BLI_assert(start + length <= elem_src->index_len);
#if GPU_TRACK_INDEX_RANGE
elem->index_type = elem_src->index_type;
elem->gl_index_type = elem_src->gl_index_type;
elem->base_index = elem_src->base_index;
#endif
elem->is_subrange = true;
elem->src = elem_src;
elem->index_start = start;
elem->index_len = length;
return elem;
}
#if GPU_TRACK_INDEX_RANGE
/* Everything remains 32 bit while building to keep things simple.
* Find min/max after, then convert to smallest index type possible. */
@ -271,6 +365,10 @@ static void indexbuf_upload_data(GPUIndexBuf *elem)
void GPU_indexbuf_use(GPUIndexBuf *elem)
{
if (elem->is_subrange) {
GPU_indexbuf_use(elem->src);
return;
}
if (elem->ibo_id == 0) {
elem->ibo_id = GPU_buf_alloc();
}
@ -285,7 +383,7 @@ void GPU_indexbuf_discard(GPUIndexBuf *elem)
if (elem->ibo_id) {
GPU_buf_free(elem->ibo_id);
}
if (elem->data) {
if (!elem->is_subrange && elem->data) {
MEM_freeN(elem->data);
}
MEM_freeN(elem);

View File

@ -218,6 +218,29 @@ int GPU_vertformat_attr_id_get(const GPUVertFormat *format, const char *name)
return -1;
}
/* Make attribute layout non-interleaved.
* Warning! This does not change data layout!
* Use direct buffer access to fill the data.
* This is for advanced usage.
*
* Deinterleaved data means all attrib data for each attrib
* is stored continuously like this :
* 000011112222
* instead of :
* 012012012012
*
* Note this is per attrib deinterleaving, NOT per component.
* */
void GPU_vertformat_deinterleave(GPUVertFormat *format)
{
/* Ideally we should change the stride and offset here. This would allow
* us to use GPU_vertbuf_attr_set / GPU_vertbuf_attr_fill. But since
* we use only 11 bits for attr->offset this limits the size of the
* buffer considerably. So instead we do the conversion when creating
* bindings in create_bindings(). */
format->deinterleaved = true;
}
uint padding(uint offset, uint alignment)
{
const uint mod = offset % alignment;
@ -391,58 +414,3 @@ void GPU_vertformat_from_interface(GPUVertFormat *format, const GPUShaderInterfa
}
}
}
/* OpenGL ES packs in a different order as desktop GL but component conversion is the same.
* Of the code here, only struct GPUPackedNormal needs to change. */
#define SIGNED_INT_10_MAX 511
#define SIGNED_INT_10_MIN -512
static int clampi(int x, int min_allowed, int max_allowed)
{
#if TRUST_NO_ONE
assert(min_allowed <= max_allowed);
#endif
if (x < min_allowed) {
return min_allowed;
}
else if (x > max_allowed) {
return max_allowed;
}
else {
return x;
}
}
static int quantize(float x)
{
int qx = x * 511.0f;
return clampi(qx, SIGNED_INT_10_MIN, SIGNED_INT_10_MAX);
}
static int convert_i16(short x)
{
/* 16-bit signed --> 10-bit signed */
/* TODO: round? */
return x >> 6;
}
GPUPackedNormal GPU_normal_convert_i10_v3(const float data[3])
{
GPUPackedNormal n = {
.x = quantize(data[0]),
.y = quantize(data[1]),
.z = quantize(data[2]),
};
return n;
}
GPUPackedNormal GPU_normal_convert_i10_s3(const short data[3])
{
GPUPackedNormal n = {
.x = convert_i16(data[0]),
.y = convert_i16(data[1]),
.z = convert_i16(data[2]),
};
return n;
}

View File

@ -27,6 +27,7 @@
#define __GPU_VERTEX_FORMAT_PRIVATE_H__
void VertexFormat_pack(GPUVertFormat *format);
void VertexFormat_deinterleave(GPUVertFormat *format, uint vertex_len);
uint padding(uint offset, uint alignment);
uint vertex_buffer_size(const GPUVertFormat *format, uint vertex_len);

View File

@ -8,7 +8,7 @@ in vec2 pos;
in float stretch;
#else
in vec4 uv_adj;
in vec2 uv_angles;
in float angle;
#endif
@ -52,6 +52,11 @@ vec3 weight_to_rgb(float weight)
#define M_PI 3.1415926535897932
vec2 angle_to_v2(float angle)
{
return vec2(cos(angle), sin(angle));
}
/* Adapted from BLI_math_vector.h */
float angle_normalized_v2v2(vec2 v1, vec2 v2)
{
@ -69,7 +74,9 @@ void main()
gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
#ifdef STRETCH_ANGLE
float uv_angle = angle_normalized_v2v2(uv_adj.xy, uv_adj.zw) / M_PI;
vec2 v1 = angle_to_v2(uv_angles.x * M_PI);
vec2 v2 = angle_to_v2(uv_angles.y * M_PI);
float uv_angle = angle_normalized_v2v2(v1, v2) / M_PI;
float stretch = 1.0 - abs(uv_angle - angle);
stretch = stretch;
stretch = 1.0 - stretch * stretch;

View File

@ -3467,7 +3467,7 @@ static void rna_def_statvis(BlenderRNA *brna)
RNA_def_property_float_sdna(prop, NULL, "overhang_min");
RNA_def_property_float_default(prop, 0.5f);
RNA_def_property_range(prop, 0.0f, DEG2RADF(180.0f));
RNA_def_property_ui_range(prop, 0.0f, DEG2RADF(180.0f), 0.001, 3);
RNA_def_property_ui_range(prop, 0.0f, DEG2RADF(180.0f), 10, 3);
RNA_def_property_ui_text(prop, "Overhang Min", "Minimum angle to display");
RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
RNA_def_property_update(prop, 0, "rna_EditMesh_update");