Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- // -----------------------------------------------------------------------------
- // Original code from SlimDX project.
- // Greetings to SlimDX Group. Original code published with the following license:
- // -----------------------------------------------------------------------------
- /*
- * Copyright (c) 2007-2011 SlimDX Group
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- using System;
- using SharpDX;
- using SharpDX.D3DCompiler;
- using SharpDX.Direct3D;
- using SharpDX.Direct3D11;
- using SharpDX.DXGI;
- using SharpDX.Windows;
- using Buffer = SharpDX.Direct3D11.Buffer;
- using Device = SharpDX.Direct3D11.Device;
- using System.Drawing;
- using GDI = System.Drawing.Imaging;
- using System.Diagnostics;
- namespace MiniTri
- {
- /// <summary>
- /// SharpDX port of SharpDX-MiniTri Direct3D 11 Sample
- /// </summary>
- internal static class Program
- {
- static Device device;
- [STAThread]
- private static void Main()
- {
- var form = new RenderForm("SharpDX - MiniTri Direct3D 11 Sample");
- // SwapChain description
- var desc = new SwapChainDescription()
- {
- BufferCount = 1,
- ModeDescription=
- new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
- new Rational(60, 1), Format.R8G8B8A8_UNorm),
- IsWindowed = true,
- OutputHandle = form.Handle,
- SampleDescription = new SampleDescription(1, 0),
- SwapEffect = SwapEffect.Discard,
- Usage = Usage.RenderTargetOutput
- };
- // Create Device and SwapChain
- ShaderResourceView textureView;
- SamplerState sampler;
- SwapChain swapChain;
- Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);
- var context = device.ImmediateContext;
- Bitmap image = new Bitmap("C:/Users/Joe/Pictures/testbitmap.jpg");
- // Ignore all windows events
- var factory = swapChain.GetParent<Factory>();
- factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
- // New RenderTargetView from the backbuffer
- var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
- var renderView = new RenderTargetView(device, backBuffer);
- // Compile Vertex and Pixel shaders
- var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None);
- var vertexShader = new VertexShader(device, vertexShaderByteCode);
- var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniTri.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None);
- var pixelShader = new PixelShader(device, pixelShaderByteCode);
- // Layout from VertexShader input signature
- var layout = new InputLayout(
- device,
- ShaderSignature.GetInputSignature(vertexShaderByteCode),
- new[]
- {
- new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
- new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0)
- });
- // Instantiate Vertex buiffer from vertex data
- var vertices = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, new[]
- {
- // 3D coordinates UV Texture coordinates
- -0.0f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
- });
- var constantBuffer = new SharpDX.Direct3D11.Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
- // Load texture and create sampler
- var texture2D = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
- {
- Width = 512,
- Height = 512,
- ArraySize = 1,
- BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | BindFlags.RenderTarget,
- Usage = SharpDX.Direct3D11.ResourceUsage.Default,
- CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
- Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
- MipLevels = 1,
- OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
- SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
- });
- texture2D = LoadTextures(image);
- textureView = new ShaderResourceView(device, texture2D);
- sampler = new SamplerState(device, new SamplerStateDescription()
- {
- Filter = Filter.MinMagMipLinear,
- AddressU = TextureAddressMode.Clamp,
- AddressV = TextureAddressMode.Clamp,
- AddressW = TextureAddressMode.Clamp,
- BorderColor = SharpDX.Color.Black,
- ComparisonFunction = Comparison.Never,
- MaximumAnisotropy = 16,
- MipLodBias = 0,
- MinimumLod = -float.MaxValue,
- MaximumLod = float.MaxValue
- });
- // Main loop
- RenderLoop.Run(form, () =>
- {
- // Prepare All the stages
- context.ClearRenderTargetView(renderView, SharpDX.Color.Black);
- context.InputAssembler.InputLayout = layout;
- context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
- context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, sizeof(float) * 6, 0));
- context.VertexShader.Set(vertexShader);
- context.Rasterizer.SetViewport(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
- context.PixelShader.SetShaderResource(0, textureView);
- context.PixelShader.SetSampler(0, sampler);
- context.PixelShader.Set(pixelShader);
- context.Draw(3, 0);
- swapChain.Present(0, PresentFlags.None);
- });
- // Release all resources
- vertexShaderByteCode.Dispose();
- vertexShader.Dispose();
- pixelShaderByteCode.Dispose();
- pixelShader.Dispose();
- vertices.Dispose();
- layout.Dispose();
- renderView.Dispose();
- backBuffer.Dispose();
- context.ClearState();
- context.Flush();
- device.Dispose();
- context.Dispose();
- swapChain.Dispose();
- factory.Dispose();
- }
- static Texture2D LoadTextures(Bitmap bitmap)
- {
- if (bitmap.PixelFormat != GDI.PixelFormat.Format32bppArgb)
- {
- bitmap = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), GDI.PixelFormat.Format32bppArgb);
- }
- var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), GDI.ImageLockMode.ReadOnly, GDI.PixelFormat.Format32bppArgb);
- var ret = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
- {
- Width = bitmap.Width,
- Height = bitmap.Height,
- ArraySize = 1,
- BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
- Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
- CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
- Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
- MipLevels = 1,
- OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
- SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
- }, new SharpDX.DataRectangle(data.Scan0, data.Stride));
- bitmap.UnlockBits(data);
- bitmap.Dispose();
- return ret;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement