Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.01 KB | None | 0 0
  1. // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // -----------------------------------------------------------------------------
  21. // Original code from SlimDX project.
  22. // Greetings to SlimDX Group. Original code published with the following license:
  23. // -----------------------------------------------------------------------------
  24. /*
  25. * Copyright (c) 2007-2011 SlimDX Group
  26. *
  27. * Permission is hereby granted, free of charge, to any person obtaining a copy
  28. * of this software and associated documentation files (the "Software"), to deal
  29. * in the Software without restriction, including without limitation the rights
  30. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  31. * copies of the Software, and to permit persons to whom the Software is
  32. * furnished to do so, subject to the following conditions:
  33. *
  34. * The above copyright notice and this permission notice shall be included in
  35. * all copies or substantial portions of the Software.
  36. *
  37. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  38. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  39. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  40. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  41. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  42. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  43. * THE SOFTWARE.
  44. */
  45. using System;
  46. using SharpDX;
  47. using SharpDX.D3DCompiler;
  48. using SharpDX.Direct3D;
  49. using SharpDX.Direct3D11;
  50. using SharpDX.DXGI;
  51. using SharpDX.Windows;
  52. using Buffer = SharpDX.Direct3D11.Buffer;
  53. using Device = SharpDX.Direct3D11.Device;
  54. using System.Drawing;
  55. using GDI = System.Drawing.Imaging;
  56. using System.Diagnostics;
  57.  
  58. namespace MiniTri
  59. {
  60.     /// <summary>
  61.     ///   SharpDX port of SharpDX-MiniTri Direct3D 11 Sample
  62.     /// </summary>
  63.     internal static class Program
  64.     {
  65.         static Device device;
  66.  
  67.         [STAThread]
  68.         private static void Main()
  69.         {
  70.             var form = new RenderForm("SharpDX - MiniTri Direct3D 11 Sample");
  71.  
  72.             // SwapChain description
  73.             var desc = new SwapChainDescription()
  74.                            {
  75.                                BufferCount = 1,
  76.                                ModeDescription=
  77.                                    new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
  78.                                                        new Rational(60, 1), Format.R8G8B8A8_UNorm),
  79.                                IsWindowed = true,
  80.                                OutputHandle = form.Handle,
  81.                                SampleDescription = new SampleDescription(1, 0),
  82.                                SwapEffect = SwapEffect.Discard,
  83.                                Usage = Usage.RenderTargetOutput
  84.                            };
  85.  
  86.             // Create Device and SwapChain
  87.             ShaderResourceView textureView;
  88.             SamplerState sampler;
  89.             SwapChain swapChain;
  90.             Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);
  91.             var context = device.ImmediateContext;
  92.  
  93.             Bitmap image = new Bitmap("C:/Users/Joe/Pictures/testbitmap.jpg");
  94.  
  95.             // Ignore all windows events
  96.             var factory = swapChain.GetParent<Factory>();
  97.             factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
  98.  
  99.             // New RenderTargetView from the backbuffer
  100.             var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
  101.             var renderView = new RenderTargetView(device, backBuffer);
  102.  
  103.             // Compile Vertex and Pixel shaders
  104.             var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None);
  105.             var vertexShader = new VertexShader(device, vertexShaderByteCode);
  106.  
  107.             var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None);
  108.             var pixelShader = new PixelShader(device, pixelShaderByteCode);
  109.  
  110.             // Layout from VertexShader input signature
  111.             var layout = new InputLayout(
  112.                 device,
  113.                 ShaderSignature.GetInputSignature(vertexShaderByteCode),
  114.                 new[]
  115.                     {
  116.                         new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
  117.                         new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0)
  118.                     });
  119.  
  120.             // Instantiate Vertex buiffer from vertex data
  121.             var vertices = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, new[]
  122.                                   {
  123.                                       // 3D coordinates              UV Texture coordinates
  124.                                       -0.0f, -0.5f, -0.5f, 1.0f,     0.0f, 1.0f,
  125.                                       -0.5f,  0.5f, -0.5f, 1.0f,     0.0f, 0.0f,
  126.                                        0.5f,  0.5f, -0.5f, 1.0f,     1.0f, 0.0f,
  127.  
  128.                             });
  129.  
  130.             var constantBuffer = new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
  131.  
  132.             // Load texture and create sampler
  133.             var texture2D = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
  134.             {
  135.                 Width = 512,
  136.                 Height = 512,
  137.                 ArraySize = 1,
  138.                 BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | BindFlags.RenderTarget,
  139.                 Usage = SharpDX.Direct3D11.ResourceUsage.Default,
  140.                 CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
  141.                 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
  142.                 MipLevels = 1,
  143.                 OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
  144.                 SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
  145.             });
  146.  
  147.             texture2D = LoadTextures(image);
  148.             textureView = new ShaderResourceView(device, texture2D);
  149.  
  150.             sampler = new SamplerState(device, new SamplerStateDescription()
  151.             {
  152.                 Filter = Filter.MinMagMipLinear,
  153.                 AddressU = TextureAddressMode.Clamp,
  154.                 AddressV = TextureAddressMode.Clamp,
  155.                 AddressW = TextureAddressMode.Clamp,
  156.                 BorderColor = SharpDX.Color.Black,
  157.                 ComparisonFunction = Comparison.Never,
  158.                 MaximumAnisotropy = 16,
  159.                 MipLodBias = 0,
  160.                 MinimumLod = -float.MaxValue,
  161.                 MaximumLod = float.MaxValue
  162.             });
  163.  
  164.             // Main loop
  165.             RenderLoop.Run(form, () =>
  166.                                       {
  167.                                           // Prepare All the stages
  168.                                           context.ClearRenderTargetView(renderView, SharpDX.Color.Black);
  169.                                           context.InputAssembler.InputLayout = layout;
  170.                                           context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
  171.                                           context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, sizeof(float) * 6, 0));
  172.                                           context.VertexShader.Set(vertexShader);
  173.                                           context.Rasterizer.SetViewport(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
  174.                                           context.PixelShader.SetShaderResource(0, textureView);
  175.                                           context.PixelShader.SetSampler(0, sampler);
  176.                                           context.PixelShader.Set(pixelShader);
  177.  
  178.                                           context.Draw(3, 0);
  179.                                           swapChain.Present(0, PresentFlags.None);
  180.                                       });
  181.  
  182.             // Release all resources
  183.             vertexShaderByteCode.Dispose();
  184.             vertexShader.Dispose();
  185.             pixelShaderByteCode.Dispose();
  186.             pixelShader.Dispose();
  187.             vertices.Dispose();
  188.             layout.Dispose();
  189.             renderView.Dispose();
  190.             backBuffer.Dispose();
  191.             context.ClearState();
  192.             context.Flush();
  193.             device.Dispose();
  194.             context.Dispose();
  195.             swapChain.Dispose();
  196.             factory.Dispose();
  197.         }
  198.  
  199.         static Texture2D LoadTextures(Bitmap bitmap)
  200.         {
  201.             if (bitmap.PixelFormat != GDI.PixelFormat.Format32bppArgb)
  202.             {
  203.                 bitmap = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), GDI.PixelFormat.Format32bppArgb);
  204.             }
  205.             var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), GDI.ImageLockMode.ReadOnly, GDI.PixelFormat.Format32bppArgb);
  206.             var ret = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
  207.             {
  208.                 Width = bitmap.Width,
  209.                 Height = bitmap.Height,
  210.                 ArraySize = 1,
  211.                 BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
  212.                 Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
  213.                 CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
  214.                 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
  215.                 MipLevels = 1,
  216.                 OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
  217.                 SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
  218.             }, new SharpDX.DataRectangle(data.Scan0, data.Stride));
  219.             bitmap.UnlockBits(data);
  220.             bitmap.Dispose();
  221.  
  222.             return ret;
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement