public XnaHardwareIndexBuffer(HardwareBufferManagerBase manager, IndexType type, int numIndices, BufferUsage usage, GraphicsDevice device, bool useSystemMemory, bool useShadowBuffer) : base(manager, Validate(type), numIndices, usage, useSystemMemory, useShadowBuffer) { if (this.type == IndexType.Size16) { _bufferType = IndexElementSize.SixteenBits; } #if !SILVERLIGHT else { _bufferType = IndexElementSize.ThirtyTwoBits; } #endif // create the buffer if (usage == BufferUsage.Dynamic || usage == BufferUsage.DynamicWriteOnly) { _xnaBuffer = new IndexBuffer(device, _bufferType, numIndices, XnaHelper.Convert(usage)); } else { _xnaBuffer = new IndexBuffer(device, _bufferType, numIndices, Microsoft.Xna.Framework.Graphics.BufferUsage.None); } _bufferBytes = new byte[sizeInBytes]; _bufferBytes.Initialize(); }
public XnaHardwareVertexBuffer(HardwareBufferManagerBase manager, VertexDeclaration vertexDeclaration, int numVertices, BufferUsage usage, GraphicsDevice dev, bool useSystemMemory, bool useShadowBuffer) : base(manager, vertexDeclaration, numVertices, usage, useSystemMemory, useShadowBuffer) { _device = dev; if (!(vertexDeclaration is XnaVertexDeclaration)) { throw new AxiomException( "Invalid VertexDeclaration supplied, must be created by HardwareBufferManager.CreateVertexDeclaration()"); } if (usage == BufferUsage.Dynamic || usage == BufferUsage.DynamicWriteOnly) { _buffer = new DynamicVertexBuffer(_device, ((XnaVertexDeclaration)vertexDeclaration).XFGVertexDeclaration, numVertices, XnaHelper.Convert(usage)); } else { _buffer = new VertexBuffer(_device, ((XnaVertexDeclaration)vertexDeclaration).XFGVertexDeclaration, numVertices, XnaHelper.Convert(usage)); } _bufferBytes = new byte[vertexDeclaration.GetVertexSize() * numVertices]; _bufferBytes.Initialize(); }
///<summary> /// Internal implementation of <see cref="HardwareBuffer.Lock"/>. ///</summary> protected override PixelBox LockImpl(BasicBox lockBox, BufferLocking options) { _lockedBox = lockBox; // Set extents and format var rval = new PixelBox(lockBox, Format); var sizeInBytes = PixelUtil.GetMemorySize(lockBox.Width, lockBox.Height, lockBox.Depth, XnaHelper.Convert(surface.Format)); if (_bufferBytes == null || _bufferBytes.Length != sizeInBytes) { _bufferBytes = new byte[sizeInBytes]; #if !SILVERLIGHT if (surface != null) { surface.GetData(mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length); } else if (cube != null) { cube.GetData(face, mipLevel, XnaHelper.ToRectangle(lockBox), _bufferBytes, 0, _bufferBytes.Length); } else { volume.GetData(mipLevel, lockBox.Left, lockBox.Top, lockBox.Right, lockBox.Bottom, lockBox.Front, lockBox.Back, _bufferBytes, 0, _bufferBytes.Length); } #endif } rval.Data = BufferBase.Wrap(_bufferBytes); return(rval); }
/// <summary> /// /// </summary> private void CreateCubeTexture() { Debug.Assert(SrcWidth > 0 && SrcHeight > 0); // use current back buffer format for render textures, else use the one // defined by this texture format var xnaPixelFormat = (Usage == TextureUsage.RenderTarget) ? _bbPixelFormat : (ChooseXnaFormat()); // how many mips to use? make sure its at least one var numMips = (MipmapCount > 0) ? MipmapCount : 1; //see comment in CreateNormalTexture() -DoubleA //MipmapsHardwareGenerated = false; //if ( _devCaps.TextureCapabilities.SupportsMipCubeMap ) //{ // MipmapsHardwareGenerated = true /*this.CanAutoGenMipMaps( xnaUsage, XFG.ResourceType.TextureCube, xnaPixelFormat ) */; // if ( MipmapsHardwareGenerated ) // { // numMips = 0; // } //} //else //{ // // no mip map support for this kind of texture // MipmapCount = 0; // numMips = 1; //} if (Usage == TextureUsage.RenderTarget) { renderTarget = (Microsoft.Xna.Framework.Graphics.Texture) (new RenderTargetCube(_device, SrcWidth, mipmapCount > 0 ? true : false, xnaPixelFormat, DepthFormat.Depth24Stencil8)) as RenderTarget2D; _cubeTexture = ((Microsoft.Xna.Framework.Graphics.Texture)renderTarget) as RenderTargetCube; CreateDepthStencil(); } else { // create the cube texture _cubeTexture = new TextureCube(_device, SrcWidth, (mipmapCount > 0) ? true : false, xnaPixelFormat); // store base reference to the texture } _texture = _cubeTexture; SetFinalAttributes(SrcWidth, SrcHeight, 1, XnaHelper.Convert(xnaPixelFormat)); if (MipmapsHardwareGenerated) { //Generating mip maps API is no longer exposed. RenderTargets will auto-generate their mipmaps //but for other textures you're S.O.L. -DoubleA. See Shawn Hargreaves response to this thread: http://forums.create.msdn.com/forums/p/71559/436835.aspx //_texture.GenerateMipMaps( GetBestFilterMethod() ); } }
private SurfaceFormat ChooseXnaFormat() { if (Format == PixelFormat.Unknown) { return(_bbPixelFormat); } return(XnaHelper.Convert(XnaHelper.GetClosestSupported(Format))); }
public XnaImageCodecStream(Texture2D texture) { ImageData.width = texture.Width; ImageData.height = texture.Height; ImageData.format = XnaHelper.Convert(texture.Format); var buffer = new byte[ImageData.width * ImageData.height * PixelUtil.GetNumElemBytes(ImageData.format)]; texture.GetData(buffer); _stream = new MemoryStream(buffer); ImageData.numMipMaps = 1; ImageData.size = buffer.Length; }
///<summary> /// Copies a region from normal memory to a region of this pixelbuffer. The source /// image can be in any pixel format supported by Axiom, and in any size. ///</summary> ///<param name="src">PixelBox containing the source pixels and format in memory</param> ///<param name="dstBox">Image.BasicBox describing the destination region in this buffer</param> ///<remarks> /// The source and destination regions dimensions don't have to match, in which /// case scaling is done. This scaling is generally done using a bilinear filter in hardware, /// but it is faster to pass the source image in the right dimensions. /// Only call this function when both buffers are unlocked. ///</remarks> public override void BlitFromMemory(PixelBox src, BasicBox dstBox) { var converted = src; var bufGCHandle = new GCHandle(); var bufSize = 0; // Get src.Data as byte[] bufSize = PixelUtil.GetMemorySize(src.Width, src.Height, src.Depth, Format); var newBuffer = new byte[bufSize]; //bufGCHandle = GCHandle.Alloc( newBuffer, GCHandleType.Pinned ); //XnaHelper.Convert(XFG.SurfaceFormat) would never have returned SurfaceFormat.Unknown anyway... //if (XnaHelper.Convert(src.Format) != XFG.SurfaceFormat.Unknown) { converted = new PixelBox(src.Width, src.Height, src.Depth, Format, BufferBase.Wrap(newBuffer)); PixelConverter.BulkPixelConversion(src, converted); } //else //{ // Memory.Copy(converted.Data, BufferBase.Wrap(newBuffer), bufSize); //} if (surface != null) { surface.SetData(mipLevel, XnaHelper.ToRectangle(dstBox), newBuffer, 0, bufSize); } else if (cube != null) { cube.SetData(face, mipLevel, XnaHelper.ToRectangle(dstBox), newBuffer, 0, bufSize); } else { throw new NotSupportedException("BlitFromMemory on Volume Textures not supported."); } // If we allocated a buffer for the temporary conversion, free it here if (bufGCHandle.IsAllocated) { bufGCHandle.Free(); } if (doMipmapGen) { GenMipmaps(); } }
/// <summary> /// Internal implementation of <see cref="HardwareBuffer.Unlock"/>. /// </summary> protected override void UnlockImpl() { //set the bytes array inside the texture if (surface != null) { surface.SetData(mipLevel, XnaHelper.ToRectangle(_lockedBox), _bufferBytes, 0, _bufferBytes.Length); } else if (cube != null) { cube.SetData(face, mipLevel, XnaHelper.ToRectangle(_lockedBox), _bufferBytes, 0, _bufferBytes.Length); } #if !SILVERLIGHT else { volume.SetData(mipLevel, _lockedBox.Left, _lockedBox.Top, _lockedBox.Right, _lockedBox.Bottom, _lockedBox.Front, _lockedBox.Back, _bufferBytes, 0, _bufferBytes.Length); } #endif }
///<summary> /// Call this to associate a Xna Texture3D with this pixel buffer ///</summary> public void Bind(GraphicsDevice device, Texture3D volume, bool update) { this.device = device; this.volume = volume; width = volume.Width; height = volume.Height; depth = volume.Depth; format = XnaHelper.Convert(volume.Format); // Default rowPitch = Width; slicePitch = Height * Width; sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format); if (((int)usage & (int)TextureUsage.RenderTarget) != 0) { CreateRenderTextures(update); } }
public void Bind(GraphicsDevice device, RenderTarget2D surface, bool update) { this.device = device; renderTarget = surface; width = surface.Width / (int)Utility.Pow(2, mipLevel); height = surface.Height / (int)Utility.Pow(2, mipLevel); depth = 1; format = XnaHelper.Convert(surface.Format); // Default rowPitch = Width; slicePitch = Height * Width; sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format); if (((int)usage & (int)TextureUsage.RenderTarget) != 0) { CreateRenderTextures(update); } }
public void Bind(GraphicsDevice device, TextureCube cube, ushort face, ushort miplevel, bool update) { this.device = device; this.cube = cube; this.face = (CubeMapFace)face; mipLevel = miplevel; width = cube.Size / (int)Utility.Pow(2, mipLevel); height = cube.Size / (int)Utility.Pow(2, mipLevel); depth = 1; format = XnaHelper.Convert(cube.Format); // Default rowPitch = Width; slicePitch = Height * Width; sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format); if (((int)usage & (int)TextureUsage.RenderTarget) != 0) { CreateRenderTextures(update); } }
public override void CopyContentsToMemory(PixelBox dst, FrameBuffer buffer) { if ((dst.Left < 0) || (dst.Right > Width) || (dst.Top < 0) || (dst.Bottom > Height) || (dst.Front != 0) || (dst.Back != 1)) { throw new Exception("Invalid box."); } var device = Driver.XnaDevice; //in 3.1, this was ResolveTexture2D, an actual RenderTarget provides the exact same //functionality, especially seeing as RenderTarget2D is a texture now. //the difference is surface is actually set on the device -DoubleA RenderTarget2D surface; var data = new byte[dst.ConsecutiveSize]; var pitch = 0; if (buffer == FrameBuffer.Auto) { buffer = FrameBuffer.Front; } #if SILVERLIGHT var mode = ((XnaRenderSystem)Root.Instance.RenderSystem).DisplayMode; surface = new RenderTarget2D(device, mode.Width, mode.Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8); #else var mode = device.DisplayMode; surface = new RenderTarget2D(device, mode.Width, mode.Height, false, SurfaceFormat.Rgba64, DepthFormat.Depth24Stencil8); #endif //ResolveTexture2D( device, mode.Width, mode.Height, 0, SurfaceFormat.Rgba32 ); #if !SILVERLIGHT if (buffer == FrameBuffer.Front) { // get the entire front buffer. This is SLOW!! device.SetRenderTarget(surface); if (IsFullScreen) { if ((dst.Left == 0) && (dst.Right == Width) && (dst.Top == 0) && (dst.Bottom == Height)) { surface.GetData(data); } else { var rect = new Rectangle(); rect.Left = dst.Left; rect.Right = dst.Right; rect.Top = dst.Top; rect.Bottom = dst.Bottom; surface.GetData(0, XnaHelper.ToRectangle(rect), data, 0, 255); } } #if !(XBOX || XBOX360 || WINDOWS_PHONE) else { var srcRect = new Rectangle(); srcRect.Left = dst.Left; srcRect.Right = dst.Right; srcRect.Top = dst.Top; srcRect.Bottom = dst.Bottom; // Adjust Rectangle for Window Menu and Chrome var point = new Point(); point.X = (int)srcRect.Left; point.Y = (int)srcRect.Top; var control = Control.FromHandle(_windowHandle); point = control.PointToScreen(point); srcRect.Top = (long)point.Y; srcRect.Left = (long)point.X; srcRect.Bottom += (long)point.Y; srcRect.Right += (long)point.X; surface.GetData(0, XnaHelper.ToRectangle(srcRect), data, 0, 255); } #endif } else { device.SetRenderTarget(surface); if ((dst.Left == 0) && (dst.Right == Width) && (dst.Top == 0) && (dst.Bottom == Height)) { surface.GetData(data); } else { var rect = new Rectangle(); rect.Left = dst.Left; rect.Right = dst.Right; rect.Top = dst.Top; rect.Bottom = dst.Bottom; surface.GetData(0, XnaHelper.ToRectangle(rect), data, 0, 255); } } #endif var format = XnaHelper.Convert(surface.Format); if (format == PixelFormat.Unknown) { throw new Exception("Unsupported format"); } var dataPtr = Memory.PinObject(data); var src = new PixelBox(dst.Width, dst.Height, 1, format, dataPtr); src.RowPitch = pitch / PixelUtil.GetNumElemBytes(format); src.SlicePitch = surface.Height * src.RowPitch; PixelConverter.BulkPixelConversion(src, dst); Memory.UnpinObject(data); surface.Dispose(); }
private void CreateNormalTexture() { Debug.Assert(SrcWidth > 0 && SrcHeight > 0); // use current back buffer format for render textures, else use the one // defined by this texture format var xnaPixelFormat = (Usage == TextureUsage.RenderTarget) ? _bbPixelFormat : ChooseXnaFormat(); // how many mips to use? make sure its at least one var numMips = (MipmapCount > 0) ? MipmapCount : 1; //bloody 'ell, it's great that Xa 4.0 checks capabilities for the programmer, but it's incredibly annoying //that it doesn't tell the programer anything about them. Anyway, there's no way for us to know if MipMaps are supported, //but in the c'tor of the Texture is a paramater bool mipMap, which, if set to true and mipMaps aren't supported, Xna will take care of it //-DoubleA //MipmapsHardwareGenerated = false; //if ( _devCaps.TextureCapabilities.SupportsMipMap ) //{ // MipmapsHardwareGenerated = CanAutoGenMipMaps( xnaUsage, XFG.ResourceType.Texture2D, xnaPixelFormat ); // if ( MipmapsHardwareGenerated ) // { // numMips = 0; // } //} //else //{ // // no mip map support for this kind of texture // this.MipmapCount = 0; // numMips = 1; //} if (Usage == TextureUsage.RenderTarget) { renderTarget = new RenderTarget2D(_device, SrcWidth, SrcHeight, MipmapCount > 0, xnaPixelFormat, DepthFormat.Depth24Stencil8); _normTexture = renderTarget; CreateDepthStencil(); } else { #if SILVERLIGHT if (!IsPowerOfTwo) { MipmapCount = 0; } #endif _normTexture = new Texture2D(_device, SrcWidth, SrcHeight, MipmapCount > 0, xnaPixelFormat); } _texture = _normTexture; SetFinalAttributes(SrcWidth, SrcHeight, 1, XnaHelper.Convert(xnaPixelFormat)); if (MipmapsHardwareGenerated) { //Generating mip maps API is no longer exposed. RenderTargets will auto-generate their mipmaps //but for other textures you're S.O.L. -DoubleA. See Shawn Hargreaves response to this thread: http://forums.create.msdn.com/forums/p/71559/436835.aspx //_texture.GenerateMipMaps( GetBestFilterMethod() ); } }
private void LoadNormalTexture() { Debug.Assert(TextureType == TextureType.OneD || TextureType == TextureType.TwoD); if (Root.Instance.RenderSystem.ConfigOptions["Use Content Pipeline"].Value == "Yes") { var acm = new AxiomContentManager((XnaRenderSystem)Root.Instance.RenderSystem, "Content"); _normTexture = acm.Load <Texture2D>(Name); _texture = _normTexture; Width = _normTexture.Width; Height = _normTexture.Height; internalResourcesCreated = true; } #if !(XBOX || XBOX360) else { Stream stream; if (Name.EndsWith(".dds")) { stream = ResourceGroupManager.Instance.OpenResource(Name); // use Xna to load the image directly from the stream //XFG.TextureCreationParameters tcp = new XFG.TextureCreationParameters(); //tcp.Filter = Microsoft.Xna.Framework.Graphics.FilterOptions.Triangle; //tcp.MipLevels = MipmapCount; //Not sure how to set MipLevels. _normTexture.LevelCount is get-only... #if SILVERLIGHT var im = new BitmapImage(); im.SetSource(stream); _normTexture = new Texture2D(_device, im.PixelWidth, im.PixelHeight, false, SurfaceFormat.Color); im.CopyTo(_normTexture); #else _normTexture = Texture2D.FromStream(_device, stream); //.FromFile( _device, stream, tcp ); #endif // store a ref for the base texture interface _texture = _normTexture; //reset stream position to read Texture information ////stream.Position = 0; // set the image data attributes //Not sure if these lines accomplish the same thing as the below commented-out ones. SetSrcAttributes(_normTexture.Width, _normTexture.Height, 1, XnaHelper.Convert(_normTexture.Format)); SetFinalAttributes(_normTexture.Width, _normTexture.Height, 1, XnaHelper.Convert(_normTexture.Format)); //XFG.TextureInformation info = XFG.Texture2D.GetTextureInformation( stream ); //SetSrcAttributes( info.Width, info.Height, 1, XnaHelper.Convert( info.Format ) ); //SetFinalAttributes( info.Width, info.Height, 1, XnaHelper.Convert( info.Format ) ); internalResourcesCreated = true; } else { // find & load resource data intro stream to allow resource group changes if required stream = ResourceGroupManager.Instance.OpenResource(Name, Group, true, this); //#if SILVERLIGHT // if (stream == null) // { // Name += ".png"; // stream = ResourceGroupManager.Instance.OpenResource( Name, Group, true, this ); // } //#endif var pos = Name.LastIndexOf("."); var 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(stream, ext); LoadImages(new[] { image }); image.Dispose(); } if (stream != null) { stream.Close(); } } #endif }