C# 从Microsoft Direct2D/Direct3D托管API转换为SharpDX-缺少类RenderStateManager、类型转换、其他问题 因为SharpDX是一个在C++ API上自动生成的包装器,所以我假设从微软的旧代码 DirectX .Direct3D < /C>托管API到SharpDX(3.5个升级到.NET 4.5 +,最终从32位进程到64位进程)是很容易的。

C# 从Microsoft Direct2D/Direct3D托管API转换为SharpDX-缺少类RenderStateManager、类型转换、其他问题 因为SharpDX是一个在C++ API上自动生成的包装器,所以我假设从微软的旧代码 DirectX .Direct3D < /C>托管API到SharpDX(3.5个升级到.NET 4.5 +,最终从32位进程到64位进程)是很容易的。,c#,direct3d,sharpdx,direct2d,C#,Direct3d,Sharpdx,Direct2d,但是,我遇到了一些不兼容和缺少的特性(类、枚举) 作为第一步,我将从SharpDX.Direct3D9开始,因为我认为它最接近旧的API 但是我有最新的SharpDX源代码,所以我一直在搜索所有的SharpDX库源代码,以防缺少的功能出现在较新版本的Direct3D中 谷歌搜索并没有阐明如何解决这些问题 遗憾的是,我不熟悉这些特性,而且我更新的代码没有文档,所以看起来我必须首先了解托管代码所做的事情,然后学习如何在C++ DirectX API中执行等价的操作,所以我知道等价的SharpDX调用

但是,我遇到了一些不兼容和缺少的特性(类、枚举)

作为第一步,我将从
SharpDX.Direct3D9
开始,因为我认为它最接近旧的API

但是我有最新的SharpDX源代码,所以我一直在搜索所有的SharpDX库源代码,以防缺少的功能出现在较新版本的Direct3D中

谷歌搜索并没有阐明如何解决这些问题

遗憾的是,我不熟悉这些特性,而且我更新的代码没有文档,所以看起来我必须首先了解托管代码所做的事情,然后学习如何在C++ DirectX API中执行等价的操作,所以我知道等价的SharpDX调用应该是什么。p> 针对这些问题提出建议,以缩短我的学习时间?


未解决的问题

缺少Microsoft.DirectX.Direct3D=>SharpDX.Direct3D9/10/11中的

  • 枚举
  • 现场(灯光采集)设备。灯光

已解决的问题


问题解决后,如下所示:


在SharpDX中,返回HRESULT的方法是
void
。例如,使用<代码> DUNT2D1,考虑<代码> ReNealTrime.EndDebug()/<代码> 解决方案:错误被抛出为
SharpDXException

try {
    renderTarget.EndDraw();
} catch (SharpDXException ex) {
    if (ex.HResult == ...)
        ...
}


使用Microsoft.DirectX=>
包括来自Nuget的内容:
-夏普DX
-SharpDX.数学

使用SharpDX
使用SharpDX.数学
使用SharpDX.Mathematics.Interop

  • 向量3=>SharpDX.数学。向量3
    或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: https://stackoverflow.com/a/46575472/199364
    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: https://stackoverflow.com/a/46575472/199364
        Return CByte(Math.Round(red * 255.0))
        ' Many people instead use a formula like this.
        ' From version of formula I added to this answer: https://stackoverflow.com/a/1914172/199364
        '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
导入System.Runtime.CompilerServices
导入SharpDX.Mathematics.Interop
模块扩展
函数AsRawVector2(点作为Drawing.PointF)作为RawVector2
返回新的RawVector2(点X、点Y)
端函数
函数AsRawColor4(颜色为Drawing.color)作为RawColor4
返回新的RawColor4(B255ToOne(color.R)、B255ToOne(color.G)、B255ToOne(color.B)、B255ToOne(color.A))
端函数
“0..255”范围内的“字节”“红色”;转换为范围“0.0..1.0”。
“见我在中间的解释:https://stackoverflow.com/a/46575472/199364
功能B255ToOne(红色为字节)为单个
'此公式在浮点域中产生等间距的值。
返回(红色/255.0F)
端函数
范围为“0.0..1.0”的“红色”;转换为范围为“0..255”。
函数OneTo255(红色为单字节)为字节
'此公式最接近输入值,并将“往返”误差降至最低。
“见我的解释:https://stackoverflow.com/a/46575472/199364
返回CByte(数学四舍五入(红色*255.0))
许多人使用这样的公式。
“根据本答案中添加的公式一的版本:https://stackoverflow.com/a/1914172/199364
'返回CByte(数学最大值(0,数学最小值(255,CInt,数学下限(红色*256.0F '))))
端函数
函数AsRawRectangle(rect As Drawing.Rectangle)作为RawRectangle
返回新的RawRectangle(rect.Left、rect.Top、rect.Right、rect.Bottom)
端函数
函数NewRectangleF(点作为图形。点,大小作为图形。大小)作为SharpDX.RectangleF
返回新矩形F(点X、点Y、大小、宽度、大小、高度)
端函数
端模块
  • BitmapBrushProperties.HorizontalExtendMode=>.ExtendModeX
  • BitmapBrushProperties.VerticalExtendMode=>.ExtendModeY
  • 椭圆.Center=>椭圆.Point
  • Matrix3x2.Scale=>Matrix3x2.Scaling
  • SharpDX.Matrix3x2.旋转:旧:角度以度为单位;新:角度以弧度为单位

使用Microsoft.DirectX.Direct3D=>使用SharpDX.Direct3D9:

  • RenderStateManager,因此缺少字段Device.RenderState=>Device.GetRenderState/SetRenderState,其中参数指定了哪个状态。
    例如:
    if(device.RenderState.Lighting)
    =>
    if(device.GetRenderState(RenderState.Lighting))

问题解决后,如下所示:


在SharpDX中返回HESREST的方法是<代码>无效>代码>。例如,使用<代码> DUNT2D1,考虑<代码> ReNealTrime.EndDebug()/<代码>。 解决方案:错误被抛出为

SharpDXException

try {
    renderTarget.EndDraw();
} catch (SharpDXException ex) {
    if (ex.HResult == ...)
        ...
}


使用Microsoft.DirectX=>
包括来自Nuget的内容:
-夏普DX
-SharpDX.数学

使用SharpDX;
使用SharpDX.数学;
使用SharpDX.Mathematics.Interop

  • 向量3=>SharpDX.数学。向量3
    或SharpDX.Mathematics.Interop.RawVector3

  • 给定一个
    Drawing.RectangleF rect
    ,将其转换为
    SharpDX.Mathematics.Interop.RawRectangleF

    new-RawRectangleF(rect.Left,rect.Top,rect.Right,rect.Bottom);

  • …其他转换,如下面的VB代码所示