7#include <QtQuick/private/qquickitem_p.h>
8#include <QtQuick/private/qquickwindow_p.h>
9#include <QtQuick/private/qsgrhisupport_p.h>
14
15
16
17
18
19
20
21
22
24QQuickRenderTargetPrivate::QQuickRenderTargetPrivate()
29QQuickRenderTargetPrivate::QQuickRenderTargetPrivate(
const QQuickRenderTargetPrivate &other)
32 pixelSize(other.pixelSize),
33 devicePixelRatio(other.devicePixelRatio),
34 sampleCount(other.sampleCount),
36 customDepthTexture(other.customDepthTexture),
37 mirrorVertically(other.mirrorVertically),
38 multisampleResolve(other.multisampleResolve)
43
44
45
46QQuickRenderTarget::QQuickRenderTarget()
47 : d(
new QQuickRenderTargetPrivate)
52
53
54void QQuickRenderTarget::detach()
60
61
62QQuickRenderTarget::QQuickRenderTarget(
const QQuickRenderTarget &other)
69
70
71QQuickRenderTarget &QQuickRenderTarget::operator=(
const QQuickRenderTarget &other)
73 qAtomicAssign(d, other.d);
78
79
80QQuickRenderTarget::~QQuickRenderTarget()
87
88
89
90bool QQuickRenderTarget::isNull()
const
92 return d->type == QQuickRenderTargetPrivate::Type::Null;
96
97
98
99
100
101
102
103
104
105qreal QQuickRenderTarget::devicePixelRatio()
const
107 return d->devicePixelRatio;
111
112
113
114
115
116
117
118
119
120
121
122void QQuickRenderTarget::setDevicePixelRatio(qreal ratio)
124 if (d->devicePixelRatio == ratio)
128 d->devicePixelRatio = ratio;
132
133
134
135
136
137
138
139
140bool QQuickRenderTarget::mirrorVertically()
const
142 return d->mirrorVertically;
147
148
149
150
151
152
153
154
155
156
157void QQuickRenderTarget::setMirrorVertically(
bool enable)
159 if (d->mirrorVertically == enable)
163 d->mirrorVertically = enable;
167
168
169
170
171
172
173QRhiTexture *QQuickRenderTarget::depthTexture()
const
175 return d->customDepthTexture;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217void QQuickRenderTarget::setDepthTexture(QRhiTexture *texture)
219 if (d->customDepthTexture == texture)
223 d->customDepthTexture = texture;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275#if QT_CONFIG(opengl) || defined(Q_QDOC)
276QQuickRenderTarget QQuickRenderTarget::fromOpenGLTexture(uint textureId, uint format,
277 const QSize &pixelSize,
int sampleCount)
279 QQuickRenderTarget rt;
280 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
283 qWarning(
"QQuickRenderTarget: textureId is invalid");
287 if (pixelSize.isEmpty()) {
288 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
292 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
293 d->pixelSize = pixelSize;
294 d->sampleCount = qMax(1, sampleCount);
296 QRhiTexture::Flags formatFlags;
297 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromGL(format, &formatFlags);
298 d->u.nativeTexture = { textureId, 0, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331QQuickRenderTarget QQuickRenderTarget::fromOpenGLTexture(uint textureId,
const QSize &pixelSize,
int sampleCount)
333 return fromOpenGLTexture(textureId, 0, pixelSize, sampleCount);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387QQuickRenderTarget QQuickRenderTarget::fromOpenGLTexture(uint textureId, uint format, QSize pixelSize,
int sampleCount,
int arraySize, Flags flags)
389 QQuickRenderTarget rt;
390 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
393 qWarning(
"QQuickRenderTarget: textureId is invalid");
397 if (pixelSize.isEmpty()) {
398 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
402 QRhiTexture::Flags formatFlags;
403 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromGL(format, &formatFlags);
405 d->pixelSize = pixelSize;
406 d->sampleCount = qMax(1, sampleCount);
407 d->multisampleResolve = flags.testFlag(Flag::MultisampleResolve);
409 if (arraySize <= 1) {
410 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
411 d->u.nativeTexture = { textureId, 0, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
413 d->type = QQuickRenderTargetPrivate::Type::NativeTextureArray;
414 d->u.nativeTextureArray = { textureId, 0, arraySize, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446QQuickRenderTarget QQuickRenderTarget::fromOpenGLRenderBuffer(uint renderbufferId,
const QSize &pixelSize,
int sampleCount)
448 QQuickRenderTarget rt;
449 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
451 if (!renderbufferId) {
452 qWarning(
"QQuickRenderTarget: renderbufferId is invalid");
456 if (pixelSize.isEmpty()) {
457 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
461 d->type = QQuickRenderTargetPrivate::Type::NativeRenderbuffer;
462 d->pixelSize = pixelSize;
463 d->sampleCount = qMax(1, sampleCount);
464 d->u.nativeRenderbufferObject = renderbufferId;
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497#if defined(Q_OS_WIN) || defined(Q_QDOC)
498QQuickRenderTarget QQuickRenderTarget::fromD3D11Texture(
void *texture, uint format,
499 const QSize &pixelSize,
int sampleCount)
501 QQuickRenderTarget rt;
502 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
505 qWarning(
"QQuickRenderTarget: texture is null");
509 if (pixelSize.isEmpty()) {
510 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
514 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
515 d->pixelSize = pixelSize;
516 d->sampleCount = qMax(1, sampleCount);
518 QRhiTexture::Flags formatFlags;
519 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromDXGI(format, &formatFlags);
520 d->u.nativeTexture = { quint64(texture), 0, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550QQuickRenderTarget QQuickRenderTarget::fromD3D11Texture(
void *texture,
const QSize &pixelSize,
int sampleCount)
552 return fromD3D11Texture(texture, 0 , pixelSize, sampleCount);
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591QQuickRenderTarget QQuickRenderTarget::fromD3D11Texture(
void *texture, uint format, QSize pixelSize,
int sampleCount, Flags flags)
593 QQuickRenderTarget rt = fromD3D11Texture(texture, format, pixelSize, sampleCount);
594 QQuickRenderTargetPrivate::get(&rt)->multisampleResolve = flags.testFlag(Flag::MultisampleResolve);
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628QQuickRenderTarget QQuickRenderTarget::fromD3D12Texture(
void *texture,
631 const QSize &pixelSize,
634 QQuickRenderTarget rt;
635 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
638 qWarning(
"QQuickRenderTarget: texture is null");
642 if (pixelSize.isEmpty()) {
643 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
647 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
648 d->pixelSize = pixelSize;
649 d->sampleCount = qMax(1, sampleCount);
651 QRhiTexture::Flags formatFlags;
652 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromDXGI(format, &formatFlags);
653 d->u.nativeTexture = { quint64(texture), resourceState, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713QQuickRenderTarget QQuickRenderTarget::fromD3D12Texture(
void *texture,
722 QQuickRenderTarget rt;
723 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
726 qWarning(
"QQuickRenderTarget: texture is null");
730 if (pixelSize.isEmpty()) {
731 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
735 QRhiTexture::Flags formatFlags;
736 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromDXGI(format, &formatFlags);
737 QRhiTexture::Flags viewFormatFlags;
738 QRhiTexture::Format rhiViewFormat = QSGRhiSupport::toRhiTextureFormatFromDXGI(viewFormat, &viewFormatFlags);
740 d->pixelSize = pixelSize;
741 d->sampleCount = qMax(1, sampleCount);
742 d->multisampleResolve = flags.testFlag(Flag::MultisampleResolve);
744 if (arraySize <= 1) {
745 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
746 d->u.nativeTexture = { quint64(texture), resourceState, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
748 d->type = QQuickRenderTargetPrivate::Type::NativeTextureArray;
749 d->u.nativeTextureArray = { quint64(texture), resourceState, arraySize, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784#if QT_CONFIG(metal) || defined(Q_QDOC)
785QQuickRenderTarget QQuickRenderTarget::fromMetalTexture(MTLTexture *texture, uint format,
786 const QSize &pixelSize,
int sampleCount)
788 QQuickRenderTarget rt;
789 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
792 qWarning(
"QQuickRenderTarget: texture is null");
796 if (pixelSize.isEmpty()) {
797 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
801 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
802 d->pixelSize = pixelSize;
803 d->sampleCount = qMax(1, sampleCount);
805 QRhiTexture::Flags formatFlags;
806 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromMetal(format, &formatFlags);
807 d->u.nativeTexture = { quint64(texture), 0, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837QQuickRenderTarget QQuickRenderTarget::fromMetalTexture(MTLTexture *texture,
const QSize &pixelSize,
int sampleCount)
839 return fromMetalTexture(texture, 0 , pixelSize, sampleCount);
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893QQuickRenderTarget QQuickRenderTarget::fromMetalTexture(MTLTexture *texture, uint format, uint viewFormat,
894 QSize pixelSize,
int sampleCount,
int arraySize, Flags flags)
896 QQuickRenderTarget rt;
897 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
900 qWarning(
"QQuickRenderTarget: texture is null");
904 if (pixelSize.isEmpty()) {
905 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
909 QRhiTexture::Flags formatFlags;
910 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromMetal(format, &formatFlags);
911 QRhiTexture::Flags viewFormatFlags;
912 QRhiTexture::Format rhiViewFormat = QSGRhiSupport::toRhiTextureFormatFromMetal(viewFormat, &viewFormatFlags);
914 d->pixelSize = pixelSize;
915 d->sampleCount = qMax(1, sampleCount);
916 d->multisampleResolve = flags.testFlag(Flag::MultisampleResolve);
918 if (arraySize <= 1) {
919 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
920 d->u.nativeTexture = { quint64(texture), 0, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
922 d->type = QQuickRenderTargetPrivate::Type::NativeTextureArray;
923 d->u.nativeTextureArray = { quint64(texture), 0, arraySize, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959#if QT_CONFIG(vulkan) || defined(Q_QDOC)
960QQuickRenderTarget QQuickRenderTarget::fromVulkanImage(VkImage image, VkImageLayout layout, VkFormat format,
const QSize &pixelSize,
int sampleCount)
962 QQuickRenderTarget rt;
963 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
965 if (image == VK_NULL_HANDLE) {
966 qWarning(
"QQuickRenderTarget: image is invalid");
970 if (pixelSize.isEmpty()) {
971 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
975 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
976 d->pixelSize = pixelSize;
977 d->sampleCount = qMax(1, sampleCount);
979 QRhiTexture::Flags formatFlags;
980 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromVulkan(format, &formatFlags);
981 d->u.nativeTexture = { quint64(image), layout, uint(rhiFormat), uint(formatFlags), uint(rhiFormat), uint(formatFlags) };
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011QQuickRenderTarget QQuickRenderTarget::fromVulkanImage(VkImage image, VkImageLayout layout,
const QSize &pixelSize,
int sampleCount)
1013 return fromVulkanImage(image, layout, VK_FORMAT_UNDEFINED, pixelSize, sampleCount);
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068QQuickRenderTarget QQuickRenderTarget::fromVulkanImage(VkImage image, VkImageLayout layout, VkFormat format, VkFormat viewFormat,
1069 QSize pixelSize,
int sampleCount,
int arraySize, Flags flags)
1071 QQuickRenderTarget rt;
1072 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
1074 if (image == VK_NULL_HANDLE) {
1075 qWarning(
"QQuickRenderTarget: image is invalid");
1079 if (pixelSize.isEmpty()) {
1080 qWarning(
"QQuickRenderTarget: Cannot create with empty size");
1084 QRhiTexture::Flags formatFlags;
1085 QRhiTexture::Format rhiFormat = QSGRhiSupport::toRhiTextureFormatFromVulkan(format, &formatFlags);
1086 QRhiTexture::Flags viewFormatFlags;
1087 QRhiTexture::Format rhiViewFormat = QSGRhiSupport::toRhiTextureFormatFromVulkan(viewFormat, &viewFormatFlags);
1089 d->pixelSize = pixelSize;
1090 d->sampleCount = qMax(1, sampleCount);
1091 d->multisampleResolve = flags.testFlag(Flag::MultisampleResolve);
1093 if (arraySize <= 1) {
1094 d->type = QQuickRenderTargetPrivate::Type::NativeTexture;
1095 d->u.nativeTexture = { quint64(image), layout, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
1097 d->type = QQuickRenderTargetPrivate::Type::NativeTextureArray;
1098 d->u.nativeTextureArray = { quint64(image), layout, arraySize, uint(rhiFormat), uint(formatFlags), uint(rhiViewFormat), uint(viewFormatFlags) };
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122QQuickRenderTarget QQuickRenderTarget::fromRhiRenderTarget(QRhiRenderTarget *renderTarget)
1124 QQuickRenderTarget rt;
1125 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
1127 if (!renderTarget) {
1128 qWarning(
"QQuickRenderTarget: Needs a valid QRhiRenderTarget");
1132 d->type = QQuickRenderTargetPrivate::Type::RhiRenderTarget;
1133 d->pixelSize = renderTarget->pixelSize();
1134 d->sampleCount = renderTarget->sampleCount();
1135 d->u.rhiRt = renderTarget;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154QQuickRenderTarget QQuickRenderTarget::fromPaintDevice(QPaintDevice *device)
1156 QQuickRenderTarget rt;
1157 QQuickRenderTargetPrivate *d = QQuickRenderTargetPrivate::get(&rt);
1159 d->type = QQuickRenderTargetPrivate::Type::PaintDevice;
1160 d->pixelSize = QSize(device->width(), device->height());
1161 d->u.paintDevice = device;
1167
1168
1169
1170
1172
1173
1174
1175
1176
1179
1180
1181bool QQuickRenderTarget::isEqual(
const QQuickRenderTarget &other)
const noexcept
1183 if (d->type != other.d->type
1184 || d->pixelSize != other.d->pixelSize
1185 || d->devicePixelRatio != other.d->devicePixelRatio
1186 || d->sampleCount != other.d->sampleCount
1187 || d->mirrorVertically != other.d->mirrorVertically
1188 || d->multisampleResolve != other.d->multisampleResolve)
1194 case QQuickRenderTargetPrivate::Type::Null:
1196 case QQuickRenderTargetPrivate::Type::NativeTexture:
1197 if (d->u.nativeTexture.object != other.d->u.nativeTexture.object
1198 || d->u.nativeTexture.layoutOrState != other.d->u.nativeTexture.layoutOrState
1199 || d->u.nativeTexture.rhiFormat != other.d->u.nativeTexture.rhiFormat
1200 || d->u.nativeTexture.rhiFormatFlags != other.d->u.nativeTexture.rhiFormatFlags
1201 || d->u.nativeTexture.rhiViewFormat != other.d->u.nativeTexture.rhiViewFormat
1202 || d->u.nativeTexture.rhiViewFormatFlags != other.d->u.nativeTexture.rhiViewFormatFlags)
1205 case QQuickRenderTargetPrivate::Type::NativeTextureArray:
1206 if (d->u.nativeTextureArray.object != other.d->u.nativeTextureArray.object
1207 || d->u.nativeTextureArray.layoutOrState != other.d->u.nativeTextureArray.layoutOrState
1208 || d->u.nativeTextureArray.arraySize != other.d->u.nativeTextureArray.arraySize
1209 || d->u.nativeTextureArray.rhiFormat != other.d->u.nativeTextureArray.rhiFormat
1210 || d->u.nativeTextureArray.rhiFormatFlags != other.d->u.nativeTextureArray.rhiFormatFlags
1211 || d->u.nativeTextureArray.rhiViewFormat != other.d->u.nativeTextureArray.rhiViewFormat
1212 || d->u.nativeTextureArray.rhiViewFormatFlags != other.d->u.nativeTextureArray.rhiViewFormatFlags)
1215 case QQuickRenderTargetPrivate::Type::NativeRenderbuffer:
1216 if (d->u.nativeRenderbufferObject != other.d->u.nativeRenderbufferObject)
1219 case QQuickRenderTargetPrivate::Type::RhiRenderTarget:
1220 if (d->u.rhiRt != other.d->u.rhiRt)
1223 case QQuickRenderTargetPrivate::Type::PaintDevice:
1224 if (d->u.paintDevice != other.d->u.paintDevice)
1235 const QSize &pixelSize,
1240 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1242 std::unique_ptr<QRhiRenderBuffer> depthStencil;
1243 if (dst->implicitBuffers.depthStencil) {
1244 if (dst->implicitBuffers.depthStencil->pixelSize() == pixelSize
1245 && dst->implicitBuffers.depthStencil->sampleCount() == sampleCount)
1247 depthStencil.reset(dst->implicitBuffers.depthStencil);
1248 dst->implicitBuffers.depthStencil =
nullptr;
1251 dst->implicitBuffers.reset(rhi);
1253 if (!depthStencil) {
1254 depthStencil.reset(rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, pixelSize, sampleCount,
1255 QSGRhiSupport::depthStencilBufferFlags()));
1256 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer for QQuickRenderTarget"));
1257 if (!depthStencil->create()) {
1258 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1265 rtDesc.setDepthStencilBuffer(depthStencil.get());
1266 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1267 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget with renderbuffer"));
1268 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1269 rt->setRenderPassDescriptor(rp.get());
1271 if (!rt->create()) {
1272 qWarning(
"Failed to build renderbuffer-based render target for QQuickRenderTarget");
1276 dst->rt.renderTarget = rt.release();
1277 dst->rt.owns =
true;
1278 dst->res.rpDesc = rp.release();
1279 dst->implicitBuffers.depthStencil = depthStencil.release();
1285 const QSize &pixelSize,
1287 bool multisampleResolve,
1295 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1296 if (sampleCount <= 1)
1297 multisampleResolve =
false;
1299 std::unique_ptr<QRhiRenderBuffer> depthStencil;
1300 if (dst->implicitBuffers.depthStencil) {
1301 if (dst->implicitBuffers.depthStencil->pixelSize() == pixelSize
1302 && dst->implicitBuffers.depthStencil->sampleCount() == sampleCount)
1304 depthStencil.reset(dst->implicitBuffers.depthStencil);
1305 dst->implicitBuffers.depthStencil =
nullptr;
1309 std::unique_ptr<QRhiTexture> colorBuffer;
1310 QRhiTexture::Flags multisampleTextureFlags;
1311 QRhiTexture::Format multisampleTextureFormat = texture->format();
1312 if (multisampleResolve) {
1313 multisampleTextureFlags = QRhiTexture::RenderTarget;
1314 if (texture->flags().testFlag(QRhiTexture::sRGB))
1315 multisampleTextureFlags |= QRhiTexture::sRGB;
1317 if (dst->implicitBuffers.multisampleTexture) {
1318 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1319 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1320 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1321 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1323 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1324 dst->implicitBuffers.multisampleTexture =
nullptr;
1329 dst->implicitBuffers.reset(rhi);
1331 if (!depthStencil) {
1332 depthStencil.reset(rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, pixelSize, sampleCount,
1333 QSGRhiSupport::depthStencilBufferFlags()));
1334 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer for QQuickRenderTarget"));
1335 if (!depthStencil->create()) {
1336 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1341 if (multisampleResolve && !colorBuffer) {
1342 colorBuffer.reset(rhi->newTexture(multisampleTextureFormat, pixelSize, sampleCount, multisampleTextureFlags));
1343 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer for QQuickRenderTarget"));
1344 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1345 if (!colorBuffer->create()) {
1346 qWarning(
"Failed to build multisample color buffer for QQuickRenderTarget");
1352 if (multisampleResolve) {
1353 colorAttachment.setTexture(colorBuffer.get());
1354 colorAttachment.setResolveTexture(texture);
1356 colorAttachment.setTexture(texture);
1359 rtDesc.setDepthStencilBuffer(depthStencil.get());
1360 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1361 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget"));
1362 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1363 rt->setRenderPassDescriptor(rp.get());
1365 if (!rt->create()) {
1366 qWarning(
"Failed to build texture render target for QQuickRenderTarget");
1370 dst->rt.renderTarget = rt.release();
1371 dst->rt.owns =
true;
1372 dst->res.rpDesc = rp.release();
1373 dst->implicitBuffers.depthStencil = depthStencil.release();
1374 if (multisampleResolve)
1375 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1381 QRhiTexture *depthTexture,
1382 const QSize &pixelSize,
1384 bool multisampleResolve,
1397 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1398 if (sampleCount <= 1)
1399 multisampleResolve =
false;
1401 std::unique_ptr<QRhiTexture> depthStencil;
1402 if (dst->implicitBuffers.depthStencilTexture) {
1403 if (dst->implicitBuffers.depthStencilTexture->pixelSize() == pixelSize
1404 && dst->implicitBuffers.depthStencilTexture->sampleCount() == sampleCount)
1406 depthStencil.reset(dst->implicitBuffers.depthStencilTexture);
1407 dst->implicitBuffers.depthStencilTexture =
nullptr;
1411 std::unique_ptr<QRhiTexture> colorBuffer;
1412 QRhiTexture::Flags multisampleTextureFlags;
1413 QRhiTexture::Format multisampleTextureFormat = texture->format();
1414 if (multisampleResolve) {
1415 multisampleTextureFlags = QRhiTexture::RenderTarget;
1416 if (texture->flags().testFlag(QRhiTexture::sRGB))
1417 multisampleTextureFlags |= QRhiTexture::sRGB;
1419 if (dst->implicitBuffers.multisampleTexture) {
1420 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1421 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1422 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1423 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1425 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1426 dst->implicitBuffers.multisampleTexture =
nullptr;
1431 dst->implicitBuffers.reset(rhi);
1433 bool needsDepthStencilBuffer =
true;
1434 if (sampleCount <= 1) {
1435 depthStencil.reset();
1436 needsDepthStencilBuffer =
false;
1438 if (depthTexture->pixelSize() != pixelSize) {
1439 qWarning(
"Custom depth texture size (%dx%d) does not match the QQuickRenderTarget (%dx%d)",
1440 depthTexture->pixelSize().width(),
1441 depthTexture->pixelSize().height(),
1443 pixelSize.height());
1446 if (depthTexture->sampleCount() > 1) {
1447 qWarning(
"Custom depth texture cannot be multisample");
1450 if (needsDepthStencilBuffer && !depthStencil) {
1451 QRhiTexture::Format multisampleDepthTextureFormat = depthTexture->format();
1452 depthStencil.reset(rhi->newTexture(multisampleDepthTextureFormat, pixelSize, sampleCount, QRhiTexture::RenderTarget));
1453 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil texture for QQuickRenderTarget"));
1454 if (!depthStencil->create()) {
1455 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1460 if (multisampleResolve && !colorBuffer) {
1461 colorBuffer.reset(rhi->newTexture(multisampleTextureFormat, pixelSize, sampleCount, multisampleTextureFlags));
1462 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer for QQuickRenderTarget"));
1463 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1464 if (!colorBuffer->create()) {
1465 qWarning(
"Failed to build multisample color buffer for QQuickRenderTarget");
1471 if (multisampleResolve) {
1472 colorAttachment.setTexture(colorBuffer.get());
1473 colorAttachment.setResolveTexture(texture);
1475 colorAttachment.setTexture(texture);
1479 if (sampleCount > 1) {
1480 rtDesc.setDepthTexture(depthStencil.get());
1481 if (rhi->isFeatureSupported(QRhi::ResolveDepthStencil))
1482 rtDesc.setDepthResolveTexture(depthTexture);
1484 qWarning(
"Depth-stencil resolve is not supported by the underlying 3D API, depth contents will not be resolved");
1486 rtDesc.setDepthTexture(depthTexture);
1489 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1490 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget"));
1491 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1492 rt->setRenderPassDescriptor(rp.get());
1494 if (!rt->create()) {
1495 qWarning(
"Failed to build texture render target for QQuickRenderTarget");
1499 dst->rt.renderTarget = rt.release();
1500 dst->rt.owns =
true;
1501 dst->res.rpDesc = rp.release();
1503 dst->implicitBuffers.depthStencilTexture = depthStencil.release();
1504 if (multisampleResolve)
1505 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1511 QRhiTexture *maybeCustomDepthTexture,
1512 const QSize &pixelSize,
1515 bool multisampleResolve,
1524 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1525 if (sampleCount <= 1)
1526 multisampleResolve =
false;
1528 std::unique_ptr<QRhiTexture> depthStencil;
1529 if (dst->implicitBuffers.depthStencilTexture) {
1530 if (dst->implicitBuffers.depthStencilTexture->pixelSize() == pixelSize
1531 && dst->implicitBuffers.depthStencilTexture->sampleCount() == sampleCount
1532 && dst->implicitBuffers.depthStencilTexture->arraySize() == arraySize)
1534 depthStencil.reset(dst->implicitBuffers.depthStencilTexture);
1535 dst->implicitBuffers.depthStencilTexture =
nullptr;
1539 std::unique_ptr<QRhiTexture> colorBuffer;
1540 QRhiTexture::Flags multisampleTextureFlags;
1541 QRhiTexture::Format multisampleTextureFormat = texture->format();
1542 if (multisampleResolve) {
1543 multisampleTextureFlags = QRhiTexture::RenderTarget;
1544 if (texture->flags().testFlag(QRhiTexture::sRGB))
1545 multisampleTextureFlags |= QRhiTexture::sRGB;
1547 if (dst->implicitBuffers.multisampleTexture) {
1548 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1549 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1550 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1551 && dst->implicitBuffers.multisampleTexture->arraySize() == arraySize
1552 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1554 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1555 dst->implicitBuffers.multisampleTexture =
nullptr;
1560 dst->implicitBuffers.reset(rhi);
1562 bool needsDepthStencilBuffer =
true;
1563 if (maybeCustomDepthTexture) {
1564 if (sampleCount <= 1) {
1565 depthStencil.reset();
1566 needsDepthStencilBuffer =
false;
1568 if (maybeCustomDepthTexture->arraySize() != arraySize) {
1569 qWarning(
"Custom depth texture array size (%d) does not match QQuickRenderTarget (%d)",
1570 maybeCustomDepthTexture->arraySize(), arraySize);
1573 if (maybeCustomDepthTexture->pixelSize() != pixelSize) {
1574 qWarning(
"Custom depth texture size (%dx%d) does not match the QQuickRenderTarget (%dx%d)",
1575 maybeCustomDepthTexture->pixelSize().width(),
1576 maybeCustomDepthTexture->pixelSize().height(),
1578 pixelSize.height());
1581 if (maybeCustomDepthTexture->sampleCount() > 1) {
1582 qWarning(
"Custom depth texture cannot be multisample");
1586 if (needsDepthStencilBuffer && !depthStencil) {
1587 QRhiTexture::Format format;
1588 if (maybeCustomDepthTexture)
1589 format = maybeCustomDepthTexture->format();
1591 format = rhi->isTextureFormatSupported(QRhiTexture::D24S8) ? QRhiTexture::D24S8 : QRhiTexture::D32FS8;
1592 depthStencil.reset(rhi->newTextureArray(format, arraySize, pixelSize, sampleCount, QRhiTexture::RenderTarget));
1593 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer (multiview) for QQuickRenderTarget"));
1594 if (!depthStencil->create()) {
1595 qWarning(
"Failed to build depth-stencil texture array for QQuickRenderTarget");
1600 if (multisampleResolve && !colorBuffer) {
1601 colorBuffer.reset(rhi->newTextureArray(multisampleTextureFormat, arraySize, pixelSize, sampleCount, multisampleTextureFlags));
1602 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer (multiview) for QQuickRenderTarget"));
1603 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1604 if (!colorBuffer->create()) {
1605 qWarning(
"Failed to build multisample texture array for QQuickRenderTarget");
1611 colorAttachment.setMultiViewCount(arraySize);
1612 if (multisampleResolve) {
1613 colorAttachment.setTexture(colorBuffer.get());
1614 colorAttachment.setResolveTexture(texture);
1616 colorAttachment.setTexture(texture);
1620 if (sampleCount > 1) {
1621 rtDesc.setDepthTexture(depthStencil.get());
1622 if (maybeCustomDepthTexture) {
1623 if (rhi->isFeatureSupported(QRhi::ResolveDepthStencil))
1624 rtDesc.setDepthResolveTexture(maybeCustomDepthTexture);
1626 qWarning(
"Depth-stencil resolve is not supported by the underlying 3D API, depth contents will not be resolved");
1630 rtDesc.setDepthTexture(depthStencil.get());
1631 else if (maybeCustomDepthTexture)
1632 rtDesc.setDepthTexture(maybeCustomDepthTexture);
1635 QRhiTextureRenderTarget::Flags rtFlags;
1636 if (!maybeCustomDepthTexture)
1637 rtFlags |= QRhiTextureRenderTarget::DoNotStoreDepthStencilContents;
1639 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc, rtFlags));
1640 rt->setName(QByteArrayLiteral(
"RT for multiview QQuickRenderTarget"));
1641 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1642 rt->setRenderPassDescriptor(rp.get());
1644 if (!rt->create()) {
1645 qWarning(
"Failed to build multiview texture render target for QQuickRenderTarget");
1649 dst->rt.renderTarget = rt.release();
1650 dst->rt.owns =
true;
1651 dst->res.rpDesc = rp.release();
1653 dst->implicitBuffers.depthStencilTexture = depthStencil.release();
1654 if (multisampleResolve)
1655 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1657 dst->rt.multiViewCount = arraySize;
1662bool QQuickRenderTargetPrivate::resolve(QRhi *rhi, QQuickWindowRenderTarget *dst)
1670 dst->implicitBuffers.reset(rhi);
1673 case Type::NativeTexture:
1675 QRhiTexture::Format format = u.nativeTexture.rhiFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1676 : QRhiTexture::Format(u.nativeTexture.rhiFormat);
1677 QRhiTexture::Format viewFormat = u.nativeTexture.rhiViewFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1678 : QRhiTexture::Format(u.nativeTexture.rhiViewFormat);
1679 const auto flags = QRhiTexture::RenderTarget | QRhiTexture::Flags(u.nativeTexture.rhiFormatFlags);
1680 std::unique_ptr<QRhiTexture> texture(rhi->newTexture(format, pixelSize, multisampleResolve ? 1 : sampleCount, flags));
1681 const bool textureIsSrgb = flags.testFlag(QRhiTexture::sRGB);
1682 const bool viewIsSrgb = QRhiTexture::Flags(u.nativeTexture.rhiViewFormatFlags).testFlag(QRhiTexture::sRGB);
1683 if (viewFormat != format || viewIsSrgb != textureIsSrgb)
1684 texture->setWriteViewFormat({ viewFormat, viewIsSrgb });
1685 if (!texture->createFrom({ u.nativeTexture.object, u.nativeTexture.layoutOrState })) {
1686 qWarning(
"Failed to build wrapper texture for QQuickRenderTarget");
1689 if (customDepthTexture) {
1690 if (!createRhiRenderTargetWithDepthTexture(texture.get(), customDepthTexture, pixelSize, sampleCount, multisampleResolve, rhi, dst))
1693 if (!createRhiRenderTarget(texture.get(), pixelSize, sampleCount, multisampleResolve, rhi, dst))
1696 dst->res.texture = texture.release();
1700 case Type::NativeTextureArray:
1702 QRhiTexture::Format format = u.nativeTextureArray.rhiFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1703 : QRhiTexture::Format(u.nativeTextureArray.rhiFormat);
1704 QRhiTexture::Format viewFormat = u.nativeTextureArray.rhiViewFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1705 : QRhiTexture::Format(u.nativeTextureArray.rhiViewFormat);
1706 const auto flags = QRhiTexture::RenderTarget | QRhiTexture::Flags(u.nativeTextureArray.rhiFormatFlags);
1707 const int arraySize = u.nativeTextureArray.arraySize;
1708 std::unique_ptr<QRhiTexture> texture(rhi->newTextureArray(format, arraySize, pixelSize, multisampleResolve ? 1 : sampleCount, flags));
1709 const bool textureIsSrgb = flags.testFlag(QRhiTexture::sRGB);
1710 const bool viewIsSrgb = QRhiTexture::Flags(u.nativeTextureArray.rhiViewFormatFlags).testFlag(QRhiTexture::sRGB);
1711 if (viewFormat != format || viewIsSrgb != textureIsSrgb)
1712 texture->setWriteViewFormat({ viewFormat, viewIsSrgb });
1713 if (!texture->createFrom({ u.nativeTextureArray.object, u.nativeTextureArray.layoutOrState })) {
1714 qWarning(
"Failed to build wrapper texture array for QQuickRenderTarget");
1717 if (!createRhiRenderTargetMultiView(texture.get(), customDepthTexture, pixelSize, arraySize, sampleCount, multisampleResolve, rhi, dst))
1719 dst->res.texture = texture.release();
1723 case Type::NativeRenderbuffer:
1725 std::unique_ptr<QRhiRenderBuffer> renderbuffer(rhi->newRenderBuffer(QRhiRenderBuffer::Color, pixelSize, sampleCount));
1726 if (!renderbuffer->createFrom({ u.nativeRenderbufferObject })) {
1727 qWarning(
"Failed to build wrapper renderbuffer for QQuickRenderTarget");
1730 if (customDepthTexture)
1731 qWarning(
"Custom depth texture is not supported with renderbuffers in QQuickRenderTarget");
1732 if (!createRhiRenderTargetWithRenderBuffer(renderbuffer.get(), pixelSize, sampleCount, rhi, dst))
1734 dst->res.renderBuffer = renderbuffer.release();
1738 case Type::RhiRenderTarget:
1739 dst->implicitBuffers.reset(rhi);
1740 dst->rt.renderTarget = u.rhiRt;
1741 dst->rt.owns =
false;
1742 if (dst->rt.renderTarget->resourceType() == QRhiResource::TextureRenderTarget) {
1743 auto texRt =
static_cast<QRhiTextureRenderTarget *>(dst->rt.renderTarget);
1744 const QRhiTextureRenderTargetDescription desc = texRt->description();
1746 for (
auto it = desc.cbeginColorAttachments(), end = desc.cendColorAttachments(); it != end; ++it) {
1747 if (it->multiViewCount() <= 1)
1749 if (first || dst->rt.multiViewCount == it->multiViewCount()) {
1751 if (it->texture() && it->texture()->flags().testFlag(QRhiTexture::TextureArray)) {
1752 if (it->texture()->arraySize() >= it->layer() + it->multiViewCount()) {
1753 dst->rt.multiViewCount = it->multiViewCount();
1755 qWarning(
"Invalid QQuickRenderTarget; needs at least %d elements in texture array, got %d",
1756 it->layer() + it->multiViewCount(),
1757 it->texture()->arraySize());
1761 qWarning(
"Invalid QQuickRenderTarget; multiview requires a texture array");
1765 qWarning(
"Inconsistent multiViewCount in QQuickRenderTarget (was %d, now found an attachment with %d)",
1766 dst->rt.multiViewCount, it->multiViewCount());
1771 if (customDepthTexture)
1772 qWarning(
"Custom depth texture is not supported with QRhiRenderTarget in QQuickRenderTarget");
1775 case Type::PaintDevice:
1776 dst->implicitBuffers.reset(rhi);
1777 dst->sw.paintDevice = u.paintDevice;
1778 dst->sw.owns =
false;
1782 Q_UNREACHABLE_RETURN(
false);
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Combined button and popup list for selecting options.
static bool createRhiRenderTargetWithRenderBuffer(QRhiRenderBuffer *renderBuffer, const QSize &pixelSize, int sampleCount, QRhi *rhi, QQuickWindowRenderTarget *dst)
static bool createRhiRenderTargetWithDepthTexture(QRhiTexture *texture, QRhiTexture *depthTexture, const QSize &pixelSize, int sampleCount, bool multisampleResolve, QRhi *rhi, QQuickWindowRenderTarget *dst)
static bool createRhiRenderTargetMultiView(QRhiTexture *texture, QRhiTexture *maybeCustomDepthTexture, const QSize &pixelSize, int arraySize, int sampleCount, bool multisampleResolve, QRhi *rhi, QQuickWindowRenderTarget *dst)
static bool createRhiRenderTarget(QRhiTexture *texture, const QSize &pixelSize, int sampleCount, bool multisampleResolve, QRhi *rhi, QQuickWindowRenderTarget *dst)