public static void ToBitmap(SharpDXInfo info) { var m_texture = Texture2D.FromSwapChain <Texture2D>(info.SwapChain, 0); var m_surface = m_texture.QueryInterface <Surface>(); D2D.RenderTarget m_renderTarget; using (D2D.Factory factory = new D2D.Factory()) { m_renderTarget = new D2D.RenderTarget( factory, m_surface, new D2D.RenderTargetProperties() { DpiX = 0.0f, // default dpi DpiY = 0.0f, // default dpi MinLevel = D2D.FeatureLevel.Level_DEFAULT, Type = D2D.RenderTargetType.Hardware, Usage = D2D.RenderTargetUsage.None, PixelFormat = new D2D.PixelFormat( Format.Unknown, D2D.AlphaMode.Premultiplied ) } ); } var m_bitmap = new D2D.Bitmap(m_renderTarget, m_surface); }
public static void UpdateIndexBuffer(SharpDXInfo info, IEnumerable <int> rawIndices) { if (rawIndices == null) { return; } // 頂点数が既存のデータと違ってたらインデックスバッファを作り直す if (rawIndices.Count() != info.rawIndices.Length) { if (!indexBufferPool.ContainsKey(rawIndices.Count())) { indexBufferPool[rawIndices.Count()] = CreateBuffer <int>(info.Device, BindFlags.IndexBuffer, rawIndices.Count()); } Buffer buffer = indexBufferPool[rawIndices.Count()]; info.UpdateBuffers(indexBuffer: buffer); var binding = new VertexBufferBinding(info.IndexBuffer, Utilities.SizeOf <int>(), 0); info.Device.ImmediateContext.InputAssembler.SetIndexBuffer(buffer, Format.R32_UInt, 0); info.rawIndices = rawIndices.ToArray(); } // インデックスバッファの値を更新 var box = info.Device.ImmediateContext.MapSubresource(info.IndexBuffer, 0, MapMode.WriteDiscard, MapFlags.None); for (int i = 0; i < rawIndices.Count(); i++) { System.Runtime.InteropServices.Marshal.StructureToPtr(rawIndices.ElementAt(i), box.DataPointer + Utilities.SizeOf <int>() * i, false); info.rawIndices[i] = rawIndices.ElementAt(i); } info.Device.ImmediateContext.UnmapSubresource(info.IndexBuffer, 0); }
//---------------------------------------------------------------------------- public static Texture2D LoadTexture(SharpDXInfo info, string texturePath) { if (!System.IO.File.Exists(texturePath)) { return(null); } var texture = Texture2D.FromFile <Texture2D>(info.Device, texturePath); return(texture); }
static Texture2D BitmapToTexture(SharpDXInfo info, System.Drawing.Bitmap bmp) { if (bmp == null) { return(null); } const string tmpFile = "_tmp.png"; bmp.Save(tmpFile); var texture = Texture2D.FromFile <Texture2D>(info.Device, tmpFile); return(texture); }
/// <summary> /// メッシュの描画に使うテクスチャを設定する。 /// 新しいテクスチャが指定されたら、現在のテクスチャリソースを破棄して作りなおす /// 従って、テクスチャの切り替えが少ないほど描画が高速になる /// </summary> public static void SwitchTexture(SharpDXInfo info, Texture2D texture) { if (texture == info.Texture) { return; } info.TextureView.Dispose(); ShaderResourceView textureView = null; if (texture != null) { textureView = new ShaderResourceView(info.Device, texture); } info.UpdateTexture(texture, textureView); info.Device.ImmediateContext.PixelShader.SetShaderResource(0, info.TextureView); }
public static SharpDXInfo Initialize(IntPtr handle, System.Drawing.Size size, IEnumerable <VertexPositionColorTexture> rawVertices, IEnumerable <int> rawIndices, Matrix viewWorldProj, string texturePath) { var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(size.Width, size.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; // Create Device and SwapChain Device device; SwapChain swapChain; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); var context = device.ImmediateContext; // Ignore all windows events var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(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 using (var vertexShaderByteCode = ShaderBytecode.CompileFromFile(DefaultShaderPath, "VS", "vs_4_0")) using (var pixelShaderByteCode = ShaderBytecode.CompileFromFile(DefaultShaderPath, "PS", "ps_4_0")) { var vertexShader = new VertexShader(device, vertexShaderByteCode); 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("COLOR", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0), }); // Instantiate buffers var vertexBuffer = CreateBuffer <VertexPositionColorTexture>(device, BindFlags.VertexBuffer, rawVertices.Count()); var indexBuffer = CreateBuffer <int>(device, BindFlags.IndexBuffer, rawIndices.Count()); var cameraBuffer = CreateConstantBuffer <Matrix>(device); var depthBuffer = new Texture2D(device, new Texture2DDescription() { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = size.Width, Height = size.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); var depthView = new DepthStencilView(device, depthBuffer); // Load texture and create sampler var texture = Texture2D.FromFile <Texture2D>(device, texturePath); var textureView = new ShaderResourceView(device, texture); var sampler = new SamplerState(device, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = 0, MaximumLod = 16, }); // initialize sharpdxlib context var info = new SharpDXInfo( device, swapChain, handle, size, depthView, renderView, vertexBuffer, indexBuffer, cameraBuffer, depthBuffer, rawVertices, rawIndices, texture, textureView, vertexShader, pixelShader, layout, backBuffer, factory); // Prepare All the stages context.InputAssembler.InputLayout = layout; context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <VertexPositionColorTexture>(), 0)); context.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0); context.VertexShader.SetConstantBuffer(0, cameraBuffer); context.VertexShader.Set(vertexShader); RasterizerStateDescription rasdesc = new RasterizerStateDescription() { CullMode = CullMode.None, FillMode = FillMode.Solid, IsFrontCounterClockwise = true, DepthBias = 0, DepthBiasClamp = 0, SlopeScaledDepthBias = 0, IsDepthClipEnabled = true, IsMultisampleEnabled = true, }; context.Rasterizer.State = new RasterizerState(device, rasdesc); context.Rasterizer.SetViewport(new Viewport(0, 0, size.Width, size.Height, 0.0f, 1)); context.PixelShader.Set(pixelShader); context.PixelShader.SetSampler(0, sampler); context.PixelShader.SetShaderResource(0, textureView); context.OutputMerger.SetTargets(depthView, renderView); UpdateVertexBuffer(info, rawVertices); UpdateIndexBuffer(info, rawIndices); UpdateCameraBuffer(info, viewWorldProj); return(info); } }
public static void SetDefaultRenderInfo(SharpDXInfo info) { defaultInfo = info; }
public static void UpdateCameraBuffer(SharpDXInfo info, Matrix worldViewProj) { info.Device.ImmediateContext.UpdateSubresource(ref worldViewProj, info.CameraBuffer); }
public static void EndDraw(SharpDXInfo info) { info.SwapChain.Present(0, PresentFlags.None); }
public static void Draw(SharpDXInfo info, PrimitiveTopology primitiveType) { info.Device.ImmediateContext.InputAssembler.PrimitiveTopology = primitiveType; info.Device.ImmediateContext.DrawIndexed(info.rawIndices.Length, 0, 0); }
public static void ClearDepthStencilView(SharpDXInfo info) { info.Device.ImmediateContext.ClearDepthStencilView(info.DepthView, DepthStencilClearFlags.Depth, 1, 0); }
public static void BeginDraw(SharpDXInfo info) { info.Device.ImmediateContext.ClearDepthStencilView(info.DepthView, DepthStencilClearFlags.Depth, 1, 0); info.Device.ImmediateContext.ClearRenderTargetView(info.RenderView, Color.Black); }