Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Native] - Implement some existing C++ methods in C# #1896

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 101 additions & 4 deletions sources/engine/Stride.Graphics/FastTextRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ namespace Stride.Graphics
/// </summary>
public class FastTextRenderer : ComponentBase
{
private static readonly VertexPositionNormalTexture[] BaseVertexBufferData =
{
// Position Normal UV Coordinates
new( new(-1, 1, 0), new(0, 0, 1), new(0, 0) ),
new( new(1, 1, 0 ), new(0, 0, 1 ), new(1, 0 )),
new( new(-1, -1, 0 ), new(0, 0, 1 ), new(0, 1 )),
new( new(1, -1, 0 ), new(0, 0, 1 ), new(1, 1 )),
};
private const int VertexBufferCount = 2;

private const int IndexStride = sizeof(int);
Expand Down Expand Up @@ -212,11 +220,9 @@ public unsafe void End([NotNull] GraphicsContext graphicsContext)
foreach (var textInfo in stringsToDraw)
{
var textLength = textInfo.Text.Length;
var textLengthPointer = new IntPtr(&textLength);

Native.NativeInvoke.xnGraphicsFastTextRendererGenerateVertices(constantInfos, textInfo.RenderingInfo, textInfo.Text, out textLengthPointer, out mappedVertexBufferPointer);
GraphicsFastTextRendererGenerateVertices(constantInfos, textInfo.RenderingInfo, textInfo.Text, ref textLength, mappedVertexBufferPointer);

charsToRenderCount += *(int*)textLengthPointer.ToPointer();
charsToRenderCount += textLength;
}
}

Expand Down Expand Up @@ -247,6 +253,97 @@ public unsafe void End([NotNull] GraphicsContext graphicsContext)
graphicsContext.CommandList.DrawIndexed(charsToRenderCount * 6);
}

public unsafe void GraphicsFastTextRendererGenerateVertices(RectangleF constantInfos, RectangleF renderInfos, string textPointer, ref int textLength, IntPtr vertexBufferPointer)
{
var vertexBuffer = (VertexPositionNormalTexture**)vertexBufferPointer;
Copy link
Collaborator

@Eideren Eideren Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use VertexPositionNormalTexture** as the parameter type instead of IntPtr, it'll be clearer for the caller and you can move the type conversion outside the loop, it's likely already inlined by the jit but might as well.

Copy link
Collaborator

@froce froce Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually incorrect as written (and crashes when enabling the Profiler for example). It should just be VertexPositionNormalTexture* as I see it.
I suggest something like
private int GraphicsFastTextRendererGenerateVertices(RectangleF constantInfos, RectangleF renderInfos, string text, Span<VertexPositionNormalTexture> vertexBuffer) or maybe
private int GraphicsFastTextRendererGenerateVertices(RectangleF constantInfos, RectangleF renderInfos, string text, ref VertexPositionNormalTexture vertexBuffer) and iterating with vertexBuffer = ref Unsafe.Add(ref vertexBuffer, 1) instead of pointer arithmetic if that's simpler/faster than Span. I see no reason to keep a C-like signature.


float fX = renderInfos.X / renderInfos.Width;
float fY = renderInfos.Y / renderInfos.Height;
float fW = constantInfos.X / renderInfos.Width;
float fH = constantInfos.Y / renderInfos.Height;

RectangleF destination = new(fX, fY, fW, fH);
RectangleF source = new(0.0f, 0.0f, constantInfos.X, constantInfos.Y);

// Copy the array length (since it may change during an iteration)
int textCharCount = textLength;

float scaledDestinationX = 0.0f;
float scaledDestinationY = -(destination.Y * 2.0f - 1.0f);

float invertedWidth = 1.0f / constantInfos.Width;
float invertedHeight = 1.0f / constantInfos.Height;

for (int i = 0; i < textCharCount; i++)
{
char currentChar = textPointer[i];

if (currentChar == 11)
{
// Tabulation
destination.X+= 8 * fX;
--textLength;
continue;
}
else if (currentChar >= 10 && currentChar <= 13)
{
// New Line
destination.X= fX;
destination.Y += fH;
scaledDestinationY = -(destination.Y * 2.0f - 1.0f);
--textLength;
continue;
}
else if (currentChar < 32 || currentChar > 126)
{
currentChar = (char)32;
}

source.X= currentChar % 32 * constantInfos.X;
source.Y = currentChar / 32 % 4 * constantInfos.Y;

scaledDestinationX = destination.X * 2.0f - 1.0f;

// 0
(*vertexBuffer)->Position.X = scaledDestinationX + BaseVertexBufferData[0].Position.X* destination.Width;
(*vertexBuffer)->Position.Y = scaledDestinationY + BaseVertexBufferData[0].Position.Y * destination.Height;

(*vertexBuffer)->TextureCoordinate.X= (source.X + BaseVertexBufferData[0].TextureCoordinate.X * source.Width) * invertedWidth;
(*vertexBuffer)->TextureCoordinate.Y = (source.Y + BaseVertexBufferData[0].TextureCoordinate.Y * source.Height) * invertedHeight;

++(*vertexBuffer);

// 1
(*vertexBuffer)->Position.X = scaledDestinationX + BaseVertexBufferData[1].Position.X * destination.Width;
(*vertexBuffer)->Position.Y = scaledDestinationY + BaseVertexBufferData[1].Position.Y * destination.Height;

(*vertexBuffer)->TextureCoordinate.X = (source.X + BaseVertexBufferData[1].TextureCoordinate.X * source.Width) * invertedWidth;
(*vertexBuffer)->TextureCoordinate.Y = (source.Y + BaseVertexBufferData[1].TextureCoordinate.Y * source.Height) * invertedHeight;

++(*vertexBuffer);

// 2
(*vertexBuffer)->Position.X = scaledDestinationX + BaseVertexBufferData[2].Position.X * destination.Width;
(*vertexBuffer)->Position.Y = scaledDestinationY + BaseVertexBufferData[2].Position.Y * destination.Height;

(*vertexBuffer)->TextureCoordinate.X = (source.X + BaseVertexBufferData[2].TextureCoordinate.X * source.Width) * invertedWidth;
(*vertexBuffer)->TextureCoordinate.Y = (source.Y + BaseVertexBufferData[2].TextureCoordinate.Y * source.Height) * invertedHeight;

++(*vertexBuffer);

// 3
(*vertexBuffer)->Position.X = scaledDestinationX + BaseVertexBufferData[3].Position.X * destination.Width;
(*vertexBuffer)->Position.Y = scaledDestinationY + BaseVertexBufferData[3].Position.Y * destination.Height;

(*vertexBuffer)->TextureCoordinate.X = (source.X + BaseVertexBufferData[3].TextureCoordinate.X * source.Width) * invertedWidth;
(*vertexBuffer)->TextureCoordinate.Y = (source.Y + BaseVertexBufferData[3].TextureCoordinate.Y * source.Height) * invertedHeight;

++(*vertexBuffer);

destination.X += destination.Width;
}
}

/// <summary>
/// Sets or gets the color to use when drawing the text.
/// </summary>
Expand Down
39 changes: 37 additions & 2 deletions sources/engine/Stride.Graphics/SpriteBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.InteropServices;
using System.Text;
using Stride.Core.Mathematics;
using Stride.Native;
using Stride.Rendering;

namespace Stride.Graphics
Expand Down Expand Up @@ -590,9 +589,45 @@ private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text,

protected override unsafe void UpdateBufferValuesFromElementInfo(ref ElementInfo elementInfo, IntPtr vertexPtr, IntPtr indexPtr, int vertexOffset)
{
var vertex = (VertexPositionColorTextureSwizzle*)vertexPtr;
fixed (SpriteDrawInfo* drawInfo = &elementInfo.DrawInfo)
Comment on lines 590 to 593
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with VertexPositionColorTextureSwizzle* if possible

{
NativeInvoke.UpdateBufferValuesFromElementInfo(new IntPtr(drawInfo), vertexPtr, indexPtr, vertexOffset);
float deltaX = 1.0f / drawInfo->TextureSize.X;
float deltaY = 1.0f / drawInfo->TextureSize.Y;

Vector2 rotation = new(1,0);

if (Math.Abs(drawInfo->Rotation) > float.Epsilon)
{
(rotation.X, rotation.Y) = MathF.SinCos(drawInfo->Rotation);
}

Vector2 origin = drawInfo->Origin;
origin.X /= Math.Max(float.Epsilon, drawInfo->Source.Width);
origin.Y /= Math.Max(float.Epsilon, drawInfo->Source.Height);

for (int j = 0; j < 4; j++)
{
Vector2 corner = CornerOffsets[j];
Vector2 position;
position.X = (corner.X - origin.X) * drawInfo->Destination.Width;
position.Y = (corner.Y - origin.Y) * drawInfo->Destination.Height;

vertex->Position.X = drawInfo->Destination.X + (position.X * rotation.X) - (position.Y * rotation.Y);
vertex->Position.Y = drawInfo->Destination.Y + (position.X * rotation.Y) + (position.Y * rotation.X);
vertex->Position.Z = drawInfo->Depth;
vertex->Position.W = 1.0f;
vertex->ColorScale = drawInfo->ColorScale;
vertex->ColorAdd = drawInfo->ColorAdd;

corner = CornerOffsets[((j ^ (int)drawInfo->SpriteEffects) + (int)drawInfo->Orientation) % 4];
vertex->TextureCoordinate.X = (drawInfo->Source.X + corner.X * drawInfo->Source.Width) * deltaX;
vertex->TextureCoordinate.Y = (drawInfo->Source.Y + corner.Y * drawInfo->Source.Height) * deltaY;

vertex->Swizzle = (int)drawInfo->Swizzle;

vertex++;
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions sources/engine/Stride.Graphics/Stride.Graphics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
</PropertyGroup>
<Import Project="..\..\targets\Stride.props" />
<PropertyGroup>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<StrideAssemblyProcessor>true</StrideAssemblyProcessor>
<StridePlatformDependent>true</StridePlatformDependent>
<StrideBuildTags>*</StrideBuildTags>
Expand Down Expand Up @@ -43,7 +41,6 @@
<GlobalPropertiesToRemove>TargetFramework;StrideGraphicsApi</GlobalPropertiesToRemove>
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
</ProjectReference>
<ProjectReference Include="..\Stride.Native\Stride.Native.csproj" />
<ProjectReference Include="..\Stride.Shaders\Stride.Shaders.csproj" />
<ProjectReference Include="..\Stride\Stride.csproj" />
<PackageReference Include="System.Memory" Version="4.5.4" />
Expand Down
112 changes: 0 additions & 112 deletions sources/engine/Stride.Native/FastTextRenderer/FastTextRenderer.c

This file was deleted.