Blender V5.3
glutil.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_userdef_types.h"
10#include "DNA_vec_types.h"
11
12#include "BLI_utildefines.hh"
13
14#include "BIF_glutil.hh"
15
17#include "IMB_imbuf_types.hh"
18
19#include "GPU_immediate.hh"
20#include "GPU_texture.hh"
21
22namespace blender {
23
24void PixelBitmapDrawer::init_vertex_attributes()
25{
26 GPUVertFormat *vert_format = immVertexFormat();
27 this->pos = GPU_vertformat_attr_add(vert_format, "pos", gpu::VertAttrType::SFLOAT_32_32);
28 this->texco = GPU_vertformat_attr_add(vert_format, "texCoord", gpu::VertAttrType::SFLOAT_32_32);
29}
30
32{
33 init_vertex_attributes();
34}
35
37{
38 init_vertex_attributes();
39
40 this->shader = GPU_shader_get_builtin_shader(builtin_shader);
41 /* Shader will be unbound in draw(). */
42 immBindBuiltinProgram(builtin_shader);
43}
44
45void PixelBitmapDrawer::draw(const float x,
46 const float y,
47 const int img_w,
48 const int img_h,
49 const gpu::TextureFormat gpu_format,
50 const bool use_filter,
51 const void *rect,
52 const float scale_x,
53 const float scale_y,
54 const float color[4])
55{
56 const float draw_width = img_w * scale_x;
57 const float draw_height = img_h * scale_y;
58
59 /* When scaling down by more than 2x, create mipmaps for the texture and
60 * use trilinear filtering. */
61 const bool use_mipmap = use_filter && (scale_x < 0.5f || scale_y < 0.5f);
62 const int mip_len = use_mipmap ? 9999 : 1;
63
65 if (use_mipmap) {
67 }
69 "immDrawPixels", img_w, img_h, mip_len, gpu_format, usage, nullptr);
70 if (tex == nullptr) {
71 return;
72 }
73
74 const bool use_float_data = ELEM(gpu_format,
75 gpu::TextureFormat::SFLOAT_16_16_16_16,
76 gpu::TextureFormat::SFLOAT_16_16_16,
77 gpu::TextureFormat::SFLOAT_16);
78 eGPUDataFormat gpu_data_format = use_float_data ? GPU_DATA_FLOAT : GPU_DATA_UBYTE;
79 GPU_texture_update(tex, gpu_data_format, rect);
80
81 GPU_texture_filter_mode(tex, use_filter);
82 if (use_mipmap) {
84 GPU_texture_mipmap_mode(tex, true, true);
85 }
87
88 GPU_texture_bind(tex, 0);
89
90 /* NOTE: Shader could be null for GLSL OCIO drawing, it is fine, since
91 * it does not need color. */
92 static const float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
93 if (this->shader != nullptr && GPU_shader_get_uniform(this->shader, "color") != -1) {
94 immUniformColor4fv((color) ? color : white);
95 }
96
97 const uint pos = this->pos, texco = this->texco;
98
100 immAttr2f(texco, 0.0f, 0.0f);
101 immVertex2f(pos, x, y);
102
103 immAttr2f(texco, 1.0f, 0.0f);
104 immVertex2f(pos, x + draw_width, y);
105
106 immAttr2f(texco, 1.0f, 1.0f);
107 immVertex2f(pos, x + draw_width, y + draw_height);
108
109 immAttr2f(texco, 0.0f, 1.0f);
110 immVertex2f(pos, x, y + draw_height);
111 immEnd();
112
113 if (this->shader) {
115 }
116
118 GPU_texture_free(tex);
119}
120
121void ED_draw_imbuf(const ImBuf *ibuf,
122 float x,
123 float y,
124 bool use_filter,
125 const ColorManagedViewSettings *view_settings,
126 const ColorManagedDisplaySettings *display_settings,
127 float zoom_x,
128 float zoom_y)
129{
130 using namespace blender::gpu;
131
132 /* Early out */
133 if (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr) {
134 return;
135 }
136
137 PixelBitmapDrawer drawer;
138
139 const ColorSpace *colorspace = ibuf->float_data() ? ibuf->float_buffer.colorspace :
141 const bool predivide = ibuf->float_data() != nullptr;
143 view_settings, display_settings, colorspace, ibuf->dither, predivide, false))
144 {
145 return;
146 }
147
148 const void *texture_data = nullptr;
150 if (ibuf->float_data()) {
151 texture_data = ibuf->float_data();
152 if (ibuf->channels == 1) {
153 format = TextureFormat::SFLOAT_16;
154 }
155 else if (ibuf->channels == 3) {
156 format = TextureFormat::SFLOAT_16_16_16;
157 }
158 else if (ibuf->channels == 4) {
159 format = TextureFormat::SFLOAT_16_16_16_16;
160 }
161 else {
162 BLI_assert_msg(0, "Incompatible number of channels for GLSL display");
163 }
164 }
165 else {
166 texture_data = ibuf->byte_data();
167 format = TextureFormat::UNORM_8_8_8_8;
168 }
169
170 if (format != TextureFormat::Invalid) {
171 drawer.draw(x, y, ibuf->x, ibuf->y, format, use_filter, texture_data, zoom_x, zoom_y, nullptr);
172 }
173
175}
176
178 const ImBuf *ibuf,
179 float x,
180 float y,
181 bool use_filter,
182 float zoom_x,
183 float zoom_y)
184{
185 ColorManagedViewSettings *view_settings;
186 ColorManagedDisplaySettings *display_settings;
187 IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
188 ED_draw_imbuf(ibuf, x, y, use_filter, view_settings, display_settings, zoom_x, zoom_y);
189}
190
191void immDrawBorderCorners(uint pos, const rcti *border, float zoomx, float zoomy)
192{
193 float delta_x = 4.0f * UI_SCALE_FAC / zoomx;
194 float delta_y = 4.0f * UI_SCALE_FAC / zoomy;
195
196 delta_x = min_ff(delta_x, border->xmax - border->xmin);
197 delta_y = min_ff(delta_y, border->ymax - border->ymin);
198
199 /* left bottom corner */
201 immVertex2f(pos, border->xmin, border->ymin + delta_y);
202 immVertex2f(pos, border->xmin, border->ymin);
203 immVertex2f(pos, border->xmin + delta_x, border->ymin);
204 immEnd();
205
206 /* left top corner */
208 immVertex2f(pos, border->xmin, border->ymax - delta_y);
209 immVertex2f(pos, border->xmin, border->ymax);
210 immVertex2f(pos, border->xmin + delta_x, border->ymax);
211 immEnd();
212
213 /* right bottom corner */
215 immVertex2f(pos, border->xmax - delta_x, border->ymin);
216 immVertex2f(pos, border->xmax, border->ymin);
217 immVertex2f(pos, border->xmax, border->ymin + delta_y);
218 immEnd();
219
220 /* right top corner */
222 immVertex2f(pos, border->xmax - delta_x, border->ymax);
223 immVertex2f(pos, border->xmax, border->ymax);
224 immVertex2f(pos, border->xmax, border->ymax - delta_y);
225 immEnd();
226}
227
228} // namespace blender
#define UI_SCALE_FAC
#define C
Definition RandGen.cpp:29
#define ELEM(...)
#define BLI_assert_msg(a, msg)
void draw(float x, float y, int img_w, int img_h, gpu::TextureFormat gpu_format, bool use_filter, const void *rect, float scale_x, float scale_y, const float color[4])
Definition glutil.cc:45
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void immEnd()
unsigned int uint
Definition BKE_text.h:55
void IMB_colormanagement_finish_glsl_draw()
void immDrawBorderCorners(unsigned int pos, const rcti *border, float zoomx, float zoomy)
Definition glutil.cc:191
GPUVertFormat * immVertexFormat()
void GPU_texture_update_mipmap_chain(gpu::Texture *texture)
void ED_draw_imbuf(const ImBuf *ibuf, float x, float y, bool use_filter, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, float zoom_x, float zoom_y)
Definition glutil.cc:121
void immBegin(GPUPrimType, uint vertex_len)
bool IMB_colormanagement_setup_glsl_draw_from_space(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, const ColorSpace *from_colorspace, float dither, bool predivide, bool do_overlay_merge, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW, float opacity=1.0f)
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_PRIM_LINE_STRIP
void immUniformColor4fv(const float rgba[4])
void immAttr2f(uint attr_id, float x, float y)
void immUnbindProgram()
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
void immVertex2f(uint attr_id, float x, float y)
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void IMB_colormanagement_display_settings_from_ctx(const bContext *C, ColorManagedViewSettings **r_view_settings, ColorManagedDisplaySettings **r_display_settings)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
int GPU_shader_get_uniform(gpu::Shader *shader, const char *name)
void ED_draw_imbuf_ctx(const bContext *C, const ImBuf *ibuf, float x, float y, bool use_filter, float zoom_x, float zoom_y)
Definition glutil.cc:177
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
void GPU_texture_bind(gpu::Texture *texture, int unit)
void GPU_texture_free(gpu::Texture *texture)
void GPU_texture_unbind(gpu::Texture *texture)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
const float * float_data() const