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 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | #ifndef GL_BACK_LEFT |
11 | #define GL_BACK_LEFT 0x0402 |
12 | #endif |
13 | #ifndef GL_BACK_RIGHT |
14 | #define GL_BACK_RIGHT 0x0403 |
15 | #endif |
16 | |
17 | namespace Qt3DRender { |
18 | namespace Render { |
19 | |
20 | AttachmentPack::AttachmentPack() |
21 | { |
22 | } |
23 | |
24 | AttachmentPack::AttachmentPack(const RenderTarget *target, |
25 | AttachmentManager *attachmentManager, |
26 | const QList<QRenderTargetOutput::AttachmentPoint> &drawBuffers) |
27 | { |
28 | // Copy attachments |
29 | const auto outputIds = target->renderOutputs(); |
30 | for (Qt3DCore::QNodeId outputId : outputIds) { |
31 | const RenderTargetOutput *output = attachmentManager->lookupResource(id: outputId); |
32 | if (output) |
33 | m_attachments.push_back(x: *output->attachment()); |
34 | } |
35 | |
36 | // Sort by attachment point |
37 | std::sort(first: m_attachments.begin(), |
38 | last: m_attachments.end(), |
39 | comp: [] (const Attachment &a, const Attachment &b) { |
40 | return a.m_point < b.m_point; |
41 | }); |
42 | |
43 | |
44 | // Create actual DrawBuffers list that is used for glDrawBuffers |
45 | |
46 | // If nothing is specified, use all the attachments as draw buffers |
47 | if (drawBuffers.empty()) { |
48 | m_drawBuffers.reserve(n: m_attachments.size()); |
49 | for (const Attachment &attachment : std::as_const(t&: m_attachments)) { |
50 | if ((attachment.m_point >= QRenderTargetOutput::Color0 && attachment.m_point <= QRenderTargetOutput::Color15) |
51 | || attachment.m_point == QRenderTargetOutput::Left |
52 | || attachment.m_point == QRenderTargetOutput::Right) |
53 | m_drawBuffers.push_back(x: attachment.m_point); |
54 | } |
55 | } else { |
56 | m_drawBuffers.reserve(n: drawBuffers.size()); |
57 | for (QRenderTargetOutput::AttachmentPoint drawBuffer : drawBuffers) { |
58 | if ((drawBuffer >= QRenderTargetOutput::Color0 && drawBuffer <= QRenderTargetOutput::Color15) |
59 | || drawBuffer == QRenderTargetOutput::Left |
60 | || drawBuffer == QRenderTargetOutput::Right) |
61 | m_drawBuffers.push_back(x: drawBuffer); |
62 | } |
63 | } |
64 | } |
65 | |
66 | // return index of given attachment within actual draw buffers list |
67 | int AttachmentPack::getDrawBufferIndex(QRenderTargetOutput::AttachmentPoint attachmentPoint) const |
68 | { |
69 | for (size_t i = 0; i < m_drawBuffers.size(); i++) |
70 | if (m_drawBuffers.at(n: i) == attachmentPoint) |
71 | return int(i); |
72 | return -1; |
73 | } |
74 | |
75 | bool operator ==(const Attachment &a, const Attachment &b) |
76 | { |
77 | return (a.m_name == b.m_name && |
78 | a.m_mipLevel == b.m_mipLevel && |
79 | a.m_layer == b.m_layer && |
80 | a.m_textureUuid == b.m_textureUuid && |
81 | a.m_point == b.m_point && |
82 | a.m_face == b.m_face); |
83 | } |
84 | |
85 | bool operator !=(const Attachment &a, const Attachment &b) |
86 | { |
87 | return !(a == b); |
88 | } |
89 | |
90 | bool operator ==(const AttachmentPack &packA, const AttachmentPack &packB) |
91 | { |
92 | return (packA.attachments() == packB.attachments() && |
93 | packA.getDrawBuffers() == packB.getDrawBuffers()); |
94 | } |
95 | |
96 | bool operator !=(const AttachmentPack &packA, const AttachmentPack &packB) |
97 | { |
98 | return !(packA == packB); |
99 | } |
100 | |
101 | } // namespace Render |
102 | } // namespace Qt3DRender |
103 | |
104 | QT_END_NAMESPACE |
105 | |