This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSpirvCompiler.h
164 lines (119 loc) · 4.78 KB
/
SpirvCompiler.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2018-2020, Zhirnov Andrey. For more information see 'LICENSE'
#pragma once
#ifndef FG_ENABLE_GLSLANG
# error can`t compile glsl without glslang library!
#endif
#include "EShaderCompilationFlags.h"
#include "framegraph/Public/ResourceEnums.h"
#include "framegraph/Public/VertexEnums.h"
#include "glslang/Include/ResourceLimits.h"
class TIntermNode;
namespace glslang
{
class TType;
class TIntermediate;
}
namespace FG
{
//
// SPIRV Compiler
//
class SpirvCompiler final
{
// types
public:
struct ShaderReflection
{
// types
using TopologyBits_t = GraphicsPipelineDesc::TopologyBits_t;
using Shader = PipelineDescription::Shader;
using PipelineLayout = PipelineDescription::PipelineLayout;
using VertexAttribs_t = GraphicsPipelineDesc::VertexAttribs_t;
using FragmentOutputs_t = GraphicsPipelineDesc::FragmentOutputs_t;
using SpecConstants_t = PipelineDescription::SpecConstants_t;
// variables
Shader shader;
PipelineLayout layout;
SpecConstants_t specConstants;
struct {
TopologyBits_t supportedTopology;
VertexAttribs_t vertexAttribs;
} vertex;
struct {
uint patchControlPoints;
} tessellation;
struct {
FragmentOutputs_t fragmentOutput;
bool earlyFragmentTests = true;
} fragment;
struct {
uint3 localGroupSize;
uint3 localGroupSpecialization;
} compute;
struct {
uint3 taskGroupSize;
uint3 taskGroupSpecialization;
uint3 meshGroupSize;
uint3 meshGroupSpecialization;
EPrimitive topology = Default;
uint maxPrimitives = 0;
uint maxIndices = 0;
uint maxVertices = 0;
} mesh;
};
private:
struct GLSLangResult;
class ShaderIncluder;
// variables
private:
Array<String> const& _directories;
EShaderCompilationFlags _compilerFlags = Default;
glslang::TIntermediate * _intermediate = null;
EShaderStages _currentStage = Default;
bool _targetVulkan = true;
uint _spirvTraget = 0; // spv_target_env
struct {
bool shaderSubgroupClock = false;
bool shaderDeviceClock = false;
bool vertexPipelineStoresAndAtomics = false;
bool fragmentStoresAndAtomics = false;
} _features;
EShaderLangFormat _debugFlags = Default;
TBuiltInResource _builtinResource;
// methods
public:
explicit SpirvCompiler (const Array<String> &);
~SpirvCompiler ();
void SetCompilationFlags (EShaderCompilationFlags flags);
void SetDebugFlags (EShaderLangFormat flags);
void SetShaderClockFeatures (bool shaderSubgroupClock, bool shaderDeviceClock);
void SetShaderFeatures (bool vertexPipelineStoresAndAtomics, bool fragmentStoresAndAtomics);
bool SetDefaultResourceLimits ();
bool SetCurrentResourceLimits (PhysicalDeviceVk_t physicalDevice);
bool Compile (EShader shaderType, EShaderLangFormat srcShaderFmt, EShaderLangFormat dstShaderFmt,
NtStringView entry, NtStringView source, StringView debugName,
OUT PipelineDescription::Shader &outShader, OUT ShaderReflection &outReflection, OUT String &log);
private:
bool _ParseGLSL (EShader shaderType, EShaderLangFormat srcShaderFmt, EShaderLangFormat dstShaderFmt,
NtStringView entry, ArrayView<const char *> source, INOUT ShaderIncluder &includer,
OUT GLSLangResult &glslangData, INOUT String &log);
bool _CompileSPIRV (const GLSLangResult &glslangData, OUT Array<uint> &spirv, INOUT String &log) const;
bool _OptimizeSPIRV (INOUT Array<uint> &spirv, INOUT String &log) const;
bool _BuildReflection (const GLSLangResult &glslangData, OUT ShaderReflection &reflection);
bool _OnCompilationFailed (ArrayView<const char *> source, INOUT String &log) const;
bool _ParseAnnotations (StringView source, INOUT ShaderReflection &) const;
bool _CheckShaderFeatures (EShader shaderType) const;
static void _GenerateResources (OUT TBuiltInResource& res);
// GLSL deserializer
private:
bool _ProcessExternalObjects (TIntermNode* root, TIntermNode* node, INOUT ShaderReflection &result) const;
bool _DeserializeExternalObjects (TIntermNode* node, INOUT ShaderReflection &result) const;
bool _ProcessShaderInfo (INOUT ShaderReflection &result) const;
bool _CalculateStructSize (const glslang::TType &bufferType, OUT BytesU &staticSize, OUT BytesU &arrayStride, OUT BytesU &offset) const;
void _MergeWithGeometryInputPrimitive (INOUT GraphicsPipelineDesc::TopologyBits_t &topologyBits, /*TLayoutGeometry*/uint type) const;
ND_ BindingIndex _ToBindingIndex (uint index) const;
ND_ EImageSampler _ExtractImageType (const glslang::TType &type) const;
ND_ EVertexType _ExtractVertexType (const glslang::TType &type) const;
ND_ EFragOutput _ExtractFragmentOutputType (const glslang::TType &type) const;
};
} // FG