Blender  V3.3
overlay_antialiasing.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. */
3 
42 #include "DRW_render.h"
43 
44 #include "ED_screen.h"
45 
46 #include "overlay_private.h"
47 
49 {
50  OVERLAY_FramebufferList *fbl = vedata->fbl;
51  OVERLAY_TextureList *txl = vedata->txl;
52  OVERLAY_PrivateData *pd = vedata->stl->pd;
54 
55  /* Small texture which will have very small impact on render-time. */
56  if (txl->dummy_depth_tx == NULL) {
57  const float pixel[1] = {1.0f};
59  }
60 
61  if (!DRW_state_is_fbo()) {
62  pd->antialiasing.enabled = false;
63  return;
64  }
65 
66  bool need_wire_expansion = (G_draw.block.size_pixel > 1.0f);
67  pd->antialiasing.enabled = need_wire_expansion ||
68  ((U.gpu_flag & USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE) != 0);
69 
70  GPUTexture *color_tex = NULL;
71  GPUTexture *line_tex = NULL;
72 
73  if (pd->antialiasing.enabled) {
76 
77  color_tex = txl->overlay_color_tx;
78  line_tex = txl->overlay_line_tx;
79  }
80  else {
81  /* Just a copy of the defaults frame-buffers. */
82  color_tex = dtxl->color_overlay;
83  }
84 
85  GPU_framebuffer_ensure_config(&fbl->overlay_color_only_fb,
86  {
87  GPU_ATTACHMENT_NONE,
88  GPU_ATTACHMENT_TEXTURE(color_tex),
89  });
90  GPU_framebuffer_ensure_config(&fbl->overlay_default_fb,
91  {
92  GPU_ATTACHMENT_TEXTURE(dtxl->depth),
93  GPU_ATTACHMENT_TEXTURE(color_tex),
94  });
95  GPU_framebuffer_ensure_config(&fbl->overlay_line_fb,
96  {
97  GPU_ATTACHMENT_TEXTURE(dtxl->depth),
98  GPU_ATTACHMENT_TEXTURE(color_tex),
99  GPU_ATTACHMENT_TEXTURE(line_tex),
100  });
101 }
102 
104 {
105  OVERLAY_TextureList *txl = vedata->txl;
106  OVERLAY_PrivateData *pd = vedata->stl->pd;
107  OVERLAY_PassList *psl = vedata->psl;
109  struct GPUShader *sh;
110  DRWShadingGroup *grp;
111 
112  if (pd->antialiasing.enabled) {
113  /* `antialiasing.enabled` is also enabled for wire expansion. Check here if
114  * anti aliasing is needed. */
115  const bool do_smooth_lines = (U.gpu_flag & USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE) != 0;
116 
118 
121  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
122  DRW_shgroup_uniform_bool_copy(grp, "doSmoothLines", do_smooth_lines);
123  DRW_shgroup_uniform_texture_ref(grp, "depthTex", &dtxl->depth);
124  DRW_shgroup_uniform_texture_ref(grp, "colorTex", &txl->overlay_color_tx);
125  DRW_shgroup_uniform_texture_ref(grp, "lineTex", &txl->overlay_line_tx);
127  }
128 
129  /* A bit out of place... not related to antialiasing. */
130  if (pd->xray_enabled) {
132 
134  grp = DRW_shgroup_create(sh, psl->xray_fade_ps);
135  DRW_shgroup_uniform_texture_ref(grp, "depthTex", &dtxl->depth);
136  DRW_shgroup_uniform_texture_ref(grp, "xrayDepthTex", &txl->temp_depth_tx);
137  DRW_shgroup_uniform_float_copy(grp, "opacity", 1.0f - pd->xray_opacity);
139  }
140 }
141 
143 {
144  OVERLAY_FramebufferList *fbl = vedata->fbl;
145  OVERLAY_TextureList *txl = vedata->txl;
146  OVERLAY_PassList *psl = vedata->psl;
147  OVERLAY_PrivateData *pd = vedata->stl->pd;
149 
150  if (pd->antialiasing.enabled) {
151  GPU_framebuffer_ensure_config(&fbl->overlay_in_front_fb,
152  {GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
153  GPU_ATTACHMENT_TEXTURE(txl->overlay_color_tx)});
154 
155  GPU_framebuffer_ensure_config(&fbl->overlay_line_in_front_fb,
156  {GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
157  GPU_ATTACHMENT_TEXTURE(txl->overlay_color_tx),
158  GPU_ATTACHMENT_TEXTURE(txl->overlay_line_tx)});
159  }
160  else {
161  GPU_framebuffer_ensure_config(&fbl->overlay_in_front_fb,
162  {GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
163  GPU_ATTACHMENT_TEXTURE(dtxl->color_overlay)});
164 
165  GPU_framebuffer_ensure_config(&fbl->overlay_line_in_front_fb,
166  {GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
167  GPU_ATTACHMENT_TEXTURE(dtxl->color_overlay),
168  GPU_ATTACHMENT_TEXTURE(txl->overlay_line_tx)});
169  }
170 
171  pd->antialiasing.do_depth_copy = !(psl->wireframe_ps == NULL ||
173  (pd->xray_enabled && pd->xray_opacity > 0.0f);
176 
177  const bool do_wireframe = pd->antialiasing.do_depth_copy ||
179  if (pd->xray_enabled || do_wireframe) {
181  }
182 }
183 
185 {
186  OVERLAY_FramebufferList *fbl = vedata->fbl;
187  OVERLAY_PrivateData *pd = vedata->stl->pd;
188 
189  if (pd->antialiasing.enabled) {
190  const float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
192  GPU_framebuffer_clear_color(fbl->overlay_line_fb, clear_col);
193  }
194 
195  /* If we are not in solid shading mode, we clear the depth. */
196  if (DRW_state_is_fbo() && pd->clear_in_front) {
197  /* TODO(fclem): This clear should be done in a global place. */
199  GPU_framebuffer_clear_depth(fbl->overlay_in_front_fb, 1.0f);
200  }
201 }
202 
204 {
205  OVERLAY_FramebufferList *fbl = vedata->fbl;
206  OVERLAY_TextureList *txl = vedata->txl;
207  OVERLAY_PrivateData *pd = vedata->stl->pd;
208 
211  /* We copy the depth of the rendered geometry to be able to compare to the overlays depth. */
212  GPU_texture_copy(txl->temp_depth_tx, dtxl->depth);
213  }
214 
215  if (DRW_state_is_fbo() && pd->xray_enabled) {
216  /* We then clear to not occlude the overlays directly. */
218  GPU_framebuffer_clear_depth(fbl->overlay_default_fb, 1.0f);
219  }
220 }
221 
223 {
224  OVERLAY_TextureList *txl = vedata->txl;
225  OVERLAY_PrivateData *pd = vedata->stl->pd;
226 
229  /* We copy the depth of the rendered geometry to be able to compare to the overlays depth. */
231  }
232 }
233 
235 {
236  OVERLAY_PassList *psl = vedata->psl;
237  OVERLAY_PrivateData *pd = vedata->stl->pd;
238 
239  if (DRW_state_is_fbo() && pd->xray_enabled && pd->xray_opacity > 0.0f) {
240  /* Partially occlude overlays using the geometry depth pass. */
242  }
243 }
244 
246 {
247  OVERLAY_PassList *psl = vedata->psl;
248  OVERLAY_PrivateData *pd = vedata->stl->pd;
250 
251  if (pd->antialiasing.enabled) {
254  }
255 }
@ USER_GPU_FLAG_OVERLAY_SMOOTH_WIRE
@ DRW_TEX_FILTER
Definition: DRW_render.h:140
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:303
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition: DRW_render.h:330
@ DRW_STATE_BLEND_MUL
Definition: DRW_render.h:333
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:690
#define DRW_shgroup_uniform_block(shgroup, name, ubo)
Definition: DRW_render.h:651
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
struct GPUShader GPUShader
Definition: GPU_shader.h:20
void GPU_texture_copy(GPUTexture *dst, GPUTexture *src)
Definition: gpu_texture.cc:503
struct GPUTexture GPUTexture
Definition: GPU_texture.h:17
@ GPU_SRGB8_A8
Definition: GPU_texture.h:121
@ GPU_DEPTH24_STENCIL8
Definition: GPU_texture.h:120
@ GPU_DEPTH_COMPONENT24
Definition: GPU_texture.h:166
@ GPU_RGBA8
Definition: GPU_texture.h:87
unsigned int U
Definition: btGjkEpa3.h:78
struct DRW_Global G_draw
Definition: draw_common.c:32
DefaultFramebufferList * DRW_viewport_framebuffer_list_get(void)
Definition: draw_manager.c:633
bool DRW_state_is_fbo(void)
DefaultTextureList * DRW_viewport_texture_list_get(void)
Definition: draw_manager.c:638
void DRW_shgroup_uniform_float_copy(DRWShadingGroup *shgroup, const char *name, const float value)
void DRW_shgroup_call_procedural_triangles(DRWShadingGroup *shgroup, Object *ob, uint tri_count)
bool DRW_pass_is_empty(DRWPass *pass)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_shgroup_uniform_texture_ref(DRWShadingGroup *shgroup, const char *name, GPUTexture **tex)
void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value)
void DRW_draw_pass(DRWPass *pass)
void DRW_texture_ensure_fullscreen_2d(GPUTexture **tex, eGPUTextureFormat format, DRWTextureFlag flags)
GPUTexture * DRW_texture_create_2d(int w, int h, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels)
ccl_gpu_kernel_postfix ccl_global float int int int int sh
void OVERLAY_antialiasing_cache_init(OVERLAY_Data *vedata)
void OVERLAY_antialiasing_cache_finish(OVERLAY_Data *vedata)
void OVERLAY_antialiasing_init(OVERLAY_Data *vedata)
void OVERLAY_xray_depth_copy(OVERLAY_Data *vedata)
void OVERLAY_antialiasing_start(OVERLAY_Data *vedata)
void OVERLAY_xray_depth_infront_copy(OVERLAY_Data *vedata)
void OVERLAY_xray_fade_draw(OVERLAY_Data *vedata)
void OVERLAY_antialiasing_end(OVERLAY_Data *vedata)
GPUShader * OVERLAY_shader_xray_fade(void)
GPUShader * OVERLAY_shader_antialiasing(void)
GlobalsUboStorage block
Definition: draw_common.h:127
struct GPUUniformBuf * block_ubo
Definition: draw_common.h:129
struct GPUFrameBuffer * overlay_only_fb
struct GPUTexture * depth
struct GPUTexture * depth_in_front
struct GPUTexture * color_overlay
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
OVERLAY_FramebufferList * fbl
OVERLAY_TextureList * txl
struct GPUFrameBuffer * overlay_line_in_front_fb
struct GPUFrameBuffer * overlay_in_front_fb
struct GPUFrameBuffer * overlay_default_fb
struct GPUFrameBuffer * overlay_line_fb
struct GPUFrameBuffer * overlay_color_only_fb
DRWPass * wireframe_ps
DRWPass * antialiasing_ps
DRWPass * wireframe_xray_ps
DRWPass * xray_fade_ps
struct OVERLAY_PrivateData::@249 antialiasing
struct OVERLAY_PrivateData * pd
struct GPUTexture * dummy_depth_tx
struct GPUTexture * temp_depth_tx
struct GPUTexture * overlay_line_tx
struct GPUTexture * overlay_color_tx