├── BeginnersTutorial-Part1 ├── images │ ├── image1.png │ └── image2.png ├── MySharpDXGame │ ├── Program.cs │ ├── packages.config │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ └── MySharpDXGame.csproj ├── MySharpDXGame.sln └── README.md ├── BeginnersTutorial-Part2 ├── images │ ├── image1.png │ └── image2.png ├── MySharpDXGame │ ├── Program.cs │ ├── packages.config │ ├── App.config │ ├── Game.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── MySharpDXGame.csproj ├── MySharpDXGame.sln └── README.md ├── BeginnersTutorial-Part3 ├── images │ └── image1.png ├── MySharpDXGame │ ├── Program.cs │ ├── packages.config │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Game.cs │ └── MySharpDXGame.csproj ├── MySharpDXGame.sln └── README.md ├── BeginnersTutorial-Part4 ├── images │ ├── image1.png │ ├── image2.png │ ├── image3.png │ ├── image4.png │ └── image5.png ├── MySharpDXGame │ ├── vertexShader.hlsl │ ├── pixelShader.hlsl │ ├── Program.cs │ ├── packages.config │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── MySharpDXGame.csproj │ └── Game.cs ├── MySharpDXGame.sln └── README.md ├── BeginnersTutorial-Part5 ├── images │ ├── Result.png │ ├── AddNewFile.png │ └── NewFileDialog.png ├── MySharpDXGame │ ├── pixelShader.hlsl │ ├── vertexShader.hlsl │ ├── Program.cs │ ├── VertexPositionColor.cs │ ├── packages.config │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── MySharpDXGame.csproj │ └── Game.cs ├── MySharpDXGame.sln └── README.md ├── README.md └── .gitignore /BeginnersTutorial-Part1/images/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part1/images/image1.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/images/image2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part1/images/image2.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/images/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part2/images/image1.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/images/image2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part2/images/image2.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/images/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part3/images/image1.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/images/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part4/images/image1.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/images/image2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part4/images/image2.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/images/image3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part4/images/image3.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/images/image4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part4/images/image4.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/images/image5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part4/images/image5.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/images/Result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part5/images/Result.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/vertexShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 main(float4 position : POSITION) : SV_POSITION 2 | { 3 | return position; 4 | } -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/images/AddNewFile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part5/images/AddNewFile.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/images/NewFileDialog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrjfalk/SharpDXTutorials/HEAD/BeginnersTutorial-Part5/images/NewFileDialog.png -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/pixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 main(float4 position : SV_POSITION) : SV_TARGET 2 | { 3 | return float4(1.0, 0.0, 0.0, 1.0); 4 | } -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/pixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET 2 | { 3 | return color; 4 | } -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MySharpDXGame 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/vertexShader.hlsl: -------------------------------------------------------------------------------- 1 | struct VSOut 2 | { 3 | float4 position : SV_POSITION; 4 | float4 color : COLOR; 5 | }; 6 | 7 | VSOut main(float4 position : POSITION, float4 color : COLOR) 8 | { 9 | VSOut output; 10 | output.position = position; 11 | output.color = color; 12 | 13 | return output; 14 | } -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MySharpDXGame 8 | { 9 | class Program 10 | { 11 | [STAThread] 12 | static void Main(string[] args) 13 | { 14 | using(Game game = new Game()) 15 | { 16 | game.Run(); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MySharpDXGame 8 | { 9 | class Program 10 | { 11 | [STAThread] 12 | static void Main(string[] args) 13 | { 14 | using(Game game = new Game()) 15 | { 16 | game.Run(); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MySharpDXGame 8 | { 9 | class Program 10 | { 11 | [STAThread] 12 | static void Main(string[] args) 13 | { 14 | using(Game game = new Game()) 15 | { 16 | game.Run(); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MySharpDXGame 8 | { 9 | class Program 10 | { 11 | [STAThread] 12 | static void Main(string[] args) 13 | { 14 | using(Game game = new Game()) 15 | { 16 | game.Run(); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/VertexPositionColor.cs: -------------------------------------------------------------------------------- 1 | using SharpDX; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace MySharpDXGame 5 | { 6 | [StructLayoutAttribute(LayoutKind.Sequential)] 7 | public struct VertexPositionColor 8 | { 9 | public readonly Vector3 Position; 10 | public readonly Color4 Color; 11 | 12 | public VertexPositionColor(Vector3 position, Color4 color) 13 | { 14 | Position = position; 15 | Color = color; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/Game.cs: -------------------------------------------------------------------------------- 1 | using SharpDX.Windows; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace MySharpDXGame 10 | { 11 | public class Game : IDisposable 12 | { 13 | private RenderForm renderForm; 14 | 15 | private const int Width = 1280; 16 | private const int Height = 720; 17 | 18 | public Game() 19 | { 20 | renderForm = new RenderForm("My first SharpDX game"); 21 | renderForm.ClientSize = new Size(Width, Height); 22 | renderForm.AllowUserResizing = false; 23 | } 24 | 25 | public void Run() 26 | { 27 | RenderLoop.Run(renderForm, RenderCallback); 28 | } 29 | 30 | private void RenderCallback() 31 | { 32 | 33 | } 34 | 35 | public void Dispose() 36 | { 37 | renderForm.Dispose(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySharpDXGame", "MySharpDXGame\MySharpDXGame.csproj", "{F2267781-DCFF-485E-BA8A-62880CA8D8DA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySharpDXGame", "MySharpDXGame\MySharpDXGame.csproj", "{F2267781-DCFF-485E-BA8A-62880CA8D8DA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySharpDXGame", "MySharpDXGame\MySharpDXGame.csproj", "{F2267781-DCFF-485E-BA8A-62880CA8D8DA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySharpDXGame", "MySharpDXGame\MySharpDXGame.csproj", "{F2267781-DCFF-485E-BA8A-62880CA8D8DA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySharpDXGame", "MySharpDXGame\MySharpDXGame.csproj", "{F2267781-DCFF-485E-BA8A-62880CA8D8DA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SharpDXTutorials 2 | Code for my SharpDX Tutorials at www.johanfalk.eu. 3 | 4 | ## Beginners Tutorial 5 | The beginners tutorial describes the basics of SharpDX, starting with setting up a project in Visual Studio. 6 | 7 | ### Part 1: Setting up a SharpDX project in Visual Studio 2013 8 | In the first part we create the Visual Studio project, and adding the required NuGet packages. Read the full tutorial [here](BeginnersTutorial-Part1/README.md). 9 | 10 | ### Part 2: Creating a window 11 | In the second tutorial we create a window which we will later render in. Read tutorial [here](BeginnersTutorial-Part2/README.md). 12 | 13 | ### Part 3: Initializing DirectX 14 | In this tutorial we initialize DirectX. We set up the swap chain and a backbuffer to render to and finally clear the backbuffer into a nice blue color. Read tutorial [here](BeginnersTutorial-Part3/README.md). 15 | 16 | ### Part 4: Drawing a triangle 17 | Finally time to draw something else than a blue screen. We draw a simple triangle from three vertices and add a simple vertex and pixel shader. Read tutorial [here](BeginnersTutorial-Part4/README.md). 18 | 19 | ### Part 5: Coloring the triangle 20 | In this tutorial we will look at setting a specific color for each vertex. The tutorial is available [here](BeginnersTutorial-Part5/README.md). -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/README.md: -------------------------------------------------------------------------------- 1 | # SharpDX Beginners Tutorial Part 1: Setting up a SharpDX project in Visual Studio 2013 2 | 3 | This is my first tutorial for using SharpDX. SharpDX allows you to render graphics using DirectX and C#. I recently started using SharpDX but there is not too much documentation and tutorials out there compared to other options, therefor I will be writing those tutorial as I go along. 4 | 5 | Note: For those familiar with SharpDX you might know that it exists a toolkit to get a quick setup to get started, which gives a Game class similar to what is found in XNA. In those tutorials I will not be using the Toolkit. 6 | 7 | In this tutorial we will not write any code, simply set up a new project in Visual Studio and add the required references. 8 | 9 | 1. The first step is to create a new Console Application and give it a name: 10 | Create new project 11 | 12 | ![](images/image1.png) 13 | 14 | 2. Next we need to add the references to SharpDX, for which we will use NuGet. Right click on “References” in the Solution Explorer and choose “Manage NuGet Packages…” 15 | 16 | 3. Search for “SharpDX”. For now we will install those named “SharpDX”, “SharpDX.Direct3D11”, “SharpDX.DXGI”, and “SharpDX.D3DCompiler”. Now if you choose “Installed Packages” it should look like this: 17 | NuGet packages 18 | As you can see, there are many SharpDX packages available on NuGet, those we added are required to render a simple scene and compile our shaders. 19 | 20 | ![](images/image2.png) 21 | 22 | We now have a correct project set up, in the next tutorial we will look at creating a simple window. -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MySharpDXGame")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MySharpDXGame")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5994e3a1-f39a-484a-bb88-22209d603528")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MySharpDXGame")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MySharpDXGame")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5994e3a1-f39a-484a-bb88-22209d603528")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MySharpDXGame")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MySharpDXGame")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5994e3a1-f39a-484a-bb88-22209d603528")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MySharpDXGame")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MySharpDXGame")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5994e3a1-f39a-484a-bb88-22209d603528")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MySharpDXGame")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MySharpDXGame")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5994e3a1-f39a-484a-bb88-22209d603528")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/Game.cs: -------------------------------------------------------------------------------- 1 | using SharpDX; 2 | using SharpDX.Direct3D; 3 | using SharpDX.DXGI; 4 | using SharpDX.Windows; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Drawing; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using D3D11 = SharpDX.Direct3D11; 12 | 13 | namespace MySharpDXGame 14 | { 15 | public class Game : IDisposable 16 | { 17 | private RenderForm renderForm; 18 | 19 | private const int Width = 1280; 20 | private const int Height = 720; 21 | 22 | private D3D11.Device d3dDevice; 23 | private D3D11.DeviceContext d3dDeviceContext; 24 | private SwapChain swapChain; 25 | private D3D11.RenderTargetView renderTargetView; 26 | 27 | /// 28 | /// Create and initialize a new game. 29 | /// 30 | public Game() 31 | { 32 | // Set window properties 33 | renderForm = new RenderForm("My first SharpDX game"); 34 | renderForm.ClientSize = new Size(Width, Height); 35 | renderForm.AllowUserResizing = false; 36 | 37 | InitializeDeviceResources(); 38 | } 39 | 40 | /// 41 | /// Start the game. 42 | /// 43 | public void Run() 44 | { 45 | // Start the render loop 46 | RenderLoop.Run(renderForm, RenderCallback); 47 | } 48 | 49 | private void RenderCallback() 50 | { 51 | Draw(); 52 | } 53 | 54 | private void InitializeDeviceResources() 55 | { 56 | ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm); 57 | 58 | // Descriptor for the swap chain 59 | SwapChainDescription swapChainDesc = new SwapChainDescription() 60 | { 61 | ModeDescription = backBufferDesc, 62 | SampleDescription = new SampleDescription(1, 0), 63 | Usage = Usage.RenderTargetOutput, 64 | BufferCount = 1, 65 | OutputHandle = renderForm.Handle, 66 | IsWindowed = true 67 | }; 68 | 69 | // Create device and swap chain 70 | D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain); 71 | d3dDeviceContext = d3dDevice.ImmediateContext; 72 | 73 | // Create render target view for back buffer 74 | using(D3D11.Texture2D backBuffer = swapChain.GetBackBuffer(0)) 75 | { 76 | renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer); 77 | } 78 | } 79 | 80 | /// 81 | /// Draw the game. 82 | /// 83 | private void Draw() 84 | { 85 | // Set back buffer as current render target view 86 | d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView); 87 | 88 | // Clear the screen 89 | d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178)); 90 | 91 | // Swap front and back buffer 92 | swapChain.Present(1, PresentFlags.None); 93 | } 94 | 95 | public void Dispose() 96 | { 97 | renderTargetView.Dispose(); 98 | swapChain.Dispose(); 99 | d3dDevice.Dispose(); 100 | d3dDeviceContext.Dispose(); 101 | renderForm.Dispose(); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/README.md: -------------------------------------------------------------------------------- 1 | # SharpDX Beginners Tutorial Part 2: Creating a window 2 | In this second tutorial we will look at creating a simple window which we later will render to. 3 | 4 | 1. Firstly we will create a new class called Game. Right click the project and select “Add -> Class…”, name the file `Game.cs`. 5 | 6 | ![Create game class](images/image1.png) 7 | 8 | 2. First we make the class public and then add a `RenderForm` along with two variables to hold the width and height of the window client size (the rendering size, does not include borders of the window). The `RenderForm` class also requires is to add a references to `SharpDX.Windows`. 9 | 10 | ```csharp 11 | using SharpDX.Windows; 12 | using System; 13 | using System.Collections.Generic; 14 | using System.Linq; 15 | using System.Text; 16 | using System.Threading.Tasks; 17 | 18 | namespace MySharpDXGame 19 | { 20 | public class Game 21 | { 22 | private RenderForm renderForm; 23 | 24 | private const int Width = 1280; 25 | private const int Height = 720; 26 | } 27 | } 28 | ``` 29 | The `RenderForm` is a subclass to a `Windows.Form` provided by SharpDX. This class, just like `Windows.Form`, provides us with a window that has borders, title bar etc. But it also provides us with a render loop which is optimized for 3D graphics. If you want to know more about this, take a look at the SlimDX (another wrapper similar to SharpDX) documentation: http://slimdx.org/tutorials/BasicWindow.php. 30 | 31 | 3. Next we will add our constructor to the `Game` class which creates the `RenderForm`. We also need to add a reference to `System.Drawing`. We will also set a title and disable the user from resizing the window. 32 | 33 | ```csharp 34 | using System.Drawing; 35 | 36 | [...] 37 | 38 | public Game() 39 | { 40 | renderForm = new RenderForm("My first SharpDX game"); 41 | renderForm.ClientSize = new Size(Width, Height); 42 | renderForm.AllowUserResizing = false; 43 | } 44 | ``` 45 | 46 | 47 | 4. Next step is to add two methods to our `Game` class, one to start the rendering/game loop and another one which will be a callback method called each frame. This is done with the following code: 48 | 49 | ```csharp 50 | public void Run() 51 | { 52 | RenderLoop.Run(renderForm, RenderCallback); 53 | } 54 | 55 | private void RenderCallback() 56 | { 57 | 58 | } 59 | ``` 60 | 61 | We pass in our `RenderForm` and method to be called each frame to the `RenderLoop.Run(…)` method. 62 | 63 | 5. We will now add a bit of clean up to our `Game` class, to make sure objects are disposed correctly. So we make our `Game` class implement the interface `IDisposable`: 64 | 65 | ```csharp 66 | public class Game : IDisposable 67 | { 68 | [...] 69 | 70 | public void Dispose() 71 | { 72 | renderForm.Dispose(); 73 | } 74 | } 75 | ``` 76 | 77 | Here we also make sure to dispose our `RenderForm`. 78 | 79 | 6. As a last step we will now run our game from the main method. So open the class `Program.cs`, which was automatically added when creating the “Console application project”, and change the `Main(…)` method to the following: 80 | 81 | ```csharp 82 | [STAThread] 83 | static void Main(string[] args) 84 | { 85 | using(Game game = new Game()) 86 | { 87 | game.Run(); 88 | } 89 | } 90 | ``` 91 | 92 | Because `Game` implements `IDisposable` it will automatically be disposed correctly due to the using statement. Read more about how this works here: https://msdn.microsoft.com/en-us/library/yh598w02.aspx. 93 | 94 | 7. Now if you run the program you should see an empty window with the correct size and title bar text: 95 | ![](images/image2.png) 96 | 97 | That was all for this tutorial, in the next part we will look at initializing the Direct3D device and set up a swap chain. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part1/MySharpDXGame/MySharpDXGame.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA} 8 | Exe 9 | Properties 10 | MySharpDXGame 11 | MySharpDXGame 12 | v4.5 13 | 512 14 | 5cbbb8b9 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll 38 | 39 | 40 | ..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll 41 | 42 | 43 | ..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll 44 | 45 | 46 | ..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll 47 | 48 | 49 | ..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll 50 | 51 | 52 | ..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 80 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part2/MySharpDXGame/MySharpDXGame.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA} 8 | Exe 9 | Properties 10 | MySharpDXGame 11 | MySharpDXGame 12 | v4.5 13 | 512 14 | e89b3ed2 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll 38 | 39 | 40 | ..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll 41 | 42 | 43 | ..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll 44 | 45 | 46 | ..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll 47 | 48 | 49 | ..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll 50 | 51 | 52 | ..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 81 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/MySharpDXGame/MySharpDXGame.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA} 8 | Exe 9 | Properties 10 | MySharpDXGame 11 | MySharpDXGame 12 | v4.5 13 | 512 14 | 550ce495 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll 38 | 39 | 40 | ..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll 41 | 42 | 43 | ..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll 44 | 45 | 46 | ..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll 47 | 48 | 49 | ..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll 50 | 51 | 52 | ..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 81 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/MySharpDXGame.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA} 8 | Exe 9 | Properties 10 | MySharpDXGame 11 | MySharpDXGame 12 | v4.5 13 | 512 14 | acb831fd 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | false 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | ..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll 39 | 40 | 41 | ..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll 42 | 43 | 44 | ..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll 45 | 46 | 47 | ..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll 48 | 49 | 50 | ..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll 51 | 52 | 53 | ..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | Always 78 | 79 | 80 | Always 81 | 82 | 83 | 84 | 91 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/MySharpDXGame.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F2267781-DCFF-485E-BA8A-62880CA8D8DA} 8 | Exe 9 | Properties 10 | MySharpDXGame 11 | MySharpDXGame 12 | v4.5 13 | 512 14 | acb831fd 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | false 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | ..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll 39 | 40 | 41 | ..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll 42 | 43 | 44 | ..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll 45 | 46 | 47 | ..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll 48 | 49 | 50 | ..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll 51 | 52 | 53 | ..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | Always 77 | 78 | 79 | Always 80 | 81 | 82 | 83 | 90 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/MySharpDXGame/Game.cs: -------------------------------------------------------------------------------- 1 | using SharpDX; 2 | using SharpDX.D3DCompiler; 3 | using SharpDX.Direct3D; 4 | using SharpDX.DXGI; 5 | using SharpDX.Windows; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Drawing; 9 | using System.Linq; 10 | using System.Text; 11 | using System.Threading.Tasks; 12 | using D3D11 = SharpDX.Direct3D11; 13 | 14 | namespace MySharpDXGame 15 | { 16 | public class Game : IDisposable 17 | { 18 | private RenderForm renderForm; 19 | 20 | private const int Width = 1280; 21 | private const int Height = 720; 22 | 23 | private D3D11.Device d3dDevice; 24 | private D3D11.DeviceContext d3dDeviceContext; 25 | private SwapChain swapChain; 26 | private D3D11.RenderTargetView renderTargetView; 27 | private Viewport viewport; 28 | 29 | // Shaders 30 | private D3D11.VertexShader vertexShader; 31 | private D3D11.PixelShader pixelShader; 32 | private ShaderSignature inputSignature; 33 | private D3D11.InputLayout inputLayout; 34 | 35 | private D3D11.InputElement[] inputElements = new D3D11.InputElement[] 36 | { 37 | new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0) 38 | }; 39 | 40 | // Triangle vertices 41 | private Vector3[] vertices = new Vector3[] { new Vector3(-0.5f, 0.5f, 0.0f), new Vector3(0.5f, 0.5f, 0.0f), new Vector3(0.0f, -0.5f, 0.0f) }; 42 | private D3D11.Buffer triangleVertexBuffer; 43 | 44 | /// 45 | /// Create and initialize a new game. 46 | /// 47 | public Game() 48 | { 49 | // Set window properties 50 | renderForm = new RenderForm("My first SharpDX game"); 51 | renderForm.ClientSize = new Size(Width, Height); 52 | renderForm.AllowUserResizing = false; 53 | 54 | InitializeDeviceResources(); 55 | InitializeShaders(); 56 | InitializeTriangle(); 57 | } 58 | 59 | /// 60 | /// Start the game. 61 | /// 62 | public void Run() 63 | { 64 | // Start the render loop 65 | RenderLoop.Run(renderForm, RenderCallback); 66 | } 67 | 68 | private void RenderCallback() 69 | { 70 | Draw(); 71 | } 72 | 73 | private void InitializeDeviceResources() 74 | { 75 | ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm); 76 | 77 | // Descriptor for the swap chain 78 | SwapChainDescription swapChainDesc = new SwapChainDescription() 79 | { 80 | ModeDescription = backBufferDesc, 81 | SampleDescription = new SampleDescription(1, 0), 82 | Usage = Usage.RenderTargetOutput, 83 | BufferCount = 1, 84 | OutputHandle = renderForm.Handle, 85 | IsWindowed = true 86 | }; 87 | 88 | // Create device and swap chain 89 | D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain); 90 | d3dDeviceContext = d3dDevice.ImmediateContext; 91 | 92 | viewport = new Viewport(0, 0, Width, Height); 93 | d3dDeviceContext.Rasterizer.SetViewport(viewport); 94 | 95 | // Create render target view for back buffer 96 | using(D3D11.Texture2D backBuffer = swapChain.GetBackBuffer(0)) 97 | { 98 | renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer); 99 | } 100 | } 101 | 102 | private void InitializeShaders() 103 | { 104 | // Compile the vertex shader code 105 | using(var vertexShaderByteCode = ShaderBytecode.CompileFromFile("vertexShader.hlsl", "main", "vs_4_0", ShaderFlags.Debug)) 106 | { 107 | // Read input signature from shader code 108 | inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode); 109 | 110 | vertexShader = new D3D11.VertexShader(d3dDevice, vertexShaderByteCode); 111 | } 112 | 113 | // Compile the pixel shader code 114 | using(var pixelShaderByteCode = ShaderBytecode.CompileFromFile("pixelShader.hlsl", "main", "ps_4_0", ShaderFlags.Debug)) 115 | { 116 | pixelShader = new D3D11.PixelShader(d3dDevice, pixelShaderByteCode); 117 | } 118 | 119 | // Set as current vertex and pixel shaders 120 | d3dDeviceContext.VertexShader.Set(vertexShader); 121 | d3dDeviceContext.PixelShader.Set(pixelShader); 122 | 123 | d3dDeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; 124 | 125 | // Create the input layout from the input signature and the input elements 126 | inputLayout = new D3D11.InputLayout(d3dDevice, inputSignature, inputElements); 127 | 128 | // Set input layout to use 129 | d3dDeviceContext.InputAssembler.InputLayout = inputLayout; 130 | } 131 | 132 | private void InitializeTriangle() 133 | { 134 | // Create a vertex buffer, and use our array with vertices as data 135 | triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices); 136 | } 137 | 138 | /// 139 | /// Draw the game. 140 | /// 141 | private void Draw() 142 | { 143 | // Set back buffer as current render target view 144 | d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView); 145 | 146 | // Clear the screen 147 | d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178)); 148 | 149 | // Set vertex buffer 150 | d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf(), 0)); 151 | 152 | // Draw the triangle 153 | d3dDeviceContext.Draw(vertices.Count(), 0); 154 | 155 | // Swap front and back buffer 156 | swapChain.Present(1, PresentFlags.None); 157 | } 158 | 159 | public void Dispose() 160 | { 161 | inputLayout.Dispose(); 162 | inputSignature.Dispose(); 163 | triangleVertexBuffer.Dispose(); 164 | vertexShader.Dispose(); 165 | pixelShader.Dispose(); 166 | renderTargetView.Dispose(); 167 | swapChain.Dispose(); 168 | d3dDevice.Dispose(); 169 | d3dDeviceContext.Dispose(); 170 | renderForm.Dispose(); 171 | } 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/MySharpDXGame/Game.cs: -------------------------------------------------------------------------------- 1 | using SharpDX; 2 | using SharpDX.D3DCompiler; 3 | using SharpDX.Direct3D; 4 | using SharpDX.DXGI; 5 | using SharpDX.Windows; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Drawing; 9 | using System.Linq; 10 | using System.Text; 11 | using System.Threading.Tasks; 12 | using D3D11 = SharpDX.Direct3D11; 13 | 14 | namespace MySharpDXGame 15 | { 16 | public class Game : IDisposable 17 | { 18 | private RenderForm renderForm; 19 | 20 | private const int Width = 1280; 21 | private const int Height = 720; 22 | 23 | private D3D11.Device d3dDevice; 24 | private D3D11.DeviceContext d3dDeviceContext; 25 | private SwapChain swapChain; 26 | private D3D11.RenderTargetView renderTargetView; 27 | private Viewport viewport; 28 | 29 | // Shaders 30 | private D3D11.VertexShader vertexShader; 31 | private D3D11.PixelShader pixelShader; 32 | private ShaderSignature inputSignature; 33 | private D3D11.InputLayout inputLayout; 34 | 35 | private D3D11.InputElement[] inputElements = new D3D11.InputElement[] 36 | { 37 | new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0), 38 | new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0, D3D11.InputClassification.PerVertexData, 0) 39 | }; 40 | 41 | // Triangle vertices 42 | private VertexPositionColor[] vertices = new VertexPositionColor[] { new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), SharpDX.Color.Red), new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), SharpDX.Color.Green), new VertexPositionColor(new Vector3(0.0f, -0.5f, 0.0f), SharpDX.Color.Blue) }; 43 | private D3D11.Buffer triangleVertexBuffer; 44 | 45 | /// 46 | /// Create and initialize a new game. 47 | /// 48 | public Game() 49 | { 50 | // Set window properties 51 | renderForm = new RenderForm("My first SharpDX game"); 52 | renderForm.ClientSize = new Size(Width, Height); 53 | renderForm.AllowUserResizing = false; 54 | 55 | InitializeDeviceResources(); 56 | InitializeShaders(); 57 | InitializeTriangle(); 58 | } 59 | 60 | /// 61 | /// Start the game. 62 | /// 63 | public void Run() 64 | { 65 | // Start the render loop 66 | RenderLoop.Run(renderForm, RenderCallback); 67 | } 68 | 69 | private void RenderCallback() 70 | { 71 | Draw(); 72 | } 73 | 74 | private void InitializeDeviceResources() 75 | { 76 | ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm); 77 | 78 | // Descriptor for the swap chain 79 | SwapChainDescription swapChainDesc = new SwapChainDescription() 80 | { 81 | ModeDescription = backBufferDesc, 82 | SampleDescription = new SampleDescription(1, 0), 83 | Usage = Usage.RenderTargetOutput, 84 | BufferCount = 1, 85 | OutputHandle = renderForm.Handle, 86 | IsWindowed = true 87 | }; 88 | 89 | // Create device and swap chain 90 | D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain); 91 | d3dDeviceContext = d3dDevice.ImmediateContext; 92 | 93 | viewport = new Viewport(0, 0, Width, Height); 94 | d3dDeviceContext.Rasterizer.SetViewport(viewport); 95 | 96 | // Create render target view for back buffer 97 | using(D3D11.Texture2D backBuffer = swapChain.GetBackBuffer(0)) 98 | { 99 | renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer); 100 | } 101 | } 102 | 103 | private void InitializeShaders() 104 | { 105 | // Compile the vertex shader code 106 | using(var vertexShaderByteCode = ShaderBytecode.CompileFromFile("vertexShader.hlsl", "main", "vs_4_0", ShaderFlags.Debug)) 107 | { 108 | // Read input signature from shader code 109 | inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode); 110 | 111 | vertexShader = new D3D11.VertexShader(d3dDevice, vertexShaderByteCode); 112 | } 113 | 114 | // Compile the pixel shader code 115 | using(var pixelShaderByteCode = ShaderBytecode.CompileFromFile("pixelShader.hlsl", "main", "ps_4_0", ShaderFlags.Debug)) 116 | { 117 | pixelShader = new D3D11.PixelShader(d3dDevice, pixelShaderByteCode); 118 | } 119 | 120 | // Set as current vertex and pixel shaders 121 | d3dDeviceContext.VertexShader.Set(vertexShader); 122 | d3dDeviceContext.PixelShader.Set(pixelShader); 123 | 124 | d3dDeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; 125 | 126 | // Create the input layout from the input signature and the input elements 127 | inputLayout = new D3D11.InputLayout(d3dDevice, inputSignature, inputElements); 128 | 129 | // Set input layout to use 130 | d3dDeviceContext.InputAssembler.InputLayout = inputLayout; 131 | } 132 | 133 | private void InitializeTriangle() 134 | { 135 | // Create a vertex buffer, and use our array with vertices as data 136 | triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices); 137 | } 138 | 139 | /// 140 | /// Draw the game. 141 | /// 142 | private void Draw() 143 | { 144 | // Set render targets 145 | d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView); 146 | 147 | // Clear the screen 148 | d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178)); 149 | 150 | // Set vertex buffer 151 | d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf(), 0)); 152 | 153 | // Draw the triangle 154 | d3dDeviceContext.Draw(vertices.Count(), 0); 155 | 156 | // Swap front and back buffer 157 | swapChain.Present(1, PresentFlags.None); 158 | } 159 | 160 | public void Dispose() 161 | { 162 | inputLayout.Dispose(); 163 | inputSignature.Dispose(); 164 | triangleVertexBuffer.Dispose(); 165 | vertexShader.Dispose(); 166 | pixelShader.Dispose(); 167 | renderTargetView.Dispose(); 168 | swapChain.Dispose(); 169 | d3dDevice.Dispose(); 170 | d3dDeviceContext.Dispose(); 171 | renderForm.Dispose(); 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /BeginnersTutorial-Part3/README.md: -------------------------------------------------------------------------------- 1 | # SharpDX Beginners Tutorial Part 3: Initializing DirectX 2 | In this part we are going to initialize DirectX. 3 | 4 | The first step we are going to do is add a new method called `InitializeDeviceResources()` to our `Game` class, like this: 5 | 6 | ```csharp 7 | private void InitializeDeviceResources() 8 | { 9 | } 10 | ``` 11 | 12 | First we will need to create a description for our back buffer, this is done with the following code: 13 | 14 | ```csharp 15 | using SharpDX.DXGI; 16 | [...] 17 | ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm); 18 | ``` 19 | 20 | - The first 2 parameters are the size of the back buffer, which in most cases should match the client width/height of the window we are rendering in. 21 | 22 | - The third parameter is the refresh rate in Hz (hertz), we set this to 60/1 = 60 Hz. 23 | 24 | - The last parameter is the format of the back buffer, here we specify a format with a red, green, blue, and alpha channel using 32-bit unsigned integer. 25 | 26 | For a complete list see the [MSDN documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173064(v=vs.85).aspx) for `DXGI_MODE_DESC`. 27 | 28 | The next step is to create a descriptor for our swap chain using the following code: 29 | 30 | ```csharp 31 | SwapChainDescription swapChainDesc = new SwapChainDescription() 32 | { 33 | ModeDescription = backBufferDesc, 34 | SampleDescription = new SampleDescription(1, 0), 35 | Usage = Usage.RenderTargetOutput, 36 | BufferCount = 1, 37 | OutputHandle = renderForm.Handle, 38 | IsWindowed = true 39 | }; 40 | ``` 41 | 42 | - `ModeDescription`: Here we provide our back buffer description. 43 | 44 | - `SampleDescription`: This is a descriptor for multisampling, we just specify one level (no multisampling) See `DXGI_SAMPLE_DESC` on [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173072(v=vs.85).aspx). 45 | 46 | - Usage: Specify if/how the CPU can access the back buffer, as we are rendering to it we specify it as `RenderTargetOutput`. For other options see [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173078(v=vs.85).aspx) for `DXGI_USAGE`. 47 | 48 | - `OutputHandle`: Handle to the window to render in. 49 | 50 | - `BufferCount`: Number of buffers, we just need 1. 51 | 52 | - `IsWindowed`: Wheather we want to be in fullscreen or windowed mode. 53 | 54 | Those are all we need to set right now, for the rest of the options, see `DXGI_SWAP_CHAIN_DESC` on [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173075(v=vs.85).aspx). 55 | 56 | Now that we have created our descriptions it is time to create the actual device and swap chain. First we declare three private class variables in our Game class: 57 | 58 | ```csharp 59 | using D3D11 = SharpDX.Direct3D11; 60 | [...] 61 | private D3D11.Device d3dDevice; 62 | private D3D11.DeviceContext d3dDeviceContext; 63 | private SwapChain swapChain; 64 | ``` 65 | 66 | We also added `using D3D11 = SharpDX.Direct3D11;`. We use this notation to avoid name conflicts between classes, and avoid having to write the full name, e.g. `SharpDX.Direct3D11.Device`. 67 | 68 | Time to actually create our device and swap chain: 69 | 70 | ```csharp 71 | using SharpDX.Direct3D; 72 | [...] 73 | D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain); 74 | d3dDeviceContext = d3dDevice.ImmediateContext; 75 | ``` 76 | 77 | - The first parameter specifies that we want to use the GPU. 78 | 79 | - We choose to not use any special flags, for possible flags see `D3D11_CREATE_DEVICE_FLAG` on [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ff476107(v=vs.85).aspx). 80 | 81 | - Third parameter is our swap chain descriptor. 82 | 83 | - The last parameters are in which variables we want to store our swap chain and device in. 84 | 85 | We also get our device context from the device. 86 | 87 | Next we will create a back buffer which we can render to. First add another private class variable to hold our render target view: 88 | 89 | ```csharp 90 | private D3D11.RenderTargetView renderTargetView; 91 | ``` 92 | 93 | We get can get this from the swap chain by specifying which type of back buffer we want, in our case a `exture2D`. Then we create a render target view from it, which we will soon use. 94 | 95 | ```csharp 96 | using (D3D11.Texture2D backBuffer = swapChain.GetBackBuffer(0)) 97 | { 98 | renderTargetView = new D3D11.RenderTargetView(d3d11Device, backBuffer); 99 | } 100 | ``` 101 | 102 | So, now we can finally render something, so why not use all of our graphic power and draw a blue screen! Let’s add a new method called `Draw()` to our Game class and add the following code: 103 | 104 | ```csharp 105 | private void Draw() 106 | { 107 | d3d11DeviceContext.OutputMerger.SetRenderTargets(renderTargetView); 108 | d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178)); 109 | swapChain.Present(1, PresentFlags.None); 110 | } 111 | ``` 112 | 113 | This code first sets the active render target view to the one we just created. Then it clears the render target view (currently our back buffer) and then swaps the back with the front buffer, making the back buffer visible. By specifying 1 as the first parameter to `Present(…)` we wait for vertical sync of the monitor before we present. This will limit the FPS to the update frequency of the monitor. 114 | 115 | Now we are almost done, we need to first call `InitializeDeviceResources()` from our Game class constructor: 116 | 117 | ```csharp 118 | public Game() 119 | { 120 | [...] 121 | 122 | InitializeDeviceResources(); 123 | } 124 | ``` 125 | 126 | Then we also need to call `Draw()` in our `RenderCallback()` method: 127 | 128 | ```csharp 129 | private void RenderCallback() 130 | { 131 | Draw(); 132 | } 133 | ``` 134 | 135 | Finally we will add some clean-up to our `Dispose()` method: 136 | 137 | ```csharp 138 | public void Dispose() 139 | { 140 | renderTargetView.Dispose(); 141 | swapChain.Dispose(); 142 | d3dDevice.Dispose(); 143 | d3dDeviceContext.Dispose(); 144 | renderForm.Dispose(); 145 | } 146 | ``` 147 | 148 | If you run this, you should now have a nice blue window, like this: 149 | ![](images/image1.png) 150 | 151 | 152 | In the next tutorial we will look at rendering our first (of many) triangle. -------------------------------------------------------------------------------- /BeginnersTutorial-Part5/README.md: -------------------------------------------------------------------------------- 1 | # SharpDX Beginners Tutorial Part 5: Coloring the triangle 2 | 3 | In part 4 we created a triangle, and we could set a single color for the whole triangle in the pixel shader. In this tutorial we will see how we can add a separate color to each vertex of the triangle. We will continue working on the code from the previous tutorial, which you can also find here. 4 | 5 | The final result will look like this: 6 | 7 | ![Final result](images/Result.png) 8 | 9 | ## 1. Creating a vertex struct 10 | Remember that when we rendered the triangle we uploaded an array of `Vector3`, because each vertex was just a position. Now we want each vertex to have both a position and a color. So we start by creating a new file called `VertexPositionColor.cs` by right clicking the project, and select `Add -> New Item...`: 11 | 12 | ![](images/AddNewFile.png) 13 | 14 | Select the type “Code File”, and name it VertexPositionColor.cs, and click on Add: 15 | 16 | ![](images/NewFileDialog.png) 17 | 18 | Now create a struct using the following code: 19 | 20 | ```csharp 21 | using SharpDX; 22 | using System.Runtime.InteropServices; 23 | 24 | namespace MySharpDXGame 25 | { 26 | [StructLayoutAttribute(LayoutKind.Sequential)] 27 | public struct VertexPositionColor 28 | { 29 | public readonly Vector3 Position; 30 | public readonly Color4 Color; 31 | 32 | public VertexPositionColor(Vector3 position, Color4 color) 33 | { 34 | Position = position; 35 | Color = color; 36 | } 37 | } 38 | } 39 | ``` 40 | 41 | So, now we have an immutable struct which has both a Position and Color. We also added the `[StructLayoutAttribute(LayoutKind.Sequential)]` attribute to the struct. This make sure that the values are laid out in memory in the same order as they are specified in the struct. This is important as we later need to tell this to the GPU. 42 | 43 | ## 2. Changing the vertices 44 | As the next step we need to replace the array of vertices in `Game.cs` from the previous tutorial with an array of our new vertex type instead. We also choose a color for each vertex. So replace this code: 45 | 46 | ```csharp 47 | private Vector3[] vertices = new Vector3[] { new Vector3(-0.5f, 0.5f, 0.0f), new Vector3(0.5f, 0.5f, 0.0f), new Vector3(0.0f, -0.5f, 0.0f) }; 48 | ``` 49 | 50 | with this code: 51 | 52 | ```csharp 53 | private VertexPositionColor[] vertices = new VertexPositionColor[] 54 | { 55 | new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), SharpDX.Color.Red), 56 | new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), SharpDX.Color.Green), 57 | new VertexPositionColor(new Vector3(0.0f, -0.5f, 0.0f), SharpDX.Color.Blue) 58 | }; 59 | ``` 60 | 61 | ## 3. Updating the VertexBuffer 62 | We will also need to change our vertex buffer to contain the new vertex struct instead of `Vector3`, so remove `` from the following line (in the `InitializeTriangle()` method): 63 | 64 | ```csharp 65 | triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices); 66 | ``` 67 | 68 | Now the compiler will infer that it is a `` from the arguments automatically instead, so if we later change it we won’t need to change here again. 69 | 70 | We also have to change the size of each element in the vertex buffer, so replace `Vector3` with `VertexPositionColor` the following line (in the `Draw()` method): 71 | 72 | ```csharp 73 | d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf(), 0)); 74 | ``` 75 | 76 | ## 4. Changing the input layout 77 | As you might recall from the previous tutorial we created an input layout this describes how the vertex data is structured. In the last tutorial it looked like this: 78 | 79 | ```csharp 80 | private D3D11.InputElement[] inputElements = new D3D11.InputElement[] 81 | { 82 | new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0) 83 | }; 84 | ``` 85 | 86 | As we also added color to each vertex we need to change the input layout to the following: 87 | 88 | ```csharp 89 | private D3D11.InputElement[] inputElements = new D3D11.InputElement[] 90 | { 91 | new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0), 92 | new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0, D3D11.InputClassification.PerVertexData, 0) 93 | }; 94 | ``` 95 | 96 | We added another `InputElement`, where the type is a `R32G32B32A32_Float`, because the `SharpDX.Color4` struct we used in our vertex struct contains 4 floats (in RGBA order). We also add a few other arguments, most are irrelevant right now, except the 4th, where we specify `0` for `POSITION`, but `12` for the `COLOR` element. This argument is the offset in the structs where this data starts (in bytes), and because the position comes first it has offset 0, and each position is a `Vector3` which contains 3 floats (x, y, z) of 4 bytes each = 12 bytes. So therefore the color data will be found after 12 bytes. 97 | 98 | ## 5. Updating the vertex shader 99 | Open up the `vertexShader.hlsl` file. Firstly, we are going to change the main function parameter list to also include the color: 100 | 101 | ```c 102 | float4 main(float4 position : POSITION, float4 color : COLOR) : SV_POSITION 103 | { 104 | [...] 105 | } 106 | ``` 107 | 108 | As you can see the vertex shader now takes in the color as well, but because the color is set in the pixel shader we also need to return the color from this function. To return multiple values from the function we need to create a struct which contains both the position and color at the top of the shader file: 109 | 110 | ```c 111 | struct VSOut 112 | { 113 | float4 position : SV_POSITION; 114 | float4 color : COLOR; 115 | }; 116 | ``` 117 | 118 | Now we change the function return value to this struct instead, and remove the `SV_POSITION` semantic: 119 | 120 | ```c 121 | VSOut main(float4 position : POSITION, float4 color : COLOR) 122 | { 123 | […] 124 | } 125 | ``` 126 | 127 | And instead of just returning the position from the shader we are going to create a `VSOut` struct and set the position and color values in that instead. So the final vertex shader should look like this: 128 | 129 | ```c 130 | struct VSOut 131 | { 132 | float4 position : SV_POSITION; 133 | float4 color : COLOR; 134 | }; 135 | 136 | VSOut main(float4 position : POSITION, float4 color : COLOR) 137 | { 138 | VSOut output; 139 | output.position = position; 140 | output.color = color; 141 | 142 | return output; 143 | } 144 | ``` 145 | 146 | ## 6. Updating the pixel shader 147 | In the pixel shader (`pixelShader.hlsl`), we need to add the color as a function parameter, and then instead of returning a red color we return the provided color, so the full pixel shader should look like this: 148 | 149 | ```c 150 | float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET 151 | { 152 | return color; 153 | } 154 | ``` 155 | 156 | ## 7. The final result 157 | We are now done, and if you run the project you should now get the following result: 158 | 159 | ![Final result.](images/Result.png) -------------------------------------------------------------------------------- /BeginnersTutorial-Part4/README.md: -------------------------------------------------------------------------------- 1 | # SharpDX Beginners Tutorial Part 4: Drawing a triangle 2 | Now that we have a window with Direct3D initialized it is finally time to draw something, and just as all other tutorials out there, we will also start with drawing a triangle! To render our first triangle there are actually quite many parts we must add, so let’s get started. 3 | 4 | ## 1. Vertices 5 | To create the triangle, we will use vertices. A vertex is an exact point in a 3D space, which can also hold additional information (which we will see in later tutorials). For now, our vertices will only be represented by 3 values, its x, y, and z-coordinates. 6 | 7 | For the triangle we will need 3 vertices, one for each corner. We will cover more details about the different coordinate system later, but for now our visible space is between -1 and 1 in x‑, y‑, and z‑directions. This is how I decided to set up the triangle, you can play around with the different values to see how triangle changes: 8 | 9 | ![](images/image1.png) 10 | 11 | 12 | So the first code we add is a variable to our `Game` class which holds those coordinates, for this we use the `Vector3` class provided in SharpDX: 13 | 14 | ```csharp 15 | private Vector3[] vertices = new Vector3[] { new Vector3(-0.5f, 0.5f, 0.0f), new Vector3(0.5f, 0.5f, 0.0f), new Vector3(0.0f, -0.5f, 0.0f) }; 16 | ``` 17 | 18 | ## 2. Vertex Buffers 19 | The vertices array we just created is stored in the system memory, but for rendering our objects we will need to transfer the data to the video memory. For this we will use Buffers. In DirectX we have three different types of buffers, Vertex, Index, and Constant buffers. The data in buffers are automatically copied from system memory to video memory when we our rendering needs the data by DirectX. 20 | 21 | The vertex buffer is what we will use now, this buffer type, as the name implies, holds data for each vertex. For now, our vertices only have a position vector, but later on we will add more information to each vertex. First step here is to add a new variable to our class which is a reference to our buffer: 22 | 23 | ```csharp 24 | private D3D11.Buffer triangleVertexBuffer; 25 | ``` 26 | 27 | Then we add a new method to our class called `InitializeTriangle`, like this: 28 | 29 | ```csharp 30 | private void InitializeTriangle() 31 | { 32 | triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices); 33 | } 34 | ``` 35 | 36 | Here the method `D3D.Buffer.Create` is used to create a new buffer, the generic type parameter `T` specifies of which data type of each element in the buffer. The first argument is the Direct3D device we wish to use. The second argument is which type of buffer we want to create, in this case a vertex buffer. Lastly we provide the initial data to load into the buffer, in this case our array of positions. 37 | Also add a call to this method at the end of the `Game` class constructor, and dispose the buffer: 38 | 39 | ```csharp 40 | public Game() 41 | { 42 | [...] 43 | InitializeTriangle(); 44 | } 45 | 46 | public void Dispose() 47 | { 48 | triangleVertexBuffer.Dispose(); 49 | [...] 50 | } 51 | ``` 52 | 53 | ## 3. Vertex and Pixel shaders 54 | The [graphics pipeline in DirectX 11](https://msdn.microsoft.com/en-us/library/windows/desktop/ff476882(v=vs.85).aspx) consists of several programmable steps. Now we will focus on the Vertex Shader Stage and Pixel Shader Stage. 55 | 56 | The vertex shader stage is responsible for processing vertices, this can include for example transformations (translating, rotating, scaling, etc). 57 | 58 | The pixel shader stage processes runs for each pixel, and received interpolated per-vertex data, as well as constant variables and textures. This shader is run for each pixel of the rendered primitive, and should return the final color of the pixel. 59 | 60 | First we add two class variables, our vertex and pixel shader: 61 | 62 | ```csharp 63 | private D3D11.VertexShader vertexShader; 64 | private D3D11.PixelShader pixelShader; 65 | ``` 66 | 67 | Next we need to compile our shader code (which we will write soon), which we place in a new private method, a using directive at the top is also required: 68 | 69 | ```csharp 70 | using SharpDX.D3DCompiler; 71 | [...] 72 | private void InitializeShaders() 73 | { 74 | using(var vertexShaderByteCode = ShaderBytecode.CompileFromFile("vertexShader.hlsl", "main", "vs_4_0", ShaderFlags.Debug)) 75 | { 76 | vertexShader = new D3D11.VertexShader(d3dDevice, vertexShaderByteCode); 77 | } 78 | using(var pixelShaderByteCode = ShaderBytecode.CompileFromFile("pixelShader.hlsl", "main", "ps_4_0", ShaderFlags.Debug)) 79 | { 80 | pixelShader = new D3D11.PixelShader(d3dDevice, pixelShaderByteCode); 81 | } 82 | } 83 | ``` 84 | 85 | Here we first point at which files to compile, `vertexShader.hlsl` and `pixelShader.hlsl`. We also specify the name of the entry point method in the shader code, `main`. Then we also set which version of HLSL to use, in this case 4.0. Finally, we also set the compilation to debug mode. 86 | 87 | The device context must now also be configured to use those shaders when drawing, so add this code to the end of the `InitializeShaders()` method: 88 | 89 | ```csharp 90 | private void InitializeShaders() 91 | { 92 | [...] 93 | // Set as current vertex and pixel shaders 94 | d3dDeviceContext.VertexShader.Set(vertexShader); 95 | d3dDeviceContext.PixelShader.Set(pixelShader); 96 | 97 | d3dDeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; 98 | } 99 | ``` 100 | 101 | Here we also set the primitive topology, this specifies how the vertices should be drawn. In this case we will use “Triangle List”, we will use other types in later tutorials, but you can check out the MSDN documentation for a good illustration of the different types. 102 | 103 | Now, let’s add the shader code: 104 | 105 | 1. Right click on the project in the solution explorer and select Add -> New Item… 106 | ![](images/image2.png) 107 | 108 | 2. Find “Text File” and enter `vertexShader.hlsl` as name. Press Add. 109 | ![](images/image3.png) 110 | 111 | 3. Select the file in the solution explorer, and in the Properties window, set “Copy to Output Directory” to “Copy Always”. 112 | ![](images/image4.png) 113 | 114 | Repeat step 1-3, but name the file `pixelShader.hlsl`. 115 | Now open `vertexShader.hlsl` and write: 116 | 117 | ```c 118 | float4 main(float4 position : POSITION) : SV_POSITION 119 | { 120 | return position; 121 | } 122 | ``` 123 | 124 | Here we create our entry point method `main`, as we specified earlier. To start with the method only returns the same position that it gets from the vertex buffer. Notice the `: POSITION` and `: SV_POSITION`, this is called a semantic and specifies the intended use of the variable, we will see more of why this is important later in this tutorial. 125 | 126 | Now open the `pixelShader.hlsl` file and enter the following code: 127 | 128 | ```c 129 | float4 main(float4 position : SV_POSITION) : SV_TARGET 130 | { 131 | return float4(1.0, 0.0, 0.0, 1.0); 132 | } 133 | ``` 134 | 135 | Again we create a main method, the parameter to the method is the output from the vertex shader. But remember that the vertex shader is run for each vertex, while pixel shader runs for each pixel, so this will be an interpolated position. From this method we return a `float4`, which is our color in the format red, green, blue, alpha. So this will produce a red color for all pixels. Worth noting is that the values in the `float4` is between `0` and `1`, so `float4(0, 0, 0, 1)` would give black, while `float4(1, 1, 1, 1)` would give a white pixel. 136 | 137 | And of course also call our `InitializeShaders()` method in our game constructor and dispose of the shaders, this should go before the `InitializeTriangle()` method: 138 | 139 | ```csharp 140 | public Game() 141 | { 142 | [...] 143 | InitializeDeviceResources(); 144 | InitializeShaders(); 145 | InitializeTriangle(); 146 | } 147 | public void Dispose() 148 | { 149 | triangleVertexBuffer.Dispose(); 150 | vertexShader.Dispose(); 151 | pixelShader.Dispose(); 152 | [...] 153 | } 154 | ``` 155 | 156 | ## 4. Input layout 157 | We now have a vertex buffer which has our vertex data. But DirectX also wants to know about how the data is structured and of what type each vertex element has, for this we use an Input Layout. This requires two step. First we need to describe each element in a vertex, and then create an input layout from that. 158 | 159 | As our vertices only have one element so far, the position, so let’s add a new array of InputElements in our Game class: 160 | 161 | ```csharp 162 | private D3D11.InputElement[] inputElements = new D3D11.InputElement[] 163 | { 164 | new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0) 165 | }; 166 | ``` 167 | 168 | The `POSITION` can be recognized from the shader code, this is called a semantic and is used to match with the input signature in the shader. The second parameter is which semantic slot to use, this is used if you have multiple `POSITION` semantics for example. Thirdly is the data type of this element, in this case 3 floats as the position for our vertices is Vector3. 169 | 170 | Next we need to get the input shader from the compiled vertex shader. First create a new variable to hold the input signature to our `Game` class: 171 | 172 | ```csharp 173 | private ShaderSignature inputSignature; 174 | ``` 175 | 176 | Then in the `InitializeShaders()` method we can get the signature from the compiled shader byte code, like this: 177 | 178 | ```csharp 179 | using(var vertexShaderByteCode = ShaderBytecode.CompileFromFile("vertexShader.hlsl", "main", "vs_4_0", ShaderFlags.Debug)) 180 | { 181 | inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode); 182 | [...] 183 | } 184 | ``` 185 | 186 | Now we need to create an input layout from the array of `InputElement` and the input signature, so add another variable to the `Game` class: 187 | 188 | ```csharp 189 | private D3D11.InputLayout inputLayout; 190 | ``` 191 | 192 | And then assign it at the end of the `InitializeShaders()` method by creating a new `InputLayout` instance. Then we set this as the current input layout on the device context. 193 | 194 | ```csharp 195 | private void InitializeShaders() 196 | { 197 | [...] 198 | inputLayout = new D3D11.InputLayout(d3dDevice, inputSignature, inputElements); 199 | d3dDeviceContext.InputAssembler.InputLayout = inputLayout; 200 | } 201 | ``` 202 | 203 | The first element is our Direct3D device, and then our input signature from the shader and lastly the input elements array. 204 | 205 | And don't forget to dispose the input layout and input signature: 206 | 207 | ```csharp 208 | public void Dispose() 209 | { 210 | inputLayout.Dispose(); 211 | inputSignature.Dispose(); 212 | [...] 213 | } 214 | ``` 215 | 216 | ## 5. Set the viewport: 217 | Before we draw anything we have to specify the viewport. DirectX uses something called Normalized Device Coordinates, specified as (-1, -1) in upper left corner, and (1, 1) in lower right corner (and therefore (0, 0) in the middle) of the screen. The viewport maps those corners to pixel coordinates. 218 | 219 | Firstly create another variable in our `Game` class for the Viewport: 220 | 221 | ```csharp 222 | private Viewport viewport; 223 | ``` 224 | 225 | In the `InitializeDeviceResources()` method, create a new viewport and set it on the device context using the following code: 226 | 227 | ```csharp 228 | // Set viewport 229 | viewport = new Viewport(0, 0, Width, Height); 230 | d3dDeviceContext.Rasterizer.SetViewport(viewport); 231 | ``` 232 | 233 | The first two parameters are the `x` and `y` position of `(-1, -1)` and the last two parameters are how the width and height of the viewport. As we want to use the full window we map it tell it to start in the upper left corner (0, 0) and set it to the full width and height of the window. 234 | 235 | ## 6. Drawing the vertex data 236 | After all this work, it is finally time to draw the triangle on the screen! This is just two method calls, which we add in the middle of our `draw()` method: 237 | 238 | ```csharp 239 | private void Draw() 240 | { 241 | d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView); 242 | d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178)); 243 | 244 | d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf(), 0)); 245 | d3dDeviceContext.Draw(vertices.Count(), 0); 246 | 247 | swapChain.Present(1, PresentFlags.None); 248 | } 249 | ``` 250 | 251 | The first method tells the device context to use the vertex buffer holding the triangle vertex data, with the second parameter specifying the size (in bytes) for the data of each vertex. To get this size we use a nice helper method available in SharpDX. 252 | 253 | The `Draw()` method on the device context draws `vertices.Count()` many vertices from our vertex buffer. The second parameter specifies the offset in our vertex buffer, by settings this to 1 for example, the first vertex would be skipped. 254 | 255 | Now when you run the program you should get the following result: 256 | 257 | ![](images/image5.png) --------------------------------------------------------------------------------