/// <summary> /// Called when [create index buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> protected override void OnCreateIndexBuffer(DeviceContext context, IElementsBufferProxy buffer, Geometry3D geometry, IDeviceResources deviceResources) { if (geometry != null && geometry.Indices != null && geometry.Indices.Count > 0) { buffer.UploadDataToBuffer(context, geometry.Indices, geometry.Indices.Count); } else { buffer.DisposeAndClear(); } }
/// <summary> /// Saves the wic texture to file. /// </summary> /// <param name="deviceResource">The device resource.</param> /// <param name="source">The source.</param> /// <param name="file">The file.</param> /// <param name="format">The format.</param> /// <returns></returns> public static bool SaveWICTextureToFile(IDeviceResources deviceResource, Texture2D source, string file, Direct2DImageFormat format) { return(SaveWICTextureToFile(deviceResource, source, file, BitmapExtensions.ToWICImageFormat(format))); }
/// <summary> /// Called when [create vertex buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> /// <param name="bufferIndex"></param> protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { if (geometry is IBillboardText billboardGeometry) { billboardGeometry.DrawTexture(deviceResources); if (billboardGeometry.BillboardVertices != null && billboardGeometry.BillboardVertices.Count > 0) { Type = billboardGeometry.Type; buffer.UploadDataToBuffer(context, billboardGeometry.BillboardVertices, billboardGeometry.BillboardVertices.Count, 0, geometry.PreDefinedVertexCount); if (texture != billboardGeometry.Texture) { RemoveAndDispose(ref textureView); texture = billboardGeometry.Texture; if (texture != null) { textureView = Collect(deviceResources.MaterialTextureManager.Register(texture)); } } } else { RemoveAndDispose(ref textureView); texture = null; buffer.UploadDataToBuffer(context, emptyVerts, 0); } } }
/// <summary> /// Initializes a new instance of the <see cref="DX11SwapChainRenderBufferProxy"/> class. /// </summary> /// <param name="deviceResource"></param> /// <param name="useDepthStencilBuffer"></param> public DX11SwapChainCompositionRenderBufferProxy(IDeviceResources deviceResource, bool useDepthStencilBuffer) : base(deviceResource, useDepthStencilBuffer) { }
/// <summary> /// Called when [create index buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> protected override void OnCreateIndexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, Geometry3D geometry, IDeviceResources deviceResources) { if (geometry != null && geometry.Indices != null && geometry.Indices.Count > 0) { buffer.UploadDataToBuffer(context, geometry.Indices, geometry.Indices.Count, 0, geometry.PreDefinedIndexCount); } else { buffer.UploadDataToBuffer(context, emptyIndices, 0); } }
public bool AttachBuffers(DeviceContextProxy context, ref int vertexBufferStartSlot, IDeviceResources deviceResources) { if (UpdateBuffers(context, deviceResources)) { context.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer.Buffer, vertextBuffer.StructureSize, vertextBuffer.Offset)); context.SetIndexBuffer(IndexBuffer.Buffer, global::SharpDX.DXGI.Format.R32_UInt, IndexBuffer.Offset); return(true); } return(false); }
protected override void OnDrawTexture(IDeviceResources deviceResources) { Texture = TextureStatic; Width = 0; Height = 0; // http://www.cyotek.com/blog/angelcode-bitmap-font-parsing-using-csharp var tempList = new List <BillboardVertex>(100); foreach (var textInfo in TextInfo) { tempList.Clear(); int x = 0; int y = 0; var w = BitmapFont.TextureSize.Width; var h = BitmapFont.TextureSize.Height; char previousCharacter; previousCharacter = ' '; var normalizedText = textInfo.Text; var rect = new RectangleF(textInfo.Origin.X, textInfo.Origin.Y, 0, 0); foreach (char character in normalizedText) { switch (character) { case '\n': x = 0; y -= BitmapFont.LineHeight; break; default: Character data = BitmapFont[character]; int kerning = BitmapFont.GetKerning(previousCharacter, character); tempList.Add(DrawCharacter(data, new Vector3(x + data.Offset.X, y - data.Offset.Y, 0), w, h, kerning, textInfo)); x += data.XAdvance + kerning; break; } previousCharacter = character; if (tempList.Count > 0) { rect.Width = Math.Max(rect.Width, x * textInfo.Scale * textureScale); rect.Height = Math.Max(rect.Height, Math.Abs(tempList.Last().OffBR.Y)); } } var halfW = rect.Width / 2; var halfH = rect.Height / 2; BillboardVertices.Add(new BillboardVertex() { Position = textInfo.Origin.ToVector4(), Background = textInfo.Background, TexTL = Vector2.Zero, TexBR = Vector2.Zero, OffTL = new Vector2(-halfW, halfH), OffBR = new Vector2(halfW, -halfH), }); textInfo.UpdateTextInfo(rect.Width, rect.Height); foreach (var vert in tempList) { var v = vert; v.OffTL += new Vector2(-halfW, halfH); v.OffBR += new Vector2(-halfW, halfH); BillboardVertices.Add(v); } Width += rect.Width; Height += rect.Height; } }
/// <summary> /// Called when [create vertex buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> /// <param name="bufferIndex"></param> protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { var billboardGeometry = geometry as IBillboardText; billboardGeometry.DrawTexture(deviceResources); if (billboardGeometry != null && billboardGeometry.BillboardVertices != null && billboardGeometry.BillboardVertices.Count > 0) { Type = billboardGeometry.Type; var data = OnBuildVertexArray(billboardGeometry, deviceResources); buffer.UploadDataToBuffer(context, data, billboardGeometry.BillboardVertices.Count); RemoveAndDispose(ref textureView); if (billboardGeometry.Texture != null) { textureView = Collect(deviceResources.MaterialTextureManager.Register(billboardGeometry.Texture)); } } else { textureView = null; buffer.DisposeAndClear(); } }
protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { // -- set geometry if given if (geometry != null && geometry.Positions != null && geometry.Positions.Count > 0) { buffer.UploadDataToBuffer(context, geometry.Positions, geometry.Positions.Count); } else { buffer.UploadDataToBuffer(context, emptyVerts, 0); } }
/// <summary> /// Called when [build vertex array]. /// </summary> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources"></param> /// <returns></returns> protected override BillboardVertex[] OnBuildVertexArray(IBillboardText geometry, IDeviceResources deviceResources) { var vertexCount = geometry.BillboardVertices.Count; var array = vertexArrayBuffer != null && vertexArrayBuffer.Length >= vertexCount ? vertexArrayBuffer : new BillboardVertex[vertexCount]; vertexArrayBuffer = array; for (var i = 0; i < vertexCount; i++) { array[i] = geometry.BillboardVertices[i]; } return(array); }
/// <summary> /// Called when [build vertex array]. /// </summary> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources"></param> /// <returns></returns> protected abstract VertexStruct[] OnBuildVertexArray(IBillboardText geometry, IDeviceResources deviceResources);
/// <summary> /// Called when [create vertex buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="bufferIndex">Index of the buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { if (geometry is MeshGeometry3D mesh) { switch (bufferIndex) { case 0: // -- set geometry if given if (geometry.Positions != null && geometry.Positions.Count > 0) { // --- get geometry var data = BuildVertexArray(mesh); buffer.UploadDataToBuffer(context, data, geometry.Positions.Count, 0, geometry.PreDefinedVertexCount); } else { //buffer.DisposeAndClear(); buffer.UploadDataToBuffer(context, emptyVerts, 0); } break; case 1: if (mesh.TextureCoordinates != null && mesh.TextureCoordinates.Count > 0) { buffer.UploadDataToBuffer(context, mesh.TextureCoordinates, mesh.TextureCoordinates.Count, 0, geometry.PreDefinedVertexCount); } else { buffer.UploadDataToBuffer(context, emptyTextureArray, 0); } break; case 2: if (geometry.Colors != null && geometry.Colors.Count > 0) { buffer.UploadDataToBuffer(context, geometry.Colors, geometry.Colors.Count, 0, geometry.PreDefinedVertexCount); } else { buffer.UploadDataToBuffer(context, emptyColorArray, 0); } break; } } }
/// <summary> /// Initializes a new instance of the <see cref="DX11RenderBufferProxyBase"/> class. /// </summary> /// <param name="deviceResource">The device resources.</param> /// <param name="useDepthStencilBuffer"></param> public DX11RenderBufferProxyBase(IDeviceResources deviceResource, bool useDepthStencilBuffer = true) { this.DeviceResources = deviceResource; DeviceContextPool = Collect(new DeviceContextPool(Device)); this.UseDepthStencilBuffer = useDepthStencilBuffer; }
public bool AttachBuffers(DeviceContextProxy context, ref int vertexBufferStartSlot, IDeviceResources deviceResources) { if (SpriteCount == 0 || IndexCount == 0) { return(false); } context.SetVertexBuffers(0, new VertexBufferBinding(VertexBufferInternal.Buffer, VertexBufferInternal.StructureSize, VertexBufferInternal.Offset)); context.SetIndexBuffer(IndexBufferInternal.Buffer, Format.R16_UInt, IndexBufferInternal.Offset); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="DX11SwapChainRenderBufferProxy"/> class. /// </summary> /// <param name="surfacePointer">The surface pointer.</param> /// <param name="deviceResource"></param> /// <param name="useDepthStencilBuffer"></param> public DX11SwapChainRenderBufferProxy(System.IntPtr surfacePointer, IDeviceResources deviceResource, bool useDepthStencilBuffer) : base(deviceResource, useDepthStencilBuffer) { surfacePtr = surfacePointer; }
protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { if (bufferIndex == 0 && geometry != null && geometry.Positions != null && geometry.Positions.Count > 0) { buffer.UploadDataToBuffer(context, geometry.Positions, geometry.Positions.Count); } }
protected virtual void OnDrawTexture(IDeviceResources deviceResources) { }
/// <summary> /// Attaches the buffers. /// </summary> /// <param name="context">The context.</param> /// <param name="vertexLayout">The vertex layout.</param> /// <param name="vertexBufferStartSlot">The vertex buffer slot.</param> /// <param name="deviceResources">The device resources.</param> /// <returns></returns> public bool AttachBuffers(DeviceContext context, InputLayout vertexLayout, ref int vertexBufferStartSlot, IDeviceResources deviceResources) { for (int i = 0; i < VertexChanged.Length; ++i) { if (VertexChanged[i] && VertexBuffer[i] != null) { lock (VertexBuffer) { if (VertexChanged[i]) { OnCreateVertexBuffer(context, VertexBuffer[i], i, Geometry, deviceResources); } VertexChanged[i] = false; updateVBinding = true; } } } if (updateVBinding) { lock (VertexBuffer) { if (updateVBinding) { vertexBufferBindings = VertexBuffer.Select(x => x != null ? new VertexBufferBinding(x.Buffer, x.StructureSize, x.Offset) : new VertexBufferBinding()).ToArray(); updateVBinding = false; } } } if (IndexChanged && IndexBuffer != null) { lock (IndexBuffer) { if (IndexChanged) { OnCreateIndexBuffer(context, IndexBuffer, Geometry, deviceResources); } IndexChanged = false; } } return(OnAttachBuffer(context, vertexLayout, ref vertexBufferStartSlot)); }
/// <summary> /// Attaches the buffers. /// </summary> /// <param name="context">The context.</param> /// <param name="vertexLayout">The vertex layout.</param> /// <param name="vertexBufferStartSlot">The vertex buffer start slot. Returns next available bind slot</param> /// <param name="deviceResources"></param> /// <returns></returns> public bool AttachBuffers(DeviceContext context, InputLayout vertexLayout, ref int vertexBufferStartSlot, IDeviceResources deviceResources) { return(true); }
/// <summary> /// Called when [create index buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> protected abstract void OnCreateIndexBuffer(DeviceContext context, IElementsBufferProxy buffer, Geometry3D geometry, IDeviceResources deviceResources);
/// <summary> /// Initializes a new instance of the <see cref="DX11SwapChainRenderBufferProxy"/> class. /// </summary> /// <param name="deviceResource"></param> public DX11SwapChainCompositionRenderBufferProxy(IDeviceResources deviceResource) : base(deviceResource) { }
protected override void OnUpdateTextureAndBillboardVertices(IDeviceResources deviceResources) { Width = 0; Height = 0; // http://www.cyotek.com/blog/angelcode-bitmap-font-parsing-using-csharp var tempList = new List <BillboardVertex>(100); foreach (var textInfo in TextInfo) { int tempPrevCount = tempList.Count; int x = 0; int y = 0; var w = BitmapFont.TextureSize.Width; var h = BitmapFont.TextureSize.Height; char previousCharacter; previousCharacter = ' '; var normalizedText = textInfo.Text; var rect = new RectangleF(textInfo.Origin.X, textInfo.Origin.Y, 0, 0); foreach (char character in normalizedText) { switch (character) { case '\n': x = 0; y -= BitmapFont.LineHeight; break; default: Character data = BitmapFont[character]; int kerning = BitmapFont.GetKerning(previousCharacter, character); tempList.Add(DrawCharacter(data, new Vector3(x + data.Offset.X, y - data.Offset.Y, 0), w, h, kerning, textInfo)); x += data.XAdvance + kerning; break; } previousCharacter = character; if (tempList.Count > 0) { rect.Width = Math.Max(rect.Width, x * textInfo.Scale * textureScale); rect.Height = Math.Max(rect.Height, Math.Abs(tempList.Last().OffBR.Y)); } } var transform = textInfo.Angle != 0 ? Matrix3x2.Rotation(textInfo.Angle) : Matrix3x2.Identity; var halfW = rect.Width / 2; var halfH = rect.Height / 2; //Add backbround vertex first. This also used for hit test BillboardVertices.Add(new BillboardVertex() { Position = textInfo.Origin.ToVector4(), Background = textInfo.Background, TexTL = Vector2.Zero, TexBR = Vector2.Zero, OffTL = Matrix3x2.TransformPoint(transform, new Vector2(-halfW, halfH)), OffBR = Matrix3x2.TransformPoint(transform, new Vector2(halfW, -halfH)), OffTR = Matrix3x2.TransformPoint(transform, new Vector2(-halfW, -halfH)), OffBL = Matrix3x2.TransformPoint(transform, new Vector2(halfW, halfH)), }); textInfo.UpdateTextInfo(rect.Width, rect.Height); for (int k = tempPrevCount; k < tempList.Count; ++k) { var v = tempList[k]; v.OffTL = Matrix3x2.TransformPoint(transform, v.OffTL + new Vector2(-halfW, halfH)); v.OffBR = Matrix3x2.TransformPoint(transform, v.OffBR + new Vector2(-halfW, halfH)); v.OffTR = Matrix3x2.TransformPoint(transform, v.OffTR + new Vector2(-halfW, halfH)); v.OffBL = Matrix3x2.TransformPoint(transform, v.OffBL + new Vector2(-halfW, halfH)); tempList[k] = v; } Width += rect.Width; Height += rect.Height; } foreach (var v in tempList) { BillboardVertices.Add(v); } }
/// <summary> /// Called when [create vertex buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> /// <param name="bufferIndex"></param> protected override void OnCreateVertexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { // -- set geometry if given if (geometry != null && geometry.Positions != null && geometry.Positions.Count > 0) { // --- get geometry var data = OnBuildVertexArray(geometry); buffer.UploadDataToBuffer(context, data, geometry.Positions.Count, 0, geometry.PreDefinedVertexCount); } else { //buffer.DisposeAndClear(); buffer.UploadDataToBuffer(context, emptyVertices, 0); } }
/// <summary> /// Attaches the buffers. /// </summary> /// <param name="context">The context.</param> /// <param name="vertexBufferStartSlot">The vertex buffer start slot. Returns next available bind slot</param> /// <param name="deviceResources"></param> /// <returns></returns> public bool AttachBuffers(DeviceContextProxy context, ref int vertexBufferStartSlot, IDeviceResources deviceResources) { return(false); }
private static bool CopyTextureToWICStream(IDeviceResources deviceResource, Texture2D staging, WICStream stream, Guid pfGuid, Guid containerFormat) { using (var encoder = new BitmapEncoder(deviceResource.WICImgFactory, containerFormat)) { var desc = staging.Description; encoder.Initialize(stream); var targetGuid = Guid.Empty; using (var frame = new BitmapFrameEncode(encoder)) { frame.Initialize(); frame.SetSize(desc.Width, desc.Height); frame.SetResolution(72, 72); switch (desc.Format) { case global::SharpDX.DXGI.Format.R32G32B32A32_Float: case global::SharpDX.DXGI.Format.R16G16B16A16_Float: targetGuid = PixelFormat.Format96bppRGBFloat; break; case global::SharpDX.DXGI.Format.R16G16B16A16_UNorm: targetGuid = PixelFormat.Format48bppBGR; break; case global::SharpDX.DXGI.Format.R32_Float: case global::SharpDX.DXGI.Format.R16_Float: case global::SharpDX.DXGI.Format.R16_UNorm: case global::SharpDX.DXGI.Format.R8_UNorm: case global::SharpDX.DXGI.Format.A8_UNorm: targetGuid = PixelFormat.Format48bppBGR; break; default: targetGuid = PixelFormat.Format24bppBGR; break; } frame.SetPixelFormat(ref targetGuid); var databox = deviceResource.Device.ImmediateContext.MapSubresource(staging, 0, MapMode.Read, MapFlags.None); try { if (targetGuid != pfGuid) { using (var bitmap = new Bitmap(deviceResource.WICImgFactory, desc.Width, desc.Height, pfGuid, new global::SharpDX.DataRectangle(databox.DataPointer, databox.RowPitch))) { using (var converter = new FormatConverter(deviceResource.WICImgFactory)) { if (converter.CanConvert(pfGuid, targetGuid)) { converter.Initialize(bitmap, targetGuid, BitmapDitherType.None, null, 0, BitmapPaletteType.MedianCut); frame.WriteSource(converter); } else { logger.LogDebug("Cannot convert"); } } } } else { frame.WritePixels(desc.Height, new global::SharpDX.DataRectangle(databox.DataPointer, databox.RowPitch), databox.RowPitch * desc.Height); } } finally { deviceResource.Device.ImmediateContext.UnmapSubresource(staging, 0); } frame.Commit(); encoder.Commit(); return(true); } } }
public bool UpdateBuffers(DeviceContextProxy context, IDeviceResources deviceResources) { return(false); }
/// <summary> /// Called when [create index buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> protected override void OnCreateIndexBuffer(DeviceContextProxy context, IElementsBufferProxy buffer, Geometry3D geometry, IDeviceResources deviceResources) { }
/// <summary> /// Initializes a new instance of the <see cref="DX11SwapChainRenderBufferProxy"/> class. /// </summary> /// <param name="surfacePointer">The surface pointer.</param> /// <param name="deviceResource"></param> public DX11SwapChainRenderBufferProxy(System.IntPtr surfacePointer, IDeviceResources deviceResource) : base(deviceResource) { surfacePtr = surfacePointer; }
protected abstract void OnUpdateTextureAndBillboardVertices(IDeviceResources deviceResources);
/// <summary> /// Called when [create vertex buffer]. /// </summary> /// <param name="context">The context.</param> /// <param name="buffer">The buffer.</param> /// <param name="geometry">The geometry.</param> /// <param name="deviceResources">The device resources.</param> /// <param name="bufferIndex"></param> protected override void OnCreateVertexBuffer(DeviceContext context, IElementsBufferProxy buffer, int bufferIndex, Geometry3D geometry, IDeviceResources deviceResources) { // -- set geometry if given if (geometry != null && geometry.Positions != null && geometry.Positions.Count > 0) { // --- get geometry var mesh = geometry as MeshGeometry3D; var data = BuildVertexArray(mesh); buffer.UploadDataToBuffer(context, data, geometry.Positions.Count); } else { buffer.DisposeAndClear(); } }