Esempio n. 1
0
		///<summary>
		///    Copies a region of this pixelbuffer to normal memory.
		///</summary>
		///<param name="srcBox">BasicBox describing the source region of this buffer</param>
		///<param name="dst">PixelBox describing the destination pixels and format in memory</param>
		///<remarks>
		///    The source and destination regions don't have to match, in which
		///    case scaling is done.
		///    Only call this function when the buffer is unlocked.
		///</remarks>
		public override void BlitToMemory( BasicBox srcBox, PixelBox dst )
		{
			// Decide on pixel format of temp surface
			PixelFormat tmpFormat = Format;
			if ( D3DHelper.ConvertEnum( dst.Format ) == D3D.Format.Unknown )
				tmpFormat = dst.Format;
			if ( surface != null )
			{
				Debug.Assert( srcBox.Depth == 1 && dst.Depth == 1 );
				// Create temp texture
				D3D.Texture tmp =
					new D3D.Texture( device, dst.Width, dst.Height,
									1, // 1 mip level ie topmost, generate no mipmaps
									0, D3DHelper.ConvertEnum( tmpFormat ),
									D3D.Pool.Scratch );
				D3D.Surface subSurface = tmp.GetSurfaceLevel( 0 );
				// Copy texture to this temp surface
				System.Drawing.Rectangle destRect, srcRect;
				srcRect = ToD3DRectangle( srcBox );
				destRect = ToD3DRectangleExtent( dst );

				D3D.Surface.FromSurface( subSurface, surface, D3D.Filter.None, 0, srcRect, destRect );

				// Lock temp surface and copy it to memory
				int pitch; // Filled in by D3D
				DX.DataRectangle data = subSurface.LockRectangle( D3D.LockFlags.ReadOnly );
				// Copy it
				PixelBox locked = new PixelBox( dst.Width, dst.Height, dst.Depth, tmpFormat );
				FromD3DLock( locked, data );
				PixelConverter.BulkPixelConversion( locked, dst );
				subSurface.UnlockRectangle();
				// Release temporary surface and texture
				subSurface.Dispose();
				tmp.Dispose();
			}
			else
			{
				// Create temp texture
				D3D.VolumeTexture tmp =
					new D3D.VolumeTexture( device, dst.Width, dst.Height, dst.Depth,
										   0, D3D.Usage.None,
										   D3DHelper.ConvertEnum( tmpFormat ),
										   D3D.Pool.Scratch );
				D3D.Volume subVolume = tmp.GetVolumeLevel( 0 );
				// Volume
				D3D.Box ddestBox = ToD3DBoxExtent( dst );
				D3D.Box dsrcBox = ToD3DBox( srcBox );

				D3D.Volume.FromVolume( subVolume, volume, D3D.Filter.None, 0, dsrcBox, ddestBox );
				// Lock temp surface and copy it to memory
				//D3D.LockedBox lbox; // Filled in by D3D
				DX.DataBox data = subVolume.LockBox( D3D.LockFlags.ReadOnly );

				// Copy it
				PixelBox locked = new PixelBox( dst.Width, dst.Height, dst.Depth, tmpFormat );
				FromD3DLock( locked, data );
				PixelConverter.BulkPixelConversion( locked, dst );
				subVolume.UnlockBox();
				// Release temporary surface and texture
				subVolume.Dispose();
				tmp.Dispose();
			}
		}
Esempio n. 2
0
		public override void Unload()
		{
			base.Unload();

			if ( IsLoaded )
			{
				if ( this._texture != null )
				{
					this._texture.Dispose();
					this._texture = null;
				}

				if ( this._normTexture != null )
				{
					LogManager.Instance.Write( "Disposed normal texture {0}", this.Name );
					this._normTexture.Dispose();
					this._normTexture = null;
				}

				if ( this._cubeTexture != null )
				{
					this._cubeTexture.Dispose();
					this._cubeTexture = null;
				}

				if ( this._volumeTexture != null )
				{
					this._volumeTexture.Dispose();
					this._volumeTexture = null;
				}
			}
		}
Esempio n. 3
0
		private void LoadVolumeTexture()
		{
			Debug.Assert( this.TextureType == TextureType.ThreeD );

			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.MipVolumeMap ) != D3D.TextureCaps.MipVolumeMap )
				{
					// 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
					_volumeTexture = D3D.VolumeTexture.FromStream( _device, stream, (int)stream.Length, -1, -1, -1, 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 volume texture.", ex );
				}

				// store off a base reference
				_texture = _volumeTexture;

				// set src and dest attributes to the same, we can't know
				D3D.VolumeDescription desc = _volumeTexture.GetLevelDescription( 0 );
				_d3dPool = desc.Pool;

				SetSrcAttributes( desc.Width, desc.Height, desc.Depth, D3DHelper.ConvertEnum( desc.Format ) );
				SetFinalAttributes( desc.Width, desc.Height, desc.Depth, D3DHelper.ConvertEnum( desc.Format ) );

				internalResourcesCreated = true;

				stream.Close();
			}
			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();
			}
		}
Esempio n. 4
0
		private void CreateVolumeTexture()
		{
			Debug.Assert( Width > 0 && Height > 0 && Depth > 0 );

			if ( ( Usage & TextureUsage.RenderTarget ) != 0 )
			{
				throw new Exception( "SDX Volume texture can not be declared as render target!!, SDXTexture.CreateVolumeTexture" );
			}

			D3D.Format sdPF = ChooseD3DFormat();
			D3D.Usage usage = 0;

			int numMips = ( RequestedMipmapCount == 0x7FFFFFFF ) ? -1 : RequestedMipmapCount + 1;
			if ( ( Usage & TextureUsage.Dynamic ) != 0 )
			{
				if ( CanUseDynamicTextures( usage, D3D.ResourceType.VolumeTexture, sdPF ) )
				{
					usage |= D3D.Usage.Dynamic;
					_dynamicTextures = true;
				}
				else
				{
					_dynamicTextures = false;
				}
			}

			// check if mip map volume textures are supported
			MipmapsHardwareGenerated = false;
			if ( ( _devCaps.TextureCaps & D3D.TextureCaps.MipVolumeMap ) != 0 )
			{
				if ( ( Usage & TextureUsage.AutoMipMap ) != 0 && RequestedMipmapCount != 0 )
				{
					MipmapsHardwareGenerated = CanAutoGenMipMaps( usage, D3D.ResourceType.VolumeTexture, sdPF );
					if ( MipmapsHardwareGenerated )
					{
						Usage |= TextureUsage.AutoMipMap;
						numMips = 0;
					}
				}
			}
			else
			{
				// no mip map support for this kind of textures :(
				MipmapCount = 0;
				numMips = 1;
			}

			// derive the pool to use
			DeterminePool();

			// create the texture
			_volumeTexture = new D3D.VolumeTexture( _device,
													Width,
													Height,
													Depth,
													numMips,
													usage,
													sdPF,
													_d3dPool );

			// store base reference to the texture
			_texture = _volumeTexture;

			// set the final texture attributes
			D3D.VolumeDescription desc = _volumeTexture.GetLevelDescription( 0 );
			SetFinalAttributes( desc.Width, desc.Height, desc.Depth, D3DHelper.ConvertEnum( desc.Format ) );

			if ( this.MipmapsHardwareGenerated )
				_texture.AutoMipGenerationFilter = GetBestFilterMethod();
		}
Esempio n. 5
0
		protected override void freeInternalResources()
		{
			if ( this._texture != null && !_texture.Disposed )
			{
				this._texture.Dispose();
				this._texture = null;
			}

			if ( this._normTexture != null && !_normTexture.Disposed )
			{
				this._normTexture.Dispose();
				this._normTexture = null;
			}

			if ( this._cubeTexture != null && !_cubeTexture.Disposed )
			{
				this._cubeTexture.Dispose();
				this._cubeTexture = null;
			}

			if ( this._volumeTexture != null && !_volumeTexture.Disposed )
			{
				this._volumeTexture.Dispose();
				this._volumeTexture = null;
			}
		}