Blender V4.3
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
9#pragma once
10
11#include "image_texture_info.hh"
12
14
17 TextureInfo &info;
18
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) {
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::draw::image_engine
void GPU_batch_init_ex(blender::gpu::Batch *batch, GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
#define GPU_BATCH_CLEAR_SAFE(batch)
Definition GPU_batch.hh:191
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
@ GPU_PRIM_TRI_FAN
#define GPU_vertbuf_create_with_format(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)
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
int GPU_vertformat_attr_id_get(const GPUVertFormat *, const char *name)
@ GPU_COMP_F32
@ GPU_COMP_I32
Create gpu::Batch for a IMAGE_ScreenSpaceTextureInfo.
Batch * GPU_batch_calloc()
Definition gpu_batch.cc:49
format
gpu::Batch * batch
Batch to draw the associated text on the screen.
rcti clipping_bounds
area of the texture in screen space.
rctf clipping_uv_bounds
uv area of the texture in screen space.