Blender V4.3
wm_stereo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdlib>
10#include <cstring>
11
12#include "DNA_listBase.h"
13
14#include "RNA_access.hh"
15#include "RNA_prototypes.hh"
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_utildefines.h"
20
21#include "BKE_context.hh"
22#include "BKE_global.hh"
23#include "BKE_report.hh"
24
25#include "BLT_translation.hh"
26
27#include "GHOST_C-api.h"
28
29#include "ED_screen.hh"
30
31#include "GPU_capabilities.hh"
32#include "GPU_immediate.hh"
33#include "GPU_texture.hh"
34#include "GPU_viewport.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38#include "wm.hh"
39#include "wm_window.hh"
40
41#include "UI_interface.hh"
42#include "UI_resources.hh"
43
45{
46 bool cross_eyed = (win->stereo3d_format->flag & S3D_SIDEBYSIDE_CROSSEYED) != 0;
47
51
53
54 const blender::int2 win_size = WM_window_native_pixel_size(win);
55
56 int soffx = win_size[0] / 2;
57 if (view == STEREO_LEFT_ID) {
58 if (!cross_eyed) {
59 soffx = 0;
60 }
61 }
62 else { /* #RIGHT_LEFT_ID. */
63 if (cross_eyed) {
64 soffx = 0;
65 }
66 }
67
68 /* `wmOrtho` for the screen has this same offset. */
69 const float halfx = GLA_PIXEL_OFS / win_size[0];
70 const float halfy = GLA_PIXEL_OFS / win_size[1];
71
72 /* Texture is already bound to GL_TEXTURE0 unit. */
73
75
76 immAttr2f(texcoord, halfx, halfy);
77 immVertex2f(pos, soffx, 0.0f);
78
79 immAttr2f(texcoord, 1.0f + halfx, halfy);
80 immVertex2f(pos, soffx + (win_size[0] * 0.5f), 0.0f);
81
82 immAttr2f(texcoord, 1.0f + halfx, 1.0f + halfy);
83 immVertex2f(pos, soffx + (win_size[0] * 0.5f), win_size[1]);
84
85 immAttr2f(texcoord, halfx, 1.0f + halfy);
86 immVertex2f(pos, soffx, win_size[1]);
87
88 immEnd();
89
91}
92
94{
98
100
101 const blender::int2 win_size = WM_window_native_pixel_size(win);
102
103 int soffy;
104 if (view == STEREO_LEFT_ID) {
105 soffy = win_size[1] * 0.5f;
106 }
107 else { /* #STEREO_RIGHT_ID. */
108 soffy = 0;
109 }
110
111 /* `wmOrtho` for the screen has this same offset. */
112 const float halfx = GLA_PIXEL_OFS / win_size[0];
113 const float halfy = GLA_PIXEL_OFS / win_size[1];
114
115 /* Texture is already bound to GL_TEXTURE0 unit. */
116
118
119 immAttr2f(texcoord, halfx, halfy);
120 immVertex2f(pos, 0.0f, soffy);
121
122 immAttr2f(texcoord, 1.0f + halfx, halfy);
123 immVertex2f(pos, win_size[0], soffy);
124
125 immAttr2f(texcoord, 1.0f + halfx, 1.0f + halfy);
126 immVertex2f(pos, win_size[0], soffy + (win_size[1] * 0.5f));
127
128 immAttr2f(texcoord, halfx, 1.0f + halfy);
129 immVertex2f(pos, 0.0f, soffy + (win_size[1] * 0.5f));
130
131 immEnd();
132
134}
135
137{
138 return ELEM(stereo_display, S3D_DISPLAY_SIDEBYSIDE, S3D_DISPLAY_TOPBOTTOM);
139}
140
141bool WM_stereo3d_enabled(wmWindow *win, bool skip_stereo3d_check)
142{
143 const bScreen *screen = WM_window_get_active_screen(win);
144 const Scene *scene = WM_window_get_active_scene(win);
145
146 /* Some 3d methods change the window arrangement, thus they shouldn't
147 * toggle on/off just because there is no 3d elements being drawn. */
149 return GHOST_GetWindowState(static_cast<GHOST_WindowHandle>(win->ghostwin)) ==
151 }
152
153 if ((skip_stereo3d_check == false) && (ED_screen_stereo3d_required(screen, scene) == false)) {
154 return false;
155 }
156
157 /* Some 3d methods change the window arrangement, thus they shouldn't
158 * toggle on/off just because there is no 3d elements being drawn. */
160 return GHOST_GetWindowState(static_cast<GHOST_WindowHandle>(win->ghostwin)) ==
162 }
163
164 return true;
165}
166
167void wm_stereo3d_mouse_offset_apply(wmWindow *win, int r_mouse_xy[2])
168{
169 if (!WM_stereo3d_enabled(win, false)) {
170 return;
171 }
172
174 const int half_x = WM_window_native_pixel_x(win) / 2;
175 /* Right half of the screen. */
176 if (r_mouse_xy[0] > half_x) {
177 r_mouse_xy[0] -= half_x;
178 }
179 r_mouse_xy[0] *= 2;
180 }
182 const int half_y = WM_window_native_pixel_y(win) / 2;
183 /* Upper half of the screen. */
184 if (r_mouse_xy[1] > half_y) {
185 r_mouse_xy[1] -= half_y;
186 }
187 r_mouse_xy[1] *= 2;
188 }
189}
190
191/************************** Stereo 3D operator **********************************/
195
197{
198 Stereo3dData *s3dd = static_cast<Stereo3dData *>(op->customdata);
199 Stereo3dFormat *s3d = &s3dd->stereo3d_format;
200 PropertyRNA *prop;
201 bool is_set = false;
202
203 prop = RNA_struct_find_property(op->ptr, "display_mode");
204 if (RNA_property_is_set(op->ptr, prop)) {
205 s3d->display_mode = RNA_property_enum_get(op->ptr, prop);
206 is_set = true;
207 }
208
209 prop = RNA_struct_find_property(op->ptr, "anaglyph_type");
210 if (RNA_property_is_set(op->ptr, prop)) {
211 s3d->anaglyph_type = RNA_property_enum_get(op->ptr, prop);
212 is_set = true;
213 }
214
215 prop = RNA_struct_find_property(op->ptr, "interlace_type");
216 if (RNA_property_is_set(op->ptr, prop)) {
217 s3d->interlace_type = RNA_property_enum_get(op->ptr, prop);
218 is_set = true;
219 }
220
221 prop = RNA_struct_find_property(op->ptr, "use_interlace_swap");
222 if (RNA_property_is_set(op->ptr, prop)) {
223 if (RNA_property_boolean_get(op->ptr, prop)) {
224 s3d->flag |= S3D_INTERLACE_SWAP;
225 }
226 else {
227 s3d->flag &= ~S3D_INTERLACE_SWAP;
228 }
229 is_set = true;
230 }
231
232 prop = RNA_struct_find_property(op->ptr, "use_sidebyside_crosseyed");
233 if (RNA_property_is_set(op->ptr, prop)) {
234 if (RNA_property_boolean_get(op->ptr, prop)) {
236 }
237 else {
238 s3d->flag &= ~S3D_SIDEBYSIDE_CROSSEYED;
239 }
240 is_set = true;
241 }
242
243 return is_set;
244}
245
247{
248 wmWindow *win = CTX_wm_window(C);
249
250 Stereo3dData *s3dd = static_cast<Stereo3dData *>(MEM_callocN(sizeof(Stereo3dData), __func__));
251 op->customdata = s3dd;
252
253 /* Store the original win stereo 3d settings in case of cancel. */
254 s3dd->stereo3d_format = *win->stereo3d_format;
255}
256
258{
260 wmWindow *win_src = CTX_wm_window(C);
261 wmWindow *win_dst = nullptr;
262 const bool is_fullscreen = WM_window_is_fullscreen(win_src);
263 char prev_display_mode = win_src->stereo3d_format->display_mode;
264 bool ok = true;
265
266 if (G.background) {
267 return OPERATOR_CANCELLED;
268 }
269
270 if (op->customdata == nullptr) {
271 /* No invoke means we need to set the operator properties here. */
274 }
275
276 Stereo3dData *s3dd = static_cast<Stereo3dData *>(op->customdata);
277 *win_src->stereo3d_format = s3dd->stereo3d_format;
278
279 if (prev_display_mode == S3D_DISPLAY_PAGEFLIP &&
280 prev_display_mode != win_src->stereo3d_format->display_mode)
281 {
282 /* In case the hardware supports page-flip but not the display. */
283 if ((win_dst = wm_window_copy_test(C, win_src, false, false))) {
284 /* Pass. */
285 }
286 else {
288 op->reports,
289 RPT_ERROR,
290 "Failed to create a window without quad-buffer support, you may experience flickering");
291 ok = false;
292 }
293 }
294 else if (win_src->stereo3d_format->display_mode == S3D_DISPLAY_PAGEFLIP) {
295 const bScreen *screen = WM_window_get_active_screen(win_src);
296
297 /* #ED_workspace_layout_duplicate() can't handle other cases yet #44688 */
298 if (screen->state != SCREENNORMAL) {
300 op->reports, RPT_ERROR, "Failed to switch to Time Sequential mode when in fullscreen");
301 ok = false;
302 }
303 /* Page-flip requires a new window to be created with the proper OS flags. */
304 else if ((win_dst = wm_window_copy_test(C, win_src, false, false))) {
306 BKE_report(op->reports, RPT_INFO, "Quad-buffer window successfully created");
307 }
308 else {
309 wm_window_close(C, wm, win_dst);
310 win_dst = nullptr;
311 BKE_report(op->reports, RPT_ERROR, "Quad-buffer not supported by the system");
312 ok = false;
313 }
314 }
315 else {
317 RPT_ERROR,
318 "Failed to create a window compatible with the time sequential display method");
319 ok = false;
320 }
321 }
322
324 if (!is_fullscreen) {
325 BKE_report(op->reports, RPT_INFO, "Stereo 3D Mode requires the window to be fullscreen");
326 }
327 }
328
330
331 if (ok) {
332 if (win_dst) {
333 wm_window_close(C, wm, win_src);
334 }
335
336 WM_event_add_notifier(C, NC_WINDOW, nullptr);
337 return OPERATOR_FINISHED;
338 }
339
340 /* Without this, the popup won't be freed properly, see #44688. */
341 CTX_wm_window_set(C, win_src);
342 win_src->stereo3d_format->display_mode = prev_display_mode;
343 return OPERATOR_CANCELLED;
344}
345
346int wm_stereo3d_set_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
347{
349
350 if (wm_stereo3d_set_properties(C, op)) {
351 return wm_stereo3d_set_exec(C, op);
352 }
353 return WM_operator_props_dialog_popup(C, op, 300, IFACE_("Set Stereo 3D"), IFACE_("Set"));
354}
355
357{
358 Stereo3dData *s3dd = static_cast<Stereo3dData *>(op->customdata);
359 uiLayout *layout = op->layout;
360 uiLayout *col;
361
362 PointerRNA stereo3d_format_ptr = RNA_pointer_create(
363 nullptr, &RNA_Stereo3dDisplay, &s3dd->stereo3d_format);
364
365 uiLayoutSetPropSep(layout, true);
366 uiLayoutSetPropDecorate(layout, false);
367
368 col = uiLayoutColumn(layout, false);
369 uiItemR(col, &stereo3d_format_ptr, "display_mode", UI_ITEM_NONE, nullptr, ICON_NONE);
370
371 switch (s3dd->stereo3d_format.display_mode) {
373 uiItemR(col, &stereo3d_format_ptr, "anaglyph_type", UI_ITEM_NONE, nullptr, ICON_NONE);
374 break;
375 }
377 uiItemR(col, &stereo3d_format_ptr, "interlace_type", UI_ITEM_NONE, nullptr, ICON_NONE);
378 uiItemR(col, &stereo3d_format_ptr, "use_interlace_swap", UI_ITEM_NONE, nullptr, ICON_NONE);
379 break;
380 }
382 uiItemR(
383 col, &stereo3d_format_ptr, "use_sidebyside_crosseyed", UI_ITEM_NONE, nullptr, ICON_NONE);
384 /* Fall-through. */
385 }
388 default: {
389 break;
390 }
391 }
392}
393
395{
396 /* The check function guarantees that the menu is updated to show the
397 * sub-options when an enum change (e.g., it shows the anaglyph options
398 * when anaglyph is on, and the interlace options when this is on. */
399 return true;
400}
401
403{
405 op->customdata = nullptr;
406}
wmWindow * CTX_wm_window(const bContext *C)
void CTX_wm_window_set(bContext *C, wmWindow *win)
wmWindowManager * CTX_wm_manager(const bContext *C)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
unsigned int uint
#define ELEM(...)
#define IFACE_(msgid)
These structs are the foundation for all linked lists in the library system.
@ S3D_INTERLACE_SWAP
@ S3D_SIDEBYSIDE_CROSSEYED
eStereoDisplayMode
@ S3D_DISPLAY_ANAGLYPH
@ S3D_DISPLAY_INTERLACE
@ S3D_DISPLAY_TOPBOTTOM
@ S3D_DISPLAY_SIDEBYSIDE
@ S3D_DISPLAY_PAGEFLIP
@ STEREO_LEFT_ID
@ SCREENNORMAL
bool ED_screen_stereo3d_required(const bScreen *screen, const Scene *scene)
GHOST C-API function and type declarations.
GHOST_TWindowState GHOST_GetWindowState(GHOST_WindowHandle windowhandle)
@ GHOST_kWindowStateFullScreen
bool GPU_stereo_quadbuffer_support()
void immEnd()
void immUnbindProgram()
void immVertex2f(uint attr_id, float x, float y)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
GPUVertFormat * immVertexFormat()
void immAttr2f(uint attr_id, float x, float y)
void immBegin(GPUPrimType, uint vertex_len)
@ GPU_PRIM_TRI_FAN
@ GPU_SHADER_3D_IMAGE
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
#define GLA_PIXEL_OFS
Read Guarded memory(de)allocation.
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
#define NC_WINDOW
Definition WM_types.hh:342
uint col
format
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
#define G(x, y, z)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
PointerRNA RNA_pointer_create(ID *id, StructRNA *type, void *data)
Stereo3dFormat stereo3d_format
Definition wm_stereo.cc:193
struct ReportList * reports
struct uiLayout * layout
struct PointerRNA * ptr
struct Stereo3dFormat * stereo3d_format
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
int WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default)
void wm_stereo3d_set_cancel(bContext *, wmOperator *op)
Definition wm_stereo.cc:402
void wm_stereo3d_draw_sidebyside(wmWindow *win, int view)
Definition wm_stereo.cc:44
bool WM_stereo3d_enabled(wmWindow *win, bool skip_stereo3d_check)
Definition wm_stereo.cc:141
void wm_stereo3d_draw_topbottom(wmWindow *win, int view)
Definition wm_stereo.cc:93
int wm_stereo3d_set_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition wm_stereo.cc:346
static bool wm_stereo3d_is_fullscreen_required(eStereoDisplayMode stereo_display)
Definition wm_stereo.cc:136
int wm_stereo3d_set_exec(bContext *C, wmOperator *op)
Definition wm_stereo.cc:257
bool wm_stereo3d_set_check(bContext *, wmOperator *)
Definition wm_stereo.cc:394
void wm_stereo3d_set_draw(bContext *, wmOperator *op)
Definition wm_stereo.cc:356
static void wm_stereo3d_set_init(bContext *C, wmOperator *op)
Definition wm_stereo.cc:246
void wm_stereo3d_mouse_offset_apply(wmWindow *win, int r_mouse_xy[2])
Definition wm_stereo.cc:167
static bool wm_stereo3d_set_properties(bContext *, wmOperator *op)
Definition wm_stereo.cc:196
blender::int2 WM_window_native_pixel_size(const wmWindow *win)
bool WM_window_is_fullscreen(const wmWindow *win)
void wm_window_close(bContext *C, wmWindowManager *wm, wmWindow *win)
Definition wm_window.cc:429
int WM_window_native_pixel_y(const wmWindow *win)
int WM_window_native_pixel_x(const wmWindow *win)
wmWindow * wm_window_copy_test(bContext *C, wmWindow *win_src, const bool duplicate_layout, const bool child)
Definition wm_window.cc:354
Scene * WM_window_get_active_scene(const wmWindow *win)
bScreen * WM_window_get_active_screen(const wmWindow *win)