internal static void Dispose() { _font = null; _fontSmall = null; _uiSpritesheet = null; _uiStylesheet = null; Stylesheet.Current = null; _whiteRegion = null; if (_white != null) { _white.Dispose(); _white = null; } if (_uiBitmap != null) { _uiBitmap.Dispose(); _uiBitmap = null; } #if !XENKO if (_uiRasterizerState != null) { _uiRasterizerState.Dispose(); _uiRasterizerState = null; } #endif }
/// <summary> /// Draws the specified texture. /// </summary> /// <param name="texture">The texture.</param> /// <param name="position">The position.</param> /// <param name="renderSize">Size of the render.</param> /// <param name="color">The color.</param> /// <param name="source">The source.</param> /// <param name="centerOrigin">if set to <c>true</c> [center origin].</param> public override void Draw(TextureBase texture, PointF position, Size renderSize, ColorW color, Rect source, bool centerOrigin) { testRectangle.X = (int)position.X; testRectangle.Y = (int)position.Y; testRectangle.Width = (int)renderSize.Width; testRectangle.Height = (int)renderSize.Height; if (isClipped && !currentScissorRectangle.Intersects(testRectangle)) { return; } sourceRect.X = (int)source.X; sourceRect.Y = (int)source.Y; sourceRect.Width = (int)source.Width; sourceRect.Height = (int)source.Height; vecColor.A = color.A; vecColor.R = color.R; vecColor.G = color.G; vecColor.B = color.B; if (centerOrigin) { origin.X = testRectangle.Width / 2f; origin.Y = testRectangle.Height / 2f; } Texture2D native = texture.GetNativeTexture() as Texture2D; spriteBatch.Draw(native, testRectangle, sourceRect, vecColor, 0, origin); }
internal static void Dispose() { _font = null; _fontSmall = null; _uiTextureRegionAtlas = null; _uiStylesheet = null; Stylesheet.Current = null; _assetManager.ClearCache(); _whiteRegion = null; if (_white != null) { _white.Dispose(); _white = null; } #if !XENKO if (_uiRasterizerState != null) { _uiRasterizerState.Dispose(); _uiRasterizerState = null; } #endif }
public static Texture2D LoadTexture2D(Stream stream) { #if !XENKO return(Texture2D.FromStream(MyraEnvironment.GraphicsDevice, stream)); #else return(Texture2D.Load(MyraEnvironment.GraphicsDevice, stream)); #endif }
public static Texture2D CreateTexture2D(int width, int height) { #if !XENKO return(new Texture2D(MyraEnvironment.GraphicsDevice, width, height, false, SurfaceFormat.Color)); #else return(Texture2D.New2D(MyraEnvironment.GraphicsDevice, width, height, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource)); #endif }
private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness) { var length = Vector2.Distance(point1, point2); var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); var scale = new Vector2(length, thickness); spriteBatch.Draw(texture, point1, color: color, rotation: angle, scale: scale); }
public TextureWithOffset(Texture2D texture) { if (texture == null) { throw new ArgumentNullException("texture"); } Texture = texture; }
/// <summary> /// Draws the geometry texture. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="position">The position.</param> /// <param name="texture">The texture.</param> /// <param name="opacity">The opacity.</param> /// <param name="depth">The depth.</param> public override void DrawGeometryTexture(GeometryBuffer buffer, PointF position, TextureBase texture, float opacity, float depth) { XenkoGeometryBuffer paradoxBuffer = buffer as XenkoGeometryBuffer; Texture2D nativeTexture = texture.GetNativeTexture() as Texture2D; paradoxBuffer.EffectInstance.Parameters.Set(SpriteEffectKeys.Color, Color.White * opacity); paradoxBuffer.EffectInstance.Parameters.Set(TexturingKeys.Texture0, nativeTexture); DrawGeometry(buffer, position, depth); }
public TextureRegion(Texture2D texture, Rectangle bounds) { if (texture == null) { throw new ArgumentNullException("texture"); } _texture = texture; _bounds = bounds; }
public static void SetData <T>(Texture2D texture, T[] data) where T : struct { #if !XENKO texture.SetData(data); #else var commandList = MyraEnvironment.Game.GraphicsContext.CommandList; texture.SetData(commandList, data); #endif }
public static void SetData(Texture2D texture, Color[] data) { #if !XENKO texture.SetData(data); #else var commandList = MyraEnvironment.Game.GraphicsContext.CommandList; texture.SetData <Color>(commandList, data); #endif }
public static Texture2D CreateTexture2D(GraphicsDevice graphicsDevice, int width, int height, byte[] data) { #if !XENKO var result = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color); result.SetData(data); return(result); #else return(Texture2D.New2D(graphicsDevice, width, height, PixelFormat.R8G8B8A8_UNorm, data)); #endif }
public TextureRegion(TextureRegion region, Rectangle bounds) { if (region == null) { throw new ArgumentNullException("region"); } _texture = region.Texture; bounds.Offset(region.Bounds.Location); _bounds = bounds; }
public static Color[] GetData(Texture2D texture) { #if !XENKO var result = new Color[texture.Width * texture.Height]; texture.GetData(result); return(result); #else var commandList = MyraEnvironment.Game.GraphicsContext.CommandList; return(texture.GetData <Color>(commandList)); #endif }
private bool NeedToRecreateTexture(Xenko.Graphics.Texture tex, Vector3 dim) { if (tex == null || !TextureDimensionsEqual(tex, dim)) { if (tex != null) { tex.Dispose(); } return(true); } return(false); }
private bool NeedToRecreateTexture(Xenko.Graphics.Texture tex, Vector3 dim, Xenko.Graphics.PixelFormat pixelFormat, MultisampleCount samples) { if (tex == null || !TextureDimensionsEqual(tex, dim) || tex.Format != pixelFormat || tex.MultisampleCount != samples) { if (tex != null) { tex.Dispose(); } return(true); } return(false); }
public static bool DisposeTextureBySpecs(Xenko.Graphics.Texture tex, Vector3 dim, Xenko.Graphics.PixelFormat pixelFormat, MultisampleCount samples) { if (tex == null || !TextureDimensionsEqual(tex, dim) || tex.Format != pixelFormat || tex.MultisampleCount != samples) { if (tex != null) { tex.Dispose(); } return(true); } return(false); }
public void Render(VoxelStorageContext storageContext, IVoxelStorage Storage, RenderDrawContext drawContext) { if (NeedToRecreateTexture(MSAARenderTarget, new Vector3(voxelizationView.ViewSize.X, voxelizationView.ViewSize.Y, 1), PixelFormat.R8G8B8A8_UNorm, MultisampleCount.X8)) { MSAARenderTarget = Texture.New(storageContext.device, TextureDescription.New2D((int)voxelizationView.ViewSize.X, (int)voxelizationView.ViewSize.Y, new MipMapCount(false), PixelFormat.R8G8B8A8_UNorm, TextureFlags.RenderTarget, 1, GraphicsResourceUsage.Default, MultisampleCount.X8), null); } drawContext.CommandList.ResetTargets(); if (MSAARenderTarget != null) { drawContext.CommandList.SetRenderTarget(null, MSAARenderTarget); } Storage.Render(storageContext, drawContext, voxelizationView); }
/// <summary> /// Creates a Texture2D from Stream and optionally premultiplies alpha /// </summary> /// <param name="graphicsDevice"></param> /// <param name="stream"></param> /// <param name="premultiplyAlpha"></param> /// <returns></returns> public static unsafe Texture2D FromStream(Stream stream, bool premultiplyAlpha) { byte[] bytes; // Rewind stream if it is at end if (stream.CanSeek && stream.Length == stream.Position) { stream.Seek(0, SeekOrigin.Begin); } // Copy it's data to memory // As some platforms dont provide full stream functionality and thus streams can't be read as it is using (var ms = new MemoryStream()) { stream.CopyTo(ms); bytes = ms.ToArray(); } // The data returned is always four channel BGRA var result = ImageResult.FromMemory(bytes, ColorComponents.RedGreenBlueAlpha); if (premultiplyAlpha) { fixed(byte *b = &result.Data[0]) { for (var i = 0; i < result.Data.Length; i += 4) { var a = b[i + 3]; b[i] = ApplyAlpha(b[i], a); b[i + 1] = ApplyAlpha(b[i + 1], a); b[i + 2] = ApplyAlpha(b[i + 2], a); } } } Texture2D texture = null; texture = CrossEngineStuff.CreateTexture2D(result.Width, result.Height); CrossEngineStuff.SetData(texture, result.Data); return(texture); }
public static TextureRegionAtlas FromJson(string json, Texture2D texture) { var root = (Dictionary <string, object>)Json.Deserialize(json); var regions = new Dictionary <string, TextureRegion>(); foreach (var pair in root) { var entry = (Dictionary <string, object>)pair.Value; var jBounds = (Dictionary <string, object>)entry[StylesheetLoader.BoundsName]; var bounds = new Rectangle(int.Parse(jBounds[StylesheetLoader.LeftName].ToString()), int.Parse(jBounds[StylesheetLoader.TopName].ToString()), int.Parse(jBounds[StylesheetLoader.WidthName].ToString()), int.Parse(jBounds[StylesheetLoader.HeightName].ToString())); TextureRegion region; if (entry[StylesheetLoader.TypeName].ToString() == "0") { region = new TextureRegion(texture, bounds); } else { var jPadding = (Dictionary <string, object>)entry[StylesheetLoader.PaddingName]; var padding = new PaddingInfo { Left = int.Parse(jPadding[StylesheetLoader.LeftName].ToString()), Top = int.Parse(jPadding[StylesheetLoader.TopName].ToString()), Right = int.Parse(jPadding[StylesheetLoader.RightName].ToString()), Bottom = int.Parse(jPadding[StylesheetLoader.BottomName].ToString()) }; region = new NinePatchRegion(texture, bounds, padding); } regions[pair.Key.ToString()] = region; } return(new TextureRegionAtlas(regions)); }
/// <summary> /// Creates the texture. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mipmap">if set to <c>true</c> [mipmap].</param> /// <param name="dynamic">if set to <c>true</c> [dynamic].</param> /// <returns></returns> public override TextureBase CreateTexture(int width, int height, bool mipmap, bool dynamic) { if (width == 0 || height == 0) { return(null); } Texture2D native = null; if (dynamic) { native = Texture2D.New2D(GraphicsDevice, width, height, PixelFormat.R8G8B8A8_UNorm, usage: GraphicsResourceUsage.Dynamic); } else { native = Texture2D.New2D(GraphicsDevice, width, height, PixelFormat.R8G8B8A8_UNorm); } XenkoTexture texture = new XenkoTexture(native); return(texture); }
public void Render(VoxelStorageContext storageContext, RenderDrawContext drawContext, RenderView view) { RenderView voxelizationView = view; Int2 ViewSize = VoxelizationViewSizes[view]; if (VoxelUtils.DisposeTextureBySpecs(MSAARenderTarget, new Vector3(ViewSize.X, ViewSize.Y, 1), PixelFormat.R8G8B8A8_UNorm, MultisampleCount)) { MSAARenderTarget = Texture.New(storageContext.device, TextureDescription.New2D(ViewSize.X, ViewSize.Y, new MipMapCount(false), PixelFormat.R8G8B8A8_UNorm, TextureFlags.RenderTarget, 1, GraphicsResourceUsage.Default, MultisampleCount), null); } drawContext.CommandList.ResetTargets(); if (MSAARenderTarget != null) { drawContext.CommandList.SetRenderTarget(null, MSAARenderTarget); } var renderSystem = drawContext.RenderContext.RenderSystem; drawContext.CommandList.SetViewport(new Viewport(0, 0, ViewSize.X, ViewSize.Y)); renderSystem.Draw(drawContext, voxelizationView, renderSystem.RenderStages[voxelizationView.RenderStages[0].Index]); }
public virtual void Collect(RenderContext Context, IShadowMapRenderer ShadowMapRenderer) { renderVoxelVolumes = Context.VisibilityGroup.Tags.Get(CurrentRenderVoxelVolumes); if (renderVoxelVolumes == null) { return; } Vector3 resolutionMax = new Vector3(0, 0, 0); int fragmentsMax = 0; int fragmentsCountMax = 0; //Create per-volume textures foreach (var pair in renderVoxelVolumes) { var volume = pair.Value; var bounds = volume.ClipMapMatrix.ScaleVector; var resolution = bounds / volume.AproxVoxelSize; //Calculate closest power of 2 on each axis resolution.X = (float)Math.Pow(2, Math.Round(Math.Log(resolution.X, 2))); resolution.Y = (float)Math.Pow(2, Math.Round(Math.Log(resolution.Y, 2))); resolution.Z = (float)Math.Pow(2, Math.Round(Math.Log(resolution.Z, 2))); resolution = new Vector3(resolution.X, resolution.Z, resolution.Y);//Temporary Vector3 ClipMapResolution = new Vector3(resolution.X, resolution.Y, resolution.Z * volume.ClipMapCount); Vector3 MipMapResolution = resolution / 2; RenderVoxelVolumeData data; if (!renderVoxelVolumeData.TryGetValue(pair.Key, out data)) { renderVoxelVolumeData.Add(pair.Key, data = new RenderVoxelVolumeData()); } data.ClipMapResolution = resolution; if (NeedToRecreateTexture(data.ClipMaps, ClipMapResolution)) { data.ClipMaps = Xenko.Graphics.Texture.New3D(Context.GraphicsDevice, (int)ClipMapResolution.X, (int)ClipMapResolution.Y, (int)ClipMapResolution.Z, new MipMapCount(false), Xenko.Graphics.PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.UnorderedAccess); } if (NeedToRecreateTexture(data.MipMaps, resolution)) { data.MipMaps = Xenko.Graphics.Texture.New3D(Context.GraphicsDevice, (int)MipMapResolution.X, (int)MipMapResolution.Y, (int)MipMapResolution.Z, new MipMapCount(true), Xenko.Graphics.PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.UnorderedAccess); } resolutionMax = Vector3.Min(new Vector3(256), Vector3.Max(resolutionMax, resolution)); fragmentsMax = Math.Max(fragmentsMax, (int)(ClipMapResolution.X * ClipMapResolution.Y * ClipMapResolution.Z)); fragmentsCountMax = Math.Max(fragmentsCountMax, (int)(ClipMapResolution.X * ClipMapResolution.Y) * volume.ClipMapCount); } //Create re-usable textures float resolutionMaxSide = Math.Max(Math.Max(resolutionMax.X, resolutionMax.Y), resolutionMax.Z); if (NeedToRecreateTexture(MSAARenderTarget, new Vector3(resolutionMaxSide, resolutionMaxSide, 1))) { MSAARenderTarget = Texture.New(Context.GraphicsDevice, TextureDescription.New2D((int)resolutionMaxSide, (int)resolutionMaxSide, new MipMapCount(false), PixelFormat.R8G8B8A8_UNorm, TextureFlags.RenderTarget, 1, GraphicsResourceUsage.Default, MultisampleCount.X8), null); } if (NeedToRecreateBuffer(Fragments, fragmentsMax)) { Fragments = Xenko.Graphics.Buffer.Structured.New(Context.GraphicsDevice, fragmentsMax, 24, true); } if (NeedToRecreateBuffer(FragmentsCounter, fragmentsCountMax)) { FragmentsCounter = Xenko.Graphics.Buffer.Typed.New(Context.GraphicsDevice, fragmentsCountMax, PixelFormat.R32_SInt, true); } Vector3 MipMapResolutionMax = resolutionMax / 2; if (TempMipMaps == null || !TextureDimensionsEqual(TempMipMaps[0], MipMapResolutionMax)) { if (TempMipMaps != null) { for (int i = 0; i < TempMipMaps.Length; i++) { TempMipMaps[0].Dispose(); } } int mipCount = 1 + (int)Math.Floor(Math.Log(Math.Max(MipMapResolutionMax.X, Math.Max(MipMapResolutionMax.Y, MipMapResolutionMax.Z)), 2)); TempMipMaps = new Xenko.Graphics.Texture[mipCount]; for (int i = 0; i < TempMipMaps.Length; i++) { TempMipMaps[i] = Xenko.Graphics.Texture.New3D(Context.GraphicsDevice, (int)MipMapResolutionMax.X, (int)MipMapResolutionMax.Y, (int)MipMapResolutionMax.Z, false, Xenko.Graphics.PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.UnorderedAccess); MipMapResolutionMax /= 2; MipMapResolutionMax = Vector3.Max(new Vector3(1), MipMapResolutionMax); } } if (Generate3DMipmaps == null) { Generate3DMipmaps = new Xenko.Rendering.ComputeEffect.ComputeEffectShader(Context) { ShaderSourceName = "Generate3DMipmaps" }; ClearBuffer = new Xenko.Rendering.ComputeEffect.ComputeEffectShader(Context) { ShaderSourceName = "ClearBuffer" }; ArrangeFragments = new Xenko.Rendering.ComputeEffect.ComputeEffectShader(Context) { ShaderSourceName = "ArrangeFragments" }; } //Create all the views reflectiveVoxelViews.Clear(); foreach (var pair in renderVoxelVolumes) { var volume = pair.Value; var data = renderVoxelVolumeData[pair.Key]; var bounds = volume.ClipMapMatrix.ScaleVector; var shadowRenderView = new RenderView(); float nearClip = 0.1f; float farClip = 999.7f; //Currently hard coded and kinda odd shadowRenderView.View = Matrix.Translation(0.0f, 1.0f, 0.0f) * Matrix.RotationX(-3.1415f / 2.0f); shadowRenderView.Projection = Matrix.OrthoRH(bounds.X * (float)Math.Pow(2, volume.ClipMapCount), bounds.Z * (float)Math.Pow(2, volume.ClipMapCount), nearClip, farClip); Matrix.Multiply(ref shadowRenderView.View, ref shadowRenderView.Projection, out shadowRenderView.ViewProjection); shadowRenderView.Frustum = new BoundingFrustum(ref shadowRenderView.ViewProjection); shadowRenderView.CullingMode = CameraCullingMode.Frustum; shadowRenderView.ViewSize = new Vector2(1024, 1024); float maxRes = Math.Max(Math.Max(data.ClipMapResolution.X, data.ClipMapResolution.Y), data.ClipMapResolution.Z); var rotmat = new Matrix(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1);//xzy var viewToTexTrans = Matrix.Translation(0.5f, 0.5f, 0.5f); var viewToTexScale = Matrix.Scaling(0.5f, 0.5f, 0.5f); var viewportAspect = Matrix.Scaling(data.ClipMapResolution / maxRes); // Matrix BaseVoxelMatrix = transmat * scalemat * rotmat; Matrix BaseVoxelMatrix = volume.ClipMapMatrix * Matrix.Identity; BaseVoxelMatrix.Invert(); BaseVoxelMatrix = BaseVoxelMatrix * Matrix.Scaling(2f, 2f, 2f) * rotmat; data.Matrix = BaseVoxelMatrix * viewToTexScale * viewToTexTrans; data.ViewportMatrix = BaseVoxelMatrix * viewportAspect; data.ClipMapCount = volume.ClipMapCount; shadowRenderView.NearClipPlane = nearClip; shadowRenderView.FarClipPlane = farClip; shadowRenderView.RenderStages.Add(VoxelStage); reflectiveVoxelViews.Add(shadowRenderView, pair.Key); // Add the render view for the current frame Context.RenderSystem.Views.Add(shadowRenderView); // Collect objects in shadow views Context.VisibilityGroup.TryCollect(shadowRenderView); ShadowMapRenderer?.RenderViewsWithShadows.Add(shadowRenderView); } }
public static object GetNativeRenderTargetView(Texture texture) { return(GetNativeRenderTargetViewImpl(texture)); }
/// <summary> /// Initializes a new instance of the <see cref="MonoGameTexture"/> class. /// </summary> /// <param name="nativeTexture">The native texture.</param> public XenkoTexture(object nativeTexture) : base(nativeTexture) { texture = nativeTexture as Texture2D; }
private static void Draw(this SpriteBatch spriteBatch, Texture2D texture, Vector2 offset, Color color, Vector2 scale, float rotation = 0.0f) { spriteBatch.Draw(texture, offset, null, color, rotation, Vector2.Zero, scale, SpriteEffects.None, 0.0f); }
public NinePatchRegion(Texture2D texture, Rectangle bounds, Thickness info) : base(texture, bounds) { _info = info; var centerWidth = bounds.Width - info.Left - info.Right; var centerHeight = bounds.Height - info.Top - info.Bottom; var y = bounds.Y; if (info.Top > 0) { if (info.Left > 0) { _topLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, info.Top)); } if (centerWidth > 0) { _topCenter = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, info.Top)); } if (info.Right > 0) { _topRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, info.Top)); } } y += info.Top; if (centerHeight > 0) { if (info.Left > 0) { _centerLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, centerHeight)); } if (centerWidth > 0) { _center = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, centerHeight)); } if (info.Right > 0) { _centerRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, centerHeight)); } } y += centerHeight; if (info.Bottom > 0) { if (info.Left > 0) { _bottomLeft = new TextureRegion(texture, new Rectangle(bounds.X, y, info.Left, info.Bottom)); } if (centerWidth > 0) { _bottomCenter = new TextureRegion(texture, new Rectangle(bounds.X + info.Left, y, centerWidth, info.Bottom)); } if (info.Right > 0) { _bottomRight = new TextureRegion(texture, new Rectangle(bounds.X + info.Left + centerWidth, y, info.Right, info.Bottom)); } } }
/// <summary> /// Covers the whole texture /// </summary> /// <param name="texture"></param> public TextureRegion(Texture2D texture) : this(texture, new Rectangle(0, 0, texture.Width, texture.Height)) { }
public TextureWithOffset(Texture2D texture, Point offset) : this(texture) { Offset = offset; }
private static CpuDescriptorHandle GetNativeRenderTargetViewImpl(Texture texture) { return(texture.NativeRenderTargetView); }