Skip to content

Commit 3fe4f53

Browse files
Checkmate50Tim Foley
and
Tim Foley
authored
Heterogeneous example (shader-slang#1399)
* Introduced heterogeneous example. Example includes C++ source and header files, and does not currently make use of the associated slang file when building. The intent of this commit is to introduce the example as a baseline for later updates as the heterogeneous model is expanded. * Changing namespace * Renamed and rewrote README * Updated example to account for compiler updates * Updated path Co-authored-by: Tim Foley <tfoleyNV@users.noreply.github.com>
1 parent ae41db8 commit 3fe4f53

File tree

10 files changed

+478
-0
lines changed

10 files changed

+478
-0
lines changed

examples/cpu-hello-world/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ using namespace Slang;
4040
#define SLANG_PRELUDE_NAMESPACE CPPPrelude
4141
#include "../../prelude/slang-cpp-types.h"
4242

43+
struct UniformState;
44+
4345
static SlangResult _innerMain(int argc, char** argv)
4446
{
4547
// First, we need to create a "session" for interacting with the Slang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Slang "CPU Hello World Heterogeneous" Example
2+
===============================
3+
4+
This example is a work-in-progress to illustrate how a heterogeneous programming example might work. It should NOT be used as a reference for working Slang code yet.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Debug|x64">
9+
<Configuration>Debug</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Release|Win32">
13+
<Configuration>Release</Configuration>
14+
<Platform>Win32</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{150CAA5A-0177-6A66-AA92-CFCB96DC2D49}</ProjectGuid>
23+
<IgnoreWarnCompileDuplicatedFilename>true</IgnoreWarnCompileDuplicatedFilename>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>heterogeneous-hello-world</RootNamespace>
26+
</PropertyGroup>
27+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
29+
<ConfigurationType>Application</ConfigurationType>
30+
<UseDebugLibraries>true</UseDebugLibraries>
31+
<CharacterSet>Unicode</CharacterSet>
32+
<PlatformToolset>v140</PlatformToolset>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
35+
<ConfigurationType>Application</ConfigurationType>
36+
<UseDebugLibraries>true</UseDebugLibraries>
37+
<CharacterSet>Unicode</CharacterSet>
38+
<PlatformToolset>v140</PlatformToolset>
39+
</PropertyGroup>
40+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
41+
<ConfigurationType>Application</ConfigurationType>
42+
<UseDebugLibraries>false</UseDebugLibraries>
43+
<CharacterSet>Unicode</CharacterSet>
44+
<PlatformToolset>v140</PlatformToolset>
45+
</PropertyGroup>
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
47+
<ConfigurationType>Application</ConfigurationType>
48+
<UseDebugLibraries>false</UseDebugLibraries>
49+
<CharacterSet>Unicode</CharacterSet>
50+
<PlatformToolset>v140</PlatformToolset>
51+
</PropertyGroup>
52+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
53+
<ImportGroup Label="ExtensionSettings">
54+
</ImportGroup>
55+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
56+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
57+
</ImportGroup>
58+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
59+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
60+
</ImportGroup>
61+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
62+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
63+
</ImportGroup>
64+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
65+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
66+
</ImportGroup>
67+
<PropertyGroup Label="UserMacros" />
68+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
69+
<LinkIncremental>true</LinkIncremental>
70+
<OutDir>..\..\bin\windows-x86\debug\</OutDir>
71+
<IntDir>..\..\intermediate\windows-x86\debug\heterogeneous-hello-world\</IntDir>
72+
<TargetName>heterogeneous-hello-world</TargetName>
73+
<TargetExt>.exe</TargetExt>
74+
</PropertyGroup>
75+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
76+
<LinkIncremental>true</LinkIncremental>
77+
<OutDir>..\..\bin\windows-x64\debug\</OutDir>
78+
<IntDir>..\..\intermediate\windows-x64\debug\heterogeneous-hello-world\</IntDir>
79+
<TargetName>heterogeneous-hello-world</TargetName>
80+
<TargetExt>.exe</TargetExt>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
83+
<LinkIncremental>false</LinkIncremental>
84+
<OutDir>..\..\bin\windows-x86\release\</OutDir>
85+
<IntDir>..\..\intermediate\windows-x86\release\heterogeneous-hello-world\</IntDir>
86+
<TargetName>heterogeneous-hello-world</TargetName>
87+
<TargetExt>.exe</TargetExt>
88+
</PropertyGroup>
89+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
90+
<LinkIncremental>false</LinkIncremental>
91+
<OutDir>..\..\bin\windows-x64\release\</OutDir>
92+
<IntDir>..\..\intermediate\windows-x64\release\heterogeneous-hello-world\</IntDir>
93+
<TargetName>heterogeneous-hello-world</TargetName>
94+
<TargetExt>.exe</TargetExt>
95+
</PropertyGroup>
96+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
97+
<ClCompile>
98+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
99+
<WarningLevel>Level3</WarningLevel>
100+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
101+
<AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
102+
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
103+
<Optimization>Disabled</Optimization>
104+
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
105+
</ClCompile>
106+
<Link>
107+
<SubSystem>Console</SubSystem>
108+
<GenerateDebugInformation>true</GenerateDebugInformation>
109+
</Link>
110+
</ItemDefinitionGroup>
111+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
112+
<ClCompile>
113+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
114+
<WarningLevel>Level3</WarningLevel>
115+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
116+
<AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
117+
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
118+
<Optimization>Disabled</Optimization>
119+
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
120+
</ClCompile>
121+
<Link>
122+
<SubSystem>Console</SubSystem>
123+
<GenerateDebugInformation>true</GenerateDebugInformation>
124+
</Link>
125+
</ItemDefinitionGroup>
126+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
127+
<ClCompile>
128+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
129+
<WarningLevel>Level3</WarningLevel>
130+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
131+
<AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
132+
<Optimization>Full</Optimization>
133+
<FunctionLevelLinking>true</FunctionLevelLinking>
134+
<IntrinsicFunctions>true</IntrinsicFunctions>
135+
<MinimalRebuild>false</MinimalRebuild>
136+
<StringPooling>true</StringPooling>
137+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
138+
</ClCompile>
139+
<Link>
140+
<SubSystem>Console</SubSystem>
141+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
142+
<OptimizeReferences>true</OptimizeReferences>
143+
</Link>
144+
</ItemDefinitionGroup>
145+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
146+
<ClCompile>
147+
<PrecompiledHeader>NotUsing</PrecompiledHeader>
148+
<WarningLevel>Level3</WarningLevel>
149+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
150+
<AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
151+
<Optimization>Full</Optimization>
152+
<FunctionLevelLinking>true</FunctionLevelLinking>
153+
<IntrinsicFunctions>true</IntrinsicFunctions>
154+
<MinimalRebuild>false</MinimalRebuild>
155+
<StringPooling>true</StringPooling>
156+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
157+
</ClCompile>
158+
<Link>
159+
<SubSystem>Console</SubSystem>
160+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
161+
<OptimizeReferences>true</OptimizeReferences>
162+
</Link>
163+
</ItemDefinitionGroup>
164+
<ItemGroup>
165+
<ClCompile Include="main.cpp" />
166+
<ClCompile Include="shader.cpp" />
167+
</ItemGroup>
168+
<ItemGroup>
169+
<None Include="shader.slang" />
170+
</ItemGroup>
171+
<ItemGroup>
172+
<ProjectReference Include="..\..\source\slang\slang.vcxproj">
173+
<Project>{DB00DA62-0533-4AFD-B59F-A67D5B3A0808}</Project>
174+
</ProjectReference>
175+
<ProjectReference Include="..\..\source\core\core.vcxproj">
176+
<Project>{F9BE7957-8399-899E-0C49-E714FDDD4B65}</Project>
177+
</ProjectReference>
178+
<ProjectReference Include="..\..\tools\gfx\gfx.vcxproj">
179+
<Project>{222F7498-B40C-4F3F-A704-DDEB91A4484A}</Project>
180+
</ProjectReference>
181+
</ItemGroup>
182+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
183+
<ImportGroup Label="ExtensionTargets">
184+
</ImportGroup>
185+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{E9C7FDCE-D52A-8D73-7EB0-C5296AF258F6}</UniqueIdentifier>
6+
</Filter>
7+
</ItemGroup>
8+
<ItemGroup>
9+
<ClCompile Include="main.cpp">
10+
<Filter>Source Files</Filter>
11+
</ClCompile>
12+
<ClCompile Include="shader.cpp">
13+
<Filter>Source Files</Filter>
14+
</ClCompile>
15+
</ItemGroup>
16+
<ItemGroup>
17+
<None Include="shader.slang">
18+
<Filter>Source Files</Filter>
19+
</None>
20+
</ItemGroup>
21+
</Project>
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// main.cpp
2+
3+
#include <stdio.h>
4+
5+
// This file implements an extremely simple example of loading and
6+
// executing a Slang shader program on the CPU.
7+
//
8+
// More information about generation C++ or CPU code can be found in docs/cpu-target.md
9+
//
10+
// NOTE! This test will only run on a system correctly where slang can find a suitable
11+
// C++ compiler - such as clang/gcc/visual studio
12+
//
13+
// The comments in the file will attempt to explain concepts as
14+
// they are introduced.
15+
//
16+
// Of course, in order to use the Slang API, we need to include
17+
// its header. We have set up the build options for this project
18+
// so that it is as simple as:
19+
#include <slang.h>
20+
21+
// Allows use of ComPtr - which we can use to scope any 'com-like' pointers easily
22+
#include <slang-com-ptr.h>
23+
// Provides macros for handling SlangResult values easily
24+
#include <slang-com-helper.h>
25+
26+
// This includes a useful small function for setting up the prelude (described more further below).
27+
#include "../../source/core/slang-test-tool-util.h"
28+
29+
// Slang namespace is used for elements support code (like core) which we use here
30+
// for ComPtr<> and TestToolUtil
31+
using namespace Slang;
32+
33+
// Slang source is converted into C++ code which is compiled by a backend compiler.
34+
// That process uses a 'prelude' which defines types and functions that are needed
35+
// for everything else to work.
36+
//
37+
// We include the prelude here, so we can directly use the types as were used by the
38+
// compiled code. It is not necessary to include the prelude, as long as memory is
39+
// laid out in the manner that the generated slang code expects.
40+
#define SLANG_PRELUDE_NAMESPACE CPPPrelude
41+
#include "../../prelude/slang-cpp-types.h"
42+
43+
struct UniformState
44+
{
45+
CPPPrelude::RWStructuredBuffer<float> ioBuffer;
46+
};
47+
48+
extern"C" void computeMain(CPPPrelude::ComputeVaryingInput* varyingInput, void* params, void* uniformState);
49+
50+
51+
static SlangResult _innerMain(int argc, char** argv)
52+
{
53+
54+
// the uniformState will be passed as a pointer to the CPU code
55+
UniformState uniformState;
56+
57+
// The contents of the buffer are modified, so we'll copy it
58+
const float startBufferContents[] = { 2.0f, -10.0f, -3.0f, 5.0f };
59+
float bufferContents[SLANG_COUNT_OF(startBufferContents)];
60+
memcpy(bufferContents, startBufferContents, sizeof(startBufferContents));
61+
62+
// Set up the ioBuffer such that it uses bufferContents. It is important to set the .count
63+
// such that bounds checking can be performed in the kernel.
64+
uniformState.ioBuffer.data = bufferContents;
65+
uniformState.ioBuffer.count = SLANG_COUNT_OF(bufferContents);
66+
67+
// In shader.slang, then entry point is attributed with `[numthreads(4, 1, 1)]` meaning each group
68+
// consists of 4 'thread' in x. Our input buffer is 4 wide, and we index the input array via `SV_DispatchThreadID`
69+
// so we only need to run a single group to execute over all of the 4 elements here.
70+
// The group range from { 0, 0, 0 } -> { 1, 1, 1 } means it will execute over the single group { 0, 0, 0 }.
71+
72+
const CPPPrelude::uint3 startGroupID = { 0, 0, 0};
73+
const CPPPrelude::uint3 endGroupID = { 1, 1, 1 };
74+
75+
CPPPrelude::ComputeVaryingInput varyingInput;
76+
varyingInput.startGroupID = startGroupID;
77+
varyingInput.endGroupID = endGroupID;
78+
79+
// We don't have any entry point parameters so that's passed as NULL
80+
// We need to cast our definition of the uniform state to the undefined CPPPrelude::UniformState as
81+
// that type is just a name to indicate what kind of thing needs to be passed in.
82+
computeMain(&varyingInput, NULL, (UniformState*)&uniformState);
83+
84+
// bufferContents holds the output
85+
86+
// Print out the values before the computation
87+
printf("Before:\n");
88+
for (float v : startBufferContents)
89+
{
90+
printf("%f, ", v);
91+
}
92+
printf("\n");
93+
94+
// Print out the values the the kernel produced
95+
printf("After: \n");
96+
for (float v : bufferContents)
97+
{
98+
printf("%f, ", v);
99+
}
100+
printf("\n");
101+
102+
return SLANG_OK;
103+
}
104+
105+
int main(int argc, char** argv)
106+
{
107+
return SLANG_SUCCEEDED(_innerMain(argc, argv)) ? 0 : -1;
108+
}

0 commit comments

Comments
 (0)