1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSPIRVSHADER_P_H
5#define QSPIRVSHADER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of a number of Qt sources files. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtShaderTools/private/qtshadertoolsglobal_p.h>
19#include <rhi/qshader.h>
20
21QT_BEGIN_NAMESPACE
22
23struct QSpirvShaderPrivate;
24
25class Q_SHADERTOOLS_PRIVATE_EXPORT QSpirvShader
26{
27public:
28 enum class GlslFlag {
29 GlslEs = 0x01,
30 FixClipSpace = 0x02,
31 FragDefaultMediump = 0x04
32 };
33 Q_DECLARE_FLAGS(GlslFlags, GlslFlag)
34
35 enum class MslFlag {
36 VertexAsCompute = 0x01,
37 WithUInt16Index = 0x02,
38 WithUInt32Index = 0x04
39 };
40 Q_DECLARE_FLAGS(MslFlags, MslFlag)
41
42 enum class RemapFlag {
43 StripOnly = 0x01
44 };
45 Q_DECLARE_FLAGS(RemapFlags, RemapFlag)
46
47 QSpirvShader();
48 ~QSpirvShader();
49
50 void setSpirvBinary(const QByteArray &spirv, QShader::Stage stage);
51
52 QShaderDescription shaderDescription() const;
53
54 QByteArray spirvBinary() const;
55 QByteArray remappedSpirvBinary(RemapFlags flags = RemapFlags(), QString *errorMessage = nullptr) const;
56
57 struct SeparateToCombinedImageSamplerMapping {
58 QByteArray textureName;
59 QByteArray samplerName;
60 QByteArray combinedSamplerName;
61 };
62
63 struct TessellationInfo {
64 // The tess.evaluation shader will likely declaer something like layout(triangles, fractional_odd_spacing, ccw) in;
65 // For Metal we need to know the tessellation mode (triangles) when generating the translated tess.control shader.
66 struct {
67 QShaderDescription::TessellationMode mode = QShaderDescription::TrianglesTessellationMode;
68 } infoForTesc;
69 // The tess.control shader will likely declare something like layout(vertices = 3) out;
70 // For Metal we need to know this value (3) when generating the translated tess.eval. shader.
71 struct {
72 int vertexCount = 3;
73 } infoForTese;
74 };
75
76 QByteArray translateToGLSL(int version,
77 GlslFlags flags,
78 QVector<SeparateToCombinedImageSamplerMapping> *separateToCombinedImageSamplerMappings) const;
79 QByteArray translateToHLSL(int version,
80 QShader::NativeResourceBindingMap *nativeBindings) const;
81 QByteArray translateToMSL(int version,
82 MslFlags flags,
83 QShader::Stage stage,
84 QShader::NativeResourceBindingMap *nativeBindings,
85 QShader::NativeShaderInfo *shaderInfo,
86 const TessellationInfo &tessInfo) const;
87 QString translationErrorMessage() const;
88
89private:
90 Q_DISABLE_COPY(QSpirvShader)
91 QSpirvShaderPrivate *d = nullptr;
92};
93
94Q_DECLARE_OPERATORS_FOR_FLAGS(QSpirvShader::GlslFlags)
95Q_DECLARE_OPERATORS_FOR_FLAGS(QSpirvShader::MslFlags)
96Q_DECLARE_OPERATORS_FOR_FLAGS(QSpirvShader::RemapFlags)
97
98QT_END_NAMESPACE
99
100#endif
101

source code of qtshadertools/src/shadertools/qspirvshader_p.h