Skip to content

Commit b72353e

Browse files
authored
Enable default cpp prelude. (shader-slang#1560)
* Enable default cpp prelude. * Print the "#include" line as a normal source if the file does not exist. * Bug fix * Fix. * Fix c++ prelude header. * Remove unnecessary fopen call.
1 parent 150218b commit b72353e

12 files changed

+176
-100
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ tests/**/*.slang-module
4242
/source/slang/hlsl.meta.slang.h
4343
/source/slang/core.meta.slang.h
4444
prelude/*.h.cpp
45+
/source/slang/cpp.hint

prelude/slang-cpp-prelude.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef SLANG_CPP_PRELUDE_H
22
#define SLANG_CPP_PRELUDE_H
33

4-
#include "../slang.h"
5-
64
// Because the signiture of isnan, isfinite, and is isinf changed in C++, we use the macro
75
// to use the version in the std namespace.
86
// https://stackoverflow.com/questions/39130040/cmath-hides-isnan-in-math-h-in-c14-c11
@@ -42,7 +40,7 @@
4240
#include "slang-cpp-scalar-intrinsics.h"
4341

4442
// TODO(JS): Hack! Output C++ code from slang can copy uninitialized variables.
45-
#if SLANG_VC
43+
#if defined(_MSC_VER)
4644
# pragma warning(disable : 4700)
4745
#endif
4846

@@ -66,4 +64,4 @@ gfx_PipelineState_0* buildPipelineState_0(gfx_ShaderProgram_0* _0, gfx_Renderer_
6664
void dispatchComputation_0(gfx_Renderer_0* _0, gfx_PipelineState_0* _1, gfx_PipelineLayout_0* _2, gfx_DescriptorSet_0* _3, uint32_t _4, uint32_t _5, uint32_t _6);
6765
gfx_BufferResource_0* unconvertBuffer_0(RWStructuredBuffer<float> _0);
6866

69-
#endif
67+
#endif

prelude/slang-cpp-scalar-intrinsics.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#ifndef SLANG_PRELUDE_SCALAR_INTRINSICS_H
22
#define SLANG_PRELUDE_SCALAR_INTRINSICS_H
33

4-
#include "../slang.h"
5-
64
#if SLANG_PROCESSOR_X86_64 && SLANG_VC
75
// If we have visual studio and 64 bit processor, we can assume we have popcnt, and can include x86 intrinsics
86
# include <intrin.h>
97
#endif
108

9+
#ifndef SLANG_FORCE_INLINE
10+
# define SLANG_FORCE_INLINE inline
11+
#endif
12+
1113
#ifdef SLANG_PRELUDE_NAMESPACE
1214
namespace SLANG_PRELUDE_NAMESPACE {
1315
#endif

prelude/slang-cpp-types.h

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef SLANG_PRELUDE_CPP_TYPES_H
22
#define SLANG_PRELUDE_CPP_TYPES_H
3-
4-
#include "../slang.h"
5-
3+
#include <stdint.h>
64
#ifndef SLANG_PRELUDE_ASSERT
75
# ifdef _DEBUG
86
# define SLANG_PRELUDE_ASSERT(VALUE) assert(VALUE)
@@ -11,6 +9,10 @@
119
# endif
1210
#endif
1311

12+
#ifndef SLANG_FORCE_INLINE
13+
# define SLANG_FORCE_INLINE inline
14+
#endif
15+
1416
#ifdef SLANG_PRELUDE_NAMESPACE
1517
namespace SLANG_PRELUDE_NAMESPACE {
1618
#endif
@@ -294,6 +296,42 @@ struct SamplerComparisonState
294296
ISamplerComparisonState* state;
295297
};
296298

299+
#ifndef SLANG_RESOURCE_SHAPE
300+
# define SLANG_RESOURCE_SHAPE
301+
typedef unsigned int SlangResourceShape;
302+
enum
303+
{
304+
SLANG_RESOURCE_BASE_SHAPE_MASK = 0x0F,
305+
306+
SLANG_RESOURCE_NONE = 0x00,
307+
308+
SLANG_TEXTURE_1D = 0x01,
309+
SLANG_TEXTURE_2D = 0x02,
310+
SLANG_TEXTURE_3D = 0x03,
311+
SLANG_TEXTURE_CUBE = 0x04,
312+
SLANG_TEXTURE_BUFFER = 0x05,
313+
314+
SLANG_STRUCTURED_BUFFER = 0x06,
315+
SLANG_BYTE_ADDRESS_BUFFER = 0x07,
316+
SLANG_RESOURCE_UNKNOWN = 0x08,
317+
SLANG_ACCELERATION_STRUCTURE = 0x09,
318+
319+
SLANG_RESOURCE_EXT_SHAPE_MASK = 0xF0,
320+
321+
SLANG_TEXTURE_FEEDBACK_FLAG = 0x10,
322+
SLANG_TEXTURE_ARRAY_FLAG = 0x40,
323+
SLANG_TEXTURE_MULTISAMPLE_FLAG = 0x80,
324+
325+
SLANG_TEXTURE_1D_ARRAY = SLANG_TEXTURE_1D | SLANG_TEXTURE_ARRAY_FLAG,
326+
SLANG_TEXTURE_2D_ARRAY = SLANG_TEXTURE_2D | SLANG_TEXTURE_ARRAY_FLAG,
327+
SLANG_TEXTURE_CUBE_ARRAY = SLANG_TEXTURE_CUBE | SLANG_TEXTURE_ARRAY_FLAG,
328+
329+
SLANG_TEXTURE_2D_MULTISAMPLE = SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG,
330+
SLANG_TEXTURE_2D_MULTISAMPLE_ARRAY =
331+
SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG | SLANG_TEXTURE_ARRAY_FLAG,
332+
};
333+
#endif
334+
297335
//
298336
struct TextureDimensions
299337
{
@@ -393,7 +431,7 @@ struct TextureDimensions
393431
}
394432
}
395433

396-
SlangResourceShape shape;
434+
uint32_t shape;
397435
uint32_t width, height, depth;
398436
uint32_t numberOfLevels;
399437
uint32_t arrayElementCount; ///< For array types, 0 otherwise

premake5.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ generatorProject("run-generators", "source/slang/")
901901
end
902902

903903
if executeBinary then
904-
filter "files:prelude/*.h"
904+
filter "files:prelude/*-prelude.h"
905905
buildmessage "slang-embed %{file.relpath}"
906906
buildcommands { '"%{cfg.targetdir}/slang-embed" %{file.relpath}' }
907907
buildoutputs { "%{file.abspath}.cpp" }
@@ -957,6 +957,7 @@ standardProject "slang"
957957
files {
958958
"prelude/slang-cuda-prelude.h.cpp",
959959
"prelude/slang-hlsl-prelude.h.cpp",
960+
"prelude/slang-cpp-prelude.h.cpp"
960961
}
961962

962963
--

slang.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,8 @@ extern "C"
18321832
SLANG_SCALAR_TYPE_UINT16,
18331833
};
18341834

1835+
#ifndef SLANG_RESOURCE_SHAPE
1836+
# define SLANG_RESOURCE_SHAPE
18351837
typedef unsigned int SlangResourceShape;
18361838
enum
18371839
{
@@ -1863,7 +1865,7 @@ extern "C"
18631865
SLANG_TEXTURE_2D_MULTISAMPLE = SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG,
18641866
SLANG_TEXTURE_2D_MULTISAMPLE_ARRAY = SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG | SLANG_TEXTURE_ARRAY_FLAG,
18651867
};
1866-
1868+
#endif
18671869
typedef unsigned int SlangResourceAccess;
18681870
enum
18691871
{

source/slang/run-generators.vcxproj

+4-26
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
<OptimizeReferences>true</OptimizeReferences>
154154
</Link>
155155
</ItemDefinitionGroup>
156+
<ItemGroup>
157+
<ClInclude Include="..\..\prelude\slang-cpp-scalar-intrinsics.h" />
158+
<ClInclude Include="..\..\prelude\slang-cpp-types.h" />
159+
</ItemGroup>
156160
<ItemGroup>
157161
<ClCompile Include="..\core\slang-string.cpp" />
158162
</ItemGroup>
@@ -170,32 +174,6 @@
170174
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">../../bin/windows-x86/release/slang-embed.exe</AdditionalInputs>
171175
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">../../bin/windows-x64/release/slang-embed.exe</AdditionalInputs>
172176
</CustomBuild>
173-
<CustomBuild Include="..\..\prelude\slang-cpp-scalar-intrinsics.h">
174-
<FileType>Document</FileType>
175-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"../../bin/windows-x86/debug/slang-embed" %(Identity)</Command>
176-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">"../../bin/windows-x64/debug/slang-embed" %(Identity)</Command>
177-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"../../bin/windows-x86/release/slang-embed" %(Identity)</Command>
178-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">"../../bin/windows-x64/release/slang-embed" %(Identity)</Command>
179-
<Outputs>../../prelude/slang-cpp-scalar-intrinsics.h.cpp</Outputs>
180-
<Message>slang-embed %(Identity)</Message>
181-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">../../bin/windows-x86/debug/slang-embed.exe</AdditionalInputs>
182-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">../../bin/windows-x64/debug/slang-embed.exe</AdditionalInputs>
183-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">../../bin/windows-x86/release/slang-embed.exe</AdditionalInputs>
184-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">../../bin/windows-x64/release/slang-embed.exe</AdditionalInputs>
185-
</CustomBuild>
186-
<CustomBuild Include="..\..\prelude\slang-cpp-types.h">
187-
<FileType>Document</FileType>
188-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"../../bin/windows-x86/debug/slang-embed" %(Identity)</Command>
189-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">"../../bin/windows-x64/debug/slang-embed" %(Identity)</Command>
190-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"../../bin/windows-x86/release/slang-embed" %(Identity)</Command>
191-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">"../../bin/windows-x64/release/slang-embed" %(Identity)</Command>
192-
<Outputs>../../prelude/slang-cpp-types.h.cpp</Outputs>
193-
<Message>slang-embed %(Identity)</Message>
194-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">../../bin/windows-x86/debug/slang-embed.exe</AdditionalInputs>
195-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">../../bin/windows-x64/debug/slang-embed.exe</AdditionalInputs>
196-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">../../bin/windows-x86/release/slang-embed.exe</AdditionalInputs>
197-
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">../../bin/windows-x64/release/slang-embed.exe</AdditionalInputs>
198-
</CustomBuild>
199177
<CustomBuild Include="..\..\prelude\slang-cuda-prelude.h">
200178
<FileType>Document</FileType>
201179
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"../../bin/windows-x86/debug/slang-embed" %(Identity)</Command>

source/slang/run-generators.vcxproj.filters

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<UniqueIdentifier>{E9C7FDCE-D52A-8D73-7EB0-C5296AF258F6}</UniqueIdentifier>
99
</Filter>
1010
</ItemGroup>
11+
<ItemGroup>
12+
<ClInclude Include="..\..\prelude\slang-cpp-scalar-intrinsics.h">
13+
<Filter>Header Files</Filter>
14+
</ClInclude>
15+
<ClInclude Include="..\..\prelude\slang-cpp-types.h">
16+
<Filter>Header Files</Filter>
17+
</ClInclude>
18+
</ItemGroup>
1119
<ItemGroup>
1220
<ClCompile Include="..\core\slang-string.cpp">
1321
<Filter>Source Files</Filter>
@@ -17,12 +25,6 @@
1725
<CustomBuild Include="..\..\prelude\slang-cpp-prelude.h">
1826
<Filter>Header Files</Filter>
1927
</CustomBuild>
20-
<CustomBuild Include="..\..\prelude\slang-cpp-scalar-intrinsics.h">
21-
<Filter>Header Files</Filter>
22-
</CustomBuild>
23-
<CustomBuild Include="..\..\prelude\slang-cpp-types.h">
24-
<Filter>Header Files</Filter>
25-
</CustomBuild>
2628
<CustomBuild Include="..\..\prelude\slang-cuda-prelude.h">
2729
<Filter>Header Files</Filter>
2830
</CustomBuild>

source/slang/slang.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
#undef NOMINMAX
4242
#endif
4343

44-
extern char const* slang_cuda_prelude;
45-
extern char const* slang_hlsl_prelude;
44+
extern Slang::String get_slang_cuda_prelude();
45+
extern Slang::String get_slang_cpp_prelude();
46+
extern Slang::String get_slang_hlsl_prelude();
4647

4748
namespace Slang {
4849

@@ -189,8 +190,9 @@ void Session::init()
189190
}
190191

191192
// Set up default prelude code for target languages that need a prelude
192-
m_languagePreludes[Index(SourceLanguage::CUDA)] = slang_cuda_prelude;
193-
m_languagePreludes[Index(SourceLanguage::HLSL)] = slang_hlsl_prelude;
193+
m_languagePreludes[Index(SourceLanguage::CUDA)] = get_slang_cuda_prelude();
194+
m_languagePreludes[Index(SourceLanguage::CPP)] = get_slang_cpp_prelude();
195+
m_languagePreludes[Index(SourceLanguage::HLSL)] = get_slang_hlsl_prelude();
194196
}
195197

196198
ISlangUnknown* Session::getInterface(const Guid& guid)

source/slang/slang.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
<ClInclude Include="slang-visitor.h" />
299299
</ItemGroup>
300300
<ItemGroup>
301+
<ClCompile Include="..\..\prelude\slang-cpp-prelude.h.cpp" />
301302
<ClCompile Include="..\..\prelude\slang-cuda-prelude.h.cpp" />
302303
<ClCompile Include="..\..\prelude\slang-hlsl-prelude.h.cpp" />
303304
<ClCompile Include="slang-ast-builder.cpp" />

source/slang/slang.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@
341341
</ClInclude>
342342
</ItemGroup>
343343
<ItemGroup>
344+
<ClCompile Include="..\..\prelude\slang-cpp-prelude.h.cpp">
345+
<Filter>Header Files</Filter>
346+
</ClCompile>
344347
<ClCompile Include="..\..\prelude\slang-cuda-prelude.h.cpp">
345348
<Filter>Header Files</Filter>
346349
</ClCompile>

0 commit comments

Comments
 (0)