1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #ifndef RENDERER_H |
52 | #define RENDERER_H |
53 | |
54 | #include "vulkanwindow.h" |
55 | #include "mesh.h" |
56 | #include "shader.h" |
57 | #include "camera.h" |
58 | #include <QFutureWatcher> |
59 | #include <QMutex> |
60 | |
61 | class Renderer : public QVulkanWindowRenderer |
62 | { |
63 | public: |
64 | Renderer(VulkanWindow *w, int initialCount); |
65 | |
66 | void preInitResources() override; |
67 | void initResources() override; |
68 | void initSwapChainResources() override; |
69 | void releaseSwapChainResources() override; |
70 | void releaseResources() override; |
71 | |
72 | void startNextFrame() override; |
73 | |
74 | bool animating() const { return m_animating; } |
75 | void setAnimating(bool a) { m_animating = a; } |
76 | |
77 | int instanceCount() const { return m_instCount; } |
78 | void addNew(); |
79 | |
80 | void yaw(float degrees); |
81 | void pitch(float degrees); |
82 | void walk(float amount); |
83 | void strafe(float amount); |
84 | |
85 | void setUseLogo(bool b); |
86 | |
87 | private: |
88 | void createPipelines(); |
89 | void createItemPipeline(); |
90 | void createFloorPipeline(); |
91 | void ensureBuffers(); |
92 | void ensureInstanceBuffer(); |
93 | void getMatrices(QMatrix4x4 *mvp, QMatrix4x4 *model, QMatrix3x3 *modelNormal, QVector3D *eyePos); |
94 | void writeFragUni(quint8 *p, const QVector3D &eyePos); |
95 | void buildFrame(); |
96 | void buildDrawCallsForItems(); |
97 | void buildDrawCallsForFloor(); |
98 | |
99 | void markViewProjDirty() { m_vpDirty = m_window->concurrentFrameCount(); } |
100 | |
101 | VulkanWindow *m_window; |
102 | QVulkanDeviceFunctions *m_devFuncs; |
103 | |
104 | bool m_useLogo = false; |
105 | Mesh m_blockMesh; |
106 | Mesh m_logoMesh; |
107 | VkBuffer m_blockVertexBuf = VK_NULL_HANDLE; |
108 | VkBuffer m_logoVertexBuf = VK_NULL_HANDLE; |
109 | struct { |
110 | VkDeviceSize vertUniSize; |
111 | VkDeviceSize fragUniSize; |
112 | VkDeviceSize uniMemStartOffset; |
113 | Shader vs; |
114 | Shader fs; |
115 | VkDescriptorPool descPool = VK_NULL_HANDLE; |
116 | VkDescriptorSetLayout descSetLayout = VK_NULL_HANDLE; |
117 | VkDescriptorSet descSet; |
118 | VkPipelineLayout pipelineLayout = VK_NULL_HANDLE; |
119 | VkPipeline pipeline = VK_NULL_HANDLE; |
120 | } m_itemMaterial; |
121 | |
122 | VkBuffer m_floorVertexBuf = VK_NULL_HANDLE; |
123 | struct { |
124 | Shader vs; |
125 | Shader fs; |
126 | VkPipelineLayout pipelineLayout = VK_NULL_HANDLE; |
127 | VkPipeline pipeline = VK_NULL_HANDLE; |
128 | } m_floorMaterial; |
129 | |
130 | VkDeviceMemory m_bufMem = VK_NULL_HANDLE; |
131 | VkBuffer m_uniBuf = VK_NULL_HANDLE; |
132 | |
133 | VkPipelineCache m_pipelineCache = VK_NULL_HANDLE; |
134 | QFuture<void> m_pipelinesFuture; |
135 | |
136 | QVector3D m_lightPos; |
137 | Camera m_cam; |
138 | |
139 | QMatrix4x4 m_proj; |
140 | int m_vpDirty = 0; |
141 | QMatrix4x4 m_floorModel; |
142 | |
143 | bool m_animating; |
144 | float m_rotation = 0.0f; |
145 | |
146 | int m_instCount; |
147 | int m_preparedInstCount = 0; |
148 | QByteArray m_instData; |
149 | VkBuffer m_instBuf = VK_NULL_HANDLE; |
150 | VkDeviceMemory m_instBufMem = VK_NULL_HANDLE; |
151 | |
152 | QFutureWatcher<void> m_frameWatcher; |
153 | bool m_framePending; |
154 | |
155 | QMutex m_guiMutex; |
156 | }; |
157 | |
158 | #endif |
159 | |