1

Because SharpDX is an automatically generated wrapper over C++ APIs, I assumed it would be easy to convert from Microsoft's older Microsoft.DirectX.Direct3D managed API to SharpDX (as part of upgrading from .NET 3.5 to .NET 4.5+, and eventually from 32-bit process to 64-bit process).

However I am encountering some incompatibilities and missing features (classes, enums).

As a first step, I am starting with SharpDX.Direct3D9, as I believe that is closest to the older API.

But I do have the latest SharpDX sources, so I have been searching ALL the SharpDX library sources, in case the missing features do appear in a newer version of Direct3D.

Google searches have not shed any light on what to do about these issues.

Unfortunately, I am not familiar with these features, and the code I am updating has no documentation, so it seems that I will have to first learn what the managed code was doing, then learn how to do the equivalent in one of the C++ DirectX APIs, so I know what the equivalent SharpDX calls should be.

Suggestions for any of these issues, to shorten my learning time?


UNRESOLVED ISSUES

Missing from Microsoft.DirectX.Direct3D => SharpDX.Direct3D9/10/11:

  • enum TextureStageStates
  • field (LightsCollection) Device.Lights

RESOLVED ISSUES

See my answer.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
  • SharpDX is a modern .NET projection, so it's a little different than the legacy Managed DirectX 1.1 assemblies. If you need an identical replacement, that's what SlimDX is really good for. – Chuck Walbourn Oct 05 '17 at 06:05
  • @ChuckWalbourn - Thank you; I agree. Indeed I used SlimDX as a stepping-stone, a first step away from the legacy solution. However, now my goal is to update to a modern, actively developed replacement. The next logical increment is SharpDX's Direct2D1 and Direct3D9. There seems to be a near complete absence of information on the necessary conversion process. (Eventually I will move to SharpDX.Direct3D11 or 12, but that is a bigger change.) – ToolmakerSteve Oct 06 '17 at 18:03

1 Answers1

0

As issues are resolved, listed here:


Methods that return an HRESULT are void in SharpDX. For example, using Direct2D1, consider renderTarget.EndDraw().
Solution: errors are thrown as SharpDXException:

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

Additional details here.


using Microsoft.DirectX =>
Include from Nuget:
- SharpDX
- SharpDX.Mathematics

using SharpDX;
using SharpDX.Mathematics;
using SharpDX.Mathematics.Interop;

  • class Vector3 => SharpDX.Mathematics.Vector3
    OR SharpDX.Mathematics.Interop.RawVector3

  • given a Drawing.RectangleF rect, convert to SharpDX.Mathematics.Interop.RawRectangleF:
    new RawRectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);

  • ... other conversions, shown in VB code below.

VB helper module:

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
  • BitmapBrushProperties.HorizontalExtendMode => .ExtendModeX
  • BitmapBrushProperties.VerticalExtendMode => .ExtendModeY
  • Ellipse.Center => Ellipse.Point
  • Matrix3x2.Scale => Matrix3x2.Scaling
  • SharpDX.Matrix3x2.Rotation: old: angles in degrees; new: angles in radians.

using Microsoft.DirectX.Direct3D => using SharpDX.Direct3D9:

  • class RenderStateManager, and therefore missing field Device.RenderState => Device.GetRenderState/SetRenderState, where parameter specifies which state.
    Ex: if (device.RenderState.Lighting) =>
    if (device.GetRenderState(RenderState.Lighting))
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196