Blender V5.0
image_batches.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "image_texture_info.hh"
12
14
17 TextureInfo &info;
18
19 GPUVertFormat format = {0};
20 int pos_id;
21 int uv_id;
22
23 public:
24 BatchUpdater(TextureInfo &info) : info(info) {}
25
27 {
28 ensure_clear_batch();
29 ensure_format();
30 init_batch();
31 }
32
33 private:
34 void ensure_clear_batch()
35 {
37 if (info.batch == nullptr) {
38 info.batch = GPU_batch_calloc();
39 }
40 }
41
42 void init_batch()
43 {
44 gpu::VertBuf *vbo = create_vbo();
46 }
47
48 template<typename DataType, typename RectType>
49 static void fill_tri_fan_from_rect(DataType result[4][2], RectType &rect)
50 {
51 result[0][0] = rect.xmin;
52 result[0][1] = rect.ymin;
53 result[1][0] = rect.xmax;
54 result[1][1] = rect.ymin;
55 result[2][0] = rect.xmax;
56 result[2][1] = rect.ymax;
57 result[3][0] = rect.xmin;
58 result[3][1] = rect.ymax;
59 }
60
61 gpu::VertBuf *create_vbo()
62 {
63 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
65 int pos[4][2];
66 fill_tri_fan_from_rect<int, rcti>(pos, info.clipping_bounds);
67 float uv[4][2];
68 fill_tri_fan_from_rect<float, rctf>(uv, info.clipping_uv_bounds);
69
70 for (int i = 0; i < 4; i++) {
71 GPU_vertbuf_attr_set(vbo, pos_id, i, pos[i]);
72 GPU_vertbuf_attr_set(vbo, uv_id, i, uv[i]);
73 }
74
75 return vbo;
76 }
77
78 void ensure_format()
79 {
80 if (format.attr_len == 0) {
81 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SINT_32_32);
82 GPU_vertformat_attr_add(&format, "uv", gpu::VertAttrType::SFLOAT_32_32);
83
84 pos_id = GPU_vertformat_attr_id_get(&format, "pos");
85 uv_id = GPU_vertformat_attr_id_get(&format, "uv");
86 }
87 }
88};
89
90} // namespace blender::image_engine
#define GPU_BATCH_CLEAR_SAFE(batch)
Definition GPU_batch.hh:183
void GPU_batch_init_ex(blender::gpu::Batch *batch, GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
@ GPU_PRIM_TRI_FAN
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
void GPU_vertbuf_attr_set(blender::gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
int GPU_vertformat_attr_id_get(const GPUVertFormat *, blender::StringRef name)
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
Batch * GPU_batch_calloc()
Definition gpu_batch.cc:44
uint pos
gpu::Batch * batch
Batch to draw the associated text on the screen.
i
Definition text_draw.cc:230