DirectX9.0C Declares or COM for CreateDevice?

  1. 7 months ago

    Eugene D

    3 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    Hello,

    I am working on trying to build a simple DirectX 9.0c spinning triangle in Windows and am not able to implement the CreateDevice method correctly with a Declare. I am unsure if I should be attempting to implement this with declares or start with COM? I have been working through online examples in the Forum and on the Xojo Blog with COM and am having a tough time understanding the implementation of COM. Before I get too far, which is the correct path to work with DirectX 9.0c with Xojo on Windows: Declare, COM, or something else?

    Below is the code which retrieves the pointer to a IDIRECT3D9 interface which is COM. I am trying to work through this in small steps :) Any help is appreciated. My guess is that I am going to need to expose the IDIRECT3D9 interface through COM.

    I have a running VB6 project that works with DirectX9.0c on Windows 10, and am using it as a guideline to develop a simple triangle program.

    Sub Action() Handles Action
      Dim D3D_SDK_VERSION as Int32 = 32 //32 = version 9.0c
      Dim d3d9 as Integer = Direct3DCreate9(D3D_SDK_VERSION)
      If d3d9 <> 0 Then
        System.DebugLog "Direct3DCreate9 interface correct"
      Else
        System.DebugLog "Direct3DCreate9 error. Check for version DirectX 9.0c"
      End If
      
      Dim pp as D3DPRESENT_PARAMETERS
      //Set Vertex format
      Dim vFlag as UInt32 //D3DFVF constants
      vFlag = D3DFVF_DIFFUSE + D3DFVF_XYZRHW
      
      pp.BackBufferCount = 1
      pp.Windowed = True
      pp.BackBufferFormat = D3DFMT_A8R8G8B8
      pp.SwapEffect = D3DSWAPEFFECT_DISCARD
      
      //CreateDevice
      Dim RetVal as Integer
      Dim mb as New MemoryBlock(8)
      RetVal = CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window1.Handle, D3DCREATE_HARDWARE_VERTEXPROCESSING, pp, mb)
      If RetVal = S_OK Then
        System.DebugLog "CreateDevice was successful"
      Else
        System.DebugLog "CreateDevice failed"
      End If
      System.DebugLog "Continue with more code"  
    End Sub

    Here is the Xojo code project that I started.

    Example1-1

    Let me know what you would like and I am willing to share all information that I have.

    I will be away from my development computer for the next 12 hours or so and will not have access to the forums. Thanks :)

    I woke up early and decided to give this a crack. Here's the code for CreateDevice

    https://www.dropbox.com/s/lccifs9enn53b3p/TestD3D9.xojo_binary_project?dl=1

    I didn't implement the constants for things as I was just getting things working as quickly as I could, I just moused over the ones you set and copied the values so that's where they came from if you're wondering what the values are.

    I also double checked and corrected the structure, the Booleans should always be Int32 (as per my site) to make sure that the sizes and alignments are correct so you'll need to CType those as shown. Also checked in 32/64, works fine.

    Hope it helps. Let me know if you get stuck further in.

  2. Julian S

    3 Jan 2019 Pre-Release Testers, Xojo Pro UK

    You'll need to implement the interfaces and use them via COM.

    I'll email you off forum as I have directx9 source in a competing product that will reduce your workload.

  3. 6 months ago

    Eugene D

    4 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    Thanks for the additional information Julian, as this will help. Do you or someone know how to implement COM interfaces in Xojo? I am going to try and understand a small portion of the VB6 code for Xojo.

    Here is a snippet of Visual Basic 6 code:

    Dim d3d9 As IDirect3D9
    Dim d3dev   As IDirect3DDevice9
    Set d3d9 = Direct3DCreate9()
    Set d3dev = d3d9.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, pp)

    Here is some incomplete Xojo code:

    //How to implement an interface?
    Dim IID_IDirect3D9 as MemoryBlock = COM.IIDFromString("{81BDCBCA-64D4-426d-AE8D-AD0147F4275C}")
    Dim d3d9 as MemoryBlock = IID_IDirect3D9
    Dim IID_IDirect3DDevice9 as MemoryBlock = COM.IIDFromString("{D0223B96-BF7A-43fd-92BD-A43B0D82B9EB}")
    Dim d3dev as MemoryBlock = IID_IDirect3DDevice9
    //More methods to be defined
    d3d9 = Direct3DCreate9(32)
    //Defined in Structures section
    Dim pp as D3DPRESENT_PARAMETERS 
    Dim RetVal as MemoryBlock
    RetVal = d3d9.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window1.Handle, D3DCREATE_HARDWARE_VERTEXPROCESSING, pp, mb)

    The ID values were taken from the d3d9 header file. I tried to find an OLE Object by looking up the ID in the RegEdit program and cross referencing it with the OLE/COM object viewer program (OLEView.exe) and there was no reference that I could find.

    Examples in the Xojo Documentation all seem to provide examples that use the OLEObject: IUnknown , and I can't seem to track it down to call the QueryInterface method to ensure the interface exists.

    Any help is appreciated, and my first step is to figure out how to make a interface in COM with Xojo when the name of a OLEObject is not available :)

  4. Julian S

    5 Jan 2019 Pre-Release Testers, Xojo Pro UK

    It's been a year since I wrote this, it partially sets up and uses IPropertyStorage which will be similar to what you need to do with IDirect3D9

    https://www.dropbox.com/s/uzw7qutc5iceten/DisableTouchWhenFullscreen.xojo_binary_project?dl=1

    Each step is commented inside DisableTouchWhenFullscreen

    As its late binding you need to set up the functions you need with reference to their positions in the virtual table. The guids and offsets required are all in the header files if you install the Windows 10 SDK

    When you install that, find propsys.h, find IPropertyStoreVtbl inside there and just above that you'll see what I'm doing for the offsets of setValueFunc and releaseFunc. Count down from the top of typedef struct IPropertyStoreVtbl, 0 1 2 3 4 5 6 7. Those are the pointer offsets for those function. I'm only using 6 SetValue and 2 Release in the code but its the same principal for all the rest.

    Now find d3d9.h and it will have all the juicy info, guids, layouts of interfaces etc. If you search straight to the first QueryInterface you'll see the interface declaration there. All the data types are listed there too, ready for conversion into xojo types, just count down the list for the ptr references, 0 1 2 then 4 5 6 7 8 etc. down to 16 for CreateDevice.

    I'm in a bit of a new year funk at the moment as I'm getting a bit despondent with xojo. If I snap out of it I'll see if I can put a d3d demo together.

  5. Julian S

    9 Jan 2019 Pre-Release Testers, Xojo Pro Answer UK

    I woke up early and decided to give this a crack. Here's the code for CreateDevice

    https://www.dropbox.com/s/lccifs9enn53b3p/TestD3D9.xojo_binary_project?dl=1

    I didn't implement the constants for things as I was just getting things working as quickly as I could, I just moused over the ones you set and copied the values so that's where they came from if you're wondering what the values are.

    I also double checked and corrected the structure, the Booleans should always be Int32 (as per my site) to make sure that the sizes and alignments are correct so you'll need to CType those as shown. Also checked in 32/64, works fine.

    Hope it helps. Let me know if you get stuck further in.

  6. Julian S

    10 Jan 2019 Pre-Release Testers, Xojo Pro UK

    Here's a slightly neater version

    https://www.dropbox.com/s/22seozna3qhh8ry/TestD3D9v2.xojo_binary_project?dl=1

  7. Eugene D

    10 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    Hi Julian,

    This is so helpful, chuckle, I can't express it in words, other than saying THANK YOU!!! I have literally been playing with this for years and have been stuck at the same spot. This is the code that I needed to get me going on the graphics journey.

    Once again, thank you!

  8. Tim K

    10 Jan 2019 Pre-Release Testers, Xojo Pro

    Looking forward to the New Book (chapter) on COM interfacing ;)

  9. Joost R

    11 Jan 2019 Pre-Release Testers, Xojo Pro The Netherlands

    @Eugene D - in your most recent book (about declares) you mentioned @Julian S twice in the Dedication on page 3. You must have foreseen you would need him later .... :) :)

  10. Eugene D

    11 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    @Tim K Looking forward to the New Book (chapter) on COM interfacing ;)

    My only next step is that I need to understand it first :)

  11. Eugene D

    11 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    @Joost R @Eugene D - in your most recent book (about declares) you mentioned @Julian S twice in the Dedication on page 3. You must have foreseen you would need him later .... :) :)

    Ahh yes, I did mention him twice. Your right, maybe it was foreshadowing. :)

  12. Steve K

    11 Jan 2019 Melbourne, Australia

    @Eugene D Ahh yes, I did mention him twice. Your right, maybe it was foreshadowing. :)

    Or a "self fulfilling prophecy"... :)

  13. Julian S

    12 Jan 2019 Pre-Release Testers, Xojo Pro UK
    Edited 6 months ago

    You are more than welcome Eugene. Ignore the two previous versions, they are wrong in some places. Here's a better version I was working on the day after I posted the above code but I didn't get chance to post this. I think you're going to have problems from the D2D paint that happens from the framework (drag the window off the screen after hitting the button) as it works nicely pre-D2D, it might need a WndProc to bypass all that, I'm just thinking out loud now as I've not really though about it that in depth.

    https://www.dropbox.com/s/q8cf22l6vrk1upk/TestD3D9v3.xojo_binary_project?dl=1

  14. Eugene D

    12 Jan 2019 Pre-Release Testers, Xojo Pro Canada scispec.ca

    Thanks for the update Julian, this is greatly appreciated.

    I modified the CreateDevice code and instead of passing an IDRECT3DDevice 9 parameters, I am passing a pointer to the CreateDevice method. This modification has changed the code in the CreateDevice method and the parameter for the CreateDevice_Func delegate. I am getting a valid memory pointer address and then create the dedev variable after the CreateDevice call. The next step is to work on the CreateVertexBuffer method, which has partially been written in the attached code.

    Thanks for the update in the project, and I'll go through it and understand the changes. This is great! Thankyou!

    Example1-1.xojo_binary_project

  15. Julian S

    12 Jan 2019 Pre-Release Testers, Xojo Pro UK

    The v3 example hopefully shows and opens up IDirect3DDevice9 nicely for you. CreateVertexBuffer is 26 * COM.SIZEOF_PTR

    If you redownload v3 I've added a note for reference and cleaned up a few things.

or Sign Up to reply!