Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
VkCV Framework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vulkan2021
VkCV Framework
Merge requests
!115
Resolve "Slang Compiler"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Slang Compiler"
144-slang-compiler
into
develop
Overview
2
Commits
7
Pipelines
0
Changes
25
Merged
Tobias Frisch
requested to merge
144-slang-compiler
into
develop
1 year ago
Overview
2
Commits
7
Pipelines
0
Changes
1
Expand
Closes
#144 (closed)
0
0
Merge request reports
Compare
version 7
version 7
6fcadc36
6 months ago
version 6
f005b1a9
6 months ago
version 5
a7496fd7
8 months ago
version 4
b05076ca
8 months ago
version 3
cb72b64c
8 months ago
version 2
b6892647
1 year ago
version 1
c6b25451
1 year ago
develop (base)
and
latest version
latest version
c7b5f3c2
7 commits,
6 months ago
version 7
6fcadc36
6 commits,
6 months ago
version 6
f005b1a9
5 commits,
6 months ago
version 5
a7496fd7
4 commits,
8 months ago
version 4
b05076ca
3 commits,
8 months ago
version 3
cb72b64c
2 commits,
8 months ago
version 2
b6892647
3 commits,
1 year ago
version 1
c6b25451
1 commit,
1 year ago
Show latest version
1 file
+
17
−
3
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
modules/shader_compiler/src/vkcv/shader/SlangCompiler.cpp
0 → 100644
+
187
−
0
Options
#include
"vkcv/shader/SlangCompiler.hpp"
#include
<cstdint>
#include
<vkcv/File.hpp>
#include
<vkcv/Logger.hpp>
#include
<slang.h>
#include
<slang-com-ptr.h>
#include
<slang-com-helper.h>
#include
<vkcv/ShaderStage.hpp>
namespace
vkcv
::
shader
{
static
uint32_t
s_CompilerCount
=
0
;
static
Slang
::
ComPtr
<
slang
::
IGlobalSession
>
s_GlobalSession
;
SlangCompiler
::
SlangCompiler
(
SlangCompileProfile
profile
)
:
Compiler
(),
m_profile
(
profile
)
{
if
(
s_CompilerCount
==
0
)
{
slang
::
createGlobalSession
(
s_GlobalSession
.
writeRef
());
}
s_CompilerCount
++
;
}
SlangCompiler
::
SlangCompiler
(
const
SlangCompiler
&
other
)
:
Compiler
(
other
),
m_profile
(
other
.
m_profile
)
{
s_CompilerCount
++
;
}
SlangCompiler
::~
SlangCompiler
()
{
s_CompilerCount
--
;
}
SlangCompiler
&
SlangCompiler
::
operator
=
(
const
SlangCompiler
&
other
)
{
m_profile
=
other
.
m_profile
;
s_CompilerCount
++
;
return
*
this
;
}
constexpr
SlangStage
findShaderLanguage
(
ShaderStage
shaderStage
)
{
switch
(
shaderStage
)
{
case
ShaderStage
::
VERTEX
:
return
SlangStage
::
SLANG_STAGE_VERTEX
;
case
ShaderStage
::
TESS_CONTROL
:
return
SlangStage
::
SLANG_STAGE_HULL
;
case
ShaderStage
::
TESS_EVAL
:
return
SlangStage
::
SLANG_STAGE_DOMAIN
;
case
ShaderStage
::
GEOMETRY
:
return
SlangStage
::
SLANG_STAGE_GEOMETRY
;
case
ShaderStage
::
FRAGMENT
:
return
SlangStage
::
SLANG_STAGE_FRAGMENT
;
case
ShaderStage
::
COMPUTE
:
return
SlangStage
::
SLANG_STAGE_COMPUTE
;
case
ShaderStage
::
TASK
:
return
SlangStage
::
SLANG_STAGE_AMPLIFICATION
;
case
ShaderStage
::
MESH
:
return
SlangStage
::
SLANG_STAGE_MESH
;
case
ShaderStage
::
RAY_GEN
:
return
SlangStage
::
SLANG_STAGE_RAY_GENERATION
;
case
ShaderStage
::
RAY_CLOSEST_HIT
:
return
SlangStage
::
SLANG_STAGE_CLOSEST_HIT
;
case
ShaderStage
::
RAY_MISS
:
return
SlangStage
::
SLANG_STAGE_MISS
;
case
ShaderStage
::
RAY_INTERSECTION
:
return
SlangStage
::
SLANG_STAGE_INTERSECTION
;
case
ShaderStage
::
RAY_ANY_HIT
:
return
SlangStage
::
SLANG_STAGE_ANY_HIT
;
case
ShaderStage
::
RAY_CALLABLE
:
return
SlangStage
::
SLANG_STAGE_CALLABLE
;
default:
return
SlangStage
::
SLANG_STAGE_NONE
;
}
}
bool
SlangCompiler
::
compileSource
(
ShaderStage
shaderStage
,
const
std
::
string
&
shaderSource
,
const
ShaderCompiledFunction
&
compiled
,
const
std
::
filesystem
::
path
&
includePath
)
{
slang
::
SessionDesc
sessionDesc
=
{};
slang
::
TargetDesc
targetDesc
=
{};
SlangSourceLanguage
lang
;
targetDesc
.
format
=
SLANG_SPIRV
;
switch
(
m_profile
)
{
case
SlangCompileProfile
::
GLSL
:
targetDesc
.
profile
=
s_GlobalSession
->
findProfile
(
"glsl_460"
);
sessionDesc
.
defaultMatrixLayoutMode
=
SLANG_MATRIX_LAYOUT_COLUMN_MAJOR
;
sessionDesc
.
allowGLSLSyntax
=
true
;
lang
=
SLANG_SOURCE_LANGUAGE_GLSL
;
break
;
case
SlangCompileProfile
::
HLSL
:
targetDesc
.
profile
=
s_GlobalSession
->
findProfile
(
"sm_5_0"
);
sessionDesc
.
defaultMatrixLayoutMode
=
SLANG_MATRIX_LAYOUT_ROW_MAJOR
;
lang
=
SLANG_SOURCE_LANGUAGE_HLSL
;
break
;
case
SlangCompileProfile
::
SPIRV
:
targetDesc
.
profile
=
s_GlobalSession
->
findProfile
(
"spirv_1_5"
);
targetDesc
.
flags
=
SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY
;
lang
=
SLANG_SOURCE_LANGUAGE_SPIRV
;
break
;
default:
lang
=
SLANG_SOURCE_LANGUAGE_UNKNOWN
;
break
;
}
sessionDesc
.
targets
=
&
targetDesc
;
sessionDesc
.
targetCount
=
1
;
const
char
*
searchPath
=
includePath
.
c_str
();
sessionDesc
.
searchPaths
=
&
searchPath
;
sessionDesc
.
searchPathCount
=
1
;
std
::
vector
<
slang
::
PreprocessorMacroDesc
>
macros
;
macros
.
reserve
(
m_defines
.
size
());
for
(
const
auto
&
define
:
m_defines
)
{
const
slang
::
PreprocessorMacroDesc
macro
=
{
define
.
first
.
c_str
(),
define
.
second
.
c_str
()
};
macros
.
push_back
(
macro
);
}
sessionDesc
.
preprocessorMacros
=
macros
.
data
();
sessionDesc
.
preprocessorMacroCount
=
macros
.
size
();
Slang
::
ComPtr
<
slang
::
ISession
>
session
;
if
(
SLANG_FAILED
(
s_GlobalSession
->
createSession
(
sessionDesc
,
session
.
writeRef
())))
{
vkcv_log
(
LogLevel
::
ERROR
,
"Compiler session could not be created"
);
return
false
;
}
Slang
::
ComPtr
<
slang
::
ICompileRequest
>
request
;
if
(
SLANG_FAILED
(
session
->
createCompileRequest
(
request
.
writeRef
())))
{
vkcv_log
(
LogLevel
::
ERROR
,
"Compilation request could not be created"
);
return
false
;
}
const
int
translationUnit
=
request
->
addTranslationUnit
(
lang
,
nullptr
);
request
->
addTranslationUnitSourceString
(
translationUnit
,
nullptr
,
shaderSource
.
c_str
());
const
int
entryPoint
=
request
->
addEntryPoint
(
translationUnit
,
"main"
,
findShaderLanguage
(
shaderStage
)
);
if
(
SLANG_FAILED
(
request
->
compile
()))
{
vkcv_log
(
LogLevel
::
ERROR
,
"Compilation process failed"
);
return
false
;
}
size_t
size
;
const
void
*
code
=
request
->
getEntryPointCode
(
entryPoint
,
&
size
);
if
(
0
==
size
)
{
code
=
request
->
getCompileRequestCode
(
&
size
);
}
if
((
0
==
size
)
||
(
!
code
))
{
vkcv_log
(
LogLevel
::
ERROR
,
"Entry point could not be found
\n
%s"
,
request
->
getDiagnosticOutput
());
return
false
;
}
std
::
vector
<
uint32_t
>
spirv
;
spirv
.
resize
(
size
/
sizeof
(
uint32_t
));
memcpy
(
spirv
.
data
(),
code
,
spirv
.
size
()
*
sizeof
(
uint32_t
));
const
std
::
filesystem
::
path
tmp_path
=
generateTemporaryFilePath
();
if
(
!
writeBinaryToFile
(
tmp_path
,
spirv
))
{
vkcv_log
(
LogLevel
::
ERROR
,
"Spir-V could not be written to disk"
);
return
false
;
}
if
(
compiled
)
{
compiled
(
shaderStage
,
tmp_path
);
}
std
::
filesystem
::
remove
(
tmp_path
);
return
true
;
}
}
Loading