summaryrefslogtreecommitdiffstats
path: root/src/common/utils/commonutils.cpp
blob: 348fb6634e66ba04a46475b5d95e12a9445769f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(opengl)
#include <QtGui/qoffscreensurface.h>
#endif

#include "commonutils_p.h"

#include <rhi/qrhi.h>

QT_BEGIN_NAMESPACE

static qreal s_maxTextureSize = 0.;

Q_LOGGING_CATEGORY(lcGraphsUtils, "qt.graphs.common.utils")

qreal CommonUtils::maxTextureSize()
{
    // Query maximum texture size only once
    if (!s_maxTextureSize) {
#if QT_CONFIG(opengl)
        std::unique_ptr<QSurface> surfacePtr;
#endif
        std::unique_ptr<QRhi> rhi;
#if defined(Q_OS_WIN)
        QRhiD3D12InitParams params;
        rhi.reset(QRhi::create(QRhi::D3D12, &params));
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
        QRhiMetalInitParams params;
        rhi.reset(QRhi::create(QRhi::Metal, &params));
#elif QT_CONFIG(opengl)
        QRhiGles2InitParams params;
        surfacePtr.reset(QRhiGles2InitParams::newFallbackSurface());
        params.fallbackSurface = surfacePtr.get();
        rhi.reset(QRhi::create(QRhi::OpenGLES2, &params));
#elif QT_CONFIG(vulkan)
        if (!qEnvironmentVariable("QSG_RHI_BACKEND").compare("vulkan")) {
            QVulkanInstance inst;
            inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions());
            if (inst.create()) {
                QRhiVulkanInitParams params;
                params.inst = &inst;
                rhi.reset(QRhi::create(QRhi::Vulkan, &params));
            } else {
                qCWarning(lcGraphsUtils, "Failed to create Vulkan instance");
            }
        }
#endif
        if (rhi)
            s_maxTextureSize = qreal(rhi->resourceLimit(QRhi::TextureSizeMax));
        else
            s_maxTextureSize = gradientTextureWidth;
    }

    return s_maxTextureSize;
}

QT_END_NAMESPACE