private void GenerateTargetMips() { for (int i = 0; i < _targetsToMipMapCount; i++) { RenderTargetBinding binding = _targetsToMipMap[i]; //Gen cube mips if (binding.IsRenderTargetCube) { RenderTargetCube rcube = binding.RenderTargetCube; D3D10Helper.CheckDisposed(rcube); if (rcube.MipCount > 1) { D3D10TextureCubeImplementation impl = rcube.Implementation as D3D10TextureCubeImplementation; _graphicsDevice.GenerateMips(impl.D3D10ShaderResourceView); } //Gen 2D tex mips } else { RenderTarget2D r2d = binding.RenderTarget2D; D3D10Helper.CheckDisposed(r2d); if (r2d.MipCount > 1) { D3D10Texture2DImplementation impl = r2d.Implementation as D3D10Texture2DImplementation; _graphicsDevice.GenerateMips(impl.D3D10ShaderResourceView); } } } }
/// <summary> /// Creates a new instance of <see cref="D3D10Texture2DImplementation"/>, for Texture2D. /// </summary> /// <param name="renderer">The D3D10 renderer.</param> /// <param name="width">The texture width in pixels.</param> /// <param name="height">The texture height in pixels.</param> /// <param name="genMipMaps">True if a mipmap chain should be generated or not.</param> /// <param name="format">The surface format.</param> /// <param name="data">The data to initialize the first mip map level.</param> /// <exception cref="Tesla.Core.TeslaException">Thrown if there was an error writing to the texture.</exception> internal D3D10Texture2DImplementation(D3D10Renderer renderer, int width, int height, bool genMipMaps, SurfaceFormat format, DataBuffer data) : base(width, height, 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)); //Setup texture description D3D.Texture2DDescription descTex = new D3D.Texture2DDescription(); descTex.ArraySize = 1; descTex.Width = width; descTex.Height = height; descTex.Usage = _usage = D3D.ResourceUsage.Default; descTex.CpuAccessFlags = D3D.CpuAccessFlags.None; descTex.Format = D3D10Helper.ToD3DSurfaceFormat(format); descTex.SampleDescription = new DXGI.SampleDescription(1, 0); //Default sample desc 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 _texture2D = new D3D.Texture2D(_graphicsDevice, descTex); _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D); //Add to tracker _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this); _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this); //Set the final mip count _mipCount = _texture2D.Description.MipLevels; //Now set the initial data if its present if (data != null) { try { this.SetData <byte>(data.ByteDataCopy, 0, null, 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); } } }
/// <summary> /// Creates a new instance of <see cref="D3D10TextureCubeImplementation"/>. /// </summary> /// <param name="renderer">D3D10 renderer</param> /// <param name="size">The size (width/height) of each cube face.</param> /// <param name="genMipMaps">True if mip levels should be generated.</param> /// <param name="format">The surface format.</param> /// <param name="data">The initial data for the first mip level.</param> /// <exception cref="System.ArgumentException">Thrown if the data buffery array does not correspond to a valid cube map (6 faces, all the same size)</exception> /// <exception cref="Tesla.Core.TeslaException">Thrown if there was an error writing to the texture.</exception> internal D3D10TextureCubeImplementation(D3D10Renderer renderer, int size, bool genMipMaps, SurfaceFormat format, DataBuffer[] data) : base(size, format) { //Set the 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)); //Setup texture description D3D.Texture2DDescription descTex = new D3D.Texture2DDescription(); descTex.ArraySize = 6; descTex.Width = size; descTex.Height = size; descTex.Usage = _usage = D3D.ResourceUsage.Default; descTex.CpuAccessFlags = D3D.CpuAccessFlags.None; descTex.Format = D3D10Helper.ToD3DSurfaceFormat(format); descTex.SampleDescription = new DXGI.SampleDescription(1, 0); 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 | D3D.ResourceOptionFlags.TextureCube; } else { descTex.BindFlags = D3D.BindFlags.ShaderResource; descTex.OptionFlags = D3D.ResourceOptionFlags.TextureCube; } //Create the texture and shader view _texture2D = new D3D.Texture2D(_graphicsDevice, descTex); _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D); //Add to tracker _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this); _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this); //Set the final mip count _mipCount = _texture2D.Description.MipLevels; //Now set the initial data if its present if (data != null) { if (data.Length != 6) { Dispose(); throw new ArgumentException("Initial data array must have six data buffers, one for each cube face."); } int num = data[0].Length; for (int i = 1; i < 6; i++) { if (data[i].Length != num) { Dispose(); throw new ArgumentException("All data buffers must be of same length"); } } try { for (int i = 0; i < 6; i++) { DataBuffer db = data[i]; this.SetData <byte>((CubeMapFace)i, db.ByteDataCopy, 0, null, 0, db.ElementSizeInBytes * db.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); } } }