private void CreateNormalTexture() { Debug.Assert( SrcWidth > 0 && SrcHeight > 0 ); // determine which D3D9 pixel format we'll use D3D.Format d3dPixelFormat = ChooseD3DFormat(); // set the appropriate usage based on the usage of this texture D3D.Usage d3dUsage = ( ( Usage & TextureUsage.RenderTarget ) == TextureUsage.RenderTarget ) ? D3D.Usage.RenderTarget : D3D.Usage.None; // how many mips to use? int numMips = RequestedMipmapCount + 1; D3D.TextureRequirements texRequire = new D3D.TextureRequirements(); texRequire.Width = SrcWidth; texRequire.Height = SrcHeight; // check texture requirements texRequire.MipLevelCount = numMips; texRequire.Format = d3dPixelFormat; //// NOTE: Although texRequire is an out parameter, it actually does //// use the data passed in with that object. texRequire = D3D.Texture.CheckRequirements( _device, SrcWidth, SrcHeight, numMips, d3dUsage, d3dPixelFormat, D3D.Pool.Default ); // Save updated texture requirements numMips = texRequire.MipLevelCount; d3dPixelFormat = texRequire.Format; if ( ( Usage & TextureUsage.Dynamic ) == TextureUsage.Dynamic ) { if ( CanUseDynamicTextures( d3dUsage, D3D.ResourceType.Texture, d3dPixelFormat ) ) { d3dUsage |= D3D.Usage.Dynamic; _dynamicTextures = true; } else { _dynamicTextures = false; } } // check if mip maps are supported on hardware MipmapsHardwareGenerated = false; if ( ( _devCaps.TextureCaps & D3D.TextureCaps.MipMap ) == D3D.TextureCaps.MipMap ) { if ( ( ( Usage & TextureUsage.AutoMipMap ) == TextureUsage.AutoMipMap ) && RequestedMipmapCount > 0 ) { MipmapsHardwareGenerated = this.CanAutoGenMipMaps( d3dUsage, D3D.ResourceType.Texture, d3dPixelFormat ); if ( MipmapsHardwareGenerated ) { d3dUsage |= D3D.Usage.AutoGenerateMipMap; numMips = 0; } } } else { // no mip map support for this kind of texture MipmapCount = 0; numMips = 1; } // create the texture _normTexture = new D3D.Texture( _device, SrcWidth, SrcHeight, numMips, d3dUsage, d3dPixelFormat, _d3dPool ); // store base reference to the texture _texture = _normTexture; // set the final texture attributes D3D.SurfaceDescription desc = _normTexture.GetLevelDescription( 0 ); SetFinalAttributes( desc.Width, desc.Height, 1, D3DHelper.ConvertEnum( desc.Format ) ); if ( MipmapsHardwareGenerated ) { _texture.AutoMipGenerationFilter = GetBestFilterMethod(); } }
private void LoadNormalTexture() { Debug.Assert( TextureType == TextureType.OneD || TextureType == TextureType.TwoD ); if ( Name.EndsWith( ".dds" ) ) { Stream stream = ResourceGroupManager.Instance.OpenResource( Name, Group, true, this ); int numMips = this.RequestedMipmapCount + 1; // check if mip map volume textures are supported if ( ( _devCaps.TextureCaps & D3D.TextureCaps.MipCubeMap ) != D3D.TextureCaps.MipCubeMap ) { // no mip map support for this kind of textures :( this.MipmapCount = 0; numMips = 1; } _d3dPool = ( Usage & TextureUsage.Dynamic ) != 0 ? D3D.Pool.Default : D3D.Pool.Managed; try { // load the cube texture from the image data stream directly this._normTexture = D3D.Texture.FromStream( _device, stream, (int)stream.Length, 0, 0, numMips, D3D.Usage.None, D3D.Format.Unknown, _d3dPool, D3D.Filter.None, D3D.Filter.None, 0 ); } catch ( Exception ex ) { FreeInternalResources(); throw new Exception( "Can't create texture.", ex ); } // store off a base reference _texture = _normTexture; // set src and dest attributes to the same, we can't know D3D.SurfaceDescription desc = _normTexture.GetLevelDescription( 0 ); _d3dPool = desc.Pool; SetSrcAttributes( desc.Width, desc.Height, 1, D3DHelper.ConvertEnum( desc.Format ) ); SetFinalAttributes( desc.Width, desc.Height, 1, D3DHelper.ConvertEnum( desc.Format ) ); internalResourcesCreated = true; } else { // find & load resource data intro stream to allow resource group changes if required Stream strm = ResourceGroupManager.Instance.OpenResource( Name, Group, true, this ); int pos = Name.LastIndexOf( "." ); String ext = Name.Substring( pos + 1 ); // Call internal LoadImages, not LoadImage since that's external and // will determine load status etc again var image = Image.FromStream( strm, ext ); LoadImages( new Image[] { image } ); image.Dispose(); strm.Close(); } }