| 1 | // Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QT3DRENDER_RENDER_RHI_RENDERVIEW_H |
| 6 | #define QT3DRENDER_RENDER_RHI_RENDERVIEW_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists for the convenience |
| 13 | // of other Qt classes. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <Qt3DRender/qparameter.h> |
| 20 | #include <Qt3DRender/qclearbuffers.h> |
| 21 | #include <Qt3DRender/qlayerfilter.h> |
| 22 | #include <Qt3DRender/private/clearbuffers_p.h> |
| 23 | #include <Qt3DRender/private/cameralens_p.h> |
| 24 | #include <Qt3DRender/private/attachmentpack_p.h> |
| 25 | #include <Qt3DRender/private/handle_types_p.h> |
| 26 | #include <Qt3DRender/private/qsortpolicy_p.h> |
| 27 | #include <Qt3DRender/private/lightsource_p.h> |
| 28 | #include <Qt3DRender/private/qmemorybarrier_p.h> |
| 29 | #include <Qt3DRender/private/qrendercapture_p.h> |
| 30 | #include <Qt3DRender/private/qblitframebuffer_p.h> |
| 31 | #include <Qt3DRender/private/qwaitfence_p.h> |
| 32 | |
| 33 | #include <Qt3DCore/private/aligned_malloc_p.h> |
| 34 | |
| 35 | #include <renderer_p.h> |
| 36 | // TODO: Move out once this is all refactored |
| 37 | #include <Qt3DRender/private/renderviewjobutils_p.h> |
| 38 | |
| 39 | #include <vector> |
| 40 | #include <QSurface> |
| 41 | #include <QMutex> |
| 42 | #include <QColor> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | namespace Qt3DRender { |
| 47 | |
| 48 | class QRenderPass; |
| 49 | |
| 50 | namespace Render { |
| 51 | |
| 52 | class NodeManagers; |
| 53 | class RenderPassFilter; |
| 54 | class TechniqueFilter; |
| 55 | class ViewportNode; |
| 56 | class Effect; |
| 57 | class RenderPass; |
| 58 | |
| 59 | namespace Rhi { |
| 60 | |
| 61 | class Renderer; |
| 62 | class RenderCommand; |
| 63 | class RHIShader; |
| 64 | |
| 65 | typedef QPair<ShaderUniform, QVariant> ActivePropertyContent; |
| 66 | typedef QPair<QString, ActivePropertyContent> ActiveProperty; |
| 67 | |
| 68 | using EntityRenderCommandData = Render::EntityRenderCommandData<RenderCommand>; |
| 69 | using EntityRenderCommandDataView = Render::EntityRenderCommandDataView<RenderCommand>; |
| 70 | using EntityRenderCommandDataViewPtr = Render::EntityRenderCommandDataViewPtr<RenderCommand>; |
| 71 | using EntityRenderCommandDataSubView = Render::EntityRenderCommandDataSubView<RenderCommand>; |
| 72 | |
| 73 | struct Q_AUTOTEST_EXPORT ClearBufferInfo |
| 74 | { |
| 75 | int drawBufferIndex = 0; |
| 76 | QRenderTargetOutput::AttachmentPoint attchmentPoint = QRenderTargetOutput::Color0; |
| 77 | QVector4D clearColor; |
| 78 | }; |
| 79 | |
| 80 | struct Q_AUTOTEST_EXPORT BlitFramebufferInfo |
| 81 | { |
| 82 | Qt3DCore::QNodeId sourceRenderTargetId; |
| 83 | Qt3DCore::QNodeId destinationRenderTargetId; |
| 84 | QRect sourceRect; |
| 85 | QRect destinationRect; |
| 86 | Qt3DRender::QRenderTargetOutput::AttachmentPoint sourceAttachmentPoint; |
| 87 | Qt3DRender::QRenderTargetOutput::AttachmentPoint destinationAttachmentPoint; |
| 88 | QBlitFramebuffer::InterpolationMethod interpolationMethod; |
| 89 | }; |
| 90 | |
| 91 | struct RenderViewUBO |
| 92 | { |
| 93 | float viewMatrix[16]; |
| 94 | float projectionMatrix[16]; |
| 95 | float uncorrectedProjectionMatrix[16]; |
| 96 | float clipCorrectionMatrix[16]; |
| 97 | float viewProjectionMatrix[16]; |
| 98 | float inverseViewMatrix[16]; |
| 99 | float inverseProjectionMatrix[16]; |
| 100 | float inverseViewProjectionMatrix[16]; |
| 101 | float viewportMatrix[16]; |
| 102 | float inverseViewportMatrix[16]; |
| 103 | float textureTransformMatrix[4]; |
| 104 | float eyePosition[3]; |
| 105 | float aspectRatio; |
| 106 | float gamma; |
| 107 | float exposure; |
| 108 | float time; |
| 109 | float yUpInNDC; |
| 110 | float yUpInFBO; |
| 111 | }; |
| 112 | static_assert(sizeof(RenderViewUBO) == sizeof(float) * (10 * 16 + 1 * 4 + 1 * 3 + 6 * 1), |
| 113 | "UBO doesn't match std140" ); |
| 114 | |
| 115 | class Q_AUTOTEST_EXPORT RenderView |
| 116 | { |
| 117 | public: |
| 118 | RenderView(); |
| 119 | ~RenderView(); |
| 120 | |
| 121 | QT3D_ALIGNED_MALLOC_AND_FREE() |
| 122 | |
| 123 | static void setRenderViewConfigFromFrameGraphLeafNode(RenderView *rv, |
| 124 | const FrameGraphNode *fgLeaf); |
| 125 | |
| 126 | // TODO: Add a way to specify a sort predicate for the RenderCommands |
| 127 | void sort(); |
| 128 | |
| 129 | void setRenderer(Renderer *renderer); |
| 130 | inline void setSurfaceSize(const QSize &size) noexcept { m_surfaceSize = size; } |
| 131 | inline Renderer *renderer() const noexcept { return m_renderer; } |
| 132 | inline NodeManagers *nodeManagers() const noexcept { return m_manager; } |
| 133 | inline const QSize &surfaceSize() const noexcept { return m_surfaceSize; } |
| 134 | inline void setDevicePixelRatio(qreal r) noexcept { m_devicePixelRatio = r; } |
| 135 | inline qreal devicePixelRatio() const noexcept { return m_devicePixelRatio; } |
| 136 | |
| 137 | inline void setRenderCameraLens(CameraLens *renderCameraLens) noexcept { m_renderCameraLens = renderCameraLens; } |
| 138 | inline CameraLens *renderCameraLens() const noexcept { return m_renderCameraLens; } |
| 139 | |
| 140 | inline void setRenderCameraEntity(Entity *renderCameraNode) noexcept { m_renderCameraNode = renderCameraNode; } |
| 141 | inline Entity *renderCameraEntity() const noexcept { return m_renderCameraNode; } |
| 142 | |
| 143 | inline void setViewMatrix(const Matrix4x4 &viewMatrix) noexcept { m_viewMatrix = viewMatrix; } |
| 144 | inline Matrix4x4 viewMatrix() const noexcept { return m_viewMatrix; } |
| 145 | |
| 146 | inline void setViewProjectionMatrix(const Matrix4x4 &viewProjectionMatrix) noexcept { m_viewProjectionMatrix = viewProjectionMatrix; } |
| 147 | inline Matrix4x4 viewProjectionMatrix() const noexcept { return m_viewProjectionMatrix; } |
| 148 | |
| 149 | inline void setEyePosition(const Vector3D &eyePos) noexcept { m_eyePos = eyePos; } |
| 150 | inline Vector3D eyePosition() const noexcept { return m_eyePos; } |
| 151 | |
| 152 | inline void setEyeViewDirection(const Vector3D &dir) noexcept { m_eyeViewDir = dir; } |
| 153 | inline Vector3D eyeViewDirection() const noexcept { return m_eyeViewDir; } |
| 154 | |
| 155 | inline void appendLayerFilter(const Qt3DCore::QNodeId layerFilterId) noexcept { m_layerFilterIds.push_back(t: layerFilterId); } |
| 156 | inline Qt3DCore::QNodeIdVector layerFilters() const noexcept { return m_layerFilterIds; } |
| 157 | |
| 158 | inline void appendProximityFilterId(const Qt3DCore::QNodeId proximityFilterId) { m_proximityFilterIds.push_back(t: proximityFilterId); } |
| 159 | inline Qt3DCore::QNodeIdVector proximityFilterIds() const { return m_proximityFilterIds; } |
| 160 | |
| 161 | inline void setRenderPassFilter(const RenderPassFilter *rpFilter) noexcept { m_passFilter = rpFilter; } |
| 162 | inline const RenderPassFilter *renderPassFilter() const noexcept { return m_passFilter; } |
| 163 | |
| 164 | inline void setTechniqueFilter(const TechniqueFilter *filter) noexcept { m_techniqueFilter = filter; } |
| 165 | inline const TechniqueFilter *techniqueFilter() const noexcept { return m_techniqueFilter; } |
| 166 | |
| 167 | inline void setRenderCommandDataView(const EntityRenderCommandDataViewPtr &renderCommandDataView) noexcept { m_renderCommandDataView = renderCommandDataView; } |
| 168 | inline EntityRenderCommandDataViewPtr renderCommandDataView() const noexcept { return m_renderCommandDataView; } |
| 169 | |
| 170 | RenderStateSet *getOrCreateStateSet(); |
| 171 | RenderStateSet *stateSet() const noexcept { return m_stateSet.data(); } |
| 172 | |
| 173 | inline bool noDraw() const noexcept { return m_noDraw; } |
| 174 | void setNoDraw(bool noDraw) noexcept { m_noDraw = noDraw; } |
| 175 | |
| 176 | inline bool isCompute() const noexcept { return m_compute; } |
| 177 | void setCompute(bool compute) noexcept { m_compute = compute; } |
| 178 | |
| 179 | void setComputeWorkgroups(int x, int y, int z) noexcept { m_workGroups[0] = x; m_workGroups[1] = y; m_workGroups[2] = z; } |
| 180 | const int *computeWorkGroups() const noexcept { return m_workGroups; } |
| 181 | inline bool frustumCulling() const noexcept { return m_frustumCulling; } |
| 182 | void setFrustumCulling(bool frustumCulling) noexcept { m_frustumCulling = frustumCulling; } |
| 183 | bool showDebugOverlay() const noexcept { return m_showDebugOverlay; } |
| 184 | void setShowDebugOverlay(bool showDebugOverlay) noexcept { m_showDebugOverlay = showDebugOverlay; } |
| 185 | |
| 186 | inline void setMaterialParameterTable(const MaterialParameterGathererData ¶meters) noexcept { m_parameters = parameters; } |
| 187 | |
| 188 | // TODO: Get rid of this overly complex memory management by splitting out the |
| 189 | // InnerData as a RenderViewConfig struct. This can be created by setRenderViewConfigFromFrameGraphLeafNode |
| 190 | // and passed along with the RenderView to the functions that populate the renderview |
| 191 | inline void setViewport(const QRectF &vp) noexcept { m_viewport = vp; } |
| 192 | inline QRectF viewport() const noexcept { return m_viewport; } |
| 193 | |
| 194 | inline float gamma() const noexcept { return m_gamma; } |
| 195 | inline void setGamma(float gamma) noexcept { m_gamma = gamma; } |
| 196 | |
| 197 | // depth and stencil ClearBuffers are cached locally |
| 198 | // color ClearBuffers are collected, as there may be multiple |
| 199 | // color buffers to be cleared. we need to apply all these at rendering |
| 200 | void addClearBuffers(const ClearBuffers *cb); |
| 201 | inline const std::vector<ClearBufferInfo> &specificClearColorBufferInfo() const { return m_specificClearColorBuffers; } |
| 202 | inline std::vector<ClearBufferInfo> &specificClearColorBufferInfo() { return m_specificClearColorBuffers; } |
| 203 | inline ClearBufferInfo globalClearColorBufferInfo() const { return m_globalClearColorBuffer; } |
| 204 | |
| 205 | inline QClearBuffers::BufferTypeFlags clearTypes() const { return m_clearBuffer; } |
| 206 | inline float clearDepthValue() const { return m_clearDepthValue; } |
| 207 | inline int clearStencilValue() const { return m_clearStencilValue; } |
| 208 | |
| 209 | RenderPassList passesAndParameters(ParameterInfoList *parameter, Entity *node, bool useDefaultMaterials = true); |
| 210 | |
| 211 | EntityRenderCommandData buildDrawRenderCommands(const Entity **entities, |
| 212 | int offset, int count) const; |
| 213 | EntityRenderCommandData buildComputeRenderCommands(const Entity **entities, |
| 214 | int offset, int count) const; |
| 215 | |
| 216 | void updateRenderCommand(const EntityRenderCommandDataSubView &subView); |
| 217 | |
| 218 | void setRenderTargetId(Qt3DCore::QNodeId renderTargetId) noexcept { m_renderTarget = renderTargetId; } |
| 219 | Qt3DCore::QNodeId renderTargetId() const noexcept { return m_renderTarget; } |
| 220 | |
| 221 | void addSortType(const QList<Qt3DRender::QSortPolicy::SortType> &sortTypes) { Qt3DCore::append(destination&: m_sortingTypes, source: sortTypes); } |
| 222 | |
| 223 | void setSurface(QSurface *surface) { m_surface = surface; } |
| 224 | QSurface *surface() const { return m_surface; } |
| 225 | |
| 226 | void setLightSources(const std::vector<LightSource> &lightSources) noexcept { m_lightSources = lightSources; } |
| 227 | void setEnvironmentLight(EnvironmentLight *environmentLight) noexcept { m_environmentLight = environmentLight; } |
| 228 | |
| 229 | void updateMatrices(); |
| 230 | |
| 231 | inline void setRenderCaptureNodeId(const Qt3DCore::QNodeId nodeId) noexcept { m_renderCaptureNodeId = nodeId; } |
| 232 | inline const Qt3DCore::QNodeId renderCaptureNodeId() const noexcept { return m_renderCaptureNodeId; } |
| 233 | inline void setRenderCaptureRequest(const QRenderCaptureRequest& request) noexcept { m_renderCaptureRequest = request; } |
| 234 | inline const QRenderCaptureRequest renderCaptureRequest() const noexcept { return m_renderCaptureRequest; } |
| 235 | |
| 236 | |
| 237 | bool isDownloadBuffersEnable() const; |
| 238 | void setIsDownloadBuffersEnable(bool isDownloadBuffersEnable); |
| 239 | |
| 240 | BlitFramebufferInfo blitFrameBufferInfo() const; |
| 241 | void setBlitFrameBufferInfo(const BlitFramebufferInfo &blitFrameBufferInfo); |
| 242 | |
| 243 | bool hasBlitFramebufferInfo() const; |
| 244 | void setHasBlitFramebufferInfo(bool hasBlitFramebufferInfo); |
| 245 | |
| 246 | bool shouldSkipSubmission() const; |
| 247 | |
| 248 | template<typename F> |
| 249 | inline void forEachCommand(F func) const |
| 250 | { |
| 251 | if (!m_renderCommandDataView) |
| 252 | return; |
| 253 | m_renderCommandDataView->forEachCommand(func); |
| 254 | } |
| 255 | |
| 256 | template<typename F> |
| 257 | inline void forEachCommand(F func) |
| 258 | { |
| 259 | if (!m_renderCommandDataView) |
| 260 | return; |
| 261 | m_renderCommandDataView->forEachCommand(func); |
| 262 | } |
| 263 | |
| 264 | inline int commandCount() const { return m_renderCommandDataView ? int(m_renderCommandDataView->size()) : 0; } |
| 265 | |
| 266 | inline const RenderViewUBO *renderViewUBO() const { return &m_renderViewUBO; } |
| 267 | |
| 268 | private: |
| 269 | void setShaderAndUniforms(RenderCommand *command, ParameterInfoList ¶meters, const Entity *entity, |
| 270 | const std::vector<LightSource> &activeLightSources, |
| 271 | EnvironmentLight *environmentLight) const; |
| 272 | |
| 273 | Renderer *m_renderer = nullptr; |
| 274 | NodeManagers *m_manager = nullptr; |
| 275 | EntityRenderCommandDataViewPtr m_renderCommandDataView; |
| 276 | |
| 277 | QSize m_surfaceSize; |
| 278 | float m_devicePixelRatio = 1.0f; |
| 279 | QRectF m_viewport = QRectF(0.0f, 0.0f, 1.0f, 1.0f); |
| 280 | float m_gamma = 2.2f; |
| 281 | |
| 282 | Qt3DCore::QNodeId m_renderCaptureNodeId; |
| 283 | QRenderCaptureRequest m_renderCaptureRequest; |
| 284 | bool m_isDownloadBuffersEnable = false; |
| 285 | |
| 286 | bool m_hasBlitFramebufferInfo = false; |
| 287 | BlitFramebufferInfo m_blitFrameBufferInfo; |
| 288 | |
| 289 | QSurface *m_surface = nullptr; |
| 290 | Qt3DCore::QNodeId m_renderTarget; |
| 291 | AttachmentPack m_attachmentPack; |
| 292 | QClearBuffers::BufferTypeFlags m_clearBuffer = QClearBuffers::None; |
| 293 | float m_clearDepthValue = 1.0f; |
| 294 | int m_clearStencilValue = 0; |
| 295 | ClearBufferInfo m_globalClearColorBuffer; // global ClearColor |
| 296 | std::vector<ClearBufferInfo> m_specificClearColorBuffers; // different draw buffers with distinct colors |
| 297 | |
| 298 | QScopedPointer<RenderStateSet> m_stateSet; |
| 299 | CameraLens *m_renderCameraLens = nullptr; |
| 300 | Entity *m_renderCameraNode = nullptr; |
| 301 | const TechniqueFilter *m_techniqueFilter = nullptr; |
| 302 | const RenderPassFilter *m_passFilter = nullptr; |
| 303 | bool m_noDraw = false; |
| 304 | bool m_compute = false; |
| 305 | bool m_frustumCulling = false; |
| 306 | bool m_showDebugOverlay = false; |
| 307 | int m_workGroups[3] = { 1, 1, 1}; |
| 308 | std::vector<Qt3DRender::QSortPolicy::SortType> m_sortingTypes; |
| 309 | Qt3DCore::QNodeIdVector m_proximityFilterIds; |
| 310 | Qt3DCore::QNodeIdVector m_layerFilterIds; |
| 311 | Matrix4x4 m_viewMatrix; |
| 312 | Matrix4x4 m_viewProjectionMatrix; |
| 313 | Matrix4x4 m_clipCorrectionMatrix; |
| 314 | Vector3D m_eyePos; |
| 315 | Vector3D m_eyeViewDir; |
| 316 | |
| 317 | MaterialParameterGathererData m_parameters; |
| 318 | mutable std::vector<LightSource> m_lightSources; |
| 319 | EnvironmentLight *m_environmentLight = nullptr; |
| 320 | |
| 321 | RenderViewUBO m_renderViewUBO; |
| 322 | |
| 323 | void setUniformValue(ShaderParameterPack &uniformPack, int nameId, |
| 324 | const UniformValue &value) const; |
| 325 | void setUniformBlockValue(ShaderParameterPack &uniformPack, const RHIShader *shader, |
| 326 | const ShaderUniformBlock &block, const UniformValue &value) const; |
| 327 | void setShaderStorageValue(ShaderParameterPack &uniformPack, const RHIShader *shader, |
| 328 | const ShaderStorageBlock &block, const UniformValue &value) const; |
| 329 | void setShaderDataValue(ShaderParameterPack &uniformPack, |
| 330 | const ShaderUniformBlock &block, |
| 331 | const Qt3DCore::QNodeId &shaderDataId) const; |
| 332 | void setDefaultUniformBlockShaderDataValue(ShaderParameterPack &uniformPack, |
| 333 | const RHIShader *shader, |
| 334 | ShaderData *shaderData, |
| 335 | const QString &structName) const; |
| 336 | void applyParameter(const Parameter *param, RenderCommand *command, |
| 337 | const RHIShader *shader) const noexcept; |
| 338 | }; |
| 339 | |
| 340 | } // namespace Rhi |
| 341 | } // namespace Render |
| 342 | } // namespace Qt3DRender |
| 343 | |
| 344 | QT_END_NAMESPACE |
| 345 | |
| 346 | #endif // QT3DRENDER_RENDER_RHI_ENDERVIEW_H |
| 347 | |