summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/cpu-hello-world/main.cpp2
-rw-r--r--examples/heterogeneous-hello-world/README.md4
-rw-r--r--examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj185
-rw-r--r--examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj.filters21
-rw-r--r--examples/heterogeneous-hello-world/main.cpp108
-rw-r--r--examples/heterogeneous-hello-world/shader.cpp126
-rw-r--r--examples/heterogeneous-hello-world/shader.slang16
7 files changed, 462 insertions, 0 deletions
diff --git a/examples/cpu-hello-world/main.cpp b/examples/cpu-hello-world/main.cpp
index 8f22aac6d..898347b47 100644
--- a/examples/cpu-hello-world/main.cpp
+++ b/examples/cpu-hello-world/main.cpp
@@ -40,6 +40,8 @@ using namespace Slang;
#define SLANG_PRELUDE_NAMESPACE CPPPrelude
#include "../../prelude/slang-cpp-types.h"
+struct UniformState;
+
static SlangResult _innerMain(int argc, char** argv)
{
// First, we need to create a "session" for interacting with the Slang
diff --git a/examples/heterogeneous-hello-world/README.md b/examples/heterogeneous-hello-world/README.md
new file mode 100644
index 000000000..709652922
--- /dev/null
+++ b/examples/heterogeneous-hello-world/README.md
@@ -0,0 +1,4 @@
+Slang "CPU Hello World Heterogeneous" Example
+===============================
+
+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. \ No newline at end of file
diff --git a/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj b/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj
new file mode 100644
index 000000000..d80fbc30b
--- /dev/null
+++ b/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj
@@ -0,0 +1,185 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{150CAA5A-0177-6A66-AA92-CFCB96DC2D49}</ProjectGuid>
+ <IgnoreWarnCompileDuplicatedFilename>true</IgnoreWarnCompileDuplicatedFilename>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>heterogeneous-hello-world</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ <PlatformToolset>v140</PlatformToolset>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>true</LinkIncremental>
+ <OutDir>..\..\bin\windows-x86\debug\</OutDir>
+ <IntDir>..\..\intermediate\windows-x86\debug\heterogeneous-hello-world\</IntDir>
+ <TargetName>heterogeneous-hello-world</TargetName>
+ <TargetExt>.exe</TargetExt>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <LinkIncremental>true</LinkIncremental>
+ <OutDir>..\..\bin\windows-x64\debug\</OutDir>
+ <IntDir>..\..\intermediate\windows-x64\debug\heterogeneous-hello-world\</IntDir>
+ <TargetName>heterogeneous-hello-world</TargetName>
+ <TargetExt>.exe</TargetExt>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ <OutDir>..\..\bin\windows-x86\release\</OutDir>
+ <IntDir>..\..\intermediate\windows-x86\release\heterogeneous-hello-world\</IntDir>
+ <TargetName>heterogeneous-hello-world</TargetName>
+ <TargetExt>.exe</TargetExt>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ <OutDir>..\..\bin\windows-x64\release\</OutDir>
+ <IntDir>..\..\intermediate\windows-x64\release\heterogeneous-hello-world\</IntDir>
+ <TargetName>heterogeneous-hello-world</TargetName>
+ <TargetExt>.exe</TargetExt>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
+ <Optimization>Disabled</Optimization>
+ <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
+ <Optimization>Disabled</Optimization>
+ <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <Optimization>Full</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <MinimalRebuild>false</MinimalRebuild>
+ <StringPooling>true</StringPooling>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <AdditionalIncludeDirectories>..\..;..\..\tools;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <Optimization>Full</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <MinimalRebuild>false</MinimalRebuild>
+ <StringPooling>true</StringPooling>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="main.cpp" />
+ <ClCompile Include="shader.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="shader.slang" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\..\source\slang\slang.vcxproj">
+ <Project>{DB00DA62-0533-4AFD-B59F-A67D5B3A0808}</Project>
+ </ProjectReference>
+ <ProjectReference Include="..\..\source\core\core.vcxproj">
+ <Project>{F9BE7957-8399-899E-0C49-E714FDDD4B65}</Project>
+ </ProjectReference>
+ <ProjectReference Include="..\..\tools\gfx\gfx.vcxproj">
+ <Project>{222F7498-B40C-4F3F-A704-DDEB91A4484A}</Project>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project> \ No newline at end of file
diff --git a/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj.filters b/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj.filters
new file mode 100644
index 000000000..0d3970688
--- /dev/null
+++ b/examples/heterogeneous-hello-world/heterogeneous-hello-world.vcxproj.filters
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{E9C7FDCE-D52A-8D73-7EB0-C5296AF258F6}</UniqueIdentifier>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="main.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="shader.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="shader.slang">
+ <Filter>Source Files</Filter>
+ </None>
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/examples/heterogeneous-hello-world/main.cpp b/examples/heterogeneous-hello-world/main.cpp
new file mode 100644
index 000000000..c6475e6f5
--- /dev/null
+++ b/examples/heterogeneous-hello-world/main.cpp
@@ -0,0 +1,108 @@
+// main.cpp
+
+#include <stdio.h>
+
+// This file implements an extremely simple example of loading and
+// executing a Slang shader program on the CPU.
+//
+// More information about generation C++ or CPU code can be found in docs/cpu-target.md
+//
+// NOTE! This test will only run on a system correctly where slang can find a suitable
+// C++ compiler - such as clang/gcc/visual studio
+//
+// The comments in the file will attempt to explain concepts as
+// they are introduced.
+//
+// Of course, in order to use the Slang API, we need to include
+// its header. We have set up the build options for this project
+// so that it is as simple as:
+#include <slang.h>
+
+// Allows use of ComPtr - which we can use to scope any 'com-like' pointers easily
+#include <slang-com-ptr.h>
+// Provides macros for handling SlangResult values easily
+#include <slang-com-helper.h>
+
+// This includes a useful small function for setting up the prelude (described more further below).
+#include "../../source/core/slang-test-tool-util.h"
+
+// Slang namespace is used for elements support code (like core) which we use here
+// for ComPtr<> and TestToolUtil
+using namespace Slang;
+
+// Slang source is converted into C++ code which is compiled by a backend compiler.
+// That process uses a 'prelude' which defines types and functions that are needed
+// for everything else to work.
+//
+// We include the prelude here, so we can directly use the types as were used by the
+// compiled code. It is not necessary to include the prelude, as long as memory is
+// laid out in the manner that the generated slang code expects.
+#define SLANG_PRELUDE_NAMESPACE CPPPrelude
+#include "../../prelude/slang-cpp-types.h"
+
+struct UniformState
+{
+ CPPPrelude::RWStructuredBuffer<float> ioBuffer;
+};
+
+extern"C" void computeMain(CPPPrelude::ComputeVaryingInput* varyingInput, void* params, void* uniformState);
+
+
+static SlangResult _innerMain(int argc, char** argv)
+{
+
+ // the uniformState will be passed as a pointer to the CPU code
+ UniformState uniformState;
+
+ // The contents of the buffer are modified, so we'll copy it
+ const float startBufferContents[] = { 2.0f, -10.0f, -3.0f, 5.0f };
+ float bufferContents[SLANG_COUNT_OF(startBufferContents)];
+ memcpy(bufferContents, startBufferContents, sizeof(startBufferContents));
+
+ // Set up the ioBuffer such that it uses bufferContents. It is important to set the .count
+ // such that bounds checking can be performed in the kernel.
+ uniformState.ioBuffer.data = bufferContents;
+ uniformState.ioBuffer.count = SLANG_COUNT_OF(bufferContents);
+
+ // In shader.slang, then entry point is attributed with `[numthreads(4, 1, 1)]` meaning each group
+ // consists of 4 'thread' in x. Our input buffer is 4 wide, and we index the input array via `SV_DispatchThreadID`
+ // so we only need to run a single group to execute over all of the 4 elements here.
+ // The group range from { 0, 0, 0 } -> { 1, 1, 1 } means it will execute over the single group { 0, 0, 0 }.
+
+ const CPPPrelude::uint3 startGroupID = { 0, 0, 0};
+ const CPPPrelude::uint3 endGroupID = { 1, 1, 1 };
+
+ CPPPrelude::ComputeVaryingInput varyingInput;
+ varyingInput.startGroupID = startGroupID;
+ varyingInput.endGroupID = endGroupID;
+
+ // We don't have any entry point parameters so that's passed as NULL
+ // We need to cast our definition of the uniform state to the undefined CPPPrelude::UniformState as
+ // that type is just a name to indicate what kind of thing needs to be passed in.
+ computeMain(&varyingInput, NULL, (UniformState*)&uniformState);
+
+ // bufferContents holds the output
+
+ // Print out the values before the computation
+ printf("Before:\n");
+ for (float v : startBufferContents)
+ {
+ printf("%f, ", v);
+ }
+ printf("\n");
+
+ // Print out the values the the kernel produced
+ printf("After: \n");
+ for (float v : bufferContents)
+ {
+ printf("%f, ", v);
+ }
+ printf("\n");
+
+ return SLANG_OK;
+}
+
+int main(int argc, char** argv)
+{
+ return SLANG_SUCCEEDED(_innerMain(argc, argv)) ? 0 : -1;
+}
diff --git a/examples/heterogeneous-hello-world/shader.cpp b/examples/heterogeneous-hello-world/shader.cpp
new file mode 100644
index 000000000..396b78cb4
--- /dev/null
+++ b/examples/heterogeneous-hello-world/shader.cpp
@@ -0,0 +1,126 @@
+#include "../../prelude/slang-cpp-prelude.h"
+
+
+namespace { // anonymous
+
+#ifdef SLANG_PRELUDE_NAMESPACE
+using namespace SLANG_PRELUDE_NAMESPACE;
+#endif
+
+struct KernelContext;
+
+
+#line 13 "shader.slang"
+struct UniformState
+{
+
+#line 4
+ RWStructuredBuffer<float> ioBuffer_0;
+
+
+
+};
+
+struct KernelContext
+{
+ UniformState* uniformState;
+ uint3 dispatchThreadID;
+ uint3 groupID;
+ uint3 groupDispatchThreadID;
+ uint3 calcGroupThreadID() const
+ {
+ uint3 v = { dispatchThreadID.x - groupDispatchThreadID.x, dispatchThreadID.y - groupDispatchThreadID.y, dispatchThreadID.z - groupDispatchThreadID.z };
+ return v;
+ }
+
+#line 8
+ void _computeMain()
+
+ {
+
+#line 10
+ uint32_t tid_0 = dispatchThreadID.x;
+
+ float i_0 = (uniformState->ioBuffer_0)[tid_0];
+ bool _S1 = i_0 < 0.50000000000000000000f;
+
+#line 13
+ float _S2 = i_0 + i_0;
+
+#line 13
+ float _S3 = (F32_sqrt((i_0)));
+
+#line 13
+ float o_0 = _S1 ? _S2 : _S3;
+
+ (uniformState->ioBuffer_0)[tid_0] = o_0;
+
+#line 8
+ return;
+ }
+
+};
+
+} // anonymous
+
+// [numthreads(4, 1, 1)]
+SLANG_PRELUDE_EXPORT
+void computeMain_Thread(ComputeThreadVaryingInput* varyingInput, void* params, void* uniformState)
+{
+ KernelContext context = {};
+ context.uniformState = (UniformState*)uniformState;
+ context.dispatchThreadID = {
+ varyingInput->groupID.x * 4 + varyingInput->groupThreadID.x,
+ varyingInput->groupID.y * 1 + varyingInput->groupThreadID.y,
+ varyingInput->groupID.z * 1 + varyingInput->groupThreadID.z
+ };
+ context._computeMain();
+}
+// [numthreads(4, 1, 1)]
+SLANG_PRELUDE_EXPORT
+void computeMain_Group(ComputeVaryingInput* varyingInput, void* params, void* uniformState)
+{
+ KernelContext context = {};
+ context.uniformState = (UniformState*)uniformState;
+ const uint3 start = {
+ varyingInput->startGroupID.x * 4,
+ varyingInput->startGroupID.y * 1,
+ varyingInput->startGroupID.z * 1
+ };
+ context.dispatchThreadID = start;
+ for (uint32_t x = start.x; x < start.x + 4; ++x)
+ {
+ context.dispatchThreadID.x = x;
+ context._computeMain();
+ }
+}
+// [numthreads(4, 1, 1)]
+SLANG_PRELUDE_EXPORT
+void computeMain(ComputeVaryingInput* varyingInput, void* params, void* uniformState)
+{
+ KernelContext context = {};
+ context.uniformState = (UniformState*)uniformState;
+ const uint3 start = {
+ varyingInput->startGroupID.x * 4,
+ varyingInput->startGroupID.y * 1,
+ varyingInput->startGroupID.z * 1
+ };
+ const uint3 end = {
+ varyingInput->endGroupID.x * 4,
+ varyingInput->endGroupID.y * 1,
+ varyingInput->endGroupID.z * 1
+ };
+ for (uint32_t z = start.z; z < end.z; ++z)
+ {
+ context.dispatchThreadID.z = z;
+ for (uint32_t y = start.y; y < end.y; ++y)
+ {
+ context.dispatchThreadID.y = y;
+ for (uint32_t x = start.x; x < end.x; ++x)
+ {
+ context.dispatchThreadID.x = x;
+ context._computeMain();
+ }
+ }
+ }
+}
diff --git a/examples/heterogeneous-hello-world/shader.slang b/examples/heterogeneous-hello-world/shader.slang
new file mode 100644
index 000000000..f650c3481
--- /dev/null
+++ b/examples/heterogeneous-hello-world/shader.slang
@@ -0,0 +1,16 @@
+// shader.slang
+
+//TEST_INPUT:ubuffer(random(float, 4096, -1.0, 1.0), stride=4):name=ioBuffer
+RWStructuredBuffer<float> ioBuffer;
+
+[shader("compute")]
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ uint tid = dispatchThreadID.x;
+
+ float i = ioBuffer[tid];
+ float o = i < 0.5 ? (i + i) : sqrt(i);
+
+ ioBuffer[tid] = o;
+}