protected void LoadFromSource( D3D9.Device d3D9Device ) { //Entering critical section this.LockDeviceAccess(); D3D9.ShaderBytecode microcode = null; // Create the shader // Assemble source into microcode try { microcode = D3D9.ShaderBytecode.Assemble( Source, null, // no #define support null, // no #include support 0 ); // standard compile options } catch ( DX.CompilationException e ) { throw new AxiomException( "Cannot assemble D3D9 shader {0} Errors:\n{1}", e, Name, e.Message ); } LoadFromMicrocode( d3D9Device, microcode ); microcode.SafeDispose(); //Leaving critical section this.UnlockDeviceAccess(); }
protected PixelBox LockBuffer( BufferResources bufferResources, BasicBox lockBox, D3D9.LockFlags flags ) { // Set extents and format // Note that we do not carry over the left/top/front here, since the returned // PixelBox will be re-based from the locking point onwards var rval = new PixelBox( lockBox.Width, lockBox.Height, lockBox.Depth, Format ); if ( bufferResources.Surface != null ) { //Surface DX.DataRectangle lrect; // Filled in by D3D if ( lockBox.Left == 0 && lockBox.Top == 0 && lockBox.Right == Width && lockBox.Bottom == Height ) { // Lock whole surface lrect = bufferResources.Surface.LockRectangle( flags ); } else { var prect = ToD3DRectangle( lockBox ); lrect = bufferResources.Surface.LockRectangle( prect, flags ); } FromD3DLock( rval, lrect ); } else if ( bufferResources.Volume != null ) { // Volume var pbox = ToD3DBox( lockBox ); // specify range to lock var lbox = bufferResources.Volume.LockBox( pbox, flags ); FromD3DLock( rval, lbox ); } return rval; }
protected void BlitToMemory( BasicBox srcBox, PixelBox dst, BufferResources srcBufferResources, D3D9.Device d3d9Device ) { // Decide on pixel format of temp surface PixelFormat tmpFormat = Format; if ( D3D9Helper.ConvertEnum( dst.Format ) != D3D9.Format.Unknown ) { tmpFormat = dst.Format; } if ( srcBufferResources.Surface != null ) { Debug.Assert( srcBox.Depth == 1 && dst.Depth == 1 ); var srcDesc = srcBufferResources.Surface.Description; var temppool = D3D9.Pool.Scratch; // if we're going to try to use GetRenderTargetData, need to use system mem pool var tryGetRenderTargetData = false; if ( ( ( srcDesc.Usage & D3D9.Usage.RenderTarget ) != 0 ) && ( srcBox.Width == dst.Width ) && ( srcBox.Height == dst.Height ) && ( srcBox.Width == Width ) && ( srcBox.Height == Height ) && ( Format == tmpFormat ) ) { tryGetRenderTargetData = true; temppool = D3D9.Pool.SystemMemory; } // Create temp texture var tmp = new D3D9.Texture( d3d9Device, dst.Width, dst.Height, 1, // 1 mip level ie topmost, generate no mipmaps 0, D3D9Helper.ConvertEnum( tmpFormat ), temppool ); var surface = tmp.GetSurfaceLevel( 0 ); // Copy texture to this temp surface var srcRect = ToD3DRectangle( srcBox ); var destRect = ToD3DRectangle( dst ); // Get the real temp surface format var dstDesc = surface.Description; tmpFormat = D3D9Helper.ConvertEnum( dstDesc.Format ); // Use fast GetRenderTargetData if we are in its usage conditions var fastLoadSuccess = false; if ( tryGetRenderTargetData ) { var result = d3d9Device.GetRenderTargetData( srcBufferResources.Surface, surface ); fastLoadSuccess = result.Success; } if ( !fastLoadSuccess ) { var res = D3D9.Surface.FromSurface( surface, srcBufferResources.Surface, D3D9.Filter.Default, 0, srcRect, destRect ); if ( res.Failure ) { surface.SafeDispose(); tmp.SafeDispose(); throw new AxiomException( "D3D9.Surface.FromSurface failed in D3D9HardwarePixelBuffer.BlitToMemory" ); } } // Lock temp surface and copy it to memory var lrect = surface.LockRectangle( D3D9.LockFlags.ReadOnly ); // Copy it var locked = new PixelBox( dst.Width, dst.Height, dst.Depth, tmpFormat ); FromD3DLock( locked, lrect ); PixelConverter.BulkPixelConversion( locked, dst ); surface.UnlockRectangle(); // Release temporary surface and texture surface.SafeDispose(); tmp.SafeDispose(); } else if ( srcBufferResources.Volume != null ) { // Create temp texture var tmp = new D3D9.VolumeTexture( d3d9Device, dst.Width, dst.Height, dst.Depth, 0, 0, D3D9Helper.ConvertEnum( tmpFormat ), D3D9.Pool.Scratch ); var surface = tmp.GetVolumeLevel( 0 ); // Volume var ddestBox = ToD3DBoxExtent( dst ); var dsrcBox = ToD3DBox( srcBox ); var res = D3D9.Volume.FromVolume( surface, srcBufferResources.Volume, D3D9.Filter.Default, 0, dsrcBox, ddestBox ); if ( res.Failure ) { surface.SafeDispose(); tmp.SafeDispose(); throw new AxiomException( "D3D9.Surface.FromVolume failed in D3D9HardwarePixelBuffer.BlitToMemory" ); } // Lock temp surface and copy it to memory var lbox = surface.LockBox( D3D9.LockFlags.ReadOnly ); // Filled in by D3D // Copy it var locked = new PixelBox( dst.Width, dst.Height, dst.Depth, tmpFormat ); FromD3DLock( locked, lbox ); PixelConverter.BulkPixelConversion( locked, dst ); surface.UnlockBox(); // Release temporary surface and texture surface.SafeDispose(); tmp.SafeDispose(); } }
public D3D9.Surface GetFSAASurface( D3D9.Device d3d9Device ) { var bufferResources = GetBufferResources( d3d9Device ); if ( bufferResources != null ) { this.ownerTexture.CreateTextureResources( d3d9Device ); bufferResources = GetBufferResources( d3d9Device ); } return bufferResources.FsaaSurface; }
protected BufferResources GetBufferResources( D3D9.Device d3d9Device ) { if ( this.mapDeviceToBufferResources.ContainsKey( d3d9Device ) ) { return this.mapDeviceToBufferResources[ d3d9Device ]; } return null; }
public D3D9Device( D3D9DeviceManager d3D9DeviceManager, int adapterNumber, IntPtr hMonitor, D3D9.DeviceType devType, D3D9.CreateFlags behaviourFlags ) { this.pDeviceManager = d3D9DeviceManager; AdapterNumber = adapterNumber; this._monitor = hMonitor; DeviceType = devType; this._focusWindow = IntPtr.Zero; this.BehaviorFlags = behaviourFlags; }
protected void PopulateDef( D3D9.ConstantDescription d3DDesc, GpuProgramParameters.GpuConstantDefinition def ) { def.ArraySize = d3DDesc.Elements; switch ( d3DDesc.Type ) { case D3D9.ParameterType.Int: switch ( d3DDesc.Columns ) { case 1: def.ConstantType = GpuProgramParameters.GpuConstantType.Int1; break; case 2: def.ConstantType = GpuProgramParameters.GpuConstantType.Int2; break; case 3: def.ConstantType = GpuProgramParameters.GpuConstantType.Int3; break; case 4: def.ConstantType = GpuProgramParameters.GpuConstantType.Int4; break; } // columns break; case D3D9.ParameterType.Float: switch ( d3DDesc.Class ) { case D3D9.ParameterClass.MatrixColumns: case D3D9.ParameterClass.MatrixRows: { var firstDim = d3DDesc.RegisterCount/d3DDesc.Elements; var secondDim = d3DDesc.Class == D3D9.ParameterClass.MatrixRows ? d3DDesc.Columns : d3DDesc.Rows; switch ( firstDim ) { case 2: switch ( secondDim ) { case 2: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_2X2; def.ElementSize = 8; // HLSL always packs break; case 3: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_2X3; def.ElementSize = 8; // HLSL always packs break; case 4: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_2X4; def.ElementSize = 8; break; } // columns break; case 3: switch ( secondDim ) { case 2: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_3X2; def.ElementSize = 12; // HLSL always packs break; case 3: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_3X3; def.ElementSize = 12; // HLSL always packs break; case 4: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_3X4; def.ElementSize = 12; break; } // columns break; case 4: switch ( secondDim ) { case 2: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_4X2; def.ElementSize = 16; // HLSL always packs break; case 3: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_4X3; def.ElementSize = 16; // HLSL always packs break; case 4: def.ConstantType = GpuProgramParameters.GpuConstantType.Matrix_4X4; def.ElementSize = 16; break; } // secondDim break; } // firstDim } break; case D3D9.ParameterClass.Scalar: case D3D9.ParameterClass.Vector: switch ( d3DDesc.Columns ) { case 1: def.ConstantType = GpuProgramParameters.GpuConstantType.Float1; break; case 2: def.ConstantType = GpuProgramParameters.GpuConstantType.Float2; break; case 3: def.ConstantType = GpuProgramParameters.GpuConstantType.Float3; break; case 4: def.ConstantType = GpuProgramParameters.GpuConstantType.Float4; break; } // columns break; } break; default: // not mapping samplers, don't need to take the space break; } ; // D3D9 pads to 4 elements def.ElementSize = GpuProgramParameters.GpuConstantDefinition.GetElementSize( def.ConstantType, true ); }
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, // No FVF here, thank you. ePool ); } catch ( Exception ex ) { throw new AxiomException( "Cannot restore D3D9 vertex buffer", ex ); } this._bufferDesc = bufferResources.VertexBuffer.Description; }
private void _createQuery( D3D9.Device d3d9Device ) { // Check if query supported. try { // create the occlusion query. this._mapDeviceToQuery[ d3d9Device ] = new D3D9.Query( d3d9Device, D3D9.QueryType.Occlusion ); } catch { this._mapDeviceToQuery[ d3d9Device ] = null; } }
protected override void LoadFromMicrocode( D3D9.Device d3D9Device, D3D9.ShaderBytecode microcode ) { D3D9.PixelShader pixelShader; var shaderWasFound = this._mapDeviceToPixelShader.TryGetValue( d3D9Device, out pixelShader ); if ( shaderWasFound ) { pixelShader.SafeDispose(); } if ( IsSupported ) { // Create the shader pixelShader = new D3D9.PixelShader( d3D9Device, microcode ); } else { LogManager.Instance.Write( "Unsupported D3D9 pixel shader '{0}' was not loaded.", _name ); pixelShader = null; } if ( shaderWasFound ) { this._mapDeviceToPixelShader[ d3D9Device ] = pixelShader; } else { this._mapDeviceToPixelShader.Add( d3D9Device, pixelShader ); } }
public override void NotifyOnDeviceDestroy( D3D9.Device d3d9Device ) { //Entering critical section this.LockDeviceAccess(); // Find the shader of this device. D3D9.PixelShader it; // Case shader found -> release it and erase from map. if ( this._mapDeviceToPixelShader.TryGetValue( d3d9Device, out it ) ) { it.SafeDispose(); this._mapDeviceToPixelShader.Remove( d3d9Device ); } //Leaving critical section this.UnlockDeviceAccess(); }
/// <see cref="ID3D9Resource.NotifyOnDeviceLost"/> public virtual void NotifyOnDeviceLost( D3D9.Device d3d9Device ) { }
/// <see cref="ID3D9Resource.NotifyOnDeviceDestroy"/> public virtual void NotifyOnDeviceDestroy( D3D9.Device d3d9Device ) { }
protected abstract void LoadFromMicrocode( D3D9.Device d3D9Device, D3D9.ShaderBytecode microcode );
public void NotifyOnDeviceLost( D3D9.Device d3d9Device ) { //Entering critical section this.LockDeviceAccess(); if ( this._bufferDesc.Pool == D3D9.Pool.Default ) { if ( this._mapDeviceToBufferResources.ContainsKey( d3d9Device ) ) { this._mapDeviceToBufferResources[ d3d9Device ].IndexBuffer.SafeDispose(); } } //Leaving critical section this.UnlockDeviceAccess(); }
private void _releaseQuery( D3D9.Device d3d9Device ) { if ( this._mapDeviceToQuery.ContainsKey( d3d9Device ) ) { // Remove from query resource map. this._mapDeviceToQuery[ d3d9Device ].SafeDispose(); this._mapDeviceToQuery.Remove( d3d9Device ); } }
public void NotifyOnDeviceReset( D3D9.Device d3d9Device ) { //Entering critical section this.LockDeviceAccess(); if ( D3D9RenderSystem.ResourceManager.CreationPolicy == D3D9ResourceManager.ResourceCreationPolicy.CreateOnAllDevices ) { CreateBuffer( d3d9Device, this._bufferDesc.Pool ); } //Leaving critical section this.UnlockDeviceAccess(); }
public void NotifyOnDeviceCreate( D3D9.Device d3d9Device ) { }
protected void ProcessParamElement( D3D9.EffectHandle parent, string prefix, int index ) { var constant = this.constTable.GetConstant( parent, index ); // Since D3D HLSL doesn't deal with naming of array and struct parameters // automatically, we have to do it by hand var desc = this.constTable.GetConstantDescription( constant ); var paramName = desc.Name; // trim the odd '$' which appears at the start of the names in HLSL if ( paramName.StartsWith( "$" ) ) { paramName = paramName.Remove( 0, 1 ); } // Also trim the '[0]' suffix if it exists, we will add our own indexing later if ( paramName.EndsWith( "[0]" ) ) { paramName.Remove( paramName.Length - 3 ); } if ( desc.Class == D3D9.ParameterClass.Struct ) { // work out a new prefix for the nextest members if its an array, we need the index prefix += paramName + "."; // Cascade into struct for ( var i = 0; i < desc.StructMembers; ++i ) { ProcessParamElement( constant, prefix, i ); } } else { // process params if ( desc.Type == D3D9.ParameterType.Float || desc.Type == D3D9.ParameterType.Int || desc.Type == D3D9.ParameterType.Bool ) { var paramIndex = desc.RegisterIndex; var name = prefix + paramName; var def = new GpuProgramParameters.GpuConstantDefinition(); def.LogicalIndex = paramIndex; // populate type, array size & element size PopulateDef( desc, def ); if ( def.IsFloat ) { def.PhysicalIndex = floatLogicalToPhysical.BufferSize; lock ( floatLogicalToPhysical.Mutex ) { floatLogicalToPhysical.Map.Add( paramIndex, new GpuProgramParameters.GpuLogicalIndexUse( def.PhysicalIndex, def.ArraySize*def.ElementSize, GpuProgramParameters. GpuParamVariability.Global ) ); floatLogicalToPhysical.BufferSize += def.ArraySize*def.ElementSize; constantDefs.FloatBufferSize = floatLogicalToPhysical.BufferSize; } } else { def.PhysicalIndex = intLogicalToPhysical.BufferSize; lock ( intLogicalToPhysical.Mutex ) { intLogicalToPhysical.Map.Add( paramIndex, new GpuProgramParameters.GpuLogicalIndexUse( def.PhysicalIndex, def.ArraySize*def.ElementSize, GpuProgramParameters. GpuParamVariability.Global ) ); intLogicalToPhysical.BufferSize += def.ArraySize*def.ElementSize; constantDefs.IntBufferSize = intLogicalToPhysical.BufferSize; } } constantDefs.Map.Add( paramName.ToLower(), def ); // Now deal with arrays constantDefs.GenerateConstantDefinitionArrayEntries( name, def ); } } }
public void NotifyOnDeviceDestroy( D3D9.Device d3d9Device ) { _releaseQuery( d3d9Device ); }
public void ReleaseSurfaces( D3D9.Device d3d9Device ) { var bufferResources = GetBufferResources( d3d9Device ); if ( bufferResources != null ) { bufferResources.Surface.SafeDispose(); bufferResources.Surface = null; bufferResources.Volume.SafeDispose(); bufferResources.Volume = null; } }
public void NotifyOnDeviceLost( D3D9.Device d3d9Device ) { _releaseQuery( d3d9Device ); }
public void Bind( D3D9.Device dev, D3D9.Volume volume, D3D9.BaseTexture mipTex ) { //Entering critical section LockDeviceAccess(); var bufferResources = GetBufferResources( dev ); var isNewBuffer = false; if ( bufferResources == null ) { bufferResources = new BufferResources(); this.mapDeviceToBufferResources.Add( dev, bufferResources ); isNewBuffer = true; } bufferResources.MipTex = mipTex; bufferResources.Volume = volume; var desc = volume.Description; width = desc.Width; height = desc.Height; depth = desc.Depth; format = D3D9Helper.ConvertEnum( desc.Format ); // Default rowPitch = Width; slicePitch = Height*Width; sizeInBytes = PixelUtil.GetMemorySize( Width, Height, Depth, Format ); if ( isNewBuffer && this.ownerTexture.IsManuallyLoaded ) { foreach ( var it in this.mapDeviceToBufferResources ) { if ( it.Value != bufferResources && it.Value.Volume != null && it.Key.TestCooperativeLevel().Success && dev.TestCooperativeLevel().Success ) { var fullBufferBox = new BasicBox( 0, 0, 0, Width, Height, Depth ); var dstBox = new PixelBox( fullBufferBox, Format ); var data = new byte[sizeInBytes]; using ( var d = BufferBase.Wrap( data ) ) { dstBox.Data = d; BlitToMemory( fullBufferBox, dstBox, it.Value, it.Key ); BlitFromMemory( dstBox, fullBufferBox, bufferResources ); Array.Clear( data, 0, sizeInBytes ); } break; } } } //Leaving critical section UnlockDeviceAccess(); }
public void NotifyOnDeviceReset( D3D9.Device d3d9Device ) { }
public void DestroyBufferResources( D3D9.Device d3d9Device ) { //Entering critical section LockDeviceAccess(); if ( this.mapDeviceToBufferResources.ContainsKey( d3d9Device ) ) { this.mapDeviceToBufferResources[ d3d9Device ].SafeDispose(); this.mapDeviceToBufferResources.Remove( d3d9Device ); } //Leaving critical section UnlockDeviceAccess(); }
public void CreateBuffer( D3D9.Device d3d9Device, D3D9.Pool ePool ) { //Entering critical section this.LockDeviceAccess(); BufferResources bufferResources; // Find the vertex buffer of this device. if ( this._mapDeviceToBufferResources.TryGetValue( d3d9Device, out bufferResources ) ) { bufferResources.IndexBuffer.SafeDispose(); } else { bufferResources = new BufferResources(); this._mapDeviceToBufferResources.Add( d3d9Device, bufferResources ); } bufferResources.IndexBuffer = null; bufferResources.IsOutOfDate = true; bufferResources.LockOffset = 0; bufferResources.LockLength = sizeInBytes; bufferResources.LockOptions = BufferLocking.Normal; bufferResources.LastUsedFrame = Root.Instance.NextFrameNumber; // Create the Index buffer try { bufferResources.IndexBuffer = new D3D9.IndexBuffer( d3d9Device, sizeInBytes, D3D9Helper.ConvertEnum( usage ), ePool, D3D9Helper.ConvertEnum( type ) ); } catch ( Exception ex ) { throw new AxiomException( "Cannot create D3D9 Index buffer", ex ); } this._bufferDesc = bufferResources.IndexBuffer.Description; //Leaving critical section this.UnlockDeviceAccess(); }
protected void Blit( D3D9.Device d3d9Device, HardwarePixelBuffer rsrc, BasicBox srcBox, BasicBox dstBox, BufferResources srcBufferResources, BufferResources dstBufferResources ) { if ( dstBufferResources.Surface != null && srcBufferResources.Surface != null ) { // Surface-to-surface var dsrcRect = ToD3DRectangle( srcBox ); var ddestRect = ToD3DRectangle( dstBox ); var srcDesc = srcBufferResources.Surface.Description; // If we're blitting from a RTT, try GetRenderTargetData // if we're going to try to use GetRenderTargetData, need to use system mem pool // romeoxbm: not used even in Ogre //var tryGetRenderTargetData = false; if ( ( srcDesc.Usage & D3D9.Usage.RenderTarget ) != 0 && srcDesc.MultiSampleType == D3D9.MultisampleType.None ) { // Temp texture var tmptex = new D3D9.Texture( d3d9Device, srcDesc.Width, srcDesc.Height, 1, // 1 mip level ie topmost, generate no mipmaps 0, srcDesc.Format, D3D9.Pool.SystemMemory ); var tmpsurface = tmptex.GetSurfaceLevel( 0 ); if ( d3d9Device.GetRenderTargetData( srcBufferResources.Surface, tmpsurface ).Success ) { // Hey, it worked // Copy from this surface instead var res = D3D9.Surface.FromSurface( dstBufferResources.Surface, tmpsurface, D3D9.Filter.Default, 0, dsrcRect, ddestRect ); if ( res.Failure ) { tmpsurface.SafeDispose(); tmptex.SafeDispose(); throw new AxiomException( "D3D9.Surface.FromSurface failed in D3D9HardwarePixelBuffer.Blit" ); } tmpsurface.SafeDispose(); tmptex.SafeDispose(); return; } } // Otherwise, try the normal method var res2 = D3D9.Surface.FromSurface( dstBufferResources.Surface, srcBufferResources.Surface, D3D9.Filter.Default, 0, dsrcRect, ddestRect ); if ( res2.Failure ) { throw new AxiomException( "D3D9.Surface.FromSurface failed in D3D9HardwarePixelBuffer.Blit" ); } } else if ( dstBufferResources.Volume != null && srcBufferResources.Volume != null ) { // Volume-to-volume var dsrcBox = ToD3DBox( srcBox ); var ddestBox = ToD3DBox( dstBox ); var res = D3D9.Volume.FromVolume( dstBufferResources.Volume, srcBufferResources.Volume, D3D9.Filter.Default, 0, dsrcBox, ddestBox ); if ( res.Failure ) { throw new AxiomException( "D3D9.Volume.FromVolume failed in D3D9HardwarePixelBuffer.Blit" ); } } else { // Software fallback base.Blit( rsrc, srcBox, dstBox ); } }
public void NotifyOnDeviceDestroy( D3D9.Device d3d9Device ) { //Entering critical section this.LockDeviceAccess(); if ( this._mapDeviceToBufferResources.ContainsKey( d3d9Device ) ) { this._mapDeviceToBufferResources[ d3d9Device ].IndexBuffer.SafeDispose(); this._mapDeviceToBufferResources[ d3d9Device ].SafeDispose(); this._mapDeviceToBufferResources.Remove( d3d9Device ); } //Leaving critical section this.UnlockDeviceAccess(); }
internal void GenMipmaps( D3D9.BaseTexture mipTex ) { Debug.Assert( mipTex != null ); // Mipmapping if ( this.HWMipmaps ) { // Hardware mipmaps mipTex.GenerateMipSubLevels(); } else { // Software mipmaps mipTex.FilterTexture( (int)D3D9.Filter.Default, D3D9.Filter.Default ); } }
protected void LoadImpl( D3D9.Device d3D9Device ) { if ( this.externalMicrocode != null ) { LoadFromMicrocode( d3D9Device, this.externalMicrocode ); } else { // Normal load-from-source approach if ( LoadFromFile ) { // find & load source code var stream = ResourceGroupManager.Instance.OpenResource( fileName, _group, true, this ); source = stream.AsString(); } // Call polymorphic load LoadFromSource( d3D9Device ); } }