| 1 | // Copyright (C) 2008-2012 NVIDIA Corporation. |
| 2 | // Copyright (C) 2020 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QSSGRHIEFFECTSYSTEM_P_H |
| 6 | #define QSSGRHIEFFECTSYSTEM_P_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 purely as an |
| 13 | // implementation detail. 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 <QtQuick3DRuntimeRender/private/qssgrendereffect_p.h> |
| 20 | #include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h> |
| 21 | #include <QtQuick3DRuntimeRender/private/qssgrendercommands_p.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | struct QSSGRhiEffectTexture; |
| 26 | class QSSGRenderer; |
| 27 | |
| 28 | class QSSGProgramGenerator; |
| 29 | class QSSGShaderLibraryManager; |
| 30 | class QSSGShaderCache; |
| 31 | |
| 32 | struct QSSGEffectSceneCacheKey |
| 33 | { |
| 34 | QByteArray m_shaderPathKey; |
| 35 | quintptr m_cmd; |
| 36 | int m_ubufIndex; |
| 37 | |
| 38 | size_t m_hashCode = 0; |
| 39 | |
| 40 | static size_t generateHashCode(const QByteArray &shaderPathKey, quintptr cmd, int ubufIndex) |
| 41 | { |
| 42 | return qHash(key: shaderPathKey) ^ qHash(key: cmd) ^ qHash(key: ubufIndex); |
| 43 | } |
| 44 | |
| 45 | void updateHashCode() |
| 46 | { |
| 47 | m_hashCode = generateHashCode(shaderPathKey: m_shaderPathKey, cmd: m_cmd, ubufIndex: m_ubufIndex); |
| 48 | } |
| 49 | |
| 50 | bool operator==(const QSSGEffectSceneCacheKey &other) const |
| 51 | { |
| 52 | return m_shaderPathKey == other.m_shaderPathKey |
| 53 | && m_cmd == other.m_cmd |
| 54 | && m_ubufIndex == other.m_ubufIndex; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | inline size_t qHash(const QSSGEffectSceneCacheKey &key) |
| 59 | { |
| 60 | return key.m_hashCode; |
| 61 | } |
| 62 | |
| 63 | class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiEffectSystem |
| 64 | { |
| 65 | public: |
| 66 | explicit QSSGRhiEffectSystem(const std::shared_ptr<QSSGRenderContextInterface> &sgContext); |
| 67 | ~QSSGRhiEffectSystem(); |
| 68 | |
| 69 | void setup(QSize outputSize); |
| 70 | QRhiTexture *process(const QSSGRenderLayer &layer, |
| 71 | QRhiTexture *inTexture, |
| 72 | QRhiTexture *inDepthTexture, |
| 73 | QVector2D cameraClipRange); |
| 74 | |
| 75 | static QSSGRenderTextureFormat::Format overriddenOutputFormat(const QSSGRenderEffect *inEffect); |
| 76 | |
| 77 | static QSSGRhiShaderPipelinePtr buildShaderForEffect(const QSSGBindShader &inCmd, |
| 78 | QSSGProgramGenerator &generator, |
| 79 | QSSGShaderLibraryManager &shaderLib, |
| 80 | QSSGShaderCache &shaderCache, |
| 81 | bool isYUpInFramebuffer, |
| 82 | int viewCount); |
| 83 | |
| 84 | private: |
| 85 | void releaseResources(); |
| 86 | QSSGRhiEffectTexture *doRenderEffect(const QSSGRenderEffect *inEffect, |
| 87 | QSSGRhiEffectTexture *inTexture, |
| 88 | quint8 viewCount); |
| 89 | |
| 90 | void allocateBufferCmd(const QSSGAllocateBuffer *inCmd, QSSGRhiEffectTexture *inTexture, const QSSGRenderEffect *inEffect, quint8 viewCount); |
| 91 | void applyInstanceValueCmd(const QSSGApplyInstanceValue *inCmd, const QSSGRenderEffect *inEffect); |
| 92 | void applyValueCmd(const QSSGApplyValue *inCmd, const QSSGRenderEffect *inEffect); |
| 93 | void bindShaderCmd(const QSSGBindShader *inCmd, const QSSGRenderEffect *inEffect, quint8 viewCount); |
| 94 | void renderCmd(QSSGRhiEffectTexture *inTexture, QSSGRhiEffectTexture *target, quint8 viewCount); |
| 95 | |
| 96 | void addCommonEffectUniforms(const QSize &inputSize, const QSize &outputSize); |
| 97 | void addTextureToShaderPipeline(const QByteArray &name, QRhiTexture *texture, const QSSGRhiSamplerDescription &samplerDesc); |
| 98 | |
| 99 | QSSGRhiEffectTexture *findTexture(const QByteArray &bufferName); |
| 100 | QSSGRhiEffectTexture *getTexture(const QByteArray &bufferName, const QSize &size, |
| 101 | QRhiTexture::Format format, bool isFinalOutput, |
| 102 | const QSSGRenderEffect *inEffect, quint8 viewCount); |
| 103 | void releaseTexture(QSSGRhiEffectTexture *texture); |
| 104 | void releaseTextures(); |
| 105 | |
| 106 | QSize m_outSize; |
| 107 | std::shared_ptr<QSSGRenderContextInterface> m_sgContext; |
| 108 | QVector<QSSGRhiEffectTexture *> m_textures; |
| 109 | QRhiTexture *m_depthTexture = nullptr; |
| 110 | QVector2D m_cameraClipRange; |
| 111 | int m_currentUbufIndex = 0; |
| 112 | QHash<QSSGEffectSceneCacheKey, QSSGRhiShaderPipelinePtr> m_shaderPipelines; |
| 113 | QSSGRhiShaderPipeline *m_currentShaderPipeline = nullptr; |
| 114 | char *m_currentUBufData = nullptr; |
| 115 | QHash<QByteArray, QSSGRhiTexture> m_currentTextures; |
| 116 | QSet<QRhiTextureRenderTarget *> m_pendingClears; |
| 117 | }; |
| 118 | |
| 119 | QT_END_NAMESPACE |
| 120 | |
| 121 | #endif |
| 122 | |