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