/// <summary> /// WIC <see cref="SharpDX.WIC.BitmapSource"/> から <see cref="SharpDX.Direct3D11.Texture2D"/> を作成する /// </summary> /// <param name="app"></param> /// <param name="bitmapSource">The WIC bitmap source</param> /// <param name="name">任意の名前</param> /// <returns>A Texture2D</returns> protected static MCTexture CreateTexture2DFromBitmap(Application app, SharpDX.WIC.BitmapSource bitmapSource, string name) { // WICイメージピクセルを受け取るためにDataStreamを割り当てます。 int stride = bitmapSource.Size.Width * 4; MCTexture tx = new MCTexture(app, name); using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true)) { // WICの内容をバッファにコピーする bitmapSource.CopyPixels(stride, buffer); Texture123DDesc d = new Texture123DDesc(); d.D2 = new Texture2DDescription() { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, ArraySize = 1, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource, Usage = SharpDX.Direct3D11.ResourceUsage.Immutable, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, MipLevels = 1, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), }; if (tx.CreateTexture2D(0, d, new DataRectangle(buffer.DataPointer, stride)) == 1) { return(null); } } return(tx); }
/// <summary> /// テクスチャーを作成する /// </summary> /// <param name="app"></param> /// <param name="name"></param> /// <param name="desc"></param> /// <returns></returns> public static MCBaseTexture CreateTexture2D(Application app, string name, Texture2DDescription desc) { int hr = -1; bool isCreateDepth = false; MCTexture tx = new MCTexture(app, name); var d = new Texture123DDesc(); if (tx == null) { throw new CreatedException(); } if ((desc.BindFlags & BindFlags.DepthStencil) != 0) { desc.BindFlags &= ~BindFlags.DepthStencil; isCreateDepth = true; } d.D2 = desc; if (tx.CreateTexture2D(0, d) != 0) { return(null); } // 1.レンダーターゲット if ((desc.BindFlags & BindFlags.RenderTarget) != 0) { RenderTargetViewDescription renderD = new RenderTargetViewDescription(); renderD.Format = desc.Format; renderD.Dimension = RenderTargetViewDimension.Texture2D; renderD.Texture2D.MipSlice = 0; hr = tx.CreateRenderTargetView(0, renderD); if (hr == -1) { return(null); } } // 2.シェーダーターゲット if ((desc.BindFlags & BindFlags.ShaderResource) != 0) { ShaderResourceViewDescription shaderD = new ShaderResourceViewDescription(); shaderD.Format = desc.Format; shaderD.Dimension = ShaderResourceViewDimension.Texture2D; shaderD.Texture2D.MostDetailedMip = 0; shaderD.Texture2D.MipLevels = 1; hr = tx.CreateShaderResourceView(0, shaderD); if (hr == -1) { return(null); } } if (isCreateDepth) { hr = tx.CreateDepthTexture(); if (hr == -1) { return(null); } } if (!app.ImageMgr.RegisterTexture(name, tx)) { return(null); } return(tx); }