Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssglightmapbaker.cpp
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
6
#
include
"qssglightmapbaker_p.h"
7
8
#
ifdef
QT_QUICK3D_HAS_LIGHTMAPPER
9
#
include
<
QGuiApplication
>
10
#
include
<
QString
>
11
#
include
<
QtQuick3DRuntimeRender
/
private
/
qssglayerrenderdata_p
.
h
>
12
#
include
<
QThreadPool
>
13
#
include
"qssgrendercontextcore.h"
14
#
if
QT_CONFIG
(
opengl
)
15
#
include
<
QOffscreenSurface
>
16
#
endif
// QT_CONFIG(opengl)
17
18
QT_BEGIN_NAMESPACE
19
20
struct
QSSGLightmapBakerPrivate
21
{
22
QSSGLightmapBaker
::
Context
ctx
;
23
std
::
unique_ptr
<
QSSGLightmapper
>
lightmapper
=
nullptr
;
24
QSSGLightmapBaker
::
Status
currentStatus
=
QSSGLightmapBaker
::
Status
::
Preparing
;
25
// Use a local threadpool to be able to set highest thread priority on the bake thread
26
QThreadPool
localThreadPool
;
27
QOffscreenSurface
*
fallbackSurface
=
nullptr
;
28
bool
waitingForInvoke
=
false
;
29
};
30
31
QSSGLightmapBaker
::
QSSGLightmapBaker
(
const
QSSGLightmapBaker
::
Context
&
ctx
)
32
:
d
(
new
QSSGLightmapBakerPrivate
)
33
{
34
d
->
ctx
=
ctx
;
35
d
->
currentStatus
=
QSSGLightmapBaker
::
Status
::
Preparing
;
36
d
->
localThreadPool
.
setMaxThreadCount
(1);
37
}
38
39
QSSGLightmapBaker
::~
QSSGLightmapBaker
()
40
{
41
delete
d
;
42
}
43
44
QSSGLightmapBaker
::
Status
QSSGLightmapBaker
::
process
()
45
{
46
if
(
d
->
waitingForInvoke
)
47
return
d
->
currentStatus
;
48
49
auto
&
env
=
d
->
ctx
.
env
;
50
auto
&
settings
=
d
->
ctx
.
settings
;
51
auto
&
callbacks
=
d
->
ctx
.
callbacks
;
52
53
if
(
d
->
currentStatus
==
Status
::
Preparing
) {
54
// We need to prepare for lightmap baking by doing another frame so that
55
// we can reload all meshes to use the original one and NOT the baked one.
56
// When disableLightmaps is set on the layer, the mesh loader will always load the
57
// original mesh and not the lightmap mesh.
58
callbacks
.
setCurrentlyBaking
(
true
);
59
callbacks
.
triggerNewFrame
(
true
);
60
61
#
if
QT_CONFIG
(
opengl
)
62
d
->
waitingForInvoke
=
true
;
63
QMetaObject
::
invokeMethod
(
qApp
, [
this
]() {
64
d
->
fallbackSurface
=
QRhiGles2InitParams
::
newFallbackSurface
();
65
d
->
currentStatus
=
Status
::
Running
;
66
d
->
waitingForInvoke
=
false
;
67
},
68
Qt
::
QueuedConnection
);
69
#
else
70
d
->
currentStatus
=
Status
::
Running
;
71
#
endif
72
}
else
if
(
d
->
currentStatus
==
Status
::
Running
) {
73
d
->
lightmapper
=
std
::
make_unique
<
QSSGLightmapper
>();
74
d
->
lightmapper
->
setRhiBackend
(
env
.
rhiCtx
->
rhi
()->
backend
());
75
d
->
lightmapper
->
setOptions
(
env
.
lmOptions
);
76
d
->
lightmapper
->
setOutputCallback
(
callbacks
.
lightmapBakingOutput
);
77
d
->
lightmapper
->
setDenoiseOnly
(
settings
.
denoiseRequested
);
78
79
// bakedLightingModels contains all models with
80
// usedInBakedLighting: true. These, together with lights that
81
// have a bakeMode set to either Indirect or All, form the
82
// lightmapped scene. A lightmap is stored persistently only
83
// for models that have their lightmapKey set.
84
const
auto
&
bakedLightingModels
=
callbacks
.
modelsToBake
();
85
for
(
int
i
= 0,
ie
=
bakedLightingModels
.
size
();
i
!=
ie
; ++
i
)
86
d
->
lightmapper
->
add
(
bakedLightingModels
[
i
]);
87
88
if
(!
d
->
lightmapper
->
setupLights
(*
env
.
renderer
)) {
89
d
->
currentStatus
=
Status
::
Finished
;
90
callbacks
.
setCurrentlyBaking
(
false
);
91
callbacks
.
triggerNewFrame
(
true
);
92
if
(
settings
.
quitWhenFinished
) {
93
qDebug
(
"Lightmap baking/denoising done, exiting application"
);
94
QMetaObject
::
invokeMethod
(
qApp
,
"quit"
);
95
}
96
return
Status
::
Finished
;
97
}
98
99
QRhiCommandBuffer
*
cb
=
env
.
rhiCtx
->
commandBuffer
();
100
cb
->
debugMarkBegin
(
"Quick3D lightmap baking/denoising"
);
101
102
d
->
currentStatus
=
Status
::
Baking
;
103
104
const
auto
bakeTask
= [
this
,
callbacks
,
settings
] {
105
#
if
QT_CONFIG
(
thread
)
106
QThread
::
currentThread
()->
setPriority
(
QThread
::
HighestPriority
);
107
#
endif
// QT_CONFIG(thread)
108
d
->
lightmapper
->
run
(
d
->
fallbackSurface
);
109
110
callbacks
.
setCurrentlyBaking
(
false
);
111
callbacks
.
triggerNewFrame
(
true
);
112
113
#
if
QT_CONFIG
(
opengl
)
114
d
->
waitingForInvoke
=
true
;
115
QMetaObject
::
invokeMethod
(
qApp
, [
this
]() {
116
delete
d
->
fallbackSurface
;
117
d
->
fallbackSurface
=
nullptr
;
118
d
->
currentStatus
=
Status
::
Finished
;
119
d
->
waitingForInvoke
=
false
;
120
},
Qt
::
QueuedConnection
);
121
#
else
122
d
->
currentStatus
=
Status
::
Finished
;
123
#
endif
124
if
(
settings
.
quitWhenFinished
) {
125
qDebug
(
"Lightmap baking/denoising done, exiting application"
);
126
QMetaObject
::
invokeMethod
(
qApp
,
"quit"
);
127
}
128
};
129
130
#
if
QT_CONFIG
(
thread
)
131
d
->
localThreadPool
.
start
(
bakeTask
);
132
#
else
133
bakeTask
();
134
#
endif
// QT_CONFIG(thread)
135
136
// Wait until lightmapper is finished initializing
137
d
->
lightmapper
->
waitForInit
();
138
cb
->
debugMarkEnd
();
139
}
140
141
return
d
->
currentStatus
;
142
}
143
144
QT_END_NAMESPACE
145
146
#
else
// QT_QUICK3D_HAS_LIGHTMAPPER
147
148
QT_BEGIN_NAMESPACE
149
150
QSSGLightmapBaker::QSSGLightmapBaker(
const
QSSGLightmapBaker::Context &ctx)
151
{
152
qWarning(
"QSSGLightmapBaker: This build has no lightmap baking support."
);
153
#
if
defined
(
qApp
)
154
if
(ctx.settings.quitWhenFinished)
155
QMetaObject::invokeMethod(qApp,
"quit"
);
156
#
else
157
Q_UNUSED(ctx);
158
#
endif
159
}
160
161
QSSGLightmapBaker
::~
QSSGLightmapBaker
() =
default
;
162
163
QSSGLightmapBaker
::
Status
QSSGLightmapBaker
::
process
()
164
{
165
return
QSSGLightmapBaker
::
Status
::
Finished
;
166
}
167
168
QT_END_NAMESPACE
169
170
#
endif
// QT_QUICK3D_HAS_LIGHTMAPPER
QSSGLightmapBaker
Definition
qssglightmapbaker_p.h:31
QSSGLightmapBaker::Status
Status
Definition
qssglightmapbaker_p.h:33
QSSGLightmapBaker::Status::Finished
@ Finished
Definition
qssglightmapbaker_p.h:37
QSSGLightmapBaker::process
Status process()
Definition
qssglightmapbaker.cpp:163
QSSGLightmapBaker::~QSSGLightmapBaker
~QSSGLightmapBaker()
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtquick3d
src
runtimerender
rendererimpl
qssglightmapbaker.cpp
Generated on
for Qt by
1.16.1