/// <summary> /// 現状フォーマ等を維持しながら、幅、高さを作り直す /// </summary> /// <param name="width"></param> /// <param name="height"></param> public override void ReSize2D(int width, int height) { if (!Is2DTexture) { throw new Exception("2D テクスチャー ではありません。"); } Texture123DDesc d123 = new Texture123DDesc(); d123.D2 = m_descTx.D2; Utilities.Dispose(ref m_aTexture[0].d2); m_aResourceTx[0] = null; m_createdTxNum = 0; if (CreateTexture2D(0, d123) != 0) { throw new Exception(); } // 2.シェーダーターゲット if (HasShaderResource) { Utilities.Dispose(ref m_aTxResourceView[0]); if (CreateShaderResourceView(0) != 0) { throw new Exception(); } } }
/// <summary> /// テクスチャーを作成する /// </summary> /// <param name="name">一意になるテクスチャー名</param> /// <param name="w"></param> /// <param name="h"></param> /// <param name="tx"></param> /// <returns></returns> public static int Create(Application app, string name, int w, int h, out MCBaseTexture tx) { tx = null; MCPrintScreenTexture printTx = new MCPrintScreenTexture(app, name); Texture2DDescription desc; SimpleD2DescInit(w, h, out desc); Texture123DDesc d123 = new Texture123DDesc(); d123.D2 = desc; if (printTx.CreateTexture2D(0, d123) != 0) { return(-1); } if (printTx.CreateShaderResourceView(0) != 0) { return(-1); } if (!app.ImageMgr.RegisterTexture(name, tx)) { return(-1); } tx = printTx; return(0); }
/// <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> /// 3Dテクスチャーを作成する。ただし、idxは順番(連番)にセットすること /// </summary> /// <param name="device"></param> /// <param name="idx">0~7</param> /// <param name="desc"></param> /// <returns></returns> internal int CreateTexture3D(int idx, Texture123DDesc desc = null) { if (idx > 7 || idx != m_createdTxNum) { Debug.Assert(false); throw new ArgumentException(); } if (m_aResourceTx[idx] != null) { Debug.Assert(false); throw new CreatedException(); } m_sourceLocation = MC_IMG_SOURCELOCATION.DEFAULT; m_aTexture[idx].d3 = new Texture3D(App.DXDevice, desc.D3); m_aTexture[idx].d3.DebugName = "Texture3D:" + m_name; m_descTx.D3 = m_aTexture[idx].d3.Description; m_type = ResourceDimension.Texture3D;; m_aResourceTx[idx] = (SharpDX.Direct3D11.Resource)m_aTexture[idx].d3; ++m_createdTxNum; return(0); }
/// <summary> /// 深度テクスチャーを作成する。単体で使用する場合はdimensionとdescを入力し、 /// CreateTextureXX,SetSourcFileTexture,SetSourcMemoryTextureは、呼び出していないものとする。 /// 呼び出していた場合は、エラーが発生する /// </summary> /// <param name="device">デバイス</param> /// <param name="dimension">1D,2Dのみ(単体で作り場合のみ使用) 省略時、ResourceDimension.Unknown</param> /// <param name="desc">1D,2Dのみ(単体で作り場合のみ使用) 省略時、null</param> /// <param name="isCreateShaderResourceView">シェーダーリソースビューを作成するか? 省略時、true</param> /// <returns></returns> internal int CreateDepthTexture(ResourceDimension dimension = ResourceDimension.Unknown, Texture123DDesc desc = null, bool isCreateShaderResourceView = true) { if (m_resourceDepth != null) { Debug.Assert(false); throw new CreatedException(); } if (m_createdTxNum == 0 && (dimension != ResourceDimension.Unknown || desc == null)) { Debug.Assert(false); throw new ArgumentException(); } if (m_createdTxNum == 0 && m_type == ResourceDimension.Unknown) { m_type = dimension; } try { DepthStencilViewDescription descDSV = new DepthStencilViewDescription(); ShaderResourceViewDescription srvd = new ShaderResourceViewDescription(); switch (m_type) { case ResourceDimension.Texture1D: { Texture1DDescription descDepth = desc != null ? desc.D1 : m_descTx.D1; descDepth.BindFlags = BindFlags.DepthStencil | (isCreateShaderResourceView ? BindFlags.ShaderResource : 0); descDepth.Format = Format.R32_Typeless; m_depth.D1 = new Texture1D(App.DXDevice, descDepth); if (m_depth.D1 == null) { throw new CreateFailedException(); } m_depth.D1.DebugName = "Depth Texture1D" + m_name; m_descDepth.d1 = descDepth; m_resourceDepth = (SharpDX.Direct3D11.Resource)(m_depth.D1); descDSV.Dimension = DepthStencilViewDimension.Texture1D; srvd.Dimension = ShaderResourceViewDimension.Texture1D; srvd.Texture2D.MipLevels = descDepth.MipLevels; break; } case ResourceDimension.Texture2D: { Texture2DDescription descDepth = desc != null ? desc.D2 : m_descTx.D2; descDepth.BindFlags = BindFlags.DepthStencil | (isCreateShaderResourceView ? BindFlags.ShaderResource : 0); descDepth.Format = Format.R32_Typeless; m_depth.D2 = new Texture2D(App.DXDevice, descDepth); if (m_depth.D2 == null) { throw new CreateFailedException(); } m_depth.D2.DebugName = "Depth Texture2D" + m_name; m_descDepth.d2 = descDepth; m_resourceDepth = (SharpDX.Direct3D11.Resource)(m_depth.D2); descDSV.Dimension = DepthStencilViewDimension.Texture2D; srvd.Dimension = ShaderResourceViewDimension.Texture2D; srvd.Texture2D.MipLevels = descDepth.MipLevels; break; } default: throw new CreateFailedException(); } // 深度ステンシルビュー descDSV.Format = Format.D32_Float; descDSV.Flags = 0; descDSV.Texture2D.MipSlice = 0; m_depthStencilView = new DepthStencilView(App.DXDevice, m_resourceDepth, descDSV); if (m_depthStencilView == null) { throw new CreateFailedException(); } // リソースビュー if (isCreateShaderResourceView) { srvd.Format = Format.R32_Float; srvd.Texture2D.MostDetailedMip = 0; m_depthResourceView = new ShaderResourceView(App.DXDevice, m_resourceDepth, srvd); if (m_depthResourceView == null) { throw new CreateFailedException(); } } m_depthStencilView.DebugName = "DepthStencilView=>" + m_name; m_resourceDepth.DebugName = m_name; } catch { if (m_depthStencilView != null) { m_depthStencilView.Dispose(); } if (m_resourceDepth != null) { m_resourceDepth.Dispose(); } if (m_depth.D1 != null) { m_depth.D1.Dispose(); } if (m_depth.D2 != null) { m_depth.D2.Dispose(); } m_depthStencilView = null; m_resourceDepth = null; m_depth.D1 = null; m_depth.D2 = null; HasDepthStencil = false; return(-1); } HasDepthStencil = true; return(0); }
/// <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); }