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