Blender V5.0
sequencer_quads_batch.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_color.hh"
13
14#include "GPU_batch.hh"
15#include "GPU_index_buffer.hh"
16#include "GPU_vertex_buffer.hh"
17
18namespace blender::ed::vse {
19
24static_assert(sizeof(ColorVertex) == 12);
25
27{
29 GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, quads_count * 2, quads_count * 4);
30 for (int i = 0; i < quads_count; i++) {
31 const uint i0 = i * 4 + 0;
32 const uint i1 = i * 4 + 1;
33 const uint i2 = i * 4 + 2;
34 const uint i3 = i * 4 + 3;
35 GPU_indexbuf_add_tri_verts(&elb, i0, i1, i2);
36 GPU_indexbuf_add_tri_verts(&elb, i2, i1, i3);
37 }
38 return GPU_indexbuf_build(&elb);
39}
40
42{
43 ibo_quads = create_quads_index_buffer(MAX_QUADS);
44
47 GPU_vertformat_attr_add(&format, "pos", blender::gpu::VertAttrType::SFLOAT_32_32);
48 GPU_vertformat_attr_add(&format, "color", blender::gpu::VertAttrType::UNORM_8_8_8_8);
49
51 GPU_vertbuf_data_alloc(*vbo_quads, MAX_QUADS * 4);
52
54 GPU_vertbuf_data_alloc(*vbo_lines, MAX_LINES * 2);
55
56 batch_quads = GPU_batch_create_ex(
59
60 batch_lines = GPU_batch_create_ex(GPU_PRIM_LINES, vbo_lines, nullptr, GPU_BATCH_OWNS_VBO);
62}
63
65{
66 BLI_assert_msg(quads_num == 0 && lines_num == 0,
67 "SeqQuadsBatch is being destroyed without drawing quads/lines it contains");
68 GPU_batch_discard(batch_quads);
69 GPU_batch_discard(batch_lines);
70}
71
73{
74 if (quads_num > 0) {
75 GPU_vertbuf_tag_dirty(vbo_quads);
76 GPU_vertbuf_use(vbo_quads);
77 GPU_batch_draw_range(batch_quads, 0, quads_num * 6);
78 quads_num = 0;
79 verts_quads = nullptr;
80 }
81 if (lines_num > 0) {
82 GPU_vertbuf_tag_dirty(vbo_lines);
83 GPU_vertbuf_use(vbo_lines);
84 GPU_batch_draw_range(batch_lines, 0, lines_num * 2);
85 lines_num = 0;
86 verts_lines = nullptr;
87 }
88}
89
91 float y1,
92 float x2,
93 float y2,
94 float x3,
95 float y3,
96 float x4,
97 float y4,
98 const uchar color1[4],
99 const uchar color2[4],
100 const uchar color3[4],
101 const uchar color4[4])
102{
103 if (quads_num >= MAX_QUADS) {
104 draw();
105 }
106 if (quads_num == 0) {
107 verts_quads = vbo_quads->data<ColorVertex>().data();
108 BLI_assert(verts_quads != nullptr);
109 }
110
111 ColorVertex v0 = {blender::float2(x1, y1), color1};
112 ColorVertex v1 = {blender::float2(x2, y2), color2};
113 ColorVertex v2 = {blender::float2(x3, y3), color3};
114 ColorVertex v3 = {blender::float2(x4, y4), color4};
115
116 *verts_quads++ = v0;
117 *verts_quads++ = v1;
118 *verts_quads++ = v2;
119 *verts_quads++ = v3;
120
121 quads_num++;
122}
123
124void SeqQuadsBatch::add_wire_quad(float x1, float y1, float x2, float y2, const uchar color[4])
125{
126 if (lines_num + 4 > MAX_LINES) {
127 draw();
128 }
129 if (lines_num == 0) {
130 verts_lines = vbo_lines->data<ColorVertex>().data();
131 BLI_assert(verts_lines != nullptr);
132 }
133
134 ColorVertex v0 = {blender::float2(x1, y1), color};
135 ColorVertex v1 = {blender::float2(x1, y2), color};
136 ColorVertex v2 = {blender::float2(x2, y1), color};
137 ColorVertex v3 = {blender::float2(x2, y2), color};
138
139 /* Left */
140 *verts_lines++ = v0;
141 *verts_lines++ = v1;
142 /* Right */
143 *verts_lines++ = v2;
144 *verts_lines++ = v3;
145 /* Bottom */
146 *verts_lines++ = v0;
147 *verts_lines++ = v2;
148 /* Top */
149 *verts_lines++ = v1;
150 *verts_lines++ = v3;
151
152 lines_num += 4;
153}
154
156 float x1, float y1, float x2, float y2, const uchar color1[4], const uchar color2[4])
157{
158 if (lines_num + 1 > MAX_LINES) {
159 draw();
160 }
161 if (lines_num == 0) {
162 verts_lines = vbo_lines->data<ColorVertex>().data();
163 BLI_assert(verts_lines != nullptr);
164 }
165
166 ColorVertex v0 = {blender::float2(x1, y1), color1};
167 ColorVertex v1 = {blender::float2(x2, y2), color2};
168
169 *verts_lines++ = v0;
170 *verts_lines++ = v1;
171
172 lines_num++;
173}
174
175} // namespace blender::ed::vse
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
unsigned char uchar
unsigned int uint
void GPU_batch_discard(blender::gpu::Batch *batch)
void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, GPUBuiltinShader shader_id)
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:46
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
void GPU_batch_draw_range(blender::gpu::Batch *batch, int vertex_first, int vertex_count)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
blender::gpu::IndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3)
@ GPU_PRIM_LINES
@ GPU_PRIM_TRIS
@ GPU_SHADER_3D_SMOOTH_COLOR
void GPU_vertbuf_use(blender::gpu::VertBuf *)
blender::gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
void GPU_vertbuf_tag_dirty(blender::gpu::VertBuf *verts)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
@ GPU_USAGE_STREAM
void GPU_vertformat_clear(GPUVertFormat *)
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void add_quad(float x1, float y1, float x2, float y2, const uchar color[4])
void add_line(float x1, float y1, float x2, float y2, const uchar color[4])
void add_wire_quad(float x1, float y1, float x2, float y2, const uchar color[4])
format
static blender::gpu::IndexBuf * create_quads_index_buffer(int quads_count)
ColorTheme4< uint8_t > ColorTheme4b
VecBase< float, 2 > float2
i
Definition text_draw.cc:230