从 Microsoft Direct2D/Direct3D 托管 API 转换为 SharpDX - 缺少 class RenderStateManager、类型转换、其他问题
Converting from Microsoft Direct2D/Direct3D managed API to SharpDX - missing class RenderStateManager, type conversions, other issues
因为 SharpDX 是在 C++ API 上自动生成的包装器,我认为很容易从 Microsoft 的旧 Microsoft.DirectX.Direct3D
管理 API 转换为 SharpDX(作为升级的一部分从 .NET 3.5 到 .NET 4.5+,最终从 32 位进程到 64 位进程)。
但是我遇到了一些不兼容和缺失的功能(类,枚举)。
作为第一步,我从 SharpDX.Direct3D9
开始,因为我认为它最接近旧的 API。
但我确实有最新的 SharpDX 源代码,所以我一直在搜索所有 SharpDX 库源代码,以防更新版本的 Direct3D 中确实出现缺少的功能。
Google 搜索没有说明如何解决这些问题。
不幸的是,我不熟悉这些功能,而且我正在更新的代码没有文档,所以我似乎必须先了解托管代码在做什么,然后再学习如何做等效的C++ DirectX APIs 之一,所以我知道等效的 SharpDX 调用应该是什么。
对这些问题有什么建议,可以缩短我的学习时间吗?
未解决的问题
Microsoft.DirectX.Direct3D => SharpDX.Direct3D9/10/11:
缺少
- 枚举TextureStageStates
- 字段(LightsCollection)Device.Lights
已解决的问题
随着问题的解决,在此处列出:
return HRESULT 的方法在 SharpDX 中是 void
。例如,使用 Direct2D1
,考虑 renderTarget.EndDraw()
.
解决方案:错误被抛出为 SharpDXException
:
try {
renderTarget.EndDraw();
} catch (SharpDXException ex) {
if (ex.HResult == ...)
...
}
使用 Microsoft.DirectX =>
从 Nuget 中包含:
- SharpDX
- SharpDX.Mathematics
使用 SharpDX;
使用 SharpDX.Mathematics;
使用 SharpDX.Mathematics.Interop;
class Vector3 => SharpDX.Mathematics.Vector3
或 SharpDX.Mathematics.Interop.RawVector3
给定一个 Drawing.RectangleF rect
,转换为 SharpDX.Mathematics.Interop.RawRectangleF
:
new RawRectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);
...其他转换,如下面的VB代码所示。
VB 辅助模块:
Imports System.Runtime.CompilerServices
Imports SharpDX.Mathematics.Interop
Module SharpDXExtensions
<Extension()>
Function AsRawVector2(point As Drawing.PointF) As RawVector2
Return New RawVector2(point.X, point.Y)
End Function
<Extension()>
Function AsRawColor4(color As Drawing.Color) As RawColor4
Return New RawColor4(B255ToOne(color.R), B255ToOne(color.G), B255ToOne(color.B), B255ToOne(color.A))
End Function
' "Byte" "red" in range "0..255"; convert to range ""0.0..1.0".
' See my explanation in middle of:
Function B255ToOne(red As Byte) As Single
' This formula results in equally-spaced values in the float domain.
Return (red / 255.0F)
End Function
' "red" in range "0.0..1.0"; convert to range "0..255".
Function OneTo255(red As Single) As Byte
' This formula most closely matches the incoming values, and minimizes "round-trip" error.
' See my explanation:
Return CByte(Math.Round(red * 255.0))
' Many people instead use a formula like this.
' From version of formula I added to this answer:
'Return CByte(Math.Max(0, Math.Min(255, CInt(Math.Floor(red * 256.0F)))))
End Function
<Extension()>
Function AsRawRectangle(rect As Drawing.Rectangle) As RawRectangle
Return New RawRectangle(rect.Left, rect.Top, rect.Right, rect.Bottom)
End Function
Function NewRectangleF(point As Drawing.Point, size As Drawing.Size) As SharpDX.RectangleF
Return New RectangleF(point.X, point.Y, size.Width, size.Height)
End Function
End Module
- BitmapBrushProperties.HorizontalExtendMode => .ExtendModeX
- BitmapBrushProperties.VerticalExtendMode => .ExtendModeY
- Ellipse.Center => Ellipse.Point
- Matrix3x2.Scale => Matrix3x2.Scaling
- SharpDX.Matrix3x2.Rotation:旧:以度为单位的角度;新:角度 弧度 .
使用 Microsoft.DirectX.Direct3D => 使用 SharpDX.Direct3D9:
- class RenderStateManager,因此缺少字段 Device.RenderState => Device。 GetRenderState/SetRenderState,其中参数指定哪个状态。
例如:if (device.RenderState.Lighting)
=>
if (device.GetRenderState(RenderState.Lighting))
因为 SharpDX 是在 C++ API 上自动生成的包装器,我认为很容易从 Microsoft 的旧 Microsoft.DirectX.Direct3D
管理 API 转换为 SharpDX(作为升级的一部分从 .NET 3.5 到 .NET 4.5+,最终从 32 位进程到 64 位进程)。
但是我遇到了一些不兼容和缺失的功能(类,枚举)。
作为第一步,我从 SharpDX.Direct3D9
开始,因为我认为它最接近旧的 API。
但我确实有最新的 SharpDX 源代码,所以我一直在搜索所有 SharpDX 库源代码,以防更新版本的 Direct3D 中确实出现缺少的功能。
Google 搜索没有说明如何解决这些问题。
不幸的是,我不熟悉这些功能,而且我正在更新的代码没有文档,所以我似乎必须先了解托管代码在做什么,然后再学习如何做等效的C++ DirectX APIs 之一,所以我知道等效的 SharpDX 调用应该是什么。
对这些问题有什么建议,可以缩短我的学习时间吗?
未解决的问题
Microsoft.DirectX.Direct3D => SharpDX.Direct3D9/10/11:
缺少- 枚举TextureStageStates
- 字段(LightsCollection)Device.Lights
已解决的问题
随着问题的解决,在此处列出:
return HRESULT 的方法在 SharpDX 中是 void
。例如,使用 Direct2D1
,考虑 renderTarget.EndDraw()
.
解决方案:错误被抛出为 SharpDXException
:
try {
renderTarget.EndDraw();
} catch (SharpDXException ex) {
if (ex.HResult == ...)
...
}
使用 Microsoft.DirectX =>
从 Nuget 中包含:
- SharpDX
- SharpDX.Mathematics
使用 SharpDX;
使用 SharpDX.Mathematics;
使用 SharpDX.Mathematics.Interop;
class Vector3 => SharpDX.Mathematics.Vector3
或 SharpDX.Mathematics.Interop.RawVector3给定一个
Drawing.RectangleF rect
,转换为SharpDX.Mathematics.Interop.RawRectangleF
:
new RawRectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);
...其他转换,如下面的VB代码所示。
VB 辅助模块:
Imports System.Runtime.CompilerServices
Imports SharpDX.Mathematics.Interop
Module SharpDXExtensions
<Extension()>
Function AsRawVector2(point As Drawing.PointF) As RawVector2
Return New RawVector2(point.X, point.Y)
End Function
<Extension()>
Function AsRawColor4(color As Drawing.Color) As RawColor4
Return New RawColor4(B255ToOne(color.R), B255ToOne(color.G), B255ToOne(color.B), B255ToOne(color.A))
End Function
' "Byte" "red" in range "0..255"; convert to range ""0.0..1.0".
' See my explanation in middle of:
Function B255ToOne(red As Byte) As Single
' This formula results in equally-spaced values in the float domain.
Return (red / 255.0F)
End Function
' "red" in range "0.0..1.0"; convert to range "0..255".
Function OneTo255(red As Single) As Byte
' This formula most closely matches the incoming values, and minimizes "round-trip" error.
' See my explanation:
Return CByte(Math.Round(red * 255.0))
' Many people instead use a formula like this.
' From version of formula I added to this answer:
'Return CByte(Math.Max(0, Math.Min(255, CInt(Math.Floor(red * 256.0F)))))
End Function
<Extension()>
Function AsRawRectangle(rect As Drawing.Rectangle) As RawRectangle
Return New RawRectangle(rect.Left, rect.Top, rect.Right, rect.Bottom)
End Function
Function NewRectangleF(point As Drawing.Point, size As Drawing.Size) As SharpDX.RectangleF
Return New RectangleF(point.X, point.Y, size.Width, size.Height)
End Function
End Module
- BitmapBrushProperties.HorizontalExtendMode => .ExtendModeX
- BitmapBrushProperties.VerticalExtendMode => .ExtendModeY
- Ellipse.Center => Ellipse.Point
- Matrix3x2.Scale => Matrix3x2.Scaling
- SharpDX.Matrix3x2.Rotation:旧:以度为单位的角度;新:角度 弧度 .
使用 Microsoft.DirectX.Direct3D => 使用 SharpDX.Direct3D9:
- class RenderStateManager,因此缺少字段 Device.RenderState => Device。 GetRenderState/SetRenderState,其中参数指定哪个状态。
例如:if (device.RenderState.Lighting)
=>
if (device.GetRenderState(RenderState.Lighting))