1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "attachmentpack_p.h"
5#include <Qt3DRender/private/rendertarget_p.h>
6#include <Qt3DRender/private/rendertargetselectornode_p.h>
7
8QT_BEGIN_NAMESPACE
9
10namespace Qt3DRender {
11namespace Render {
12
13AttachmentPack::AttachmentPack()
14{
15}
16
17AttachmentPack::AttachmentPack(const RenderTarget *target,
18 AttachmentManager *attachmentManager,
19 const QList<QRenderTargetOutput::AttachmentPoint> &drawBuffers)
20{
21 // Copy attachments
22 const auto outputIds = target->renderOutputs();
23 for (Qt3DCore::QNodeId outputId : outputIds) {
24 const RenderTargetOutput *output = attachmentManager->lookupResource(id: outputId);
25 if (output)
26 m_attachments.push_back(x: *output->attachment());
27 }
28
29 // Sort by attachment point
30 std::sort(first: m_attachments.begin(),
31 last: m_attachments.end(),
32 comp: [] (const Attachment &a, const Attachment &b) {
33 return a.m_point < b.m_point;
34 });
35
36
37 // Create actual DrawBuffers list that is used for glDrawBuffers
38
39 // If nothing is specified, use all the attachments as draw buffers
40 if (drawBuffers.empty()) {
41 m_drawBuffers.reserve(n: m_attachments.size());
42 for (const Attachment &attachment : std::as_const(t&: m_attachments))
43 // only consider Color Attachments
44 if (attachment.m_point <= QRenderTargetOutput::Color15)
45 m_drawBuffers.push_back(x: (int) attachment.m_point);
46 } else {
47 m_drawBuffers.reserve(n: drawBuffers.size());
48 for (QRenderTargetOutput::AttachmentPoint drawBuffer : drawBuffers)
49 if (drawBuffer <= QRenderTargetOutput::Color15)
50 m_drawBuffers.push_back(x: (int) drawBuffer);
51 }
52}
53
54// return index of given attachment within actual draw buffers list
55int AttachmentPack::getDrawBufferIndex(QRenderTargetOutput::AttachmentPoint attachmentPoint) const
56{
57 for (size_t i = 0; i < m_drawBuffers.size(); i++)
58 if (m_drawBuffers.at(n: i) == (int)attachmentPoint)
59 return int(i);
60 return -1;
61}
62
63bool operator ==(const Attachment &a, const Attachment &b)
64{
65 return (a.m_name == b.m_name &&
66 a.m_mipLevel == b.m_mipLevel &&
67 a.m_layer == b.m_layer &&
68 a.m_textureUuid == b.m_textureUuid &&
69 a.m_point == b.m_point &&
70 a.m_face == b.m_face);
71}
72
73bool operator !=(const Attachment &a, const Attachment &b)
74{
75 return !(a == b);
76}
77
78bool operator ==(const AttachmentPack &packA, const AttachmentPack &packB)
79{
80 return (packA.attachments() == packB.attachments() &&
81 packA.getGlDrawBuffers() == packB.getGlDrawBuffers());
82}
83
84bool operator !=(const AttachmentPack &packA, const AttachmentPack &packB)
85{
86 return !(packA == packB);
87}
88
89} // namespace Render
90} // namespace Qt3DRender
91
92QT_END_NAMESPACE
93

source code of qt3d/src/render/backend/attachmentpack.cpp