├── .gitignore ├── Grasshopper.sln ├── LICENSE.txt ├── README.md ├── packages └── repositories.config ├── samples ├── Core │ ├── 01. HelloWorld │ │ ├── 01. HelloWorld.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ └── packages.config │ ├── 02. SimpleQuad │ │ ├── 02. SimpleQuad.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ └── packages.config │ ├── 03. SimpleInstancing │ │ ├── 03. SimpleInstancing.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ └── packages.config │ ├── 04. SimpleCube │ │ ├── 04. SimpleCube.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ └── packages.config │ ├── 05. ManyCubes │ │ ├── 05. ManyCubes.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ └── packages.config │ ├── 06. SimpleTexturedCube │ │ ├── 06. SimpleTexturedCube.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ ├── Textures │ │ │ └── rabbit.jpg │ │ └── packages.config │ ├── 07. ManyTexturedCubes │ │ ├── 07. ManyTexturedCubes.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ ├── Textures │ │ │ ├── cat.jpg │ │ │ ├── dog.jpg │ │ │ ├── fish.jpg │ │ │ ├── rabbit.jpg │ │ │ └── snail.jpg │ │ └── packages.config │ ├── 08. FreeLookCamera │ │ ├── 08. FreeLookCamera.csproj │ │ ├── App.config │ │ ├── App │ │ │ ├── CubeInstance.cs │ │ │ ├── CubesDemo.cs │ │ │ └── SceneData.cs │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ ├── Textures │ │ │ ├── cat.jpg │ │ │ ├── dog.jpg │ │ │ ├── fish.jpg │ │ │ ├── rabbit.jpg │ │ │ └── snail.jpg │ │ └── packages.config │ ├── 09. MultiTexturedCube │ │ ├── 09. MultiTexturedCube.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ ├── Resources.Designer.cs │ │ │ └── Resources.resx │ │ ├── Resources │ │ │ ├── PixelShader.hlsl │ │ │ └── VertexShader.hlsl │ │ ├── Textures │ │ │ ├── dirt.png │ │ │ ├── grass-side.png │ │ │ └── grass-top.png │ │ └── packages.config │ └── 10. DynamicTexture │ │ ├── 10. DynamicTexture.csproj │ │ ├── App.config │ │ ├── Program.cs │ │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ │ ├── Resources │ │ ├── PixelShader.hlsl │ │ └── VertexShader.hlsl │ │ └── packages.config ├── Images │ ├── cube-multitexture.gif │ ├── cube-rainbow.gif │ ├── cube.gif │ ├── cubes-freelook.gif │ ├── cubes-rainbow.gif │ └── cubes.gif └── todo.txt └── src ├── core-engine ├── Grasshopper.Procedural │ ├── Graphics │ │ └── Primitives │ │ │ ├── Cube.cs │ │ │ ├── CubeBuilder.cs │ │ │ ├── QuadBuilder.cs │ │ │ ├── TriangleBuilder.cs │ │ │ ├── VertexMetadata.cs │ │ │ └── VertexMetadataExtensions.cs │ ├── Grasshopper.Procedural.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── notes.txt │ └── packages.config └── Grasshopper │ ├── Core │ ├── RateLimiter.cs │ └── TickCounter.cs │ ├── FrameContext.cs │ ├── Graphics │ ├── IDeviceManager.cs │ ├── IGraphicsContext.cs │ ├── IGraphicsContextFactory.cs │ ├── Materials │ │ ├── IMaterialManager.cs │ │ ├── ITextureResource.cs │ │ ├── ITextureResourceManager.cs │ │ ├── ITextureSampler.cs │ │ ├── ITextureSamplerManager.cs │ │ ├── Material.cs │ │ ├── PixelFormat.cs │ │ ├── PixelShaderSpec.cs │ │ ├── ShaderInputElementFormat.cs │ │ ├── ShaderInputElementPurpose.cs │ │ ├── ShaderInputElementSpec.cs │ │ ├── ShaderSpec.cs │ │ ├── TextureFiltering.cs │ │ ├── TextureSamplerSettings.cs │ │ ├── TextureWrapping.cs │ │ └── VertexShaderSpec.cs │ ├── Primitives │ │ ├── Color.cs │ │ ├── Color4.cs │ │ ├── Quad.cs │ │ ├── TextureCoordinate.cs │ │ ├── Triangle.cs │ │ └── VertexPosColTex.cs │ ├── Rendering │ │ ├── Blending │ │ │ ├── BlendEquation.cs │ │ │ ├── BlendSettings.cs │ │ │ ├── ColorComponents.cs │ │ │ ├── IBlendSettings.cs │ │ │ ├── IBlendState.cs │ │ │ ├── IBlendStateManager.cs │ │ │ └── PreBlend.cs │ │ ├── Buffers │ │ │ ├── ConstantBufferInterfaces.cs │ │ │ ├── IConstantBufferManager.cs │ │ │ ├── IConstantBufferManagerFactory.cs │ │ │ ├── IConstantBufferResource.cs │ │ │ ├── IndexBufferInterfaces.cs │ │ │ ├── MeshBuffer.cs │ │ │ ├── MeshBufferManager.cs │ │ │ ├── MeshLocation.cs │ │ │ └── VertexBufferInterfaces.cs │ │ ├── DrawType.cs │ │ ├── IAppWindow.cs │ │ ├── IDrawingContext.cs │ │ ├── IRenderTarget.cs │ │ ├── IRenderTargetFactory.cs │ │ ├── ITextureDrawingContext.cs │ │ ├── ITextureRenderTarget.cs │ │ ├── IWindowDrawingContext.cs │ │ ├── IWindowRenderTarget.cs │ │ ├── Rasterization │ │ │ ├── Antialiasing.cs │ │ │ ├── IRasterizerSettings.cs │ │ │ ├── IRasterizerState.cs │ │ │ ├── IRasterizerStateManager.cs │ │ │ ├── RasterizerSettings.cs │ │ │ ├── TriangleCulling.cs │ │ │ └── WindingOrder.cs │ │ └── RenderManager.cs │ └── SceneManagement │ │ ├── Camera.cs │ │ ├── Mesh.cs │ │ ├── MeshInstance.cs │ │ └── Model.cs │ ├── Grasshopper.csproj │ ├── Grasshopper.nuspec │ ├── GrasshopperApp.cs │ ├── Input │ ├── IInputContext.cs │ ├── KeyboardEvent.cs │ └── MouseEvent.cs │ ├── Math │ ├── Calc.cs │ └── Transformation.cs │ ├── Platform │ ├── ActivatablePlatformResource.cs │ ├── ActivatablePlatformResourceManager.cs │ ├── IActivatablePlatformResource.cs │ ├── IActivatablePlatformResourceManager.cs │ ├── IFileSource.cs │ ├── IFileStore.cs │ ├── IIndexActivatablePlatformResource.cs │ ├── IIndexActivatablePlatformResourceManager.cs │ ├── IPlatformResource.cs │ ├── IPlatformResourceManager.cs │ ├── IndexActivatablePlatformResource.cs │ ├── IndexActivatablePlatformResourceManager.cs │ ├── PlatformResource.cs │ └── PlatformResourceManager.cs │ ├── Properties │ ├── Annotations.cs │ └── AssemblyInfo.cs │ ├── README.md │ └── packages.config └── platform ├── Grasshopper.SharpDX ├── Graphics │ ├── ActivatableD3DResource.cs │ ├── AppWindow.cs │ ├── ConversionExtensions.cs │ ├── DeviceManager.cs │ ├── GraphicsContext.cs │ ├── GraphicsContextFactory.cs │ ├── IndexActivatableD3DResource.cs │ ├── Materials │ │ ├── DynamicTexture2DResource.cs │ │ ├── MaterialManager.cs │ │ ├── MaterialResource.cs │ │ ├── ShaderResource.cs │ │ ├── ShaderResourceWriter.cs │ │ ├── Texture2DArrayResource.cs │ │ ├── Texture2DFileResource.cs │ │ ├── TextureResourceManager.cs │ │ ├── TextureSampler.cs │ │ └── TextureSamplerManager.cs │ ├── Rendering │ │ ├── BlendState.cs │ │ ├── BlendStateManager.cs │ │ ├── Buffers │ │ │ ├── ConstantBufferManager.cs │ │ │ ├── ConstantBufferManagerFactory.cs │ │ │ ├── ConstantBufferResource.cs │ │ │ ├── IndexBufferManager.cs │ │ │ ├── IndexBufferManagerFactory.cs │ │ │ ├── IndexBufferResource.cs │ │ │ ├── VertexBufferManager.cs │ │ │ ├── VertexBufferManagerFactory.cs │ │ │ └── VertexBufferResource.cs │ │ ├── DrawingContext.cs │ │ ├── Internal │ │ │ ├── DepthBuffer.cs │ │ │ ├── TextureBuffer.cs │ │ │ └── WindowTextureBuffer.cs │ │ ├── RasterizerState.cs │ │ ├── RasterizerStateManager.cs │ │ ├── RenderManager.cs │ │ ├── RenderTarget.cs │ │ ├── RenderTargetFactory.cs │ │ ├── WindowDrawingContext.cs │ │ └── WindowRenderTarget.cs │ ├── TextureDrawingContext.cs │ └── TextureRenderTarget.cs ├── Grasshopper.SharpDX.csproj ├── Input │ └── InputContext.cs ├── Properties │ └── AssemblyInfo.cs ├── SharpDXBootstrapper.cs ├── app.config └── packages.config └── Grasshopper.WindowsFileSystem ├── FileSource.cs ├── FileStore.cs ├── Grasshopper.WindowsFileSystem.csproj ├── Properties └── AssemblyInfo.cs └── WindowsFileSystemBootstrapper.cs /.gitignore: -------------------------------------------------------------------------------- 1 | [Dd]ebug/ 2 | [Rr]elease/ 3 | [Bb]in/ 4 | [Oo]bj/ 5 | Deploy/ 6 | Build/ 7 | Logs/ 8 | packages/*/* 9 | _ReSharper*/* 10 | TestResults/* 11 | 12 | *.csuser 13 | *.suo 14 | *.ReSharper 15 | *.DotSettings 16 | *.user 17 | *.tmp 18 | *.orig 19 | *.bak 20 | *.zip 21 | *.rar 22 | *.userprefs 23 | *.pidb 24 | *.db 25 | *.ncrunchproject 26 | *.ncrunchsolution 27 | *.cache 28 | *.sublime-workspace 29 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Grasshopper: C# Game Engine 2 | http://github.com/axefrog/Grasshopper 3 | 4 | Copyright (c) 2015, Nathan Ridley 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | 3. Neither the name of the copyright holder nor the names of its contributors 18 | may be used to endorse or promote products derived from this software 19 | without specific prior written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /packages/repositories.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /samples/Core/01. HelloWorld/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /samples/Core/01. HelloWorld/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper; 3 | using Grasshopper.Core; 4 | using Grasshopper.Graphics; 5 | using Grasshopper.Graphics.Primitives; 6 | using Grasshopper.SharpDX; 7 | 8 | namespace HelloWorld 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | using(var app = new GrasshopperApp().UseSharpDX()) 15 | using(var gfx = app.Graphics.CreateContext(enableDebugMode: true)) 16 | 17 | // In this sample we'll create two windows and display each of them with different colours and 18 | // titles displaying different information 19 | using(var main = gfx.RenderTargetFactory.CreateWindow()) 20 | using(var other = gfx.RenderTargetFactory.CreateWindow()) 21 | { 22 | main.Window.Visible = true; 23 | other.Window.Visible = true; 24 | 25 | // Our main loop runs as fast as possible, but we only want to render at 60fps 26 | var fpsLimiter = new RateLimiter(60); 27 | 28 | app.Run(frame => 29 | { 30 | if(!fpsLimiter.Ready()) 31 | return true; 32 | 33 | // The first window will show our true frame rate which should be over a million cycles 34 | // per second (of which only 60 per second will include the redraws below) 35 | main.Render(context => 36 | { 37 | context.Window.Title = "Hello, window #1! Current ticks per second: " + frame.FramesPerSecond.ToString("0"); 38 | context.Clear(Color.CornflowerBlue); 39 | context.Present(); 40 | }); 41 | 42 | // The second window will show the current time 43 | other.Render(context => 44 | { 45 | context.Window.Title = "Hello, window #2! It's currently " + DateTime.UtcNow.ToString("F"); 46 | context.Clear(Color.Tomato); 47 | context.Present(); 48 | }); 49 | 50 | // Closing a window sends an exit request, which we can treat as we see fit. In this 51 | // case, we will terminate the application by returning false from the main loop only 52 | // when both windows are closed. 53 | return !(main.Terminated && other.Terminated); 54 | }); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /samples/Core/01. HelloWorld/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("HelloWorld")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("HelloWorld")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("3735856e-ac9e-4654-93d2-5d0af46d94bd")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /samples/Core/01. HelloWorld/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("SimpleQuad")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("SimpleQuad")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("b5543442-9935-481d-bdd9-40a4381dcc6c")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.0 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SimpleQuad.Properties { 12 | /// 13 | /// A strongly-typed resource class, for looking up localized strings, etc. 14 | /// 15 | // This class was auto-generated by the StronglyTypedResourceBuilder 16 | // class via a tool like ResGen or Visual Studio. 17 | // To add or remove a member, edit your .ResX file then rerun ResGen 18 | // with the /str option, or rebuild your VS project. 19 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 20 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 21 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 22 | internal class Resources { 23 | 24 | private static global::System.Resources.ResourceManager resourceMan; 25 | 26 | private static global::System.Globalization.CultureInfo resourceCulture; 27 | 28 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 29 | internal Resources() { 30 | } 31 | 32 | /// 33 | /// Returns the cached ResourceManager instance used by this class. 34 | /// 35 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 36 | internal static global::System.Resources.ResourceManager ResourceManager { 37 | get { 38 | if (object.ReferenceEquals(resourceMan, null)) { 39 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SimpleQuad.Properties.Resources", typeof(Resources).Assembly); 40 | resourceMan = temp; 41 | } 42 | return resourceMan; 43 | } 44 | } 45 | 46 | /// 47 | /// Overrides the current thread's CurrentUICulture property for all 48 | /// resource lookups using this strongly typed resource class. 49 | /// 50 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 51 | internal static global::System.Globalization.CultureInfo Culture { 52 | get { 53 | return resourceCulture; 54 | } 55 | set { 56 | resourceCulture = value; 57 | } 58 | } 59 | 60 | /// 61 | /// Looks up a localized string similar to . 62 | /// 63 | internal static string PixelShader { 64 | get { 65 | return ResourceManager.GetString("PixelShader", resourceCulture); 66 | } 67 | } 68 | 69 | /// 70 | /// Looks up a localized string similar to . 71 | /// 72 | internal static string VertexShader { 73 | get { 74 | return ResourceManager.GetString("VertexShader", resourceCulture); 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 PSMain(float4 position : SV_POSITION, float4 color : COLOR) : SV_Target 2 | { 3 | return color; 4 | } -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | struct VOut 2 | { 3 | float4 position : SV_POSITION; 4 | float4 color : COLOR; 5 | }; 6 | 7 | struct VIn 8 | { 9 | float4 position: POSITION; 10 | float4 color: COLOR; 11 | }; 12 | 13 | VOut VSMain(VIn input) 14 | { 15 | VOut output; 16 | output.position = input.position; 17 | output.color = input.color; 18 | return output; 19 | } 20 | -------------------------------------------------------------------------------- /samples/Core/02. SimpleQuad/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /samples/Core/03. SimpleInstancing/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/03. SimpleInstancing/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("SimpleInstancing")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("SimpleInstancing")] 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("a5a6e5cf-2fad-48e4-ab8d-7fb8eebeb15d")] 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 | -------------------------------------------------------------------------------- /samples/Core/03. SimpleInstancing/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 PSMain(float4 position : SV_POSITION, float4 color : COLOR) : SV_Target 2 | { 3 | return color; 4 | } -------------------------------------------------------------------------------- /samples/Core/03. SimpleInstancing/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | struct VOut 2 | { 3 | float4 position : SV_POSITION; 4 | float4 color : COLOR; 5 | }; 6 | 7 | struct VIn 8 | { 9 | // standard vertex elements 10 | float4 position: POSITION; 11 | float4 color: COLOR; 12 | 13 | // instance elements 14 | float4x4 translation: CUSTOM; 15 | }; 16 | 17 | VOut VSMain(VIn input) 18 | { 19 | VOut output; 20 | output.position = mul(input.translation, input.position); 21 | output.color = input.color; 22 | return output; 23 | } 24 | -------------------------------------------------------------------------------- /samples/Core/03. SimpleInstancing/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("SimpleCube")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("SimpleCube")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("05798c13-7482-441d-aa3f-264f18a1f1a5")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.0 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SimpleCube.Properties { 12 | /// 13 | /// A strongly-typed resource class, for looking up localized strings, etc. 14 | /// 15 | // This class was auto-generated by the StronglyTypedResourceBuilder 16 | // class via a tool like ResGen or Visual Studio. 17 | // To add or remove a member, edit your .ResX file then rerun ResGen 18 | // with the /str option, or rebuild your VS project. 19 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 20 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 21 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 22 | internal class Resources { 23 | 24 | private static global::System.Resources.ResourceManager resourceMan; 25 | 26 | private static global::System.Globalization.CultureInfo resourceCulture; 27 | 28 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 29 | internal Resources() { 30 | } 31 | 32 | /// 33 | /// Returns the cached ResourceManager instance used by this class. 34 | /// 35 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 36 | internal static global::System.Resources.ResourceManager ResourceManager { 37 | get { 38 | if (object.ReferenceEquals(resourceMan, null)) { 39 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SimpleCube.Properties.Resources", typeof(Resources).Assembly); 40 | resourceMan = temp; 41 | } 42 | return resourceMan; 43 | } 44 | } 45 | 46 | /// 47 | /// Overrides the current thread's CurrentUICulture property for all 48 | /// resource lookups using this strongly typed resource class. 49 | /// 50 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 51 | internal static global::System.Globalization.CultureInfo Culture { 52 | get { 53 | return resourceCulture; 54 | } 55 | set { 56 | resourceCulture = value; 57 | } 58 | } 59 | 60 | /// 61 | /// Looks up a localized string similar to . 62 | /// 63 | internal static string PixelShader { 64 | get { 65 | return ResourceManager.GetString("PixelShader", resourceCulture); 66 | } 67 | } 68 | 69 | /// 70 | /// Looks up a localized string similar to . 71 | /// 72 | internal static string VertexShader { 73 | get { 74 | return ResourceManager.GetString("VertexShader", resourceCulture); 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 PSMain(float4 position : SV_POSITION, float4 color : COLOR) : SV_Target 2 | { 3 | return color; 4 | } -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 worldViewProjection; 4 | } 5 | 6 | struct VOut 7 | { 8 | float4 position : SV_POSITION; 9 | float4 color : COLOR; 10 | }; 11 | 12 | struct VIn 13 | { 14 | float4 position: POSITION; 15 | float4 color: COLOR; 16 | }; 17 | 18 | VOut VSMain(VIn input) 19 | { 20 | VOut output; 21 | output.position = mul(input.position, worldViewProjection); 22 | output.color = input.color; 23 | return output; 24 | } 25 | -------------------------------------------------------------------------------- /samples/Core/04. SimpleCube/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/05. ManyCubes/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/05. ManyCubes/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("ManyCubes")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("ManyCubes")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("fa6b20ef-d717-4378-91f2-063c64bd7866")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /samples/Core/05. ManyCubes/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | float4 PSMain(float4 position : SV_POSITION, float4 color : COLOR) : SV_Target 2 | { 3 | return color; 4 | } -------------------------------------------------------------------------------- /samples/Core/05. ManyCubes/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 view; 4 | float4x4 projection; 5 | float secondsElapsed; 6 | } 7 | 8 | struct VOut 9 | { 10 | float4 position : SV_POSITION; 11 | float4 color : COLOR; 12 | }; 13 | 14 | struct VIn 15 | { 16 | float4 position: POSITION; 17 | float4 color: COLOR; 18 | 19 | // instance data 20 | float4 cubePosition: CUSTOM0; 21 | float4 cubeRotation: CUSTOM1; 22 | float4 cubeScale: CUSTOM2; 23 | }; 24 | 25 | static float PI = 3.1415926535897932384626433832795; 26 | float4x4 calculateRotationMatrix(float4 rotation) 27 | { 28 | float3 axis = normalize(float3(rotation.x, rotation.y, rotation.z)); 29 | float angle = secondsElapsed * rotation.w * PI * 2; 30 | float s = sin(angle); 31 | float c = cos(angle); 32 | float oc = 1.0 - c; 33 | return float4x4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 34 | oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, 35 | oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 36 | 0.0, 0.0, 0.0, 1.0); 37 | } 38 | 39 | VOut VSMain(VIn input) 40 | { 41 | float4 vertexPosition = float4(input.position.x * input.cubeScale.x, input.position.y * input.cubeScale.y, input.position.z * input.cubeScale.z, 1); 42 | float4x4 rotation = calculateRotationMatrix(input.cubeRotation); 43 | 44 | VOut output; 45 | output.position = mul(vertexPosition, rotation) + input.cubePosition; 46 | output.position = mul(mul(output.position, view), projection); 47 | output.color = input.color; 48 | return output; 49 | } 50 | -------------------------------------------------------------------------------- /samples/Core/05. ManyCubes/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/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("06. SimpleTexturedCube")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("06. SimpleTexturedCube")] 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("dfa547f3-c7b1-4393-911c-1d5d70d8678c")] 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 | -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | Texture2D ShaderTexture : register(t0); 2 | SamplerState Sampler : register(s0); 3 | 4 | struct VOut 5 | { 6 | float4 position : SV_POSITION; 7 | float2 texcoord: TEXCOORD; 8 | }; 9 | 10 | float4 PSMain(VOut input) : SV_Target 11 | { 12 | return ShaderTexture.Sample(Sampler, input.texcoord); 13 | } -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 worldViewProjection; 4 | } 5 | 6 | struct VOut 7 | { 8 | float4 position : SV_POSITION; 9 | float2 texcoord: TEXCOORD; 10 | }; 11 | 12 | struct VIn 13 | { 14 | float4 position: POSITION; 15 | float4 color: COLOR; 16 | float2 texcoord: TEXCOORD; 17 | }; 18 | 19 | VOut VSMain(VIn input) 20 | { 21 | VOut output; 22 | output.position = mul(input.position, worldViewProjection); 23 | output.texcoord = input.texcoord; 24 | return output; 25 | } 26 | -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/Textures/rabbit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/06. SimpleTexturedCube/Textures/rabbit.jpg -------------------------------------------------------------------------------- /samples/Core/06. SimpleTexturedCube/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/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("07. ManyTexturedCubes")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("07. ManyTexturedCubes")] 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("e013f94a-017e-4e23-8b09-3bc730595306")] 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 | -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | Texture2D ShaderTexture0 : register(t0); 2 | Texture2D ShaderTexture1 : register(t1); 3 | Texture2D ShaderTexture2 : register(t2); 4 | Texture2D ShaderTexture3 : register(t3); 5 | Texture2D ShaderTexture4 : register(t4); 6 | 7 | SamplerState Sampler : register(s0); 8 | 9 | struct VOut 10 | { 11 | float4 position : SV_POSITION; 12 | float2 texcoord: TEXCOORD; 13 | int tex: CUSTOM; 14 | }; 15 | 16 | float4 PSMain(VOut input) : SV_Target 17 | { 18 | float4 color; 19 | [flatten] 20 | switch (input.tex) 21 | { 22 | case 1: 23 | color = ShaderTexture1.Sample(Sampler, input.texcoord); 24 | break; 25 | case 2: 26 | color = ShaderTexture2.Sample(Sampler, input.texcoord); 27 | break; 28 | case 3: 29 | color = ShaderTexture3.Sample(Sampler, input.texcoord); 30 | break; 31 | case 4: 32 | color = ShaderTexture4.Sample(Sampler, input.texcoord); 33 | break; 34 | default: 35 | color = ShaderTexture0.Sample(Sampler, input.texcoord); 36 | break; 37 | } 38 | return color; 39 | } -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 view; 4 | float4x4 projection; 5 | float secondsElapsed; 6 | } 7 | 8 | struct VOut 9 | { 10 | float4 position : SV_POSITION; 11 | float2 texcoord: TEXCOORD; 12 | int tex : CUSTOM; 13 | }; 14 | 15 | struct VIn 16 | { 17 | float4 position: POSITION; 18 | float4 color: COLOR; 19 | float2 texcoord: TEXCOORD; 20 | float2 pad0: PADDING0; 21 | 22 | // instance data 23 | float4 cubePosition: CUSTOM0; 24 | float4 cubeRotation: CUSTOM1; 25 | float4 cubeScale: CUSTOM2; 26 | int tex: CUSTOM3; 27 | float3 pad1: PADDING1; 28 | }; 29 | 30 | static float PI = 3.1415926535897932384626433832795; 31 | float4x4 calculateRotationMatrix(float4 rotation) 32 | { 33 | float3 axis = normalize(float3(rotation.x, rotation.y, rotation.z)); 34 | float angle = secondsElapsed * rotation.w * PI * 2; 35 | float s = sin(angle); 36 | float c = cos(angle); 37 | float oc = 1.0 - c; 38 | return float4x4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 39 | oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, 40 | oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 41 | 0.0, 0.0, 0.0, 1.0); 42 | } 43 | 44 | VOut VSMain(VIn input) 45 | { 46 | float4 vertexPosition = float4(input.position.x * input.cubeScale.x, input.position.y * input.cubeScale.y, input.position.z * input.cubeScale.z, 1); 47 | float4x4 rotation = calculateRotationMatrix(input.cubeRotation); 48 | 49 | VOut output; 50 | output.position = mul(vertexPosition, rotation) + input.cubePosition; 51 | output.position = mul(mul(output.position, view), projection); 52 | output.texcoord = input.texcoord; 53 | output.tex = input.tex; 54 | return output; 55 | } 56 | -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Textures/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/07. ManyTexturedCubes/Textures/cat.jpg -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Textures/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/07. ManyTexturedCubes/Textures/dog.jpg -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Textures/fish.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/07. ManyTexturedCubes/Textures/fish.jpg -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Textures/rabbit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/07. ManyTexturedCubes/Textures/rabbit.jpg -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/Textures/snail.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/07. ManyTexturedCubes/Textures/snail.jpg -------------------------------------------------------------------------------- /samples/Core/07. ManyTexturedCubes/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/App/CubeInstance.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace FreeLookCamera.App 5 | { 6 | [StructLayout(LayoutKind.Sequential)] 7 | struct CubeInstance 8 | { 9 | public Vector4 Position; 10 | public Vector4 Rotation; 11 | public Vector4 Scale; 12 | public int Texture; 13 | public Vector3 _pad0; 14 | } 15 | } -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/App/SceneData.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace FreeLookCamera.App 5 | { 6 | [StructLayout(LayoutKind.Sequential)] 7 | struct SceneData 8 | { 9 | public Matrix4x4 View; 10 | public Matrix4x4 Projection; 11 | public float SecondsElapsed; 12 | private readonly Vector3 _pad0; 13 | } 14 | } -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/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("08. SimpleScene")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("08. SimpleScene")] 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("c8381183-8441-4cbb-817f-7a51a12595e2")] 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 | -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | Texture2D ShaderTexture0 : register(t0); 2 | Texture2D ShaderTexture1 : register(t1); 3 | Texture2D ShaderTexture2 : register(t2); 4 | Texture2D ShaderTexture3 : register(t3); 5 | Texture2D ShaderTexture4 : register(t4); 6 | 7 | SamplerState Sampler : register(s0); 8 | 9 | struct VOut 10 | { 11 | float4 position : SV_POSITION; 12 | float2 texcoord: TEXCOORD; 13 | int tex: CUSTOM; 14 | }; 15 | 16 | float4 PSMain(VOut input) : SV_Target 17 | { 18 | float4 color; 19 | [flatten] 20 | switch (input.tex) 21 | { 22 | case 1: 23 | color = ShaderTexture1.Sample(Sampler, input.texcoord); 24 | break; 25 | case 2: 26 | color = ShaderTexture2.Sample(Sampler, input.texcoord); 27 | break; 28 | case 3: 29 | color = ShaderTexture3.Sample(Sampler, input.texcoord); 30 | break; 31 | case 4: 32 | color = ShaderTexture4.Sample(Sampler, input.texcoord); 33 | break; 34 | default: 35 | color = ShaderTexture0.Sample(Sampler, input.texcoord); 36 | break; 37 | } 38 | return color; 39 | } -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 view; 4 | float4x4 projection; 5 | float secondsElapsed; 6 | } 7 | 8 | struct VOut 9 | { 10 | float4 position : SV_POSITION; 11 | float2 texcoord: TEXCOORD; 12 | int tex : CUSTOM; 13 | }; 14 | 15 | struct VIn 16 | { 17 | float4 position: POSITION; 18 | float4 color: COLOR; 19 | float2 texcoord: TEXCOORD; 20 | float2 pad0: PADDING0; 21 | 22 | // instance data 23 | float4 cubePosition: CUSTOM0; 24 | float4 cubeRotation: CUSTOM1; 25 | float4 cubeScale: CUSTOM2; 26 | int tex: CUSTOM3; 27 | float3 pad1: PADDING1; 28 | }; 29 | 30 | static float PI = 3.1415926535897932384626433832795; 31 | float4x4 calculateRotationMatrix(float4 rotation) 32 | { 33 | float3 axis = normalize(float3(rotation.x, rotation.y, rotation.z)); 34 | float angle = secondsElapsed * rotation.w * PI * 2; 35 | float s = sin(angle); 36 | float c = cos(angle); 37 | float oc = 1.0 - c; 38 | return float4x4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 39 | oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, 40 | oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 41 | 0.0, 0.0, 0.0, 1.0); 42 | } 43 | 44 | VOut VSMain(VIn input) 45 | { 46 | float4 vertexPosition = float4(input.position.x * input.cubeScale.x, input.position.y * input.cubeScale.y, input.position.z * input.cubeScale.z, 1); 47 | float4x4 rotation = calculateRotationMatrix(input.cubeRotation); 48 | 49 | VOut output; 50 | 51 | // switch between the following two lines to enable or disable cube rotation 52 | output.position = vertexPosition + input.cubePosition; 53 | //output.position = mul(vertexPosition, rotation) + input.cubePosition; 54 | 55 | output.position = mul(mul(output.position, view), projection); 56 | output.texcoord = input.texcoord; 57 | output.tex = input.tex; 58 | return output; 59 | } 60 | -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Textures/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/08. FreeLookCamera/Textures/cat.jpg -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Textures/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/08. FreeLookCamera/Textures/dog.jpg -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Textures/fish.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/08. FreeLookCamera/Textures/fish.jpg -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Textures/rabbit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/08. FreeLookCamera/Textures/rabbit.jpg -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/Textures/snail.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/08. FreeLookCamera/Textures/snail.jpg -------------------------------------------------------------------------------- /samples/Core/08. FreeLookCamera/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/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("09. MultiTexturedCube")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("09. MultiTexturedCube")] 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("93b7da4f-122a-493a-aa55-4518e6b14052")] 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 | -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | Texture2DArray ShaderTexture : register(t0); 2 | 3 | SamplerState Sampler : register(s0); 4 | 5 | struct VOut 6 | { 7 | float4 position : SV_POSITION; 8 | float2 texcoord: TEXCOORD; 9 | uint face : CUSTOM; 10 | }; 11 | 12 | float4 PSMain(VOut input) : SV_Target 13 | { 14 | int textureIndex = 0; 15 | [flatten] 16 | switch (input.face) 17 | { 18 | case 0: textureIndex = 1; break; // front 19 | case 1: textureIndex = 1; break; // back 20 | case 2: textureIndex = 1; break; // left 21 | case 3: textureIndex = 1; break; // right 22 | case 4: textureIndex = 0; break; // top 23 | case 5: textureIndex = 2; break; // bottom 24 | } 25 | return ShaderTexture.Sample(Sampler, float3(input.texcoord, textureIndex)); 26 | } -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | cbuffer ViewData : register(b0) 2 | { 3 | float4x4 worldViewProjection; 4 | } 5 | 6 | struct VOut 7 | { 8 | float4 position: SV_POSITION; 9 | float2 texcoord: TEXCOORD; 10 | uint face: CUSTOM; 11 | }; 12 | 13 | struct VIn 14 | { 15 | float4 position: POSITION; 16 | float4 color: COLOR; 17 | float2 texcoord: TEXCOORD; 18 | uint face: CUSTOM; 19 | }; 20 | 21 | VOut VSMain(VIn input) 22 | { 23 | VOut output; 24 | output.position = mul(input.position, worldViewProjection); 25 | output.texcoord = input.texcoord; 26 | output.face = input.face; 27 | return output; 28 | } 29 | -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/Textures/dirt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/09. MultiTexturedCube/Textures/dirt.png -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/Textures/grass-side.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/09. MultiTexturedCube/Textures/grass-side.png -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/Textures/grass-top.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Core/09. MultiTexturedCube/Textures/grass-top.png -------------------------------------------------------------------------------- /samples/Core/09. MultiTexturedCube/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Core/10. DynamicTexture/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /samples/Core/10. DynamicTexture/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("10. DynamicTexture")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("10. DynamicTexture")] 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("96c2d0bb-322f-4fd9-831d-de0960c5ef25")] 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 | -------------------------------------------------------------------------------- /samples/Core/10. DynamicTexture/Resources/PixelShader.hlsl: -------------------------------------------------------------------------------- 1 | Texture2D ShaderTexture : register(t0); 2 | SamplerState Sampler : register(s0); 3 | 4 | struct VOut 5 | { 6 | float4 position : SV_POSITION; 7 | float4 color : COLOR; 8 | float2 texcoord: TEXCOORD; 9 | }; 10 | 11 | float4 PSMain(VOut input) : SV_Target 12 | { 13 | return ShaderTexture.Sample(Sampler, input.texcoord); 14 | } -------------------------------------------------------------------------------- /samples/Core/10. DynamicTexture/Resources/VertexShader.hlsl: -------------------------------------------------------------------------------- 1 | struct VOut 2 | { 3 | float4 position : SV_POSITION; 4 | float4 color : COLOR; 5 | float2 texcoord: TEXCOORD; 6 | }; 7 | 8 | struct VIn 9 | { 10 | float4 position: POSITION; 11 | float4 color: COLOR; 12 | float2 texcoord: TEXCOORD; 13 | }; 14 | 15 | VOut VSMain(VIn input) 16 | { 17 | VOut output; 18 | output.position = input.position; 19 | output.color = input.color; 20 | output.texcoord = input.texcoord; 21 | return output; 22 | } 23 | -------------------------------------------------------------------------------- /samples/Core/10. DynamicTexture/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /samples/Images/cube-multitexture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cube-multitexture.gif -------------------------------------------------------------------------------- /samples/Images/cube-rainbow.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cube-rainbow.gif -------------------------------------------------------------------------------- /samples/Images/cube.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cube.gif -------------------------------------------------------------------------------- /samples/Images/cubes-freelook.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cubes-freelook.gif -------------------------------------------------------------------------------- /samples/Images/cubes-rainbow.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cubes-rainbow.gif -------------------------------------------------------------------------------- /samples/Images/cubes.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axefrog/Grasshopper/c3e79707560c36fc62ba96dc604a8f3cb15dc45f/samples/Images/cubes.gif -------------------------------------------------------------------------------- /samples/todo.txt: -------------------------------------------------------------------------------- 1 | Developer notes only. Please ignore. 2 | 3 | ## Core Engine 4 | 5 | - input 6 | - compositing of render targets (UI over main scene, picture-in-picture, etc.) 7 | - expose custom vertex formats 8 | - improve shader system by creating a modular (text-based) foundation for a blueprints-style system down the track 9 | - load meshes from disk (assimp.net -> custom grasshopper format) 10 | - scene management 11 | - switchable cameras 12 | - freelook component tied to a camera, using entity component system 13 | 14 | ## GridWorld 15 | 16 | - simple room scene 17 | 18 | 19 | ## Meta 20 | 21 | - add XML comments to all code 22 | - create a Github pages site explaining all the code in depth, stepping through from absolute basics, and including private internals 23 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Graphics/Primitives/Cube.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Numerics; 4 | using Grasshopper.Graphics.Primitives; 5 | using Grasshopper.Graphics.SceneManagement; 6 | 7 | namespace Grasshopper.Procedural.Graphics.Primitives 8 | { 9 | public static class Cube 10 | { 11 | public static Mesh Unit(string id) 12 | { 13 | return Unit(id, Color.White); 14 | } 15 | 16 | public static Mesh Unit(string id, Color color) 17 | { 18 | return Unit(id, color, color, color, color, color, color, color, color); 19 | } 20 | 21 | public static Mesh Unit(string id, Color a, Color b, Color c, Color d, Color e, Color f, Color g, Color h) 22 | { 23 | var front = Quad.XY(-0.5f, 0.5f, -0.5f, 0.5f, 0.5f, a, b, c, d); 24 | var back = Quad.XY( 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, e, f, g, h); 25 | var left = Quad.YZ(-0.5f, 0.5f, -0.5f, 0.5f, -0.5f, f, a, d, g); 26 | var right = Quad.YZ(-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, b, e, h, c); 27 | var top = Quad.XZ(-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, f, e, b, a); 28 | var bottom = Quad.XZ(-0.5f, 0.5f, -0.5f, 0.5f, -0.5f, d, c, e, f); 29 | 30 | return new Mesh(id, new[] { front, back, left, right, top, bottom }.SelectMany(m => m)); 31 | } 32 | 33 | public static IEnumerable Generate() 34 | { 35 | yield return new VertexMetadata() { Color = Color.Red, FaceIndex = 0, Position = new Vector4()}; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Graphics/Primitives/CubeBuilder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Primitives; 5 | 6 | namespace Grasshopper.Procedural.Graphics.Primitives 7 | { 8 | public class CubeBuilder : IEnumerable 9 | { 10 | private Color[] _colors; 11 | private float _size = 1.0f; 12 | 13 | public static CubeBuilder New { get { return new CubeBuilder(); } } 14 | 15 | public CubeBuilder Colors(params Color[] colors) 16 | { 17 | if(colors.Length == 0) 18 | colors = null; 19 | _colors = colors ?? new[] { Color.Red, Color.LimeGreen, Color.Yellow, Color.Orange, Color.Blue, Color.Magenta, Color.White, Color.Cyan }; 20 | return this; 21 | } 22 | 23 | public CubeBuilder Size(float length = 1.0f) 24 | { 25 | _size = length; 26 | return this; 27 | } 28 | 29 | public IEnumerator GetEnumerator() 30 | { 31 | if(_colors == null) Colors(); 32 | if(_colors.Length != 8) 33 | { 34 | var colors = _colors; 35 | _colors = new Color[8]; 36 | for(var i = 0; i < 8; i++) 37 | _colors[i] = colors[i % colors.Length]; 38 | } 39 | Color a = _colors[0], b = _colors[1], c = _colors[2], d = _colors[3], 40 | e = _colors[4], f = _colors[5], g = _colors[6], h = _colors[7]; 41 | var half = _size / 2; 42 | return new[] 43 | { 44 | QuadBuilder.New.Face(0).XY(-half, half, -half, half, half).Colors(a, b, c, d), 45 | QuadBuilder.New.Face(1).XY( half, -half, -half, half, -half).Colors(e, f, g, h), 46 | QuadBuilder.New.Face(2).YZ(-half, half, -half, half, -half).Colors(f, a, d, g), 47 | QuadBuilder.New.Face(3).YZ(-half, half, half, -half, half).Colors(b, e, h, c), 48 | QuadBuilder.New.Face(4).XZ(-half, half, half, -half, half).Colors(f, e, b, a), 49 | QuadBuilder.New.Face(5).XZ(-half, half, -half, half, -half).Colors(d, c, h, g), 50 | } 51 | .SelectMany(v => v) 52 | .GetEnumerator(); 53 | } 54 | 55 | IEnumerator IEnumerable.GetEnumerator() 56 | { 57 | return GetEnumerator(); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Graphics/Primitives/TriangleBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Numerics; 5 | using Grasshopper.Graphics.Primitives; 6 | 7 | namespace Grasshopper.Procedural.Graphics.Primitives 8 | { 9 | public class TriangleBuilder : IEnumerable 10 | { 11 | private int _vertexCount; 12 | private readonly Vector3[] _xyz = new Vector3[3]; 13 | private readonly Vector2[] _uv = new Vector2[3]; 14 | private Color[] _colors; 15 | private uint _faceIndex; 16 | private uint _triangleIndex; 17 | 18 | public static TriangleBuilder New { get { return new TriangleBuilder(); } } 19 | 20 | public TriangleBuilder Colors(params Color[] colors) 21 | { 22 | if(colors.Length != 3) 23 | throw new ArgumentException("Triangle builder requires exactly 3 colors to be specified. You specified " + colors.Length + "."); 24 | _colors = colors; 25 | return this; 26 | } 27 | 28 | public TriangleBuilder Vertex(Vector3 pos, Vector2 uv) 29 | { 30 | if(_vertexCount == 3) _vertexCount = 0; 31 | _xyz[_vertexCount] = pos; 32 | _uv[_vertexCount++] = uv; 33 | return this; 34 | } 35 | 36 | public TriangleBuilder Vertex(float x, float y, float z, float u, float v) 37 | { 38 | if(_vertexCount == 3) _vertexCount = 0; 39 | _xyz[_vertexCount] = new Vector3(x, y, z); 40 | _uv[_vertexCount++] = new Vector2(u, v); 41 | return this; 42 | } 43 | 44 | public TriangleBuilder FaceIndex(uint faceIndex) 45 | { 46 | _faceIndex = faceIndex; 47 | return this; 48 | } 49 | 50 | public TriangleBuilder TriangleIndex(uint triangleIndex) 51 | { 52 | _triangleIndex = triangleIndex; 53 | return this; 54 | } 55 | 56 | public IEnumerator GetEnumerator() 57 | { 58 | if(_colors == null) Colors(); 59 | var colorIndex = 0; 60 | for(var i = 0; i < 3; i++) 61 | { 62 | yield return new VertexMetadata(_xyz[i].X, _xyz[i].Y, _xyz[i].Z, _uv[i].X, _uv[i].Y, _colors[colorIndex], _faceIndex, _triangleIndex); 63 | if(++colorIndex >= _colors.Length) colorIndex = 0; 64 | } 65 | } 66 | 67 | IEnumerator IEnumerable.GetEnumerator() 68 | { 69 | return GetEnumerator(); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Graphics/Primitives/VertexMetadata.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using Grasshopper.Graphics.Primitives; 3 | 4 | namespace Grasshopper.Procedural.Graphics.Primitives 5 | { 6 | public class VertexMetadata 7 | { 8 | public VertexMetadata() 9 | { 10 | } 11 | 12 | public VertexMetadata(float x, float y, float z, float u, float v, Color color, uint faceIndex, uint triangleIndex) 13 | { 14 | Position = new Vector4(x, y, z, 1f); 15 | TextureCoordinate = new Vector2(u, v); 16 | Color = color; 17 | FaceIndex = faceIndex; 18 | TriangleIndex = triangleIndex; 19 | } 20 | 21 | public Vector4 Position { get; set; } 22 | public Vector2 TextureCoordinate { get; set; } 23 | public Color Color { get; set; } 24 | public uint FaceIndex { get; set; } 25 | public uint TriangleIndex { get; set; } 26 | 27 | protected bool Equals(VertexMetadata other) 28 | { 29 | return Position.Equals(other.Position) && TextureCoordinate.Equals(other.TextureCoordinate) && Color.Equals(other.Color) && FaceIndex == other.FaceIndex && TriangleIndex == other.TriangleIndex; 30 | } 31 | 32 | public override bool Equals(object obj) 33 | { 34 | if(ReferenceEquals(null, obj)) return false; 35 | if(ReferenceEquals(this, obj)) return true; 36 | if(obj.GetType() != this.GetType()) return false; 37 | return Equals((VertexMetadata)obj); 38 | } 39 | 40 | public override int GetHashCode() 41 | { 42 | unchecked 43 | { 44 | int hashCode = Position.GetHashCode(); 45 | hashCode = (hashCode * 397) ^ TextureCoordinate.GetHashCode(); 46 | hashCode = (hashCode * 397) ^ Color.GetHashCode(); 47 | hashCode = (hashCode * 397) ^ (int)FaceIndex; 48 | hashCode = (hashCode * 397) ^ (int)TriangleIndex; 49 | return hashCode; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Graphics/Primitives/VertexMetadataExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Rendering; 5 | using Grasshopper.Graphics.SceneManagement; 6 | 7 | namespace Grasshopper.Procedural.Graphics.Primitives 8 | { 9 | public static class VertexMetadataExtensions 10 | { 11 | public static Mesh ToMesh(this IEnumerable vertexData, string id, Func createVertex) 12 | where T : struct 13 | { 14 | var map = new Dictionary(); 15 | uint nextIndex = 0; 16 | var vertices = new List(); 17 | var indices = new List(); 18 | 19 | foreach(var vertex in vertexData.Select(createVertex)) 20 | { 21 | uint index; 22 | if(!map.TryGetValue(vertex, out index)) 23 | { 24 | map.Add(vertex, index = nextIndex++); 25 | vertices.Add(vertex); 26 | } 27 | indices.Add(index); 28 | } 29 | 30 | return new Mesh(id, vertices.ToArray(), indices.ToArray(), DrawType.Triangles); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Grasshopper.Procedural")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Grasshopper.Procedural")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("093e8aaa-8f3b-4671-a2a6-6ca4678fc8b1")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/notes.txt: -------------------------------------------------------------------------------- 1 | ## Note about procedural generation and custom vertex formats 2 | 3 | When generating procedural meshes, we could be using any combination of 4 | different vertex elements depending on our needs. A generic procedural 5 | generator can't know in advance what our vertex requirements are, and must 6 | therefore generate an intermediate object which we can convert to our custom 7 | vertex format. The intermediate object will contain all the information that 8 | we might want about each vertex, and we can select the elements we want when 9 | we perform the conversion to our preferred vertex format. -------------------------------------------------------------------------------- /src/core-engine/Grasshopper.Procedural/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Core/RateLimiter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Core 4 | { 5 | public class RateLimiter 6 | { 7 | private readonly long _minWaitTime; 8 | private readonly long _start = DateTime.Now.Ticks; 9 | 10 | // This holds the next tick count at which CanRun returns true. We always increment this by the exact desired 11 | // single-frame duration to ensure we track the frame count limit as accurately as possible and to prevent us 12 | // slowly getting out of sync and thus not maintaining the desired frame rate. 13 | private long _next; 14 | 15 | // Some scenarios may cause temporary performance issues and may cause CanRun to not be called for a long 16 | // period of time, which means that when performance is restored, the game would appear to run in fast forward 17 | // until _next catches up to the current elapsed time. _threshold is maintained exactly one additional frame 18 | // worth of ticks ahead of _next and if has already been exceeded when CanRun is checked, we synchronize _next 19 | // with the current elapsed time to ensure frame rate stays consistent. 20 | private long _threshold; 21 | 22 | public RateLimiter(double maxPerSecond) 23 | { 24 | _minWaitTime = Convert.ToInt64(TimeSpan.TicksPerSecond / maxPerSecond); 25 | _next = 0; 26 | _threshold = 0; 27 | } 28 | 29 | public bool Ready() 30 | { 31 | var ticks = DateTime.Now.Ticks - _start; 32 | if(ticks < _next) 33 | return false; 34 | if(ticks > _threshold) 35 | _next = ticks + _minWaitTime; 36 | else 37 | _next += _minWaitTime; 38 | _threshold = _next + _minWaitTime; 39 | return true; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Core/TickCounter.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace Grasshopper.Core 4 | { 5 | public class TickCounter 6 | { 7 | private readonly int _maxSamples; 8 | int tickIndex; 9 | long tickSum; 10 | readonly long[] tickList; 11 | readonly Stopwatch clock; 12 | long frameCount; 13 | 14 | public TickCounter(int maxSamples = 100) 15 | { 16 | _maxSamples = maxSamples; 17 | tickList = new long[maxSamples]; 18 | clock = Stopwatch.StartNew(); 19 | } 20 | 21 | double CalcAverageTick(long newtick) 22 | { 23 | tickSum -= tickList[tickIndex]; /* subtract value falling off */ 24 | tickSum += newtick; /* add new value */ 25 | tickList[tickIndex] = newtick; /* save new value so it can be subtracted later */ 26 | if(++tickIndex == _maxSamples) /* inc buffer index */ 27 | tickIndex = 0; 28 | 29 | if(frameCount < _maxSamples) 30 | return (double)tickSum / frameCount; 31 | return (double)tickSum / _maxSamples; 32 | } 33 | 34 | internal void Tick() 35 | { 36 | frameCount++; 37 | var averageTick = CalcAverageTick(clock.ElapsedTicks) / Stopwatch.Frequency; 38 | TicksPerSecond = 1d / averageTick; 39 | AverageTickDuration = averageTick * 1000d; 40 | clock.Restart(); 41 | } 42 | 43 | public double TicksPerSecond { get; private set; } 44 | public double AverageTickDuration { get; private set; } 45 | } 46 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/FrameContext.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper 2 | { 3 | public class FrameContext 4 | { 5 | private float _prevElapsed; 6 | 7 | public FrameContext(GrasshopperApp app) 8 | { 9 | App = app; 10 | NextFrame(); 11 | } 12 | 13 | internal void NextFrame() 14 | { 15 | ElapsedSeconds = App.ElapsedSeconds; 16 | DeltaSeconds = ElapsedSeconds - _prevElapsed; 17 | _prevElapsed = App.ElapsedSeconds; 18 | FramesPerSecond = (float)App.TickCounter.TicksPerSecond; 19 | AverageFrameDuration = (float)App.TickCounter.AverageTickDuration; 20 | FrameNumber++; 21 | } 22 | 23 | public GrasshopperApp App { get; private set; } 24 | public float ElapsedSeconds { get; private set; } 25 | public float DeltaSeconds { get; private set; } 26 | public float FramesPerSecond { get; private set; } 27 | public float AverageFrameDuration { get; private set; } 28 | public long FrameNumber { get; private set; } 29 | } 30 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/IDeviceManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Graphics 4 | { 5 | public interface IDeviceManager : IDisposable 6 | { 7 | event Action Initialized; 8 | void Initialize(); 9 | } 10 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/IGraphicsContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Materials; 3 | using Grasshopper.Graphics.Rendering; 4 | using Grasshopper.Graphics.Rendering.Blending; 5 | using Grasshopper.Graphics.Rendering.Buffers; 6 | using Grasshopper.Graphics.Rendering.Rasterization; 7 | 8 | namespace Grasshopper.Graphics 9 | { 10 | /// 11 | /// This is the grand overlord of rendering with a single graphics device. Multiple windows, renderers 12 | /// and other device-dependent resources are all directly or indirectly managed by this class. Multiple 13 | /// graphics contexts can exist, but they cannot share resources between each other. 14 | /// 15 | public interface IGraphicsContext : IDisposable 16 | { 17 | void Initialize(); 18 | IDeviceManager DeviceManager { get; } 19 | ITextureResourceManager TextureResourceManager { get; } 20 | ITextureSamplerManager TextureSamplerManager { get; } 21 | IMaterialManager MaterialManager { get; } 22 | IBlendStateManager BlendStateManager { get; } 23 | IRasterizerStateManager RasterizerStateManager { get; } 24 | 25 | IRenderTargetFactory RenderTargetFactory { get; } 26 | IVertexBufferManagerFactory VertexBufferManagerFactory { get; } 27 | IIndexBufferManagerFactory IndexBufferManagerFactory { get; } 28 | IConstantBufferManagerFactory ConstantBufferManagerFactory { get; } 29 | } 30 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/IGraphicsContextFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics 2 | { 3 | public interface IGraphicsContextFactory 4 | { 5 | IGraphicsContext CreateContext(bool enableDebugMode = false); 6 | } 7 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/IMaterialManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | 4 | namespace Grasshopper.Graphics.Materials 5 | { 6 | public interface IMaterialManager : IPlatformResourceManager 7 | { 8 | Material Create(string id); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ITextureResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.Graphics.Materials 6 | { 7 | public interface ITextureResource : IIndexActivatablePlatformResource 8 | { 9 | TextureType TextureType { get; } 10 | TextureDataSource DataSource { get; } 11 | PixelFormat PixelFormat { get; } 12 | } 13 | 14 | public interface ITexture1DResource : ITextureResource 15 | { 16 | int Size { get; } 17 | } 18 | 19 | public interface ITexture2DResource : ITextureResource 20 | { 21 | int Width { get; } 22 | int Height { get; } 23 | } 24 | 25 | public interface IDynamicTexture2DResource : ITexture2DResource 26 | { 27 | IShaderResourceWriter BeginWrite(); 28 | } 29 | 30 | public interface IShaderResourceWriter : IDisposable 31 | { 32 | void Write(T value) where T : struct; 33 | void WriteRange(T[] values) where T : struct; 34 | void Seek(long offset, SeekOrigin origin); 35 | } 36 | 37 | //public interface IRenderTargetTexture : ITexture2DResource 38 | //{ 39 | //} 40 | 41 | public interface ITexture2DArray : ITexture2DResource 42 | { 43 | } 44 | 45 | public enum TextureType 46 | { 47 | //Texture1D, 48 | Texture2D, 49 | //Texture3D, 50 | //Texture1DArray, 51 | Texture2DArray, 52 | //TextureCube 53 | } 54 | 55 | public enum TextureDataSource 56 | { 57 | /// 58 | /// The pixel data comes from the file system 59 | /// 60 | FileSystem, 61 | /// 62 | /// The pixel data is written directly by the software 63 | /// 64 | Dynamic, 65 | /// 66 | /// The pixel data results from draw calls to a render target 67 | /// 68 | RenderTarget, 69 | /// 70 | /// The pixel data is acquired internally by the system from reference sources 71 | /// 72 | Internal 73 | } 74 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ITextureResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.Graphics.Materials 6 | { 7 | public interface ITextureResourceManager : IIndexActivatablePlatformResourceManager 8 | { 9 | ITextureResource Create2DFromFile(string id, string path); 10 | ITextureResource Create2DFromFile(string id, IFileSource fileSource); 11 | IDynamicTexture2DResource Create2DDynamic(string id, int width, int height, PixelFormat pixelFormat = PixelFormat.R8G8B8A8_UNorm); 12 | ITextureResource Create2DArray(string textureArrayId, params string[] sourceTextureIds); 13 | } 14 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ITextureSampler.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Materials 4 | { 5 | public interface ITextureSampler : IIndexActivatablePlatformResource 6 | { 7 | TextureSamplerSettings Settings { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ITextureSamplerManager.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Materials 4 | { 5 | public interface ITextureSamplerManager : IIndexActivatablePlatformResourceManager 6 | { 7 | ITextureSampler Create(string id, TextureSamplerSettings settings); 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/Material.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.Graphics.Materials 6 | { 7 | public abstract class Material : ActivatablePlatformResource 8 | { 9 | protected Material(string id) : base(id) 10 | { 11 | Textures = new List(); 12 | Samplers = new List(); 13 | } 14 | 15 | public bool IsTranslucent { get; set; } 16 | public List Textures { get; set; } 17 | public List Samplers { get; set; } 18 | public VertexShaderSpec VertexShaderSpec { get; set; } 19 | public PixelShaderSpec PixelShaderSpec { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/PixelFormat.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public enum PixelFormat 4 | { 5 | Unknown = 0, 6 | R32G32B32A32_Float, 7 | R8G8B8A8_UNorm, 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/PixelShaderSpec.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public class PixelShaderSpec : ShaderSpec 4 | { 5 | public TextureSamplerSettings SamplerSettings { get; private set; } 6 | 7 | public PixelShaderSpec(string source, TextureSamplerSettings samplerSettings = null) : base(source) 8 | { 9 | SamplerSettings = samplerSettings ?? TextureSamplerSettings.Default(); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ShaderInputElementFormat.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public enum ShaderInputElementFormat 4 | { 5 | Int32, 6 | UInt32, 7 | Float, 8 | Float2, 9 | Float3, 10 | Float4, 11 | Matrix4x4 12 | } 13 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ShaderInputElementPurpose.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Graphics.Materials 4 | { 5 | public enum ShaderInputElementPurpose 6 | { 7 | Position, 8 | Color, 9 | TextureCoordinate, 10 | Normal, 11 | Padding, 12 | Custom 13 | } 14 | 15 | public static class ShaderInputElementPurposeExtensions 16 | { 17 | public static ShaderInputElementSpec CreateSpec(this ShaderInputElementPurpose purpose) 18 | { 19 | switch(purpose) 20 | { 21 | case ShaderInputElementPurpose.Position: 22 | return new ShaderInputElementSpec(ShaderInputElementFormat.Float4, purpose); 23 | case ShaderInputElementPurpose.Normal: 24 | return new ShaderInputElementSpec(ShaderInputElementFormat.Float4, purpose); 25 | case ShaderInputElementPurpose.TextureCoordinate: 26 | return new ShaderInputElementSpec(ShaderInputElementFormat.Float2, purpose); 27 | case ShaderInputElementPurpose.Color: 28 | return new ShaderInputElementSpec(ShaderInputElementFormat.Float4, purpose); 29 | default: 30 | throw new NotSupportedException("Cannot automatically generate input element spec from this value"); 31 | } 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ShaderInputElementSpec.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public class ShaderInputElementSpec 4 | { 5 | public ShaderInputElementFormat Format { get; private set; } 6 | public ShaderInputElementPurpose Purpose { get; private set; } 7 | 8 | public ShaderInputElementSpec(ShaderInputElementFormat format, ShaderInputElementPurpose purpose) 9 | { 10 | Format = format; 11 | Purpose = purpose; 12 | } 13 | 14 | public ShaderInputElementSpec(ShaderInputElementFormat format) 15 | { 16 | Format = format; 17 | Purpose = ShaderInputElementPurpose.Custom; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/ShaderSpec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Graphics.Materials 4 | { 5 | public abstract class ShaderSpec 6 | { 7 | protected ShaderSpec(string source) 8 | : this(Guid.NewGuid().ToString(), source) 9 | { 10 | } 11 | 12 | protected ShaderSpec(string id, string source) 13 | { 14 | Id = id; 15 | Source = source; 16 | } 17 | 18 | public string Id { get; private set; } 19 | public string Source { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/TextureFiltering.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public enum TextureFiltering 4 | { 5 | MinMagMipPoint, 6 | MinMagPointMipLinear, 7 | MinPointMagLinearMipPoint, 8 | MinPointMagMipLinear, 9 | MinLinearMagMipPoint, 10 | MinLinearMagPointMipLinear, 11 | MinMagLinearMipPoint, 12 | MinMagMipLinear, 13 | Anisotropic, 14 | ComparisonMinMagMipPoint, 15 | ComparisonMinMagPointMipLinear, 16 | ComparisonMinPointMagLinearMipPoint, 17 | ComparisonMinPointMagMipLinear, 18 | ComparisonMinLinearMagMipPoint, 19 | ComparisonMinLinearMagPointMipLinear, 20 | ComparisonMinMagLinearMipPoint, 21 | ComparisonMinMagMipLinear, 22 | ComparisonAnisotropic, 23 | MinimumMinMagMipPoint, 24 | MinimumMinMagPointMipLinear, 25 | MinimumMinPointMagLinearMipPoint, 26 | MinimumMinPointMagMipLinear, 27 | MinimumMinLinearMagMipPoint, 28 | MinimumMinLinearMagPointMipLinear, 29 | MinimumMinMagLinearMipPoint, 30 | MinimumMinMagMipLinear, 31 | MinimumAnisotropic, 32 | MaximumMinMagMipPoint, 33 | MaximumMinMagPointMipLinear, 34 | MaximumMinPointMagLinearMipPoint, 35 | MaximumMinPointMagMipLinear, 36 | MaximumMinLinearMagMipPoint, 37 | MaximumMinLinearMagPointMipLinear, 38 | MaximumMinMagLinearMipPoint, 39 | MaximumMinMagMipLinear, 40 | MaximumAnisotropic, 41 | } 42 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/TextureSamplerSettings.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public class TextureSamplerSettings 4 | { 5 | private TextureSamplerSettings() 6 | { 7 | } 8 | 9 | public TextureSamplerSettings(TextureWrapping wrapU, TextureWrapping wrapV, TextureWrapping wrapW, TextureFiltering filter) 10 | { 11 | WrapU = wrapU; 12 | WrapV = wrapV; 13 | WrapW = wrapW; 14 | Filter = filter; 15 | } 16 | 17 | public TextureWrapping WrapU { get; private set; } 18 | public TextureWrapping WrapV { get; private set; } 19 | public TextureWrapping WrapW { get; private set; } 20 | public TextureFiltering Filter { get; private set; } 21 | 22 | public static TextureSamplerSettings Default() 23 | { 24 | return new TextureSamplerSettings 25 | { 26 | WrapU = TextureWrapping.Clamp, 27 | WrapV = TextureWrapping.Clamp, 28 | WrapW = TextureWrapping.Clamp, 29 | Filter = TextureFiltering.MinMagMipLinear 30 | }; 31 | } 32 | 33 | protected bool Equals(TextureSamplerSettings other) 34 | { 35 | return WrapU == other.WrapU && WrapV == other.WrapV && WrapW == other.WrapW && Filter == other.Filter; 36 | } 37 | 38 | public override bool Equals(object obj) 39 | { 40 | if(ReferenceEquals(null, obj)) return false; 41 | if(ReferenceEquals(this, obj)) return true; 42 | if(obj.GetType() != this.GetType()) return false; 43 | return Equals((TextureSamplerSettings)obj); 44 | } 45 | 46 | public override int GetHashCode() 47 | { 48 | unchecked 49 | { 50 | int hashCode = (int)WrapU; 51 | hashCode = (hashCode * 397) ^ (int)WrapV; 52 | hashCode = (hashCode * 397) ^ (int)WrapW; 53 | hashCode = (hashCode * 397) ^ (int)Filter; 54 | return hashCode; 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/TextureWrapping.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Materials 2 | { 3 | public enum TextureWrapping 4 | { 5 | Wrap, 6 | Mirror, 7 | Clamp, 8 | Border, 9 | MirrorOnce, 10 | } 11 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Materials/VertexShaderSpec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Grasshopper.Graphics.Materials 5 | { 6 | public class VertexShaderSpec : ShaderSpec 7 | { 8 | public VertexShaderSpec(string id, string source, IEnumerable perVertexElements, IEnumerable perInstanceElements = null) 9 | : base(id, source) 10 | { 11 | if(perVertexElements == null) 12 | throw new ArgumentNullException("perVertexElements"); 13 | PerVertexElements = perVertexElements; 14 | PerInstanceElements = perInstanceElements ?? new ShaderInputElementSpec[0]; 15 | } 16 | 17 | public VertexShaderSpec(string source, IEnumerable perVertexElements, IEnumerable perInstanceElements = null) 18 | : base(source) 19 | { 20 | if(perVertexElements == null) 21 | throw new ArgumentNullException("perVertexElements"); 22 | PerVertexElements = perVertexElements; 23 | PerInstanceElements = perInstanceElements ?? new ShaderInputElementSpec[0]; 24 | } 25 | 26 | public IEnumerable PerInstanceElements { get; private set; } 27 | public IEnumerable PerVertexElements { get; private set; } 28 | } 29 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Primitives/Color4.cs: -------------------------------------------------------------------------------- 1 | // Type: Grasshopper.Graphics.Color4 2 | // Assembly: SharpDX, Version=2.5.0.0, Culture=neutral, PublicKeyToken=627a3d6d1956f55a 3 | // MVID: FA03B5B6-823F-48BF-8919-09A7E490B380 4 | // Assembly location: D:\Dropbox\Work\Sandbox\Game Dev\SharpDx4.1\packages\SharpDX.2.5.0\lib\net40\SharpDX.dll 5 | 6 | using System; 7 | using System.Runtime.InteropServices; 8 | 9 | namespace Grasshopper.Graphics.Primitives 10 | { 11 | /// 12 | /// Represents a color in the form of rgba. 13 | /// 14 | [StructLayout(LayoutKind.Sequential, Pack = 4)] 15 | public struct Color4 : IEquatable, IFormattable 16 | { 17 | /// 18 | /// The red component of the color. 19 | /// 20 | public float Red; 21 | /// 22 | /// The green component of the color. 23 | /// 24 | public float Green; 25 | /// 26 | /// The blue component of the color. 27 | /// 28 | public float Blue; 29 | /// 30 | /// The alpha component of the color. 31 | /// 32 | public float Alpha; 33 | 34 | public static implicit operator Color4(Color color) 35 | { 36 | return color.ToColor4(); 37 | } 38 | 39 | public bool Equals(Color4 other) 40 | { 41 | return Red.Equals(other.Red) && Green.Equals(other.Green) && Blue.Equals(other.Blue) && Alpha.Equals(other.Alpha); 42 | } 43 | 44 | public override bool Equals(object obj) 45 | { 46 | if(ReferenceEquals(null, obj)) return false; 47 | return obj is Color4 && Equals((Color4)obj); 48 | } 49 | 50 | public override int GetHashCode() 51 | { 52 | unchecked 53 | { 54 | // ReSharper disable NonReadonlyFieldInGetHashCode 55 | var hashCode = Red.GetHashCode(); 56 | hashCode = (hashCode * 397) ^ Green.GetHashCode(); 57 | hashCode = (hashCode * 397) ^ Blue.GetHashCode(); 58 | hashCode = (hashCode * 397) ^ Alpha.GetHashCode(); 59 | // ReSharper restore NonReadonlyFieldInGetHashCode 60 | return hashCode; 61 | } 62 | } 63 | 64 | public string ToString(string format, IFormatProvider formatProvider) 65 | { 66 | throw new NotImplementedException(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Primitives/TextureCoordinate.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | 3 | namespace Grasshopper.Graphics.Primitives 4 | { 5 | [StructLayout(LayoutKind.Sequential)] 6 | public struct TextureCoordinate 7 | { 8 | public float U { get; set; } 9 | public float V { get; set; } 10 | 11 | public static TextureCoordinate From(float u, float v) 12 | { 13 | return new TextureCoordinate { U = u, V = v }; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Primitives/Triangle.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace Grasshopper.Graphics.Primitives 6 | { 7 | [StructLayout(LayoutKind.Sequential)] 8 | public struct Triangle where T : struct 9 | { 10 | public readonly T A; 11 | public readonly T B; 12 | public readonly T C; 13 | } 14 | 15 | [StructLayout(LayoutKind.Sequential)] 16 | public struct Triangle : IEnumerable 17 | { 18 | public readonly VertexPosColTex A; 19 | public readonly VertexPosColTex B; 20 | public readonly VertexPosColTex C; 21 | 22 | public Triangle(VertexPosColTex a, VertexPosColTex b, VertexPosColTex c) 23 | { 24 | A = a; 25 | B = b; 26 | C = c; 27 | } 28 | 29 | public static Triangle From(VertexPosColTex a, VertexPosColTex b, VertexPosColTex c) 30 | { 31 | return new Triangle(a, b, c); 32 | } 33 | 34 | public IEnumerator GetEnumerator() 35 | { 36 | yield return A; 37 | yield return B; 38 | yield return C; 39 | } 40 | 41 | IEnumerator IEnumerable.GetEnumerator() 42 | { 43 | return GetEnumerator(); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Primitives/VertexPosColTex.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using System.Runtime.InteropServices; 3 | using Grasshopper.Graphics.Materials; 4 | 5 | namespace Grasshopper.Graphics.Primitives 6 | { 7 | [StructLayout(LayoutKind.Sequential)] 8 | public struct VertexPosColTex 9 | { 10 | public readonly Vector4 Position; 11 | public readonly Color4 Color; 12 | public readonly TextureCoordinate TextureCoordinate; 13 | 14 | public static readonly ShaderInputElementSpec[] ShaderInputLayout = 15 | { 16 | ShaderInputElementPurpose.Position.CreateSpec(), 17 | ShaderInputElementPurpose.Color.CreateSpec(), 18 | ShaderInputElementPurpose.TextureCoordinate.CreateSpec(), 19 | new ShaderInputElementSpec(ShaderInputElementFormat.Float2, ShaderInputElementPurpose.Padding), 20 | }; 21 | 22 | public VertexPosColTex(Vector4 position, Color color, TextureCoordinate textureCoordinate) 23 | : this(position, color.ToColor4(), textureCoordinate) 24 | { 25 | } 26 | 27 | public VertexPosColTex(Vector4 position, Color4 color, TextureCoordinate textureCoordinate) 28 | { 29 | Position = position; 30 | Color = color; 31 | TextureCoordinate = textureCoordinate; 32 | } 33 | 34 | public VertexPosColTex Scale(float scale) 35 | { 36 | return new VertexPosColTex(Position*scale, Color, TextureCoordinate); 37 | } 38 | 39 | public static VertexPosColTex From(Vector4 pos) 40 | { 41 | return From(pos, Primitives.Color.White); 42 | } 43 | 44 | public static VertexPosColTex From(Vector4 pos, TextureCoordinate coord) 45 | { 46 | return From(pos, Primitives.Color.White, coord); 47 | } 48 | 49 | public static VertexPosColTex From(Vector4 pos, Color color) 50 | { 51 | return From(pos, color, TextureCoordinate.From(0.0f, 0.0f)); 52 | } 53 | 54 | public static VertexPosColTex From(Vector4 pos, Color color, TextureCoordinate coord) 55 | { 56 | return new VertexPosColTex(pos, color, coord); 57 | } 58 | 59 | public static VertexPosColTex From(float x, float y, float z, float u, float v, Color color = default(Color)) 60 | { 61 | return From(new Vector4(x, y, z, 1.0f), color, TextureCoordinate.From(u, v)); 62 | } 63 | 64 | public bool Equals(VertexPosColTex other) 65 | { 66 | return Position.Equals(other.Position) && TextureCoordinate.Equals(other.TextureCoordinate); 67 | } 68 | 69 | public override bool Equals(object obj) 70 | { 71 | if(ReferenceEquals(null, obj)) return false; 72 | return obj is VertexPosColTex && Equals((VertexPosColTex)obj); 73 | } 74 | 75 | public override int GetHashCode() 76 | { 77 | unchecked 78 | { 79 | var hashCode = Position.GetHashCode(); 80 | hashCode = (hashCode*397) ^ TextureCoordinate.GetHashCode(); 81 | return hashCode; 82 | } 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/BlendEquation.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Blending 2 | { 3 | public enum BlendEquation 4 | { 5 | AddBothValues, 6 | SubtractSecondFromFirst, 7 | SubtractFirstFromSecond, 8 | Minimum, 9 | Maximum 10 | } 11 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/BlendSettings.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Blending 2 | { 3 | public class BlendSettings : IBlendSettings 4 | { 5 | public bool EnableBlend { get; set; } 6 | public bool EnableAlphaToCoverage { get; set; } 7 | public PreBlend FirstRGB { get; set; } 8 | public PreBlend SecondRGB { get; set; } 9 | public BlendEquation OutputRGB { get; set; } 10 | public PreBlend FirstAlpha { get; set; } 11 | public PreBlend SecondAlpha { get; set; } 12 | public BlendEquation OutputAlpha { get; set; } 13 | public ColorComponents OutputColorComponents { get; set; } 14 | 15 | public BlendSettings Clone() 16 | { 17 | return new BlendSettings 18 | { 19 | EnableBlend = EnableBlend, 20 | EnableAlphaToCoverage = EnableAlphaToCoverage, 21 | FirstRGB = FirstRGB, 22 | SecondRGB = SecondRGB, 23 | OutputRGB = OutputRGB, 24 | FirstAlpha = FirstAlpha, 25 | SecondAlpha = SecondAlpha, 26 | OutputAlpha = OutputAlpha, 27 | OutputColorComponents = OutputColorComponents, 28 | }; 29 | } 30 | 31 | public static IBlendSettings None() 32 | { 33 | var settings = (BlendSettings)DefaultEnabled(); 34 | settings.EnableBlend = false; 35 | return settings; 36 | } 37 | 38 | public static IBlendSettings DefaultEnabled() 39 | { 40 | return new BlendSettings 41 | { 42 | EnableBlend = true, 43 | EnableAlphaToCoverage = false, 44 | FirstRGB = PreBlend.SourceAlpha, 45 | SecondRGB = PreBlend.InverseSourceAlpha, 46 | OutputRGB = BlendEquation.AddBothValues, 47 | FirstAlpha = PreBlend.One, 48 | SecondAlpha = PreBlend.Zero, 49 | OutputAlpha = BlendEquation.AddBothValues, 50 | OutputColorComponents = ColorComponents.All 51 | }; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/ColorComponents.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Blending 2 | { 3 | public enum ColorComponents 4 | { 5 | Red = 1, 6 | Green = 2, 7 | Blue = 4, 8 | Alpha = 8, 9 | All = Red | Green | Blue | Alpha 10 | } 11 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/IBlendSettings.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Blending 2 | { 3 | public interface IBlendSettings 4 | { 5 | bool EnableBlend { get; } 6 | bool EnableAlphaToCoverage { get; } 7 | PreBlend FirstRGB { get; } 8 | PreBlend SecondRGB { get; } 9 | BlendEquation OutputRGB { get; } 10 | PreBlend FirstAlpha { get; } 11 | PreBlend SecondAlpha { get; } 12 | BlendEquation OutputAlpha { get; } 13 | ColorComponents OutputColorComponents { get; } 14 | 15 | BlendSettings Clone(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/IBlendState.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Blending 4 | { 5 | public interface IBlendState : IActivatablePlatformResource 6 | { 7 | IBlendSettings Settings { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/IBlendStateManager.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Blending 4 | { 5 | public interface IBlendStateManager : IActivatablePlatformResourceManager 6 | { 7 | IBlendState Create(string id, IBlendSettings settings); 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Blending/PreBlend.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Blending 2 | { 3 | public enum PreBlend 4 | { 5 | Zero = 1, 6 | One = 2, 7 | SourceColor = 3, 8 | InverseSourceColor = 4, 9 | SourceAlpha = 5, 10 | InverseSourceAlpha = 6, 11 | DestinationAlpha = 7, 12 | InverseDestinationAlpha = 8, 13 | DestinationColor = 9, 14 | InverseDestinationColor = 10, 15 | SourceAlphaSaturate = 11, 16 | BlendFactor = 14, 17 | InverseBlendFactor = 15, 18 | SecondarySourceColor = 16, 19 | InverseSecondarySourceColor = 17, 20 | SecondarySourceAlpha = 18, 21 | InverseSecondarySourceAlpha = 19, 22 | } 23 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/ConstantBufferInterfaces.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | 4 | namespace Grasshopper.Graphics.Rendering.Buffers 5 | { 6 | public interface IConstantBufferManagerFactory 7 | { 8 | IConstantBufferManager Create() 9 | where T : struct; 10 | } 11 | 12 | public interface IConstantBufferManager : IIndexActivatablePlatformResourceManager> 13 | where T : struct 14 | { 15 | IConstantBufferResource Create(string id); 16 | void Update(string id, T data); 17 | void Update(string id, ref T data); 18 | } 19 | 20 | public interface IConstantBufferResource : IIndexActivatablePlatformResource 21 | where T : struct 22 | { 23 | void Update(T data); 24 | void Update(ref T data); 25 | } 26 | 27 | public interface IConstantBufferDataWriter : IDisposable 28 | where T : struct 29 | { 30 | string Id { get; } 31 | void WriteData(ref T data); 32 | } 33 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/IConstantBufferManager.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Buffers 4 | { 5 | public interface IConstantBufferManager : IIndexActivatablePlatformResourceManager> 6 | where T : struct 7 | { 8 | IConstantBufferResource Create(string id); 9 | IConstantBufferResource Create(string id, ref T data); 10 | void Update(string id, ref T data); 11 | } 12 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/IConstantBufferManagerFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Buffers 2 | { 3 | public interface IConstantBufferManagerFactory 4 | { 5 | IConstantBufferManager Create() where T : struct; 6 | } 7 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/IConstantBufferResource.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Buffers 4 | { 5 | public interface IConstantBufferResource : IIndexActivatablePlatformResource 6 | where T : struct 7 | { 8 | void Update(ref T newData); 9 | } 10 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/IndexBufferInterfaces.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | 4 | namespace Grasshopper.Graphics.Rendering.Buffers 5 | { 6 | public interface IIndexBufferManagerFactory 7 | { 8 | IIndexBufferManager Create(); 9 | } 10 | 11 | public interface IIndexBufferManager : IActivatablePlatformResourceManager 12 | { 13 | IIndexBufferResource Create(string id); 14 | } 15 | 16 | public interface IIndexBufferResource : IActivatablePlatformResource 17 | { 18 | IIndexBufferDataWriter BeginWrite(int totalIndicesInBuffer); 19 | } 20 | 21 | public interface IIndexBufferDataWriter : IDisposable 22 | { 23 | string Id { get; } 24 | int TotalVertices { get; } 25 | long Position { get; set; } 26 | long Length { get; } 27 | void Write(uint data); 28 | void Write(uint[] data); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/MeshBuffer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.SceneManagement; 5 | using Grasshopper.Platform; 6 | 7 | namespace Grasshopper.Graphics.Rendering.Buffers 8 | { 9 | public class MeshBuffer : ActivatablePlatformResource where T : struct 10 | { 11 | private readonly IVertexBufferManager _vertexBufferManager; 12 | private readonly IIndexBufferManager _indexBufferManager; 13 | private IVertexBufferResource _vertexBuffer; 14 | private IIndexBufferResource _indexBuffer; 15 | private readonly Dictionary _locations = new Dictionary(); 16 | 17 | public MeshBuffer(string id, IVertexBufferManager vertexBufferManager, IIndexBufferManager indexBufferManager, IEnumerable> meshes) 18 | : base(id) 19 | { 20 | _vertexBufferManager = vertexBufferManager; 21 | _indexBufferManager = indexBufferManager; 22 | Meshes = meshes; 23 | } 24 | 25 | public IEnumerable> Meshes { get; set; } 26 | 27 | protected override void InitializeInternal() 28 | { 29 | _vertexBuffer = _vertexBufferManager.Create(Id); 30 | _indexBuffer = _indexBufferManager.Create(Id); 31 | 32 | var vertexCount = Meshes.Select(m => m.Vertices.Length).Sum(); 33 | var indexCount = Meshes.Select(m => m.Indices.Length).Sum(); 34 | 35 | using(var vertexWriter = _vertexBuffer.BeginWrite(vertexCount)) 36 | using(var indexWriter = _indexBuffer.BeginWrite(indexCount)) 37 | { 38 | int vbOffset = 0, ibOffset = 0; 39 | foreach(var mesh in Meshes) 40 | { 41 | _locations.Add(mesh.Id, new MeshLocation(mesh.Indices.Length, vbOffset, ibOffset, mesh.DrawType)); 42 | for(var i = 0; i < mesh.Vertices.Length; i++) 43 | vertexWriter.Write(ref mesh.Vertices[i]); 44 | foreach(var index in mesh.Indices) 45 | indexWriter.Write(index); 46 | vbOffset += mesh.Vertices.Length; 47 | ibOffset += mesh.Indices.Length; 48 | } 49 | } 50 | } 51 | 52 | protected override void UninitializeInternal() 53 | { 54 | _vertexBuffer.Dispose(); 55 | _indexBuffer.Dispose(); 56 | } 57 | 58 | protected override void SetActive() 59 | { 60 | _vertexBuffer.Activate(0); 61 | _indexBuffer.Activate(); 62 | } 63 | 64 | public MeshLocation this[string id] 65 | { 66 | get 67 | { 68 | MeshLocation loc; 69 | if(!_locations.TryGetValue(id, out loc)) 70 | throw new ArgumentOutOfRangeException("id", "The specified mesh does not exist in this buffer"); 71 | return loc; 72 | } 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/MeshBufferManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Grasshopper.Graphics.SceneManagement; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.Graphics.Rendering.Buffers 6 | { 7 | public class MeshBufferManager : ActivatablePlatformResourceManager> where T : struct 8 | { 9 | private readonly IVertexBufferManager _vertexBufferManager; 10 | private readonly IIndexBufferManager _indexBufferManager; 11 | private readonly Dictionary> _meshBuffers = new Dictionary>(); 12 | 13 | public MeshBufferManager(IGraphicsContext graphicsContext) 14 | { 15 | _vertexBufferManager = graphicsContext.VertexBufferManagerFactory.Create(); 16 | _indexBufferManager = graphicsContext.IndexBufferManagerFactory.Create(); 17 | Disposed += OnDisposed; 18 | } 19 | 20 | public MeshBuffer Create(string id) 21 | { 22 | return Create(id, new Mesh[0]); 23 | } 24 | 25 | public MeshBuffer Create(string id, params Mesh[] meshes) 26 | { 27 | return Create(id, (IEnumerable>)meshes); 28 | } 29 | 30 | public MeshBuffer Create(string id, IEnumerable> meshes) 31 | { 32 | return Add(new MeshBuffer(id, _vertexBufferManager, _indexBufferManager, meshes)); 33 | } 34 | 35 | private void OnDisposed() 36 | { 37 | _vertexBufferManager.Dispose(); 38 | _indexBufferManager.Dispose(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/MeshLocation.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Buffers 2 | { 3 | /// 4 | /// Represents location information required to draw a specific mesh stored within a packed vertex buffer 5 | /// 6 | public class MeshLocation 7 | { 8 | public MeshLocation(int indexCount, int vertexBufferOffset, int indexBufferOffset, DrawType drawType) 9 | { 10 | IndexCount = indexCount; 11 | VertexBufferOffset = vertexBufferOffset; 12 | IndexBufferOffset = indexBufferOffset; 13 | DrawType = drawType; 14 | } 15 | 16 | /// 17 | /// The number of indices required to draw the mesh at this location 18 | /// 19 | public int IndexCount { get; private set; } 20 | /// 21 | /// The position of the first vertex representing this mesh in the buffer 22 | /// 23 | public int VertexBufferOffset { get; private set; } 24 | /// 25 | /// The position of the vertex index representing this mesh's triangles in the buffer 26 | /// 27 | public int IndexBufferOffset { get; private set; } 28 | /// 29 | /// The type of graphics primitives to draw 30 | /// 31 | public DrawType DrawType { get; set; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Buffers/VertexBufferInterfaces.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | 4 | namespace Grasshopper.Graphics.Rendering.Buffers 5 | { 6 | public interface IVertexBufferManagerFactory 7 | { 8 | IVertexBufferManager Create() 9 | where TVertex : struct; 10 | } 11 | 12 | public interface IVertexBufferManager : IIndexActivatablePlatformResourceManager> 13 | where TVertex : struct 14 | { 15 | IVertexBufferResource Create(string id); 16 | } 17 | 18 | public interface IVertexBufferResource : IIndexActivatablePlatformResource 19 | where TVertex : struct 20 | { 21 | IVertexBufferDataWriter BeginWrite(int totalItemsInBuffer); 22 | } 23 | 24 | public interface IVertexBufferDataWriter : IDisposable 25 | { 26 | string Id { get; } 27 | int TotalVertices { get; } 28 | long Position { get; set; } 29 | long Length { get; } 30 | void Write(ref TVertex data); 31 | void Write(TVertex[] data); 32 | } 33 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/DrawType.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | /// 4 | /// Indicates the type of drawing that should occur, given a set of vertices and vertex indices 5 | /// 6 | public enum DrawType 7 | { 8 | Points, 9 | LineStrip, 10 | LineList, 11 | Triangles, 12 | TriangleStrip 13 | } 14 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IAppWindow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Graphics.Rendering 4 | { 5 | public interface IAppWindow : IDisposable 6 | { 7 | int Left { get; set; } 8 | int Top { get; set; } 9 | int Width { get; set; } 10 | int Height { get; set; } 11 | int ClientWidth { get; } 12 | int ClientHeight { get; } 13 | string Title { get; set; } 14 | bool Resizable { get; set; } 15 | bool ShowBordersAndTitle { get; set; } 16 | bool Visible { get; set; } 17 | void SetSize(int width, int height); 18 | void SetFullScreen(bool enabled = true); 19 | bool ShowCursor { get; set; } 20 | bool LockCursor { get; set; } 21 | void SetCursorPosition(int x, int y); 22 | void SetCursorPositionToCenter(); 23 | 24 | event AppWindowSimpleEventHandler SizeChanged; 25 | bool NextFrame(AppWindowFrameExecutionHandler run); 26 | bool NextFrame(); 27 | void SetWindowMaximized(); 28 | void SetWindowNormalSize(); 29 | void SetWindowMinimized(); 30 | } 31 | 32 | public delegate void AppWindowSimpleEventHandler(IAppWindow win); 33 | public delegate bool AppWindowFrameExecutionHandler(IAppWindow win); 34 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IDrawingContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Primitives; 3 | 4 | namespace Grasshopper.Graphics.Rendering 5 | { 6 | /// 7 | /// Passed as an argument when a render target's Render method is called. 8 | /// Exposes functionality that is only relevant while a render target is 9 | /// active for rendering a frame. 10 | /// 11 | public interface IDrawingContext : IDisposable 12 | { 13 | void Activate(); 14 | void SetDrawType(DrawType drawType); 15 | void DrawIndexed(int indexCount, int indexStartLocation, int vertexStartLocation); 16 | void DrawIndexedInstanced(int indexCountPerInstance, int instanceCount, int indexStartLocation, int vertexStartLocation, int instanceStartLocation); 17 | void Clear(Color color); 18 | void Draw(int vertexCount, int vertexStartLocation); 19 | void DrawInstanced(int vertexCount, int instanceCount, int vertexStartLocation, int instanceStartLocation); 20 | } 21 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IRenderTarget.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Graphics.Rendering 4 | { 5 | public interface IRenderTarget : IDisposable 6 | where T : IDrawingContext 7 | { 8 | void Render(FrameContext frame, RenderFrameHandlerEx renderFrame); 9 | void Render(RenderFrameHandler renderFrame); 10 | bool Terminated { get; } 11 | } 12 | 13 | public delegate void RenderFrameHandlerEx(FrameContext frame, T context) 14 | where T : IDrawingContext; 15 | 16 | public delegate void RenderFrameHandler(T context) 17 | where T : IDrawingContext; 18 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IRenderTargetFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | public interface IRenderTargetFactory 4 | { 5 | IWindowRenderTarget CreateWindow(); 6 | } 7 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/ITextureDrawingContext.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | public interface ITextureDrawingContext : IDrawingContext 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/ITextureRenderTarget.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | public interface ITextureRenderTarget : IRenderTarget 4 | { 5 | void ActivateTextureResource(int index); 6 | } 7 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IWindowDrawingContext.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | public interface IWindowDrawingContext : IDrawingContext 4 | { 5 | IGraphicsContext Graphics { get; } 6 | IAppWindow Window { get; } 7 | void Present(); 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/IWindowRenderTarget.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering 2 | { 3 | public interface IWindowRenderTarget : IRenderTarget 4 | { 5 | IAppWindow Window { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/Antialiasing.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Rasterization 2 | { 3 | public enum Antialiasing 4 | { 5 | None, 6 | Multisample, 7 | LinesOnly 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/IRasterizerSettings.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Rasterization 2 | { 3 | public interface IRasterizerSettings 4 | { 5 | WindingOrder WindingOrder { get; } 6 | bool RenderWireframe { get; } 7 | bool EnableDepthTest { get; } 8 | TriangleCulling TriangleCulling { get; } 9 | Antialiasing Antialiasing { get; } 10 | RasterizerSettings Clone(); 11 | } 12 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/IRasterizerState.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Rasterization 4 | { 5 | public interface IRasterizerState : IActivatablePlatformResource 6 | { 7 | IRasterizerSettings Settings { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/IRasterizerStateManager.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.Graphics.Rendering.Rasterization 4 | { 5 | public interface IRasterizerStateManager : IActivatablePlatformResourceManager 6 | { 7 | IRasterizerState Create(string id, IRasterizerSettings settings); 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/TriangleCulling.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Rasterization 2 | { 3 | public enum TriangleCulling 4 | { 5 | DrawFrontFacing, 6 | DrawBackFacing, 7 | DrawAll 8 | } 9 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/Rasterization/WindingOrder.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Graphics.Rendering.Rasterization 2 | { 3 | public enum WindingOrder 4 | { 5 | Clockwise, 6 | Counterclockwise 7 | } 8 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/Rendering/RenderManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Grasshopper.Graphics.Materials; 4 | using Grasshopper.Graphics.SceneManagement; 5 | 6 | namespace Grasshopper.Graphics.Rendering 7 | { 8 | public abstract class RenderManager 9 | { 10 | private readonly IGraphicsContext _graphics; 11 | 12 | protected RenderManager(IGraphicsContext graphics) 13 | { 14 | _graphics = graphics; 15 | // todo: subscribe to changes to asset libraries and build a dirty list for each library which will be processed on the next call to UpdateAssets. also observe changes to group dormancy. 16 | // 1. if a mesh has been removed, check the asset group and if it's empty, destroy the buffer. ignore removed meshes; we'll leave them in place for performance reasons. 17 | // 2. if a mesh has been added or updated, rebuild the buffer for now. later we may be able to optimise to do in-place buffer updates if it's an issue, but it probably won't be. 18 | } 19 | 20 | /// 21 | /// Update buffer/shader data (and associated quick-access index for internal use) in GPU memory from asset libraries. 22 | /// 23 | public void UpdateAssets() 24 | { 25 | // todo: process the dirty list and update indexes of asset resources in GPU memory, ready for rendering, then clear the list. 26 | } 27 | 28 | private RenderQueue ConstructRenderQueue(IEnumerable items) 29 | { 30 | throw new NotImplementedException(); 31 | } 32 | 33 | private void Render(RenderQueue queue) 34 | { 35 | } 36 | 37 | public void Render() 38 | { 39 | } 40 | 41 | public void Render(IEnumerable items) 42 | { 43 | var queue = ConstructRenderQueue(items); 44 | Render(queue); 45 | // todo: implement render queue sorting. persist render queue between frames and only update it when the set of renderable items changes. 46 | // todo: construct/update instance buffers when instance data is out of date 47 | } 48 | 49 | // todo: the render queue will end up with a structure that defines what to draw, how many instances of it to draw, and so forth, and will pass that information the following method for final instanced rendering 50 | // protected abstract void Render(...) 51 | } 52 | 53 | // Switching shaders appears to be more costly than switching buffers, so we'll optimise on that basis. 54 | internal class RenderQueue : IDisposable 55 | { 56 | // 1. sort by material 57 | // 2. sort by vertex/index buffer 58 | // 3. the set of instances 59 | //Dictionary 60 | 61 | internal class MaterialMetadata 62 | { 63 | 64 | } 65 | 66 | internal class BufferMetadata 67 | { 68 | } 69 | 70 | public void Dispose() 71 | { 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/SceneManagement/Mesh.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Grasshopper.Graphics.Primitives; 4 | using Grasshopper.Graphics.Rendering; 5 | 6 | namespace Grasshopper.Graphics.SceneManagement 7 | { 8 | public class Mesh where T : struct 9 | { 10 | public string Id { get; private set; } 11 | public T[] Vertices { get; private set; } 12 | public uint[] Indices { get; private set; } 13 | public DrawType DrawType { get; private set; } 14 | 15 | public Mesh(string id, T[] vertices, uint[] indices, DrawType drawType) 16 | { 17 | Id = id; 18 | Vertices = vertices; 19 | Indices = indices; 20 | DrawType = drawType; 21 | } 22 | } 23 | 24 | public class Mesh 25 | { 26 | public string Id { get; private set; } 27 | public VertexPosColTex[] Vertices { get; private set; } 28 | public uint[] Indices { get; private set; } 29 | public DrawType DrawType { get; private set; } 30 | 31 | public Mesh(IEnumerable triangles) 32 | : this(Guid.NewGuid().ToString(), triangles) 33 | { 34 | } 35 | 36 | public Mesh(string id, IEnumerable triangles, DrawType drawType = DrawType.Triangles) 37 | { 38 | Id = id; 39 | DrawType = drawType; 40 | ReadTriangles(triangles); 41 | } 42 | 43 | public Mesh Scale(float scale) 44 | { 45 | for(var i = 0; i < Vertices.Length; i++) 46 | { 47 | var vertex = Vertices[i]; 48 | Vertices[i] = vertex.Scale(scale); 49 | } 50 | return this; 51 | } 52 | 53 | private Mesh ReadTriangles(IEnumerable triangles) 54 | { 55 | var map = new Dictionary(); 56 | var vertices = new List(); 57 | var indices = new List(); 58 | 59 | foreach(var triangle in triangles) 60 | { 61 | foreach(var vertex in triangle) 62 | { 63 | uint index; 64 | if(!map.TryGetValue(vertex, out index)) 65 | { 66 | index = (uint)vertices.Count; 67 | map.Add(vertex, index); 68 | vertices.Add(vertex); 69 | } 70 | indices.Add(index); 71 | } 72 | } 73 | 74 | Vertices = vertices.ToArray(); 75 | Indices = indices.ToArray(); 76 | 77 | return this; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/SceneManagement/MeshInstance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Artemis.Interface; 4 | using Grasshopper.Math; 5 | 6 | namespace Grasshopper.Graphics.SceneManagement 7 | { 8 | public class MeshInstance : IComponent 9 | { 10 | public MeshInstance() 11 | { 12 | } 13 | 14 | public MeshInstance(string meshId, string materialId) 15 | { 16 | MeshId = meshId; 17 | MaterialId = materialId; 18 | } 19 | 20 | public Guid InstanceId { get; set; } 21 | public string MeshId { get; set; } 22 | public string MaterialId { get; set; } 23 | public Transformation Transformation { get; set; } 24 | } 25 | 26 | public interface IRenderable : IEnumerable 27 | { 28 | } 29 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Graphics/SceneManagement/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using Grasshopper.Math; 5 | 6 | namespace Grasshopper.Graphics.SceneManagement 7 | { 8 | public class Model : IEnumerable 9 | { 10 | public Model() 11 | { 12 | MeshInstances = new Dictionary(); 13 | } 14 | 15 | public Dictionary MeshInstances { get; set; } 16 | 17 | public ModelMeshInstance WithMesh(string id) 18 | { 19 | var mesh = new ModelMeshInstance(this, id); 20 | MeshInstances.Add(mesh.InstanceId, mesh); 21 | return mesh; 22 | } 23 | 24 | public IEnumerator GetEnumerator() 25 | { 26 | return MeshInstances.Values.GetEnumerator(); 27 | } 28 | 29 | IEnumerator IEnumerable.GetEnumerator() 30 | { 31 | return GetEnumerator(); 32 | } 33 | } 34 | 35 | public class ModelMeshInstance : MeshInstance 36 | { 37 | internal ModelMeshInstance(Model ownerModel, string meshId) 38 | { 39 | InstanceId = Guid.NewGuid(); 40 | OwnerModel = ownerModel; 41 | MeshId = meshId; 42 | Transformation = new Transformation(); 43 | } 44 | 45 | internal Model OwnerModel { get; set; } 46 | 47 | public ModelMeshInstance WithMaterial(string materialId) 48 | { 49 | MaterialId = materialId; 50 | return this; 51 | } 52 | 53 | public Model And() 54 | { 55 | return OwnerModel; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Grasshopper.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $id$ 5 | $version$ 6 | $title$ 7 | $author$ 8 | $author$ 9 | http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE 10 | http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE 11 | http://ICON_URL_HERE_OR_DELETE_THIS_LINE 12 | false 13 | $description$ 14 | Summary of changes made in this release of the package. 15 | Copyright 2015 16 | Tag1 Tag2 17 | 18 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/GrasshopperApp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using Grasshopper.Core; 4 | using Grasshopper.Graphics; 5 | using Grasshopper.Graphics.Rendering; 6 | using Grasshopper.Input; 7 | using Grasshopper.Platform; 8 | 9 | namespace Grasshopper 10 | { 11 | public class GrasshopperApp : IDisposable 12 | { 13 | private DateTime _startTime; 14 | private bool _exiting; 15 | private FrameContext _frameContext; 16 | 17 | public GrasshopperApp() 18 | { 19 | } 20 | 21 | public IFileStore Files { get; set; } 22 | public IGraphicsContextFactory Graphics { get; set; } 23 | public IInputContext Input { get; set; } 24 | public TickCounter TickCounter { get; private set; } 25 | public TimeSpan Elapsed { get { return DateTime.UtcNow - _startTime; } } 26 | public float ElapsedSeconds { get { return (float)Elapsed.TotalSeconds; } } 27 | 28 | private void Initialize() 29 | { 30 | TickCounter = new TickCounter(); 31 | _frameContext = new FrameContext(this); 32 | _startTime = DateTime.UtcNow; 33 | } 34 | 35 | private void NextFrame() 36 | { 37 | TickCounter.Tick(); 38 | _frameContext.NextFrame(); 39 | } 40 | 41 | public void Run(IRenderTarget renderTarget, RenderFrameHandlerEx main) 42 | where TRendererContext : IDrawingContext 43 | { 44 | Initialize(); 45 | while(!renderTarget.Terminated && !_exiting) 46 | { 47 | renderTarget.Render(_frameContext, main); 48 | NextFrame(); 49 | } 50 | } 51 | 52 | public virtual void Run(MainLoopFrameHandler main) 53 | { 54 | Initialize(); 55 | while(main(_frameContext) && !_exiting) 56 | NextFrame(); 57 | } 58 | 59 | public void Exit() 60 | { 61 | _exiting = true; 62 | } 63 | 64 | public virtual void Dispose() 65 | { 66 | } 67 | } 68 | 69 | public delegate bool MainLoopFrameHandler(FrameContext frame); 70 | } 71 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Input/IInputContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Input 4 | { 5 | public interface IInputContext : IDisposable 6 | { 7 | IObservable MouseEvents { get; } 8 | IObservable KeyboardEvents { get; } 9 | void PostMouseEvent(MouseEvent mouseEvent); 10 | void PostKeyboardEvent(KeyboardEvent keyboardEvent); 11 | } 12 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Input/MouseEvent.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Graphics.Rendering; 2 | 3 | namespace Grasshopper.Input 4 | { 5 | public class MouseEvent 6 | { 7 | public MouseEvent(IAppWindow window, MouseButton button, ButtonState buttonState, int x, int y) 8 | { 9 | Type = MouseEventType.Button; 10 | Window = window; 11 | Button = button; 12 | ButtonState = buttonState; 13 | X = x; 14 | Y = y; 15 | } 16 | 17 | public MouseEvent(IAppWindow window, int deltaX, int deltaY, int x, int y) 18 | { 19 | Type = MouseEventType.Move; 20 | Window = window; 21 | DeltaX = deltaX; 22 | DeltaY = deltaY; 23 | X = x; 24 | Y = y; 25 | } 26 | 27 | public MouseEvent(IAppWindow window, int wheelDelta, int x, int y) 28 | { 29 | Type = MouseEventType.Wheel; 30 | Window = window; 31 | WheelDelta = wheelDelta; 32 | X = x; 33 | Y = y; 34 | } 35 | 36 | public IAppWindow Window { get; private set; } 37 | public MouseEventType Type { get; private set; } 38 | public MouseButton Button { get; private set; } 39 | public ButtonState ButtonState { get; private set; } 40 | public int DeltaX { get; private set; } 41 | public int DeltaY { get; private set; } 42 | public int X { get; private set; } 43 | public int Y { get; private set; } 44 | public int WheelDelta { get; private set; } 45 | } 46 | 47 | public enum MouseEventType 48 | { 49 | Button, 50 | Wheel, 51 | Move 52 | } 53 | 54 | public enum MouseButton 55 | { 56 | Left = 1, 57 | Right, 58 | Middle, 59 | Extra1, 60 | Extra2 61 | } 62 | 63 | public enum ButtonState 64 | { 65 | Up, 66 | Down 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Math/Calc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Grasshopper.Math 9 | { 10 | public static class Calc 11 | { 12 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 13 | public static void Swap(ref T a, ref T b) 14 | { 15 | var t = a; 16 | a = b; 17 | b = t; 18 | } 19 | 20 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 21 | public static float Clamp(float value, float lowerBound, float upperBound) 22 | { 23 | if(lowerBound > upperBound) Swap(ref lowerBound, ref upperBound); 24 | return value > upperBound ? upperBound : value < lowerBound ? lowerBound : value; 25 | } 26 | 27 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 28 | public static float InverseLerp(float value, float lowerBound, float upperBound) 29 | { 30 | if(lowerBound > upperBound) Swap(ref lowerBound, ref upperBound); 31 | var range = upperBound - lowerBound; 32 | value = Clamp((value - lowerBound) / (upperBound - lowerBound), 0.0f, 1.0f); 33 | return lowerBound + range * value; 34 | } 35 | 36 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 37 | public static float SmoothStep(float value, float lowerBound = 0.0f, float upperBound = 1.0f) 38 | { 39 | if(value <= lowerBound) return lowerBound; 40 | if(value >= upperBound) return upperBound; 41 | var range = upperBound - lowerBound; 42 | var x = (value - lowerBound) / range; 43 | return (x * x * (3 - 2 * x)) * range + lowerBound; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Math/Transformation.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | 3 | namespace Grasshopper.Math 4 | { 5 | public class Transformation 6 | { 7 | public Transformation() 8 | { 9 | Scale = 1.0f; 10 | Rotation = Quaternion.Identity; 11 | } 12 | 13 | public float Scale { get; set; } 14 | public Vector3 Position { get; set; } 15 | public Quaternion Rotation { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/ActivatablePlatformResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public abstract class ActivatablePlatformResource : PlatformResource, IActivatablePlatformResource 6 | { 7 | protected ActivatablePlatformResource(string id) : base(id) 8 | { 9 | } 10 | 11 | public event ActivatablePlatformResourceEventHandler Activated; 12 | 13 | public void Activate() 14 | { 15 | if(!IsInitialized) 16 | Initialize(); 17 | 18 | SetActive(); 19 | NotifyActivated(); 20 | } 21 | 22 | protected void NotifyActivated() 23 | { 24 | var handler = Activated; 25 | if(handler != null) 26 | handler(this); 27 | } 28 | 29 | protected abstract void SetActive(); 30 | } 31 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/ActivatablePlatformResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace Grasshopper.Platform 5 | { 6 | public abstract class ActivatablePlatformResourceManager : PlatformResourceManager, IActivatablePlatformResourceManager 7 | where T : IActivatablePlatformResource 8 | { 9 | protected ActivatablePlatformResourceManager() 10 | { 11 | ResourceAdded += resource => 12 | { 13 | resource.Activated += OnResourceActivated; 14 | }; 15 | ResourceRemoved += resource => 16 | { 17 | resource.Activated -= OnResourceActivated; 18 | }; 19 | } 20 | 21 | private void OnResourceActivated(IPlatformResource resource) 22 | { 23 | var handler = ResourceActivated; 24 | if(handler != null) 25 | handler((T)resource); 26 | } 27 | 28 | public event ActivatablePlatformResourceEventHandler ResourceActivated; 29 | 30 | public T Activate(string id) 31 | { 32 | var resource = this[id]; 33 | resource.Activate(); 34 | return resource; 35 | } 36 | 37 | public void Activate(params string[] ids) 38 | { 39 | Activate((IEnumerable)ids); 40 | } 41 | 42 | public void Activate(IEnumerable ids) 43 | { 44 | Activate(ids.Select(id => this[id])); 45 | } 46 | 47 | protected virtual void Activate(IEnumerable resources) 48 | { 49 | foreach(var resource in resources) 50 | resource.Activate(); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IActivatablePlatformResource.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Platform 2 | { 3 | public interface IActivatablePlatformResource : IPlatformResource 4 | { 5 | event ActivatablePlatformResourceEventHandler Activated; 6 | void Activate(); 7 | } 8 | 9 | public delegate void ActivatablePlatformResourceEventHandler(IPlatformResource resource); 10 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IActivatablePlatformResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public interface IActivatablePlatformResourceManager : IPlatformResourceManager 6 | where T : IActivatablePlatformResource 7 | { 8 | event ActivatablePlatformResourceEventHandler ResourceActivated; 9 | T Activate(string id); 10 | void Activate(params string[] ids); 11 | void Activate(IEnumerable ids); 12 | } 13 | 14 | public delegate void ActivatablePlatformResourceEventHandler(T resource) where T : IPlatformResource; 15 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IFileSource.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public interface IFileSource 6 | { 7 | Stream OpenRead(); 8 | string Path { get; } 9 | int Size { get; } 10 | } 11 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IFileStore.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Platform 2 | { 3 | public interface IFileStore 4 | { 5 | IFileSource GetFile(string path); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IIndexActivatablePlatformResource.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.Platform 2 | { 3 | /// 4 | /// Represents an that is registered as active via a hardware location index. 5 | /// Only one resource of a given type can be active in the hardware slot of that type at a time. Multiple 6 | /// resources of a given type can be simultaneously active, with one active resource per slot. 7 | /// 8 | public interface IIndexActivatablePlatformResource : IPlatformResource 9 | { 10 | event IndexActivatablePlatformResourceEventHandler Activated; 11 | void Activate(int index); 12 | } 13 | 14 | public delegate void IndexActivatablePlatformResourceEventHandler(IPlatformResource resource, int index); 15 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IIndexActivatablePlatformResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public interface IIndexActivatablePlatformResourceManager : IPlatformResourceManager 6 | where T : IIndexActivatablePlatformResource 7 | { 8 | event AssignablePlatformResourceEventHandler ResourceAssigned; 9 | void Activate(int index, string id); 10 | void Activate(int firstIndex, params string[] ids); 11 | void Activate(int firstIndex, IEnumerable ids); 12 | } 13 | 14 | public delegate void AssignablePlatformResourceEventHandler(T resource, int index) where T : IPlatformResource; 15 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IPlatformResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | /// 6 | /// Represents a resource that is controlled by the current selected platform. Examples include 7 | /// buffers and shaders on the GPU, audio resources, and other resources that take significant 8 | /// hardware resources. These may need to be unloaded and reloaded in certain circumstances and 9 | /// always need to be disposed of correctly when no longer needed. 10 | /// 11 | public interface IPlatformResource : IDisposable 12 | { 13 | string Id { get; } 14 | bool IsInitialized { get; } 15 | bool IsDisposed { get; } 16 | void Initialize(); 17 | void Uninitialize(); 18 | event PlatformResourceEventHandler Initialized; 19 | event PlatformResourceEventHandler Uninitialized; 20 | event PlatformResourceEventHandler Disposed; 21 | } 22 | 23 | public delegate void PlatformResourceEventHandler(IPlatformResource resource); 24 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IPlatformResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Grasshopper.Platform 5 | { 6 | /// 7 | /// Represents a library of IPlatformResources of a given type 8 | /// 9 | public interface IPlatformResourceManager : IEnumerable, IDisposable 10 | where T : IPlatformResource 11 | { 12 | /// 13 | /// Removes the resource from control by the resource manager, but does not uninitialize or dispose it. 14 | /// 15 | /// The id of the resource to remove 16 | /// The removed resource 17 | T Remove(string id); 18 | /// 19 | /// Removes and disposes of the specified resource 20 | /// 21 | /// The id of the resource to remove 22 | void RemoveAndDispose(string id); 23 | void Initialize(string id); 24 | void Uninitialize(string id); 25 | bool Exists(string id); 26 | bool IsInitialized(string id); 27 | T this[string id] { get; } 28 | event PlatformResourceEventHandler ResourceAdded; 29 | event PlatformResourceEventHandler ResourceRemoved; 30 | event PlatformResourceEventHandler ResourceInitialized; 31 | event PlatformResourceEventHandler ResourceUninitialized; 32 | event PlatformResourceEventHandler ResourceDisposed; 33 | event Action Disposing; 34 | event Action Disposed; 35 | } 36 | 37 | public delegate void PlatformResourceEventHandler(T resource) where T : IPlatformResource; 38 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IndexActivatablePlatformResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public abstract class IndexActivatablePlatformResource : PlatformResource, IIndexActivatablePlatformResource 6 | { 7 | protected IndexActivatablePlatformResource(string id) : base(id) 8 | { 9 | } 10 | 11 | public event IndexActivatablePlatformResourceEventHandler Activated; 12 | 13 | public void Activate(int index) 14 | { 15 | if(!IsInitialized) 16 | Initialize(); 17 | 18 | ActivateAtIndex(index); 19 | NotifyActivated(index); 20 | } 21 | 22 | protected void NotifyActivated(int index) 23 | { 24 | var handler = Activated; 25 | if(handler != null) 26 | handler(this, index); 27 | } 28 | 29 | protected abstract void ActivateAtIndex(int index); 30 | 31 | public void SetActivatedExternally(int index) 32 | { 33 | NotifyActivated(index); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/IndexActivatablePlatformResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace Grasshopper.Platform 5 | { 6 | public abstract class IndexActivatablePlatformResourceManager : PlatformResourceManager, IIndexActivatablePlatformResourceManager 7 | where T : IIndexActivatablePlatformResource 8 | { 9 | public event AssignablePlatformResourceEventHandler ResourceAssigned; 10 | 11 | protected IndexActivatablePlatformResourceManager() 12 | { 13 | ResourceAdded += resource => 14 | { 15 | resource.Activated += OnResourceActivated; 16 | }; 17 | ResourceRemoved += resource => 18 | { 19 | resource.Activated -= OnResourceActivated; 20 | }; 21 | } 22 | 23 | private void OnResourceActivated(IPlatformResource resource, int index) 24 | { 25 | var handler = ResourceAssigned; 26 | if(handler != null) 27 | handler((T)resource, index); 28 | } 29 | 30 | public void Activate(int index, string id) 31 | { 32 | var resource = this[id]; 33 | resource.Activate(index); 34 | } 35 | 36 | public void Activate(int firstIndex, params string[] ids) 37 | { 38 | Activate(firstIndex, (IEnumerable)ids); 39 | } 40 | 41 | public void Activate(int firstIndex, IEnumerable ids) 42 | { 43 | Activate(firstIndex, ids.Select(id => this[id])); 44 | } 45 | 46 | protected virtual void Activate(int firstIndex, IEnumerable resources) 47 | { 48 | var index = firstIndex; 49 | foreach(var resource in resources) 50 | resource.Activate(index++); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Platform/PlatformResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Grasshopper.Platform 4 | { 5 | public abstract class PlatformResource : IPlatformResource 6 | { 7 | protected PlatformResource(string id) 8 | { 9 | if(id == null) throw new ArgumentNullException("id"); 10 | Id = id; 11 | } 12 | 13 | public string Id { get; private set; } 14 | public bool IsInitialized { get; private set; } 15 | public bool IsDisposed { get; private set; } 16 | 17 | public void Initialize() 18 | { 19 | if(IsInitialized) 20 | Uninitialize(); 21 | 22 | InitializeInternal(); 23 | 24 | var handler = Initialized; 25 | if(handler != null) 26 | handler(this); 27 | 28 | IsInitialized = true; 29 | } 30 | 31 | public void Uninitialize() 32 | { 33 | UninitializeInternal(); 34 | 35 | var handler = Uninitialized; 36 | if(handler != null) 37 | handler(this); 38 | 39 | IsInitialized = false; 40 | } 41 | 42 | protected abstract void InitializeInternal(); 43 | protected abstract void UninitializeInternal(); 44 | 45 | public event PlatformResourceEventHandler Initialized; 46 | public event PlatformResourceEventHandler Uninitialized; 47 | public event PlatformResourceEventHandler Disposed; 48 | 49 | public void Dispose() 50 | { 51 | if(IsDisposed) 52 | return; 53 | 54 | Uninitialize(); 55 | 56 | var handler = Disposed; 57 | if(handler != null) 58 | handler(this); 59 | 60 | IsDisposed = true; 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Grasshopper.Core")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Grasshopper.Core")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("54984018-71c7-4535-ac8d-50d5e3928e49")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/README.md: -------------------------------------------------------------------------------- 1 | This is the base Grasshopper engine project and provides all platform-independent 2 | functionality for generalised rendering, sound, input, etc. It does not provide 3 | any functionality for any specific game type; any type of game could hypothetically 4 | be built from the structures provided. -------------------------------------------------------------------------------- /src/core-engine/Grasshopper/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/ActivatableD3DResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Grasshopper.Platform; 7 | 8 | namespace Grasshopper.SharpDX.Graphics 9 | { 10 | internal abstract class ActivatableD3DResource : ActivatablePlatformResource 11 | { 12 | protected DeviceManager DeviceManager { get; private set; } 13 | 14 | protected ActivatableD3DResource(DeviceManager deviceManager, string id) 15 | : base(id) 16 | { 17 | DeviceManager = deviceManager; 18 | DeviceManager.Initialized += () => 19 | { 20 | if(IsInitialized) 21 | Initialize(); 22 | }; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/DeviceManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics; 3 | using SharpDX.Direct3D; 4 | using SharpDX.Direct3D11; 5 | using Device = SharpDX.Direct3D11.Device; 6 | using Device1 = SharpDX.Direct3D11.Device1; 7 | 8 | namespace Grasshopper.SharpDX.Graphics 9 | { 10 | public class DeviceManager : IDeviceManager 11 | { 12 | public Device1 Device { get; private set; } 13 | public DeviceContext1 Context { get; private set; } 14 | public bool IsInitialized { get; private set; } 15 | 16 | public event Action Initialized; 17 | 18 | public void Initialize() 19 | { 20 | DestroyResources(); 21 | 22 | var flags = DeviceCreationFlags.BgraSupport; 23 | #if DEBUG 24 | flags |= DeviceCreationFlags.Debug; 25 | #endif 26 | 27 | var featureLevels = new[] 28 | { 29 | FeatureLevel.Level_11_1, 30 | FeatureLevel.Level_11_0 31 | }; 32 | 33 | using(var device = new Device(DriverType.Hardware, flags, featureLevels)) 34 | { 35 | Device = device.QueryInterface(); 36 | Context = device.ImmediateContext.QueryInterface(); 37 | } 38 | 39 | IsInitialized = true; 40 | 41 | // todo: Reinitialize all dependent resources by having them hook this event 42 | var handler = Initialized; 43 | if(handler != null) 44 | handler(); 45 | } 46 | 47 | public void GetDeviceCapabilities() 48 | { 49 | } 50 | 51 | private void DestroyResources() 52 | { 53 | IsInitialized = false; 54 | if(Device != null) 55 | { 56 | Device.Dispose(); 57 | Device = null; 58 | } 59 | } 60 | 61 | public void Dispose() 62 | { 63 | DestroyResources(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/GraphicsContextFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics; 3 | using Grasshopper.Input; 4 | using Grasshopper.Platform; 5 | 6 | namespace Grasshopper.SharpDX.Graphics 7 | { 8 | class GraphicsContextFactory : IGraphicsContextFactory 9 | { 10 | private readonly Lazy _assets; 11 | private readonly IInputContext _input; 12 | 13 | public GraphicsContextFactory(Lazy assets, IInputContext input) 14 | { 15 | _assets = assets; 16 | _input = input; 17 | } 18 | 19 | public IGraphicsContext CreateContext(bool enableDebugMode = false) 20 | { 21 | var gfx = new GraphicsContext(_assets, _input); 22 | gfx.Initialize(); 23 | return gfx; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/IndexActivatableD3DResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | 4 | namespace Grasshopper.SharpDX.Graphics 5 | { 6 | abstract class IndexActivatableD3DResource : IndexActivatablePlatformResource 7 | { 8 | protected DeviceManager DeviceManager { get; private set; } 9 | 10 | protected IndexActivatableD3DResource(DeviceManager deviceManager, string id) : base(id) 11 | { 12 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 13 | 14 | DeviceManager = deviceManager; 15 | DeviceManager.Initialized += () => 16 | { 17 | if(IsInitialized) 18 | Initialize(); 19 | }; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/DynamicTexture2DResource.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Graphics.Materials; 2 | using SharpDX.Direct3D11; 3 | using SharpDX.DXGI; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Materials 6 | { 7 | class DynamicTexture2DResource : ShaderResource, IDynamicTexture2DResource 8 | { 9 | private readonly PixelFormat _pixelFormat; 10 | private Texture2DDescription _descr; 11 | private Texture2D _texture; 12 | 13 | public DynamicTexture2DResource(DeviceManager deviceManager, string id, int width, int height, PixelFormat pixelFormat) : base(deviceManager, id) 14 | { 15 | _pixelFormat = pixelFormat; 16 | Width = width; 17 | Height = height; 18 | } 19 | 20 | public TextureType TextureType { get { return TextureType.Texture2D; } } 21 | public TextureDataSource DataSource { get { return TextureDataSource.Dynamic; } } 22 | public PixelFormat PixelFormat { get { return _pixelFormat; } } 23 | public int Width { get; private set; } 24 | public int Height { get; private set; } 25 | 26 | protected override void InitializeInternal() 27 | { 28 | _descr = new Texture2DDescription 29 | { 30 | CpuAccessFlags = CpuAccessFlags.Write, 31 | ArraySize = 1, 32 | BindFlags = BindFlags.ShaderResource, 33 | Height = Width, 34 | Width = Height, 35 | SampleDescription = new SampleDescription(1, 0), 36 | MipLevels = 1, 37 | Format = _pixelFormat.ToDXGIFormat(), 38 | Usage = ResourceUsage.Dynamic 39 | }; 40 | _texture = new Texture2D(DeviceManager.Device, _descr); 41 | ShaderResourceView = new ShaderResourceView(DeviceManager.Device, _texture); 42 | } 43 | 44 | protected override void UninitializeInternal() 45 | { 46 | if(ShaderResourceView != null) 47 | { 48 | ShaderResourceView.Dispose(); 49 | ShaderResourceView = null; 50 | } 51 | if(_texture != null) 52 | { 53 | _texture.Dispose(); 54 | _texture = null; 55 | _descr = default(Texture2DDescription); 56 | } 57 | } 58 | 59 | public IShaderResourceWriter BeginWrite() 60 | { 61 | return new ShaderResourceWriter(DeviceManager, _texture); 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/MaterialManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Materials; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Materials 6 | { 7 | class MaterialManager : ActivatablePlatformResourceManager, IMaterialManager 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | private readonly ITextureResourceManager _textureResourceManager; 11 | private readonly ITextureSamplerManager _textureSamplerManager; 12 | 13 | public MaterialManager(GraphicsContext gfx) 14 | { 15 | if(gfx == null) throw new ArgumentNullException("gfx"); 16 | 17 | _deviceManager = gfx.DeviceManager; 18 | _textureResourceManager = gfx.TextureResourceManager; 19 | _textureSamplerManager = gfx.TextureSamplerManager; 20 | } 21 | 22 | public Material Create(string id) 23 | { 24 | return Add(new MaterialResource(_deviceManager, _textureResourceManager, _textureSamplerManager, id), false); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/ShaderResource.cs: -------------------------------------------------------------------------------- 1 | using SharpDX.Direct3D11; 2 | 3 | namespace Grasshopper.SharpDX.Graphics.Materials 4 | { 5 | internal abstract class ShaderResource : IndexActivatableD3DResource 6 | { 7 | protected ShaderResource(DeviceManager deviceManager, string id) : base(deviceManager, id) 8 | { 9 | } 10 | 11 | public ShaderResourceView ShaderResourceView { get; protected set; } 12 | 13 | protected override void ActivateAtIndex(int index) 14 | { 15 | DeviceManager.Context.PixelShader.SetShaderResource(index, ShaderResourceView); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/ShaderResourceWriter.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Grasshopper.Graphics.Materials; 3 | using SharpDX; 4 | using SharpDX.Direct3D11; 5 | 6 | namespace Grasshopper.SharpDX.Graphics.Materials 7 | { 8 | internal class ShaderResourceWriter : IShaderResourceWriter 9 | where T : Resource 10 | { 11 | private readonly DeviceManager _deviceManager; 12 | private readonly T _resource; 13 | private readonly DataStream _stream; 14 | private DataBox _dataBox; 15 | 16 | public ShaderResourceWriter(DeviceManager deviceManager, T resource) 17 | { 18 | _deviceManager = deviceManager; 19 | _resource = resource; 20 | _dataBox = deviceManager.Context.MapSubresource(resource, 0, MapMode.WriteDiscard, MapFlags.None, out _stream); 21 | } 22 | 23 | public void Write(T value) where T : struct 24 | { 25 | _stream.Write(value); 26 | } 27 | 28 | public void WriteRange(T[] values) where T : struct 29 | { 30 | _stream.WriteRange(values); 31 | } 32 | 33 | public void Seek(long offset, SeekOrigin origin) 34 | { 35 | _stream.Seek(offset, origin); 36 | } 37 | 38 | public void Dispose() 39 | { 40 | _deviceManager.Context.UnmapSubresource(_resource, 0); 41 | _stream.Dispose(); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/Texture2DArrayResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Materials; 5 | using SharpDX.Direct3D11; 6 | using SharpDX.DXGI; 7 | 8 | namespace Grasshopper.SharpDX.Graphics.Materials 9 | { 10 | class Texture2DArrayResource : ShaderResource, ITextureResource 11 | { 12 | private readonly Texture2DFileResource[] _textures; 13 | private Texture2D _textureArray; 14 | private Format _format = Format.Unknown; 15 | 16 | public Texture2DArrayResource(DeviceManager deviceManager, string id, IEnumerable sourceTextures) 17 | : base(deviceManager, id) 18 | { 19 | if(sourceTextures == null) throw new ArgumentNullException("sourceTextures"); 20 | _textures = sourceTextures.ToArray(); 21 | } 22 | 23 | public TextureType TextureType { get { return TextureType.Texture2DArray; } } 24 | public TextureDataSource DataSource { get { return TextureDataSource.Internal; } } 25 | public PixelFormat PixelFormat { get { return _format.ToPixelFormat(); } } 26 | 27 | protected override void InitializeInternal() 28 | { 29 | var sourceTextures = _textures.Select(t => 30 | { 31 | var view = t.ShaderResourceView; 32 | if(view == null) 33 | throw new InvalidOperationException(string.Format("Texture array cannot be created because source texture '{0}' is not initialized", t.Id)); 34 | return view.Resource.QueryInterface(); 35 | }).ToArray(); 36 | 37 | var descr = sourceTextures[0].Description; 38 | descr.ArraySize = _textures.Length; 39 | _format = descr.Format; 40 | _textureArray = new Texture2D(DeviceManager.Device, descr); 41 | ShaderResourceView = new ShaderResourceView(DeviceManager.Device, _textureArray); 42 | 43 | var mipLevels = descr.MipLevels; 44 | for(var i = 0; i < mipLevels; i++) 45 | { 46 | for(var j = 0; j < _textures.Length; j++) 47 | { 48 | var texture = sourceTextures[j]; 49 | DeviceManager.Context.CopySubresourceRegion(texture, i, null, _textureArray, mipLevels * j + i); 50 | } 51 | } 52 | } 53 | 54 | protected override void UninitializeInternal() 55 | { 56 | if(ShaderResourceView != null) 57 | { 58 | ShaderResourceView.Dispose(); 59 | ShaderResourceView = null; 60 | } 61 | if(_textureArray != null) 62 | { 63 | _textureArray.Dispose(); 64 | _textureArray = null; 65 | } 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/Texture2DFileResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Materials; 3 | using Grasshopper.Platform; 4 | using SharpDX.Direct3D11; 5 | 6 | namespace Grasshopper.SharpDX.Graphics.Materials 7 | { 8 | class Texture2DFileResource : ShaderResource, ITexture2DResource 9 | { 10 | private readonly IFileSource _fileSource; 11 | private Texture2DDescription _descr; 12 | 13 | public Texture2DFileResource(DeviceManager deviceManager, string id, IFileSource fileSource) 14 | : base(deviceManager, id) 15 | { 16 | if(fileSource == null) throw new ArgumentNullException("fileSource"); 17 | _fileSource = fileSource; 18 | } 19 | 20 | public TextureType TextureType { get { return TextureType.Texture2D; } } 21 | public TextureDataSource DataSource { get { return TextureDataSource.FileSystem; } } 22 | public PixelFormat PixelFormat { get { return _descr.Format.ToPixelFormat(); } } 23 | public int Width { get { return _descr.Width; } } 24 | public int Height { get { return _descr.Height; } } 25 | 26 | protected override void InitializeInternal() 27 | { 28 | // todo: make sure the texture has actually loaded successfully - note that it may not be loading from a regular file, so we can't rely on FileNotFoundException bubbling up; wrap it in a local exception 29 | using(var stream = _fileSource.OpenRead()) 30 | ShaderResourceView = ShaderResourceView.FromStream(DeviceManager.Device, stream, _fileSource.Size); 31 | _descr = ShaderResourceView.ResourceAs().Description; 32 | } 33 | 34 | protected override void UninitializeInternal() 35 | { 36 | _descr = default(Texture2DDescription); 37 | if(ShaderResourceView != null) 38 | { 39 | ShaderResourceView.Dispose(); 40 | ShaderResourceView = null; 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/TextureResourceManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Materials; 5 | using Grasshopper.Platform; 6 | 7 | namespace Grasshopper.SharpDX.Graphics.Materials 8 | { 9 | class TextureResourceManager : IndexActivatablePlatformResourceManager, ITextureResourceManager 10 | { 11 | private readonly DeviceManager _deviceManager; 12 | private readonly Lazy _fileStore; 13 | 14 | public TextureResourceManager(DeviceManager deviceManager, Lazy fileStore) 15 | { 16 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 17 | if(fileStore == null) throw new ArgumentNullException("fileStore"); 18 | 19 | _deviceManager = deviceManager; 20 | _fileStore = fileStore; 21 | } 22 | 23 | protected override void Activate(int firstIndex, IEnumerable resources) 24 | { 25 | _deviceManager.Context.PixelShader.SetShaderResources(firstIndex, resources.Select((r, i) => 26 | { 27 | var texture = (ShaderResource)r; 28 | texture.SetActivatedExternally(firstIndex + i); 29 | return texture.ShaderResourceView; 30 | }).ToArray()); 31 | } 32 | 33 | public ITextureResource Create2DFromFile(string id, string path) 34 | { 35 | var assetSource = _fileStore.Value.GetFile(path); 36 | return Create2DFromFile(id, assetSource); 37 | } 38 | 39 | public ITextureResource Create2DFromFile(string id, IFileSource fileSource) 40 | { 41 | return Add(new Texture2DFileResource(_deviceManager, id, fileSource)); 42 | } 43 | 44 | public IDynamicTexture2DResource Create2DDynamic(string id, int width, int height, PixelFormat pixelFormat) 45 | { 46 | return (DynamicTexture2DResource)Add(new DynamicTexture2DResource(_deviceManager, id, width, height, pixelFormat)); 47 | } 48 | 49 | public ITextureResource Create2DArray(string textureArrayId, params string[] sourceTextureIds) 50 | { 51 | return Add(new Texture2DArrayResource(_deviceManager, textureArrayId, sourceTextureIds.Select(id => 52 | { 53 | var sourceTexture = this[id] as Texture2DFileResource; 54 | if(sourceTexture == null) 55 | throw new InvalidOperationException(string.Format("Texture resource '{0}' cannot be used in a texture array as it is of type {1}", textureArrayId, this[id].GetType().FullName)); 56 | return sourceTexture; 57 | }))); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Materials/TextureSamplerManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Materials; 5 | using Grasshopper.Platform; 6 | 7 | namespace Grasshopper.SharpDX.Graphics.Materials 8 | { 9 | class TextureSamplerManager : IndexActivatablePlatformResourceManager, ITextureSamplerManager 10 | { 11 | private readonly DeviceManager _deviceManager; 12 | private readonly Dictionary _samplers = new Dictionary(); 13 | 14 | public TextureSamplerManager(DeviceManager deviceManager) 15 | { 16 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 17 | _deviceManager = deviceManager; 18 | } 19 | 20 | protected override void Activate(int firstIndex, IEnumerable resources) 21 | { 22 | _deviceManager.Context.PixelShader.SetSamplers(firstIndex, 23 | resources.Select(sampler => ((TextureSampler)sampler).SamplerState).ToArray()); 24 | } 25 | 26 | public ITextureSampler Create(string id, TextureSamplerSettings settings) 27 | { 28 | return Add(new TextureSampler(_deviceManager, id, settings)); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/BlendStateManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Blending; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering 6 | { 7 | class BlendStateManager : ActivatablePlatformResourceManager, IBlendStateManager 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | 11 | public BlendStateManager(DeviceManager deviceManager) 12 | { 13 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 14 | _deviceManager = deviceManager; 15 | } 16 | 17 | public IBlendState Create(string id, IBlendSettings settings) 18 | { 19 | return Add(new BlendState(_deviceManager, id, settings)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/ConstantBufferManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Graphics.Rendering.Buffers; 5 | using Grasshopper.Platform; 6 | 7 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 8 | { 9 | class ConstantBufferManager : IndexActivatablePlatformResourceManager>, IConstantBufferManager where T : struct 10 | { 11 | private readonly DeviceManager _deviceManager; 12 | 13 | public ConstantBufferManager(DeviceManager deviceManager) 14 | { 15 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 16 | _deviceManager = deviceManager; 17 | } 18 | 19 | public IConstantBufferResource Create(string id) 20 | { 21 | return Add(new ConstantBufferResource(_deviceManager, id)); 22 | } 23 | 24 | public void Update(string id, T data) 25 | { 26 | this[id].Update(ref data); 27 | } 28 | 29 | public void Update(string id, ref T data) 30 | { 31 | this[id].Update(ref data); 32 | } 33 | 34 | protected override void Activate(int firstIndex, IEnumerable> resources) 35 | { 36 | _deviceManager.Context.VertexShader.SetConstantBuffers(firstIndex, resources.Select((r, i) => 37 | { 38 | var resource = (ConstantBufferResource)r; 39 | resource.SetActivatedExternally(firstIndex + i); 40 | return resource.Buffer; 41 | }).ToArray()); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/ConstantBufferManagerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 5 | { 6 | class ConstantBufferManagerFactory : IConstantBufferManagerFactory 7 | { 8 | private readonly DeviceManager _deviceManager; 9 | 10 | public ConstantBufferManagerFactory(DeviceManager deviceManager) 11 | { 12 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 13 | _deviceManager = deviceManager; 14 | } 15 | 16 | public IConstantBufferManager Create() where T : struct 17 | { 18 | return new ConstantBufferManager(_deviceManager); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/ConstantBufferResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | using SharpDX; 4 | using SharpDX.Direct3D11; 5 | using Buffer = SharpDX.Direct3D11.Buffer; 6 | 7 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 8 | { 9 | class ConstantBufferResource : IndexActivatableD3DResource, IConstantBufferResource 10 | where T : struct 11 | { 12 | private static readonly int _sizeofT = Utilities.SizeOf(); 13 | 14 | public ConstantBufferResource(DeviceManager deviceManager, string id) 15 | : base(deviceManager, id) 16 | { 17 | } 18 | 19 | public Buffer Buffer { get; private set; } 20 | 21 | public void Update(T data) 22 | { 23 | Update(ref data); 24 | } 25 | 26 | public void Update(ref T data) 27 | { 28 | if(!IsInitialized) 29 | throw new Exception(); 30 | DeviceManager.Context.UpdateSubresource(ref data, Buffer); 31 | } 32 | 33 | protected override void InitializeInternal() 34 | { 35 | Buffer = new Buffer(DeviceManager.Device, _sizeofT, ResourceUsage.Default, BindFlags.ConstantBuffer, 36 | CpuAccessFlags.None, ResourceOptionFlags.None, _sizeofT); 37 | } 38 | 39 | protected override void UninitializeInternal() 40 | { 41 | if(Buffer != null) 42 | { 43 | Buffer.Dispose(); 44 | Buffer = null; 45 | } 46 | } 47 | 48 | protected override void ActivateAtIndex(int index) 49 | { 50 | DeviceManager.Context.VertexShader.SetConstantBuffer(index, Buffer); 51 | } 52 | 53 | private class ConstantBufferWriter : IConstantBufferDataWriter 54 | { 55 | private DeviceManager _deviceManager; 56 | private Buffer _buffer; 57 | 58 | public ConstantBufferWriter(string id, DeviceManager deviceManager, Buffer buffer) 59 | { 60 | _deviceManager = deviceManager; 61 | _buffer = buffer; 62 | Id = id; 63 | } 64 | 65 | public string Id { get; private set; } 66 | public void WriteData(ref T data) 67 | { 68 | _deviceManager.Context.UpdateSubresource(ref data, _buffer); 69 | } 70 | 71 | public void Dispose() 72 | { 73 | _deviceManager = null; 74 | _buffer = null; 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/IndexBufferManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 6 | { 7 | class IndexBufferManager : ActivatablePlatformResourceManager, IIndexBufferManager 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | 11 | public IndexBufferManager(DeviceManager deviceManager) 12 | { 13 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 14 | _deviceManager = deviceManager; 15 | } 16 | 17 | public IIndexBufferResource Create(string id) 18 | { 19 | return Add(new IndexBufferResource(_deviceManager, id)); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/IndexBufferManagerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 5 | { 6 | class IndexBufferManagerFactory : IIndexBufferManagerFactory 7 | { 8 | private readonly DeviceManager _deviceManager; 9 | 10 | public IndexBufferManagerFactory(DeviceManager deviceManager) 11 | { 12 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 13 | _deviceManager = deviceManager; 14 | } 15 | 16 | public IIndexBufferManager Create() 17 | { 18 | return new IndexBufferManager(_deviceManager); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/VertexBufferManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 6 | { 7 | class VertexBufferManager : IndexActivatablePlatformResourceManager>, IVertexBufferManager 8 | where T : struct 9 | { 10 | private readonly DeviceManager _deviceManager; 11 | 12 | public VertexBufferManager(DeviceManager deviceManager) 13 | { 14 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 15 | _deviceManager = deviceManager; 16 | } 17 | 18 | public IVertexBufferResource Create(string id) 19 | { 20 | return Add(new VertexBufferResource(_deviceManager, id)); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Buffers/VertexBufferManagerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Buffers; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering.Buffers 5 | { 6 | internal class VertexBufferManagerFactory : IVertexBufferManagerFactory 7 | { 8 | private readonly DeviceManager _deviceManager; 9 | 10 | public VertexBufferManagerFactory(DeviceManager deviceManager) 11 | { 12 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 13 | _deviceManager = deviceManager; 14 | } 15 | 16 | public IVertexBufferManager Create() where T : struct 17 | { 18 | return new VertexBufferManager(_deviceManager); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/DrawingContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering; 3 | using SharpDX.Direct3D; 4 | using Color = Grasshopper.Graphics.Primitives.Color; 5 | 6 | namespace Grasshopper.SharpDX.Graphics.Rendering 7 | { 8 | public abstract class DrawingContext : IDrawingContext 9 | { 10 | protected DeviceManager DeviceManager { get; private set; } 11 | 12 | protected DrawingContext(DeviceManager deviceManager) 13 | { 14 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 15 | DeviceManager = deviceManager; 16 | } 17 | 18 | public void Activate() 19 | { 20 | MakeTargetsActive(); 21 | } 22 | 23 | public void SetDrawType(DrawType drawType) 24 | { 25 | switch(drawType) 26 | { 27 | case DrawType.Points: 28 | DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList; 29 | break; 30 | case DrawType.LineList: 31 | DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList; 32 | break; 33 | case DrawType.LineStrip: 34 | DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip; 35 | break; 36 | case DrawType.Triangles: 37 | DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; 38 | break; 39 | case DrawType.TriangleStrip: 40 | DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; 41 | break; 42 | default: 43 | throw new NotSupportedException("Unexpected draw type: " + drawType); 44 | } 45 | } 46 | 47 | public void Draw(int vertexCount, int vertexStartLocation) 48 | { 49 | DeviceManager.Context.Draw(vertexCount, vertexStartLocation); 50 | } 51 | 52 | public void DrawInstanced(int vertexCount, int instanceCount, int vertexStartLocation, int instanceStartLocation) 53 | { 54 | DeviceManager.Context.DrawInstanced(vertexCount, instanceCount, vertexStartLocation, instanceStartLocation); 55 | } 56 | 57 | public void DrawIndexed(int indexCount, int indexStartLocation, int vertexStartLocation) 58 | { 59 | DeviceManager.Context.DrawIndexed(indexCount, indexStartLocation, vertexStartLocation); 60 | } 61 | 62 | public void DrawIndexedInstanced(int indexCountPerInstance, int instanceCount, int indexStartLocation, int vertexStartLocation, int instanceStartLocation) 63 | { 64 | DeviceManager.Context.DrawIndexedInstanced(indexCountPerInstance, instanceCount, indexStartLocation, vertexStartLocation, instanceStartLocation); 65 | } 66 | 67 | protected abstract void MakeTargetsActive(); 68 | public abstract void Clear(Color color); 69 | 70 | protected bool IsDisposed { get; private set; } 71 | protected abstract void DestroyResources(); 72 | 73 | public void Dispose() 74 | { 75 | DestroyResources(); 76 | IsDisposed = true; 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Internal/DepthBuffer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SharpDX.Direct3D11; 3 | using SharpDX.DXGI; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering.Internal 6 | { 7 | internal class DepthBuffer : IDisposable 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | private Texture2DDescription _zBufferTextureDescription; 11 | 12 | public DepthBuffer(DeviceManager deviceManager, int width, int height) 13 | { 14 | _deviceManager = deviceManager; 15 | Width = width; 16 | Height = height; 17 | 18 | SampleCount = 4; 19 | SampleQuality = 4; 20 | } 21 | 22 | public int Width { get; private set; } 23 | public int Height { get; private set; } 24 | public bool Initialized { get; private set; } 25 | internal DepthStencilView DepthStencilView { get; private set; } 26 | 27 | public int SampleCount { get; set; } 28 | public int SampleQuality { get; set; } 29 | 30 | public void Initialize() 31 | { 32 | if(!_deviceManager.IsInitialized) 33 | throw new InvalidOperationException("Device manager is not initialized"); 34 | 35 | if(Initialized) 36 | Uninitialize(); 37 | 38 | _zBufferTextureDescription = new Texture2DDescription 39 | { 40 | Format = Format.D32_Float_S8X24_UInt, 41 | ArraySize = 1, 42 | MipLevels = 1, 43 | Width = Width, 44 | Height = Height, 45 | SampleDescription = new SampleDescription(SampleCount, SampleQuality), 46 | Usage = ResourceUsage.Default, 47 | BindFlags = BindFlags.DepthStencil, 48 | CpuAccessFlags = CpuAccessFlags.None, 49 | OptionFlags = ResourceOptionFlags.None 50 | }; 51 | using(var zBufferTexture = new Texture2D(_deviceManager.Device, _zBufferTextureDescription)) 52 | DepthStencilView = new DepthStencilView(_deviceManager.Device, zBufferTexture); 53 | 54 | Initialized = true; 55 | } 56 | 57 | public void Uninitialize() 58 | { 59 | if(DepthStencilView != null) 60 | { 61 | DepthStencilView.Dispose(); 62 | DepthStencilView = null; 63 | } 64 | 65 | Initialized = false; 66 | } 67 | 68 | public void Resize(int width, int height) 69 | { 70 | Width = width; 71 | Height = height; 72 | 73 | if(Initialized) 74 | Initialize(); 75 | } 76 | 77 | public void Dispose() 78 | { 79 | Uninitialize(); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/Internal/TextureBuffer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SharpDX.Direct3D11; 3 | using SharpDX.DXGI; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering.Internal 6 | { 7 | internal class TextureBuffer : IDisposable 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | 11 | public TextureBuffer(DeviceManager deviceManager, int width, int height) 12 | { 13 | _deviceManager = deviceManager; 14 | Width = width; 15 | Height = height; 16 | } 17 | 18 | public int Width { get; private set; } 19 | public int Height { get; private set; } 20 | public bool Initialized { get; private set; } 21 | public ShaderResourceView ShaderResourceView { get; private set; } 22 | public Texture2D Texture { get; private set; } 23 | 24 | public bool UseMipMaps { get; set; } 25 | public int SampleCount { get; set; } 26 | public int SampleQuality { get; set; } 27 | 28 | public void Initialize() 29 | { 30 | if(!_deviceManager.IsInitialized) 31 | throw new InvalidOperationException("Device manager is not initialized"); 32 | 33 | if(Initialized) 34 | Uninitialize(); 35 | 36 | var textureDescription = new Texture2DDescription 37 | { 38 | CpuAccessFlags = CpuAccessFlags.None, 39 | ArraySize = 1, 40 | BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, 41 | Height = Width, 42 | Width = Height, 43 | SampleDescription = new SampleDescription(SampleCount, SampleQuality), 44 | MipLevels = UseMipMaps ? 0 : 1, 45 | Format = Format.B8G8R8A8_UNorm, 46 | }; 47 | 48 | Texture = new Texture2D(_deviceManager.Device, textureDescription); 49 | new RenderTargetView(_deviceManager.Device, Texture); 50 | ShaderResourceView = new ShaderResourceView(_deviceManager.Device, Texture); 51 | } 52 | 53 | public void Uninitialize() 54 | { 55 | if(Texture != null) 56 | { 57 | Texture.Dispose(); 58 | Texture = null; 59 | } 60 | if(ShaderResourceView != null) 61 | { 62 | ShaderResourceView.Dispose(); 63 | ShaderResourceView = null; 64 | } 65 | Initialized = false; 66 | } 67 | 68 | public void Resize(int width, int height) 69 | { 70 | Width = width; 71 | Height = height; 72 | 73 | if(Initialized) 74 | Initialize(); 75 | } 76 | 77 | public void Dispose() 78 | { 79 | Uninitialize(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/RasterizerState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Rasterization; 3 | using SharpDX.Direct3D11; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering 6 | { 7 | class RasterizerState : ActivatableD3DResource, IRasterizerState 8 | { 9 | public RasterizerState(DeviceManager deviceManager, string id, IRasterizerSettings settings) : base(deviceManager, id) 10 | { 11 | if(settings == null) throw new ArgumentNullException("settings"); 12 | Settings = settings; 13 | } 14 | 15 | public global::SharpDX.Direct3D11.RasterizerState State { get; private set; } 16 | public IRasterizerSettings Settings { get; private set; } 17 | 18 | protected override void InitializeInternal() 19 | { 20 | var desc = RasterizerStateDescription.Default(); 21 | desc.IsFrontCounterClockwise = Settings.WindingOrder == WindingOrder.Counterclockwise; 22 | desc.FillMode = Settings.RenderWireframe ? FillMode.Wireframe : FillMode.Solid; 23 | desc.IsDepthClipEnabled = Settings.EnableDepthTest; 24 | desc.IsMultisampleEnabled = Settings.Antialiasing == Antialiasing.Multisample; 25 | desc.IsAntialiasedLineEnabled = Settings.Antialiasing == Antialiasing.LinesOnly; 26 | switch(Settings.TriangleCulling) 27 | { 28 | case TriangleCulling.DrawFrontFacing: 29 | desc.CullMode = CullMode.Back; 30 | break; 31 | case TriangleCulling.DrawBackFacing: 32 | desc.CullMode = CullMode.Front; 33 | break; 34 | case TriangleCulling.DrawAll: 35 | desc.CullMode = CullMode.None; 36 | break; 37 | } 38 | State = new global::SharpDX.Direct3D11.RasterizerState(DeviceManager.Device, desc); 39 | } 40 | 41 | protected override void UninitializeInternal() 42 | { 43 | if(State != null) 44 | { 45 | State.Dispose(); 46 | State = null; 47 | } 48 | } 49 | 50 | protected override void SetActive() 51 | { 52 | DeviceManager.Context.Rasterizer.State = State; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/RasterizerStateManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering.Rasterization; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering 6 | { 7 | class RasterizerStateManager : ActivatablePlatformResourceManager, IRasterizerStateManager 8 | { 9 | private readonly DeviceManager _deviceManager; 10 | 11 | public RasterizerStateManager(DeviceManager deviceManager) 12 | { 13 | if(deviceManager == null) throw new ArgumentNullException("deviceManager"); 14 | _deviceManager = deviceManager; 15 | } 16 | 17 | public IRasterizerState Create(string id, IRasterizerSettings settings) 18 | { 19 | return Add(new RasterizerState(_deviceManager, id, settings)); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/RenderManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.SharpDX.Graphics.Materials; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering 5 | { 6 | class RenderManager : Grasshopper.Graphics.Rendering.RenderManager 7 | { 8 | private readonly GraphicsContext _graphics; 9 | private readonly MaterialManager _materials; 10 | 11 | public RenderManager(GraphicsContext graphics) : base(graphics) 12 | { 13 | if(graphics == null) throw new ArgumentNullException("deviceManager"); 14 | _graphics = graphics; 15 | _materials = new MaterialManager(graphics); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/RenderTarget.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering 5 | { 6 | public class RenderTarget : IRenderTarget 7 | where T : IDrawingContext 8 | { 9 | private T _targetContext; 10 | 11 | public bool Terminated { get; protected set; } 12 | 13 | public RenderTarget(T drawingContext) 14 | { 15 | _targetContext = drawingContext; 16 | } 17 | 18 | public virtual void Render(FrameContext frame, RenderFrameHandlerEx renderFrame) 19 | { 20 | if(Terminated) return; 21 | _targetContext.Activate(); 22 | renderFrame(frame, _targetContext); 23 | } 24 | 25 | public virtual void Render(RenderFrameHandler renderFrame) 26 | { 27 | if(Terminated) return; 28 | _targetContext.Activate(); 29 | renderFrame(_targetContext); 30 | } 31 | 32 | protected event Action Disposing; 33 | 34 | public void Dispose() 35 | { 36 | var handler = Disposing; 37 | if(handler != null) 38 | handler(); 39 | 40 | _targetContext.Dispose(); 41 | _targetContext = default(T); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/RenderTargetFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Graphics.Rendering; 3 | using Grasshopper.Input; 4 | 5 | namespace Grasshopper.SharpDX.Graphics.Rendering 6 | { 7 | class RenderTargetFactory : IRenderTargetFactory 8 | { 9 | private readonly GraphicsContext _graphics; 10 | private readonly IInputContext _input; 11 | 12 | public RenderTargetFactory(GraphicsContext graphics, IInputContext input) 13 | { 14 | if(graphics == null) throw new ArgumentNullException("graphics"); 15 | if(input == null) throw new ArgumentNullException("input"); 16 | _graphics = graphics; 17 | _input = input; 18 | } 19 | 20 | public IWindowRenderTarget CreateWindow() 21 | { 22 | var renderer = new WindowRenderTarget(_graphics, _input); 23 | renderer.Initialize(); 24 | return renderer; 25 | } 26 | 27 | //public IWindowRenderTarget CreateCompositeWindow() 28 | //{ 29 | // var renderer = new WindowRenderTarget(_graphicsContext.DeviceManager, _input); 30 | // renderer.Initialize(); 31 | // return renderer; 32 | //} 33 | 34 | public ITextureRenderTarget CreateTexture(int width, int height) 35 | { 36 | var renderHost = new TextureRenderTarget(_graphics.DeviceManager, width, height); 37 | renderHost.Initialize(); 38 | return renderHost; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/Rendering/WindowRenderTarget.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Graphics.Rendering; 2 | using Grasshopper.Input; 3 | 4 | namespace Grasshopper.SharpDX.Graphics.Rendering 5 | { 6 | class WindowRenderTarget : RenderTarget, IWindowRenderTarget 7 | { 8 | private readonly WindowDrawingContext _drawingContext; 9 | 10 | public WindowRenderTarget(GraphicsContext graphics, IInputContext input) : this(new WindowDrawingContext(graphics, input)) 11 | { 12 | } 13 | 14 | private WindowRenderTarget(WindowDrawingContext drawingContext) : base(drawingContext) 15 | { 16 | _drawingContext = drawingContext; 17 | } 18 | 19 | public IAppWindow Window { get { return _drawingContext.Window; } } 20 | 21 | private bool CheckWindowStatus() 22 | { 23 | if(Window != null && !Window.NextFrame()) 24 | { 25 | Terminated = true; 26 | return false; 27 | } 28 | return true; 29 | } 30 | 31 | public override void Render(FrameContext frame, RenderFrameHandlerEx renderFrame) 32 | { 33 | if(!CheckWindowStatus()) return; 34 | base.Render(frame, renderFrame); 35 | } 36 | 37 | public override void Render(RenderFrameHandler renderFrame) 38 | { 39 | if(!CheckWindowStatus()) return; 40 | base.Render(renderFrame); 41 | } 42 | 43 | public void Initialize() 44 | { 45 | _drawingContext.Initialize(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/TextureDrawingContext.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Graphics.Rendering; 2 | using Grasshopper.SharpDX.Graphics.Rendering; 3 | using Grasshopper.SharpDX.Graphics.Rendering.Internal; 4 | using SharpDX.Direct3D11; 5 | using Color = Grasshopper.Graphics.Primitives.Color; 6 | using Color4 = SharpDX.Color4; 7 | 8 | namespace Grasshopper.SharpDX.Graphics 9 | { 10 | class TextureDrawingContext : DrawingContext, ITextureDrawingContext 11 | { 12 | private readonly TextureBuffer _textureBuffer; 13 | private readonly DepthBuffer _depthBuffer; 14 | private bool _isInitialized; 15 | private RenderTargetView _renderTargetView; 16 | 17 | public TextureDrawingContext(DeviceManager deviceManager, int width, int height) 18 | : base(deviceManager: deviceManager) 19 | { 20 | Width = width; 21 | Height = height; 22 | 23 | _textureBuffer = new TextureBuffer(deviceManager, width, height); 24 | _depthBuffer = new DepthBuffer(deviceManager, width, height); 25 | } 26 | 27 | public int Width { get; private set; } 28 | public int Height { get; private set; } 29 | 30 | public void Initialize() 31 | { 32 | if(_isInitialized) 33 | DestroyResources(); 34 | 35 | _textureBuffer.Initialize(); 36 | _depthBuffer.Initialize(); 37 | _renderTargetView = new RenderTargetView(DeviceManager.Device, _textureBuffer.Texture); 38 | _isInitialized = true; 39 | } 40 | 41 | protected override void DestroyResources() 42 | { 43 | _textureBuffer.Dispose(); 44 | _depthBuffer.Dispose(); 45 | _renderTargetView.Dispose(); 46 | _isInitialized = false; 47 | } 48 | 49 | protected override void MakeTargetsActive() 50 | { 51 | DeviceManager.Context.OutputMerger.SetTargets(_depthBuffer.DepthStencilView, _renderTargetView); 52 | } 53 | 54 | public override void Clear(Color color) 55 | { 56 | if(_depthBuffer.DepthStencilView != null) 57 | DeviceManager.Context.ClearDepthStencilView(_depthBuffer.DepthStencilView, DepthStencilClearFlags.Depth, 1f, 0); 58 | 59 | if(_renderTargetView != null) 60 | DeviceManager.Context.ClearRenderTargetView(_renderTargetView, new Color4(color.ToRgba())); 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Graphics/TextureRenderTarget.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Graphics.Rendering; 2 | using Grasshopper.SharpDX.Graphics.Rendering; 3 | 4 | namespace Grasshopper.SharpDX.Graphics 5 | { 6 | class TextureRenderTarget : RenderTarget, ITextureRenderTarget 7 | { 8 | private readonly TextureDrawingContext _drawingContext; 9 | 10 | public TextureRenderTarget(DeviceManager deviceManager, int width, int height) 11 | : this(new TextureDrawingContext(deviceManager, width, height)) 12 | { 13 | } 14 | 15 | private TextureRenderTarget(TextureDrawingContext drawingContext) : base(drawingContext) 16 | { 17 | _drawingContext = drawingContext; 18 | } 19 | 20 | public void Initialize() 21 | { 22 | _drawingContext.Initialize(); 23 | } 24 | 25 | public void ActivateTextureResource(int index) 26 | { 27 | 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Grasshopper.SharpDX")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Grasshopper.SharpDX")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("82b3f64b-8d8a-4dfd-971d-546e8fa4bfc0")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/SharpDXBootstrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Grasshopper.Platform; 3 | using Grasshopper.SharpDX.Graphics; 4 | using Grasshopper.SharpDX.Input; 5 | 6 | namespace Grasshopper.SharpDX 7 | { 8 | public static class SharpDXBootstrapper 9 | { 10 | public static T UseSharpDX(this T app) where T : GrasshopperApp 11 | { 12 | app.Input = new InputContext(); 13 | var factory = new GraphicsContextFactory(new Lazy(() => 14 | { 15 | if(app.Files == null) 16 | throw new InvalidOperationException("No implementation for IFileStore was initialized. Did you forget to reference and call app.UseWindowsFileSystem() or a platform-specific alternative?"); 17 | return app.Files; 18 | }), app.Input); 19 | app.Graphics = factory; 20 | return app; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.SharpDX/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.WindowsFileSystem/FileSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Grasshopper.Platform; 4 | 5 | namespace Grasshopper.WindowsFileSystem 6 | { 7 | public class FileSource : IFileSource 8 | { 9 | private readonly FileInfo _location; 10 | 11 | public FileSource(string path) 12 | { 13 | _location = new FileInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)); 14 | Path = path; 15 | Size = (int)new FileInfo(_location.FullName).Length; 16 | } 17 | 18 | public Stream OpenRead() 19 | { 20 | return File.OpenRead(_location.FullName); 21 | } 22 | 23 | public string Path { get; private set; } 24 | public int Size { get; private set; } 25 | } 26 | } -------------------------------------------------------------------------------- /src/platform/Grasshopper.WindowsFileSystem/FileStore.cs: -------------------------------------------------------------------------------- 1 | using Grasshopper.Platform; 2 | 3 | namespace Grasshopper.WindowsFileSystem 4 | { 5 | public class FileStore : IFileStore 6 | { 7 | public IFileSource GetFile(string path) 8 | { 9 | return new FileSource(path); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.WindowsFileSystem/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("Grasshopper.WindowsFileSystem")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("Grasshopper.WindowsFileSystem")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("ec3870ce-4ca5-44d1-87ba-cb00c8cd9f00")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /src/platform/Grasshopper.WindowsFileSystem/WindowsFileSystemBootstrapper.cs: -------------------------------------------------------------------------------- 1 | namespace Grasshopper.WindowsFileSystem 2 | { 3 | public static class WindowsFileSystemBootstrapper 4 | { 5 | public static T UseWindowsFileSystem(this T app) where T : GrasshopperApp 6 | { 7 | var assetReader = new FileStore(); 8 | app.Files = assetReader; 9 | return app; 10 | } 11 | } 12 | } 13 | --------------------------------------------------------------------------------