Refactor: Move stroke_cache to gpencil runtime

This moves the `stroke_cache` that was previously stored on every
drawing to the `GreasePencilRuntime` struct.

Since this cache is only used for when a user is drawing a stroke, there
can only ever be one. It makes more sense to have this cache on the
data-block runtime data, rather than every drawings runtime data.
This commit is contained in:
Falk David 2023-06-26 15:49:18 +02:00
parent 5dd29c9a05
commit 2418252ea7
4 changed files with 72 additions and 71 deletions

@ -61,8 +61,6 @@ class DrawingRuntime {
* Triangle cache for all the strokes in the drawing.
*/
mutable SharedCache<Vector<uint3>> triangles_cache;
StrokeCache stroke_cache;
};
class Drawing : public ::GreasePencilDrawing {
@ -78,11 +76,6 @@ class Drawing : public ::GreasePencilDrawing {
*/
Span<uint3> triangles() const;
void tag_positions_changed();
/**
* A buffer for a single stroke while drawing.
*/
Span<StrokePoint> stroke_buffer() const;
bool has_stroke_buffer() const;
};
class LayerGroup;
@ -382,10 +375,17 @@ class GreasePencilRuntime {
* Allocated and freed by the drawing code. See `DRW_grease_pencil_batch_cache_*` functions.
*/
void *batch_cache = nullptr;
bke::greasepencil::StrokeCache stroke_cache;
public:
GreasePencilRuntime() {}
~GreasePencilRuntime() {}
/**
* A buffer for a single stroke while drawing.
*/
Span<bke::greasepencil::StrokePoint> stroke_buffer() const;
bool has_stroke_buffer() const;
};
} // namespace blender::bke

@ -346,16 +346,6 @@ void Drawing::tag_positions_changed()
this->runtime->triangles_cache.tag_dirty();
}
bool Drawing::has_stroke_buffer() const
{
return this->runtime->stroke_cache.points.size() > 0;
}
Span<StrokePoint> Drawing::stroke_buffer() const
{
return this->runtime->stroke_cache.points.as_span();
}
TreeNode::TreeNode()
{
this->next = this->prev = nullptr;
@ -986,6 +976,23 @@ void BKE_grease_pencil_batch_cache_free(GreasePencil *grease_pencil)
/** \} */
/* ------------------------------------------------------------------- */
/** \name Grease Pencil runtime API
* \{ */
bool blender::bke::GreasePencilRuntime::has_stroke_buffer() const
{
return this->stroke_cache.points.size() > 0;
}
blender::Span<blender::bke::greasepencil::StrokePoint> blender::bke::GreasePencilRuntime::
stroke_buffer() const
{
return this->stroke_cache.points.as_span();
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Grease Pencil data-block API
* \{ */

@ -222,11 +222,6 @@ static void grease_pencil_geom_batch_ensure(GreasePencil &grease_pencil, int cfr
int verts_start_offsets_size = curves.curves_num();
int tris_start_offsets_size = curves.curves_num();
if (drawing->has_stroke_buffer()) {
verts_start_offsets_size++;
/* TODO: triangles for stroke buffer. */
// tris_start_offsets_size++;
}
Array<int> verts_start_offsets(verts_start_offsets_size);
Array<int> tris_start_offsets(tris_start_offsets_size);
@ -258,19 +253,17 @@ static void grease_pencil_geom_batch_ensure(GreasePencil &grease_pencil, int cfr
total_triangles_num += (curves.points_num() + num_cyclic) * 2;
total_triangles_num += drawing->triangles().size();
if (drawing->has_stroke_buffer()) {
const int num_buffer_points = drawing->stroke_buffer().size();
total_verts_num += 1 + num_buffer_points + 1;
total_triangles_num += num_buffer_points * 2;
verts_start_offsets[curves.curves_range().size()] = v_offset;
/* TODO: triangles for stroke buffer. */
v_offset += 1 + num_buffer_points + 1;
}
verts_start_offsets_per_visible_drawing.append(std::move(verts_start_offsets));
tris_start_offsets_per_visible_drawing.append(std::move(tris_start_offsets));
}
if (grease_pencil.runtime->has_stroke_buffer()) {
const int num_buffer_points = grease_pencil.runtime->stroke_buffer().size();
total_verts_num += 1 + num_buffer_points + 1;
total_triangles_num += num_buffer_points * 2;
/* TODO: triangles for stroke buffer. */
}
static GPUVertFormat format_edit_points_pos = {0};
if (format_edit_points_pos.attr_len == 0) {
GPU_vertformat_attr_add(&format_edit_points_pos, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
@ -432,51 +425,51 @@ static void grease_pencil_geom_batch_ensure(GreasePencil &grease_pencil, int cfr
verts_slice.last().mat = -1;
}
});
}
if (drawing.has_stroke_buffer()) {
Span<bke::greasepencil::StrokePoint> points = drawing.stroke_buffer();
const int verts_start_offset = verts_start_offsets.last();
const int num_verts = 1 + points.size() + 1;
IndexRange verts_range = IndexRange(verts_start_offset, num_verts);
MutableSpan<GreasePencilStrokeVert> verts_slice = verts.slice(verts_range);
MutableSpan<GreasePencilColorVert> cols_slice = cols.slice(verts_range);
const int material_nr = drawing.runtime->stroke_cache.mat;
if (grease_pencil.runtime->has_stroke_buffer()) {
Span<bke::greasepencil::StrokePoint> points = grease_pencil.runtime->stroke_buffer();
const int verts_start_offset = v_offset;
const int num_verts = 1 + points.size() + 1;
IndexRange verts_range = IndexRange(verts_start_offset, num_verts);
MutableSpan<GreasePencilStrokeVert> verts_slice = verts.slice(verts_range);
MutableSpan<GreasePencilColorVert> cols_slice = cols.slice(verts_range);
const int material_nr = grease_pencil.runtime->stroke_cache.mat;
verts_slice.first().mat = -1;
for (const int i : IndexRange(points.size())) {
const int idx = i + 1;
GreasePencilStrokeVert &s_vert = verts_slice[idx];
GreasePencilColorVert &c_vert = cols_slice[idx];
const bke::greasepencil::StrokePoint &point = points[i];
verts_slice.first().mat = -1;
for (const int i : IndexRange(points.size())) {
const int idx = i + 1;
GreasePencilStrokeVert &s_vert = verts_slice[idx];
GreasePencilColorVert &c_vert = cols_slice[idx];
const bke::greasepencil::StrokePoint &point = points[i];
copy_v3_v3(s_vert.pos, point.position);
s_vert.radius = point.radius;
s_vert.opacity = point.opacity;
s_vert.point_id = verts_range[idx];
s_vert.stroke_id = verts_range.first();
s_vert.mat = material_nr;
copy_v3_v3(s_vert.pos, point.position);
s_vert.radius = point.radius;
s_vert.opacity = point.opacity;
s_vert.point_id = verts_range[idx];
s_vert.stroke_id = verts_range.first();
s_vert.mat = material_nr;
/* TODO */
s_vert.packed_asp_hard_rot = pack_rotation_aspect_hardness(0.0f, 1.0f, 1.0f);
/* TODO */
s_vert.u_stroke = 0;
/* TODO */
s_vert.uv_fill[0] = s_vert.uv_fill[1] = 0;
/* TODO */
s_vert.packed_asp_hard_rot = pack_rotation_aspect_hardness(0.0f, 1.0f, 1.0f);
/* TODO */
s_vert.u_stroke = 0;
/* TODO */
s_vert.uv_fill[0] = s_vert.uv_fill[1] = 0;
/* TODO */
copy_v4_v4(c_vert.vcol, float4(0.0f, 0.0f, 0.0f, 0.0f));
copy_v4_v4(c_vert.fcol, float4(0.0f, 0.0f, 0.0f, 0.0f));
/* TODO */
copy_v4_v4(c_vert.vcol, float4(0.0f, 0.0f, 0.0f, 0.0f));
copy_v4_v4(c_vert.fcol, float4(0.0f, 0.0f, 0.0f, 0.0f));
/* TODO */
c_vert.fcol[3] = (int(c_vert.fcol[3] * 10000.0f) * 10.0f) + 1.0f;
/* TODO */
c_vert.fcol[3] = (int(c_vert.fcol[3] * 10000.0f) * 10.0f) + 1.0f;
int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
GPU_indexbuf_add_tri_verts(&ibo, v_mat + 0, v_mat + 1, v_mat + 2);
GPU_indexbuf_add_tri_verts(&ibo, v_mat + 2, v_mat + 1, v_mat + 3);
}
verts_slice.last().mat = -1;
int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
GPU_indexbuf_add_tri_verts(&ibo, v_mat + 0, v_mat + 1, v_mat + 2);
GPU_indexbuf_add_tri_verts(&ibo, v_mat + 2, v_mat + 1, v_mat + 3);
}
verts_slice.last().mat = -1;
}
/* Mark last 2 verts as invalid. */

@ -72,7 +72,7 @@ struct PaintOperationExecutor {
bke::greasepencil::StrokePoint new_point{
proj_pos, stroke_extension.pressure * 100.0f, 1.0f, float4(1.0f)};
drawing.runtime->stroke_cache.points.append(std::move(new_point));
grease_pencil.runtime->stroke_cache.points.append(std::move(new_point));
BKE_grease_pencil_batch_cache_dirty_tag(&grease_pencil, BKE_GREASEPENCIL_BATCH_DIRTY_ALL);
}
@ -106,7 +106,8 @@ void PaintOperation::on_stroke_done(const bContext &C)
bke::greasepencil::Drawing &drawing_eval =
reinterpret_cast<GreasePencilDrawing *>(grease_pencil_eval.drawings()[index_eval])->wrap();
const Span<bke::greasepencil::StrokePoint> stroke_points = drawing_eval.stroke_buffer();
const Span<bke::greasepencil::StrokePoint> stroke_points =
grease_pencil_eval.runtime->stroke_buffer();
CurvesGeometry &curves = drawing_orig.geometry.wrap();
int num_old_curves = curves.curves_num();
@ -162,7 +163,7 @@ void PaintOperation::on_stroke_done(const bContext &C)
return true;
});
drawing_eval.runtime->stroke_cache.clear();
grease_pencil_eval.runtime->stroke_cache.clear();
drawing_orig.tag_positions_changed();
radii.finish();