/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (!IsDisposed) { //Dispose of managed resources if (disposing) { if (_shaderResourceView != null) { if (_renderer != null) { _renderer.Resources.RemoveTrackedObject(_shaderResourceView.ComPointer); } _shaderResourceView.Dispose(); _shaderResourceView = null; } if (_texture3D != null) { if (_renderer != null) { _renderer.Resources.RemoveTrackedObject(_texture3D.ComPointer); } _texture3D.Dispose(); _texture3D = null; } if (_staging != null) { if (_renderer != null) { _renderer.Resources.RemoveTrackedObject(_staging.ComPointer); } _staging.Dispose(); _staging = null; } } base.Dispose(disposing); } }
/// <summary> /// Creates a new instance of <see cref="D3D10Texture3DImplementation"/>. /// </summary> /// <param name="renderer">The D3D0 renderer.</param> /// <param name="width">The width of the texture.</param> /// <param name="height">The height of the texture.</param> /// <param name="depth">The depth of the texture.</param> /// <param name="genMipMaps">True if mip map subresources should be generated or not.</param> /// <param name="format">The surface format.</param> /// <param name="data">The initial data for the first mip map.</param> internal D3D10Texture3DImplementation(D3D10Renderer renderer, int width, int height, int depth, bool genMipMaps, SurfaceFormat format, DataBuffer data) : base(width, height, depth, format) { //Set common properties _renderer = renderer; _graphicsDevice = _renderer.GraphicsDevice; //Do we want to generate mip maps, and do we have the data to do so? (if not, cancel mip map generation) bool canGenMipmaps = (genMipMaps && (data != null)); D3D.Texture3DDescription descTex = new D3D.Texture3DDescription(); descTex.Width = width; descTex.Height = height; descTex.Depth = depth; descTex.Usage = _usage = D3D.ResourceUsage.Default; descTex.CpuAccessFlags = D3D.CpuAccessFlags.None; descTex.Format = D3D10Helper.ToD3DSurfaceFormat(format); descTex.MipLevels = (genMipMaps) ? 0 : 1; //Set mip map generation params if (canGenMipmaps) { descTex.BindFlags = D3D.BindFlags.ShaderResource | D3D.BindFlags.RenderTarget; descTex.OptionFlags = D3D.ResourceOptionFlags.GenerateMipMaps; } else { descTex.BindFlags = D3D.BindFlags.ShaderResource; descTex.OptionFlags = D3D.ResourceOptionFlags.None; } //Create the texture and shader view _texture3D = new D3D.Texture3D(_graphicsDevice, descTex); _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture3D); //Add to tracker _renderer.Resources.AddTrackedObject(_texture3D.ComPointer, this); _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this); //Set the final mip count _mipCount = _texture3D.Description.MipLevels; //Now set the initial data if its present if (data != null) { try { this.SetData <byte>(data.ByteDataCopy, 0, 0, width, 0, height, 0, depth, 0, data.ElementSizeInBytes * data.Length); if (genMipMaps) { _graphicsDevice.GenerateMips(_shaderResourceView); } //Dispose of the staging texture if (_staging != null) { _staging.Dispose(); _staging = null; } } catch (Exception e) { Dispose(); throw new TeslaException("Error setting Texture data.", e); } } }