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 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer for QQuickRenderTarget"));
1256 if (!depthStencil->create()) {
1257 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1264 rtDesc.setDepthStencilBuffer(depthStencil.get());
1265 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1266 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget with renderbuffer"));
1267 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1268 rt->setRenderPassDescriptor(rp.get());
1270 if (!rt->create()) {
1271 qWarning(
"Failed to build renderbuffer-based render target for QQuickRenderTarget");
1275 dst->rt.renderTarget = rt.release();
1276 dst->rt.owns =
true;
1277 dst->res.rpDesc = rp.release();
1278 dst->implicitBuffers.depthStencil = depthStencil.release();
1284 const QSize &pixelSize,
1286 bool multisampleResolve,
1294 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1295 if (sampleCount <= 1)
1296 multisampleResolve =
false;
1298 std::unique_ptr<QRhiRenderBuffer> depthStencil;
1299 if (dst->implicitBuffers.depthStencil) {
1300 if (dst->implicitBuffers.depthStencil->pixelSize() == pixelSize
1301 && dst->implicitBuffers.depthStencil->sampleCount() == sampleCount)
1303 depthStencil.reset(dst->implicitBuffers.depthStencil);
1304 dst->implicitBuffers.depthStencil =
nullptr;
1308 std::unique_ptr<QRhiTexture> colorBuffer;
1309 QRhiTexture::Flags multisampleTextureFlags;
1310 QRhiTexture::Format multisampleTextureFormat = texture->format();
1311 if (multisampleResolve) {
1312 multisampleTextureFlags = QRhiTexture::RenderTarget;
1313 if (texture->flags().testFlag(QRhiTexture::sRGB))
1314 multisampleTextureFlags |= QRhiTexture::sRGB;
1316 if (dst->implicitBuffers.multisampleTexture) {
1317 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1318 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1319 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1320 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1322 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1323 dst->implicitBuffers.multisampleTexture =
nullptr;
1328 dst->implicitBuffers.reset(rhi);
1330 if (!depthStencil) {
1331 depthStencil.reset(rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, pixelSize, sampleCount));
1332 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer for QQuickRenderTarget"));
1333 if (!depthStencil->create()) {
1334 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1339 if (multisampleResolve && !colorBuffer) {
1340 colorBuffer.reset(rhi->newTexture(multisampleTextureFormat, pixelSize, sampleCount, multisampleTextureFlags));
1341 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer for QQuickRenderTarget"));
1342 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1343 if (!colorBuffer->create()) {
1344 qWarning(
"Failed to build multisample color buffer for QQuickRenderTarget");
1350 if (multisampleResolve) {
1351 colorAttachment.setTexture(colorBuffer.get());
1352 colorAttachment.setResolveTexture(texture);
1354 colorAttachment.setTexture(texture);
1357 rtDesc.setDepthStencilBuffer(depthStencil.get());
1358 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1359 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget"));
1360 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1361 rt->setRenderPassDescriptor(rp.get());
1363 if (!rt->create()) {
1364 qWarning(
"Failed to build texture render target for QQuickRenderTarget");
1368 dst->rt.renderTarget = rt.release();
1369 dst->rt.owns =
true;
1370 dst->res.rpDesc = rp.release();
1371 dst->implicitBuffers.depthStencil = depthStencil.release();
1372 if (multisampleResolve)
1373 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1379 QRhiTexture *depthTexture,
1380 const QSize &pixelSize,
1382 bool multisampleResolve,
1395 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1396 if (sampleCount <= 1)
1397 multisampleResolve =
false;
1399 std::unique_ptr<QRhiTexture> depthStencil;
1400 if (dst->implicitBuffers.depthStencilTexture) {
1401 if (dst->implicitBuffers.depthStencilTexture->pixelSize() == pixelSize
1402 && dst->implicitBuffers.depthStencilTexture->sampleCount() == sampleCount)
1404 depthStencil.reset(dst->implicitBuffers.depthStencilTexture);
1405 dst->implicitBuffers.depthStencilTexture =
nullptr;
1409 std::unique_ptr<QRhiTexture> colorBuffer;
1410 QRhiTexture::Flags multisampleTextureFlags;
1411 QRhiTexture::Format multisampleTextureFormat = texture->format();
1412 if (multisampleResolve) {
1413 multisampleTextureFlags = QRhiTexture::RenderTarget;
1414 if (texture->flags().testFlag(QRhiTexture::sRGB))
1415 multisampleTextureFlags |= QRhiTexture::sRGB;
1417 if (dst->implicitBuffers.multisampleTexture) {
1418 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1419 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1420 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1421 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1423 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1424 dst->implicitBuffers.multisampleTexture =
nullptr;
1429 dst->implicitBuffers.reset(rhi);
1431 bool needsDepthStencilBuffer =
true;
1432 if (sampleCount <= 1) {
1433 depthStencil.reset();
1434 needsDepthStencilBuffer =
false;
1436 if (depthTexture->pixelSize() != pixelSize) {
1437 qWarning(
"Custom depth texture size (%dx%d) does not match the QQuickRenderTarget (%dx%d)",
1438 depthTexture->pixelSize().width(),
1439 depthTexture->pixelSize().height(),
1441 pixelSize.height());
1444 if (depthTexture->sampleCount() > 1) {
1445 qWarning(
"Custom depth texture cannot be multisample");
1448 if (needsDepthStencilBuffer && !depthStencil) {
1449 QRhiTexture::Format multisampleDepthTextureFormat = depthTexture->format();
1450 depthStencil.reset(rhi->newTexture(multisampleDepthTextureFormat, pixelSize, sampleCount, QRhiTexture::RenderTarget));
1451 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil texture for QQuickRenderTarget"));
1452 if (!depthStencil->create()) {
1453 qWarning(
"Failed to build depth-stencil buffer for QQuickRenderTarget");
1458 if (multisampleResolve && !colorBuffer) {
1459 colorBuffer.reset(rhi->newTexture(multisampleTextureFormat, pixelSize, sampleCount, multisampleTextureFlags));
1460 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer for QQuickRenderTarget"));
1461 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1462 if (!colorBuffer->create()) {
1463 qWarning(
"Failed to build multisample color buffer for QQuickRenderTarget");
1469 if (multisampleResolve) {
1470 colorAttachment.setTexture(colorBuffer.get());
1471 colorAttachment.setResolveTexture(texture);
1473 colorAttachment.setTexture(texture);
1477 if (sampleCount > 1) {
1478 rtDesc.setDepthTexture(depthStencil.get());
1479 if (rhi->isFeatureSupported(QRhi::ResolveDepthStencil))
1480 rtDesc.setDepthResolveTexture(depthTexture);
1482 qWarning(
"Depth-stencil resolve is not supported by the underlying 3D API, depth contents will not be resolved");
1484 rtDesc.setDepthTexture(depthTexture);
1487 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc));
1488 rt->setName(QByteArrayLiteral(
"RT for QQuickRenderTarget"));
1489 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1490 rt->setRenderPassDescriptor(rp.get());
1492 if (!rt->create()) {
1493 qWarning(
"Failed to build texture render target for QQuickRenderTarget");
1497 dst->rt.renderTarget = rt.release();
1498 dst->rt.owns =
true;
1499 dst->res.rpDesc = rp.release();
1501 dst->implicitBuffers.depthStencilTexture = depthStencil.release();
1502 if (multisampleResolve)
1503 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1509 QRhiTexture *maybeCustomDepthTexture,
1510 const QSize &pixelSize,
1513 bool multisampleResolve,
1522 sampleCount = QSGRhiSupport::chooseSampleCount(sampleCount, rhi);
1523 if (sampleCount <= 1)
1524 multisampleResolve =
false;
1526 std::unique_ptr<QRhiTexture> depthStencil;
1527 if (dst->implicitBuffers.depthStencilTexture) {
1528 if (dst->implicitBuffers.depthStencilTexture->pixelSize() == pixelSize
1529 && dst->implicitBuffers.depthStencilTexture->sampleCount() == sampleCount
1530 && dst->implicitBuffers.depthStencilTexture->arraySize() == arraySize)
1532 depthStencil.reset(dst->implicitBuffers.depthStencilTexture);
1533 dst->implicitBuffers.depthStencilTexture =
nullptr;
1537 std::unique_ptr<QRhiTexture> colorBuffer;
1538 QRhiTexture::Flags multisampleTextureFlags;
1539 QRhiTexture::Format multisampleTextureFormat = texture->format();
1540 if (multisampleResolve) {
1541 multisampleTextureFlags = QRhiTexture::RenderTarget;
1542 if (texture->flags().testFlag(QRhiTexture::sRGB))
1543 multisampleTextureFlags |= QRhiTexture::sRGB;
1545 if (dst->implicitBuffers.multisampleTexture) {
1546 if (dst->implicitBuffers.multisampleTexture->pixelSize() == pixelSize
1547 && dst->implicitBuffers.multisampleTexture->format() == multisampleTextureFormat
1548 && dst->implicitBuffers.multisampleTexture->sampleCount() == sampleCount
1549 && dst->implicitBuffers.multisampleTexture->arraySize() == arraySize
1550 && dst->implicitBuffers.multisampleTexture->flags().testFlags(multisampleTextureFlags))
1552 colorBuffer.reset(dst->implicitBuffers.multisampleTexture);
1553 dst->implicitBuffers.multisampleTexture =
nullptr;
1558 dst->implicitBuffers.reset(rhi);
1560 bool needsDepthStencilBuffer =
true;
1561 if (maybeCustomDepthTexture) {
1562 if (sampleCount <= 1) {
1563 depthStencil.reset();
1564 needsDepthStencilBuffer =
false;
1566 if (maybeCustomDepthTexture->arraySize() != arraySize) {
1567 qWarning(
"Custom depth texture array size (%d) does not match QQuickRenderTarget (%d)",
1568 maybeCustomDepthTexture->arraySize(), arraySize);
1571 if (maybeCustomDepthTexture->pixelSize() != pixelSize) {
1572 qWarning(
"Custom depth texture size (%dx%d) does not match the QQuickRenderTarget (%dx%d)",
1573 maybeCustomDepthTexture->pixelSize().width(),
1574 maybeCustomDepthTexture->pixelSize().height(),
1576 pixelSize.height());
1579 if (maybeCustomDepthTexture->sampleCount() > 1) {
1580 qWarning(
"Custom depth texture cannot be multisample");
1584 if (needsDepthStencilBuffer && !depthStencil) {
1585 QRhiTexture::Format format;
1586 if (maybeCustomDepthTexture)
1587 format = maybeCustomDepthTexture->format();
1589 format = rhi->isTextureFormatSupported(QRhiTexture::D24S8) ? QRhiTexture::D24S8 : QRhiTexture::D32FS8;
1590 depthStencil.reset(rhi->newTextureArray(format, arraySize, pixelSize, sampleCount, QRhiTexture::RenderTarget));
1591 depthStencil->setName(QByteArrayLiteral(
"Depth-stencil buffer (multiview) for QQuickRenderTarget"));
1592 if (!depthStencil->create()) {
1593 qWarning(
"Failed to build depth-stencil texture array for QQuickRenderTarget");
1598 if (multisampleResolve && !colorBuffer) {
1599 colorBuffer.reset(rhi->newTextureArray(multisampleTextureFormat, arraySize, pixelSize, sampleCount, multisampleTextureFlags));
1600 colorBuffer->setName(QByteArrayLiteral(
"Multisample color buffer (multiview) for QQuickRenderTarget"));
1601 colorBuffer->setWriteViewFormat(texture->writeViewFormat());
1602 if (!colorBuffer->create()) {
1603 qWarning(
"Failed to build multisample texture array for QQuickRenderTarget");
1609 colorAttachment.setMultiViewCount(arraySize);
1610 if (multisampleResolve) {
1611 colorAttachment.setTexture(colorBuffer.get());
1612 colorAttachment.setResolveTexture(texture);
1614 colorAttachment.setTexture(texture);
1618 if (sampleCount > 1) {
1619 rtDesc.setDepthTexture(depthStencil.get());
1620 if (maybeCustomDepthTexture) {
1621 if (rhi->isFeatureSupported(QRhi::ResolveDepthStencil))
1622 rtDesc.setDepthResolveTexture(maybeCustomDepthTexture);
1624 qWarning(
"Depth-stencil resolve is not supported by the underlying 3D API, depth contents will not be resolved");
1628 rtDesc.setDepthTexture(depthStencil.get());
1629 else if (maybeCustomDepthTexture)
1630 rtDesc.setDepthTexture(maybeCustomDepthTexture);
1633 QRhiTextureRenderTarget::Flags rtFlags;
1634 if (!maybeCustomDepthTexture)
1635 rtFlags |= QRhiTextureRenderTarget::DoNotStoreDepthStencilContents;
1637 std::unique_ptr<QRhiTextureRenderTarget> rt(rhi->newTextureRenderTarget(rtDesc, rtFlags));
1638 rt->setName(QByteArrayLiteral(
"RT for multiview QQuickRenderTarget"));
1639 std::unique_ptr<QRhiRenderPassDescriptor> rp(rt->newCompatibleRenderPassDescriptor());
1640 rt->setRenderPassDescriptor(rp.get());
1642 if (!rt->create()) {
1643 qWarning(
"Failed to build multiview texture render target for QQuickRenderTarget");
1647 dst->rt.renderTarget = rt.release();
1648 dst->rt.owns =
true;
1649 dst->res.rpDesc = rp.release();
1651 dst->implicitBuffers.depthStencilTexture = depthStencil.release();
1652 if (multisampleResolve)
1653 dst->implicitBuffers.multisampleTexture = colorBuffer.release();
1655 dst->rt.multiViewCount = arraySize;
1660bool QQuickRenderTargetPrivate::resolve(QRhi *rhi, QQuickWindowRenderTarget *dst)
1668 dst->implicitBuffers.reset(rhi);
1671 case Type::NativeTexture:
1673 QRhiTexture::Format format = u.nativeTexture.rhiFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1674 : QRhiTexture::Format(u.nativeTexture.rhiFormat);
1675 QRhiTexture::Format viewFormat = u.nativeTexture.rhiViewFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1676 : QRhiTexture::Format(u.nativeTexture.rhiViewFormat);
1677 const auto flags = QRhiTexture::RenderTarget | QRhiTexture::Flags(u.nativeTexture.rhiFormatFlags);
1678 std::unique_ptr<QRhiTexture> texture(rhi->newTexture(format, pixelSize, multisampleResolve ? 1 : sampleCount, flags));
1679 const bool textureIsSrgb = flags.testFlag(QRhiTexture::sRGB);
1680 const bool viewIsSrgb = QRhiTexture::Flags(u.nativeTexture.rhiViewFormatFlags).testFlag(QRhiTexture::sRGB);
1681 if (viewFormat != format || viewIsSrgb != textureIsSrgb)
1682 texture->setWriteViewFormat({ viewFormat, viewIsSrgb });
1683 if (!texture->createFrom({ u.nativeTexture.object, u.nativeTexture.layoutOrState })) {
1684 qWarning(
"Failed to build wrapper texture for QQuickRenderTarget");
1687 if (customDepthTexture) {
1688 if (!createRhiRenderTargetWithDepthTexture(texture.get(), customDepthTexture, pixelSize, sampleCount, multisampleResolve, rhi, dst))
1691 if (!createRhiRenderTarget(texture.get(), pixelSize, sampleCount, multisampleResolve, rhi, dst))
1694 dst->res.texture = texture.release();
1698 case Type::NativeTextureArray:
1700 QRhiTexture::Format format = u.nativeTextureArray.rhiFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1701 : QRhiTexture::Format(u.nativeTextureArray.rhiFormat);
1702 QRhiTexture::Format viewFormat = u.nativeTextureArray.rhiViewFormat == QRhiTexture::UnknownFormat ? QRhiTexture::RGBA8
1703 : QRhiTexture::Format(u.nativeTextureArray.rhiViewFormat);
1704 const auto flags = QRhiTexture::RenderTarget | QRhiTexture::Flags(u.nativeTextureArray.rhiFormatFlags);
1705 const int arraySize = u.nativeTextureArray.arraySize;
1706 std::unique_ptr<QRhiTexture> texture(rhi->newTextureArray(format, arraySize, pixelSize, multisampleResolve ? 1 : sampleCount, flags));
1707 const bool textureIsSrgb = flags.testFlag(QRhiTexture::sRGB);
1708 const bool viewIsSrgb = QRhiTexture::Flags(u.nativeTextureArray.rhiViewFormatFlags).testFlag(QRhiTexture::sRGB);
1709 if (viewFormat != format || viewIsSrgb != textureIsSrgb)
1710 texture->setWriteViewFormat({ viewFormat, viewIsSrgb });
1711 if (!texture->createFrom({ u.nativeTextureArray.object, u.nativeTextureArray.layoutOrState })) {
1712 qWarning(
"Failed to build wrapper texture array for QQuickRenderTarget");
1715 if (!createRhiRenderTargetMultiView(texture.get(), customDepthTexture, pixelSize, arraySize, sampleCount, multisampleResolve, rhi, dst))
1717 dst->res.texture = texture.release();
1721 case Type::NativeRenderbuffer:
1723 std::unique_ptr<QRhiRenderBuffer> renderbuffer(rhi->newRenderBuffer(QRhiRenderBuffer::Color, pixelSize, sampleCount));
1724 if (!renderbuffer->createFrom({ u.nativeRenderbufferObject })) {
1725 qWarning(
"Failed to build wrapper renderbuffer for QQuickRenderTarget");
1728 if (customDepthTexture)
1729 qWarning(
"Custom depth texture is not supported with renderbuffers in QQuickRenderTarget");
1730 if (!createRhiRenderTargetWithRenderBuffer(renderbuffer.get(), pixelSize, sampleCount, rhi, dst))
1732 dst->res.renderBuffer = renderbuffer.release();
1736 case Type::RhiRenderTarget:
1737 dst->implicitBuffers.reset(rhi);
1738 dst->rt.renderTarget = u.rhiRt;
1739 dst->rt.owns =
false;
1740 if (dst->rt.renderTarget->resourceType() == QRhiResource::TextureRenderTarget) {
1741 auto texRt =
static_cast<QRhiTextureRenderTarget *>(dst->rt.renderTarget);
1742 const QRhiTextureRenderTargetDescription desc = texRt->description();
1744 for (
auto it = desc.cbeginColorAttachments(), end = desc.cendColorAttachments(); it != end; ++it) {
1745 if (it->multiViewCount() <= 1)
1747 if (first || dst->rt.multiViewCount == it->multiViewCount()) {
1749 if (it->texture() && it->texture()->flags().testFlag(QRhiTexture::TextureArray)) {
1750 if (it->texture()->arraySize() >= it->layer() + it->multiViewCount()) {
1751 dst->rt.multiViewCount = it->multiViewCount();
1753 qWarning(
"Invalid QQuickRenderTarget; needs at least %d elements in texture array, got %d",
1754 it->layer() + it->multiViewCount(),
1755 it->texture()->arraySize());
1759 qWarning(
"Invalid QQuickRenderTarget; multiview requires a texture array");
1763 qWarning(
"Inconsistent multiViewCount in QQuickRenderTarget (was %d, now found an attachment with %d)",
1764 dst->rt.multiViewCount, it->multiViewCount());
1769 if (customDepthTexture)
1770 qWarning(
"Custom depth texture is not supported with QRhiRenderTarget in QQuickRenderTarget");
1773 case Type::PaintDevice:
1774 dst->implicitBuffers.reset(rhi);
1775 dst->sw.paintDevice = u.paintDevice;
1776 dst->sw.owns =
false;
1780 Q_UNREACHABLE_RETURN(
false);
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
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)