Blender  V3.3
image_batches.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. */
3 
8 #pragma once
9 
10 #include "image_texture_info.hh"
11 
13 class BatchUpdater {
14  TextureInfo &info;
15 
16  GPUVertFormat format = {0};
17  int pos_id;
18  int uv_id;
19 
20  public:
21  BatchUpdater(TextureInfo &info) : info(info)
22  {
23  }
24 
25  void update_batch()
26  {
27  ensure_clear_batch();
28  ensure_format();
29  init_batch();
30  }
31 
33  {
35  }
36 
37  private:
38  void ensure_clear_batch()
39  {
41  if (info.batch == nullptr) {
42  info.batch = GPU_batch_calloc();
43  }
44  }
45 
46  void init_batch()
47  {
48  GPUVertBuf *vbo = create_vbo();
50  }
51 
52  GPUVertBuf *create_vbo()
53  {
55  GPU_vertbuf_data_alloc(vbo, 4);
56  float pos[4][2];
57  fill_tri_fan_from_rctf(pos, info.clipping_bounds);
58  float uv[4][2];
59  fill_tri_fan_from_rctf(uv, info.clipping_uv_bounds);
60 
61  for (int i = 0; i < 4; i++) {
62  GPU_vertbuf_attr_set(vbo, pos_id, i, pos[i]);
63  GPU_vertbuf_attr_set(vbo, uv_id, i, uv[i]);
64  }
65 
66  return vbo;
67  }
68 
69  static void fill_tri_fan_from_rctf(float result[4][2], rctf &rect)
70  {
71  result[0][0] = rect.xmin;
72  result[0][1] = rect.ymin;
73  result[1][0] = rect.xmax;
74  result[1][1] = rect.ymin;
75  result[2][0] = rect.xmax;
76  result[2][1] = rect.ymax;
77  result[3][0] = rect.xmin;
78  result[3][1] = rect.ymax;
79  }
80 
81  void ensure_format()
82  {
83  if (format.attr_len == 0) {
86 
87  pos_id = GPU_vertformat_attr_id_get(&format, "pos");
88  uv_id = GPU_vertformat_attr_id_get(&format, "uv");
89  }
90  }
91 };
GPUBatch * GPU_batch_calloc(void)
Definition: gpu_batch.cc:36
void GPU_batch_init_ex(GPUBatch *batch, GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:53
#define GPU_BATCH_CLEAR_SAFE(batch)
Definition: GPU_batch.h:224
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition: GPU_batch.h:216
@ GPU_BATCH_OWNS_VBO
Definition: GPU_batch.h:30
@ GPU_PRIM_TRI_FAN
Definition: GPU_primitive.h:25
#define GPU_vertbuf_create_with_format(format)
struct GPUVertBuf GPUVertBuf
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
void GPU_vertbuf_attr_set(GPUVertBuf *, uint a_idx, uint v_idx, const void *data)
@ GPU_FETCH_FLOAT
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
Create GPUBatch for a IMAGE_ScreenSpaceTextureInfo.
BatchUpdater(TextureInfo &info)
void discard_batch()
void update_batch()
uint pos
format
Definition: logImageCore.h:38
GPUBatch * batch
Batch to draw the associated text on the screen.
rctf clipping_uv_bounds
uv area of the texture in screen space.
rctf clipping_bounds
area of the texture in screen space.
float xmax
Definition: DNA_vec_types.h:69
float xmin
Definition: DNA_vec_types.h:69
float ymax
Definition: DNA_vec_types.h:70
float ymin
Definition: DNA_vec_types.h:70