public void CreateBuffer(D3D9.Device d3d9Device, D3D9.Pool ePool) { // Find the vertex buffer of this device. BufferResources bufferResources; if (this._mapDeviceToBufferResources.TryGetValue(d3d9Device, out bufferResources)) { bufferResources.VertexBuffer.SafeDispose(); } else { bufferResources = new BufferResources(); this._mapDeviceToBufferResources.Add(d3d9Device, bufferResources); } bufferResources.VertexBuffer = null; bufferResources.IsOutOfDate = true; bufferResources.LockOffset = 0; bufferResources.LockLength = sizeInBytes; bufferResources.LockOptions = BufferLocking.Normal; bufferResources.LastUsedFrame = Root.Instance.NextFrameNumber; // Create the vertex buffer try { bufferResources.VertexBuffer = new D3D9.VertexBuffer(d3d9Device, sizeInBytes, D3D9Helper.ConvertEnum(usage), 0, ePool); } catch (Exception ex) { throw new AxiomException("Cannot restore D3D9 vertex buffer", ex); } this._bufferDesc = bufferResources.VertexBuffer.Description; }
private void _loadNormalTexture( D3D9.Device d3d9Device, MemoryStream[] loadedStreams ) { Debug.Assert( TextureType == TextureType.OneD || TextureType == TextureType.TwoD ); // DDS load? if ( GetSourceFileType() == "dds" ) { // Use D3DX Debug.Assert( loadedStreams.Length == 1 ); var d3dUsage = D3D9.Usage.None; var numMips = 0; if ( requestedMipmapCount == (int)TextureMipmap.Unlimited ) { numMips = -1; } else if ( requestedMipmapCount == 0 ) { numMips = -3; } else { numMips = requestedMipmapCount + 1; } var device = D3D9RenderSystem.DeviceManager.GetDeviceFromD3D9Device( d3d9Device ); var rkCurCaps = device.D3D9DeviceCaps; // check if mip map volume textures are supported if ( ( rkCurCaps.TextureCaps & D3D9.TextureCaps.MipCubeMap ) != D3D9.TextureCaps.MipCubeMap ) { // no mip map support for this kind of textures :( MipmapCount = 0; numMips = 1; } // Determine D3D pool to use var pool = UseDefaultPool() ? D3D9.Pool.Default : D3D9.Pool.Managed; // Get or create new texture resources structure. var textureResources = _getTextureResources( d3d9Device ); if ( textureResources != null ) { _freeTextureResources( d3d9Device, textureResources ); } else { textureResources = _allocateTextureResources( d3d9Device ); } try { textureResources.NormalTexture = D3D9.Texture.FromMemory( d3d9Device, loadedStreams[ 0 ].GetBuffer(), -1, -1, // dims numMips, d3dUsage, D3D9.Format.Unknown, pool, D3D9.Filter.Default, D3D9.Filter.Default, 0 // colour key ); } catch ( Exception ex ) { // romeoxbm: this statement is not present in Ogre implementation, // but maybe it should be.. FreeInternalResources(); throw new AxiomException( "Can't create texture.", ex ); } textureResources.BaseTexture = textureResources.NormalTexture.QueryInterface<D3D9.BaseTexture>(); // set src and dest attributes to the same, we can't know var texDesc = textureResources.NormalTexture.GetLevelDescription( 0 ); this._d3dPool = texDesc.Pool; // set src and dest attributes to the same, we can't know _setSrcAttributes( texDesc.Width, texDesc.Height, 1, D3D9Helper.ConvertEnum( texDesc.Format ) ); _setFinalAttributes( d3d9Device, textureResources, texDesc.Width, texDesc.Height, 1, D3D9Helper.ConvertEnum( texDesc.Format ) ); if ( hwGamma ) { this._hwGammaReadSupported = _canUseHardwareGammaCorrection( d3d9Device, texDesc.Usage, D3D9.ResourceType.Texture, texDesc.Format, false ); } internalResourcesCreated = true; } else { // find & load resource data intro stream to allow resource group changes if required Debug.Assert( loadedStreams.Length == 1 ); var pos = _name.LastIndexOf( "." ); var ext = string.Empty; if ( pos != -1 ) { ext = _name.Substring( pos + 1 ); } var img = Image.FromStream( loadedStreams[ 0 ], ext ); if ( img.Height == 0 ) { throw new AxiomException( "Image height == 0 in {0}", _name ); } if ( img.Width == 0 ) { throw new AxiomException( "Image width == 0 in {0}", _name ); } // Call internal _loadImages, not loadImage since that's external and // will determine load status etc again LoadImages( new Image[] { img } ); } }
protected override void load() { if ( !internalResourcesCreated ) { // NB: Need to initialise pool to some value other than D3DPOOL_DEFAULT, // otherwise, if the texture loading failed, it might re-create as empty // texture when device lost/restore. The actual pool will be determined later. this._d3dPool = D3D9.Pool.Managed; } //Entering critical section this.LockDeviceAccess(); foreach ( var d3d9Device in D3D9RenderSystem.ResourceCreationDevices ) { _load( d3d9Device ); } //Leaving critical section this.UnlockDeviceAccess(); }
private void _loadCubeTexture( D3D9.Device d3d9Device, MemoryStream[] loadedStreams ) { Debug.Assert( TextureType == TextureType.CubeMap, "this.TextureType == TextureType.CubeMap" ); if ( GetSourceFileType() == "dds" ) { // find & load resource data Debug.Assert( this._loadedStreams.Length == 1 ); var d3dUsage = D3D9.Usage.None; var numMips = requestedMipmapCount == (int)TextureMipmap.Unlimited ? -1 : requestedMipmapCount + 1; var device = D3D9RenderSystem.DeviceManager.GetDeviceFromD3D9Device( d3d9Device ); var rkCurCaps = device.D3D9DeviceCaps; // check if mip map volume textures are supported if ( ( rkCurCaps.TextureCaps & D3D9.TextureCaps.MipCubeMap ) != D3D9.TextureCaps.MipCubeMap ) { // no mip map support for this kind of textures :( MipmapCount = 0; numMips = 1; } // Determine D3D pool to use var pool = UseDefaultPool() ? D3D9.Pool.Default : D3D9.Pool.Managed; // Get or create new texture resources structure. var textureResources = _getTextureResources( d3d9Device ); if ( textureResources != null ) { _freeTextureResources( d3d9Device, textureResources ); } else { textureResources = _allocateTextureResources( d3d9Device ); } try { textureResources.CubeTexture = D3D9.CubeTexture.FromMemory( d3d9Device, loadedStreams[ 0 ].GetBuffer(), (int)loadedStreams[ 0 ].Length, numMips, d3dUsage, D3D9.Format.Unknown, pool, D3D9.Filter.Default, D3D9.Filter.Default, 0 // colour Key ); } catch ( Exception ex ) { freeInternalResources(); throw new AxiomException( "Can't create cube texture.", ex ); } textureResources.BaseTexture = textureResources.CubeTexture.QueryInterface<D3D9.BaseTexture>(); var texDesc = textureResources.CubeTexture.GetLevelDescription( 0 ); this._d3dPool = texDesc.Pool; // set src and dest attributes to the same, we can't know _setSrcAttributes( texDesc.Width, texDesc.Height, 1, D3D9Helper.ConvertEnum( texDesc.Format ) ); _setFinalAttributes( d3d9Device, textureResources, texDesc.Width, texDesc.Height, 1, D3D9Helper.ConvertEnum( texDesc.Format ) ); if ( hwGamma ) { this._hwGammaReadSupported = _canUseHardwareGammaCorrection( d3d9Device, texDesc.Usage, D3D9.ResourceType.CubeTexture, texDesc.Format, false ); } internalResourcesCreated = true; } else { Debug.Assert( loadedStreams.Length == 6 ); var ext = string.Empty; var pos = _name.LastIndexOf( "." ); if ( pos != -1 ) { ext = _name.Substring( pos + 1 ); } var images = new List<Image>( 6 ); for ( var i = 0; i < 6; i++ ) { images.Add( Image.FromStream( loadedStreams[ i ], ext ) ); } LoadImages( images.ToArray() ); } }
private void _determinePool() { if ( UseDefaultPool() ) { this._d3dPool = D3D9.Pool.Default; } else { this._d3dPool = D3D9.Pool.Managed; } }