flat assembler
Message board for the users of flat assembler.

Index > Windows > [SOLVED] Fasm & D3D. can anyone help?

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 02 Feb 2018, 14:23
Can anyone help with D3D?
In attachment fasm root dir with only one example.
All includes shortened only for successfull compilation of that example or for displaying of testing new syntax for implementing interfaces.

I got workable exe with no faults. But...

I succed to Direct3DCreate9, succed to IDirect3D9.GetAdapterDisplayMode, and fault to IDirect3D9.CreateDevice.

src file looks like:
Code:
format PE GUI 4.0
entry main.start

include 'win32aII.inc'
include '..\INTERFACES\directx\macro.inc'
include '..\INTERFACES\FUNDAMENTALS\IUNKNOWN.inc'
include '..\INTERFACES\directx\d3d9.inc'
include '\directx\d3dx9types.inc'
include '\directx\d3dtypes.inc'

WINDOWWIDTH  = 640
WINDOWHEIGHT = 480
D3D9b_SDK_VERSION = 31



section '.data' data readable writeable
  flt_1              dd 1.0
  wc                 WNDCLASSEX sizeof.WNDCLASSEX, CS_HREDRAW or CS_VREDRAW, WndProc,0,0,0,0,0,BLACK_PEN,0,szAppName,0
  szClassName        TCHAR 'demoD3D', 0
  szAppName          TCHAR '[01] Init D3D', 0
  szIsFullScreen     TCHAR 'Fullscreen mode ?', 0
  errCreateD3D       TCHAR 'Direct3DCreate failed !', 0
  errGetAdapterDM    TCHAR 'GetAdapterDisplayMode failed !', 0
  errCreateDevice    TCHAR 'CreateDevice failed !', 0
  align              4
  ;hwndmain           dd ?
  ;hdc                dd ?
  d3dpp              D3DPRESENT_PARAMETERS 0,0,0,2,0,0,D3DSWAPEFFECT_DISCARD,0,0,1,D3DFMT_D16,0,0,0
  DisplayMode        D3DDISPLAYMODE
  d3d9               IDirect3D9
  device             IDirect3DDevice9
  rect               RECT
  msg                MSG
  bReady             dd ?

section '.code' code readable executable
  main:
   .start:
     invoke  GetModuleHandle, 0
     mov     [wc.hInstance], eax
     invoke  RegisterClassEx, wc

     invoke  MessageBox, 0, szIsFullScreen, szAppName, MB_YESNO
     xor     eax,IDYES
     mov     [d3dpp.Windowed],eax
     mov     ecx, WS_POPUP or WS_VISIBLE
     jz      @F
     mov     ecx, WS_OVERLAPPEDWINDOW or WS_VISIBLE
   @@:
     invoke  CreateWindowEx, NULL, szClassName, szAppName, ecx,\
             100, 75, WINDOWWIDTH, WINDOWHEIGHT, NULL, NULL, [wc.hInstance],NULL
     mov     [d3dpp.hDeviceWindow], eax

     invoke  ShowWindow, [d3dpp.hDeviceWindow], SW_SHOWNORMAL
     invoke  UpdateWindow, [d3dpp.hDeviceWindow]

     stdcall InitDirect3D
     or      eax,eax
     jz      .init_ok
     invoke  MessageBox, [d3dpp.hDeviceWindow], eax, szAppName, MB_OK
     invoke  SendMessage, [d3dpp.hDeviceWindow], WM_CLOSE, 0, 0
     jmp     .msgLoop
   .init_ok:
     inc     [bReady]
   .msgLoop:
     mov     eax,[msg.message]
     cmp     eax, WM_QUIT
     jz      .exitLoop
     invoke  PeekMessage, msg, NULL, 0, 0, PM_REMOVE
     or      eax,eax
     jz      .idleuse
     invoke  TranslateMessage, msg
     invoke  DispatchMessage, msg
     jmp     .msgLoop
   .idleuse:
     mov     eax, [bReady]
     or      eax,eax
     jz      .msgLoop
     stdcall RenderScene
     jmp     .msgLoop
   .error:
     invoke  MessageBox, [d3dpp.hDeviceWindow], eax, szAppName, MB_OK
   .exitLoop:
     stdcall DestroyDirect3D
     mov     eax,[msg.wParam]
     invoke  ExitProcess, eax

proc WndProc hWnd, uMsg, wParam, lParam
     push    esi edi ebx
     mov     eax, [uMsg]
     cmp     eax, WM_KEYDOWN
     jz      .wmkeydown
     cmp     eax, WM_SETCURSOR
     jz      .wmsetcursor
     cmp     eax, WM_DESTROY
     jz      .wmdestroy
     cmp     eax, WM_CLOSE
     jnz     .def
     xor     eax, eax
     mov     [bReady], eax
   .def:
     invoke  DefWindowProc, [hWnd], [uMsg], [wParam], [lParam]
     jmp     .RetProc
   .wmkeydown:
     mov     eax, [wParam]
     cmp     eax, VK_ESCAPE
     jnz     .def
     invoke  SendMessage, [hWnd], WM_CLOSE, NULL, NULL
     jmp     .ExitProc
   .wmsetcursor:
     mov     eax,[d3dpp.Windowed]
     or      eax, eax
     jnz     .def
     invoke  SetCursor, NULL
     jmp     .ExitProc
   .wmdestroy:
     invoke  PostQuitMessage, NULL
   .ExitProc:
     xor     eax, eax
   .RetProc:
     pop     ebx edi esi
     ret
endp

proc InitDirect3D
     invoke  Direct3DCreate9, D3D9b_SDK_VERSION
     or      eax,eax
     jnz     @F
     mov     eax,errCreateD3D
     jmp     main.error
   @@:
     mov     [d3d9],eax
     comcall [d3d9],IDirect3D9,GetAdapterDisplayMode, NULL, DisplayMode
     or      eax,eax
     jz     @F
     mov     eax,errGetAdapterDM
     jmp     main.error
   @@:
     mov     eax,[DisplayMode.Format]
     mov     [d3dpp.BackBufferFormat],eax

     mov     eax,[d3dpp.Windowed]
     or      eax,eax
     jnz     @F
     mov     [d3dpp.SwapEffect], D3DSWAPEFFECT_FLIP
   @@:
     invoke  GetClientRect, [d3dpp.hDeviceWindow], rect
     mov     eax, [rect.right]
     mov     [d3dpp.BackBufferWidth], eax                                          ; ñîõðàíÿåì â ñòðóêòóðå D3DPRESENT_PARAMETERS
     mov     eax, [rect.bottom]
     mov     [d3dpp.BackBufferHeight], eax

     comcall [d3d9],IDirect3D9,CreateDevice, NULL, D3DDEVTYPE_HAL, [d3dpp.hDeviceWindow], \
                                            D3DCREATE_SOFTWARE_VERTEXPROCESSING,d3dpp,device
     or      eax,eax
     jz     @F
     mov     eax,errCreateDevice
     jmp     main.error
   @@:
     ret
endp

proc DestroyDirect3D
     mov     eax,[device]
     or      eax,eax
     jz      @F
     comcall eax,IDirect3DDevice9,Release
     xor     eax,eax
     mov     [device],eax
   @@:
     mov     eax,[d3d9]
     or      eax,eax
     jz      @F
     comcall eax,IDirect3DDevice9,Release
     xor     eax,eax
     mov     [d3d9],eax
   @@:
     ret
endp

proc RenderScene
     comcall [device],IDirect3DDevice9,TestCooperativeLevel
     cmp     eax,D3DERR_DEVICELOST                                             ; åñëè óñòðîéñòâî ïîòåðÿíî
     jz      @render_exit
     cmp     eax, D3DERR_DEVICENOTRESET                                             ; åñëè óñòðîéñòâî ïîòåðÿíî
     jnz     @reseted
     comcall [device],IDirect3DDevice9,Reset,d3dpp
   @reseted:
     comcall [device],IDirect3DDevice9,Clear,0,NULL,D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER,$808080,flt_1,0
     comcall [device],IDirect3DDevice9,BeginScene
     nop
     comcall [device],IDirect3DDevice9,EndScene
     comcall [device],IDirect3DDevice9,Present, NULL, NULL, NULL, NULL
   @render_exit:
     ret
endp

section '.idata' import data readable writeable
  library  kernel32, 'KERNEL32.DLL',\                                        ; Èñïîëüçóåìûå DLL
           user32,   'USER32.DLL',\
           gdi32,    'GDI32.DLL',\
           d3d9,     'D3D9.DLL'

     include  'api\kernel32.inc'
     include  'api\user32.inc'
     include  'api\gdi32.inc'
     include  'api\directx\d3d9.inc'       

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.


Last edited by ProMiNick on 03 Feb 2018, 20:24; edited 1 time in total
Post 02 Feb 2018, 14:23
View user's profile Send private message Send e-mail Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 02 Feb 2018, 22:48
ProMiNick wrote:
I succed to Direct3DCreate9, succed to IDirect3D9.GetAdapterDisplayMode, and fault to IDirect3D9.CreateDevice.

Your program fails to CreateWindowEx with GetLastError = ERROR_CANNOT_FIND_WND_CLASS. You have an error in your WNDCLASSEX initialization: you specify szAppName as window class name; should be szClassName.

So, when Direct3D comes in play and tries to initialize on your window, it fails ’cause there’s no window to use.

Note also that there’s plenty of other mistakes and smells in your code. For example, this line:
Code:
     comcall [device],IDirect3DDevice9,Clear,0,NULL,D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER,$808080,flt_1,0    

will use address of flt_1 dword instead of actual value of 1.0. And it might not even be a valid float. Just replace it with 1.0.

Another mistake is that you use BLACK_PEN in your WNDCLASSEX initialization. This constant is not intended to be used here: if you read MSDN carefully, you’ll find out that you should use COLOR_* constant plus 1 here. BLACK_PEN equals 7 equals COLOR_WINDOWFRAME+1 and that is how Windows sees your WNDCLASSEX.

Besides, you’re going to redraw the whole client area anyway, so I’d suggest using 0 to avoid automatic background erasing.
Post 02 Feb 2018, 22:48
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20623
Location: In your JS exploiting you and your system
revolution 03 Feb 2018, 04:19
DimonSoft wrote:
Your program fails to CreateWindowEx with GetLastError = ERROR_CANNOT_FIND_WND_CLASS.
The assumption of success is always dangerous. there are many things that can cause an API call to fail, not just bad input parameters. Even if the code guarantees to always provide correct parameters programmers should still check the return code in case some other problem has occurred out of their control.

There are many places in that code where the return codes are ignored, so there are many places where this code can fail. And thus many opportunities to have hard to debug problems that seem to defy logic.
Post 03 Feb 2018, 04:19
View user's profile Send private message Visit poster's website Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 03 Feb 2018, 20:23
Thanks for help.
Later I will create examples of using Direct3D antology (from DX5 to DX11, I got SDKs of theese) (maybe & use of DX12) and I will share it with community in my pack fasm&fasmgAllinOne.
Structuring & naming in SDKs I dislike, so I create my own: common incs for equates and structures that stay unchanged from version to version, all the rest maybe in single inc for each version, or maybe splited to equates & structs.
My goal for future is carcas for 3D app, with selecting bitness (x86/x64) and engine (pure GDI, OpenGL, DX & version of DX) and examples of same things realized in different engines.

And one more question: thread switching is slow? is it more effective realize in main thread thread-like-management: split code parts in minimal logic complete parts and query them?
Post 03 Feb 2018, 20:23
View user's profile Send private message Send e-mail Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 05 Feb 2018, 09:24
ProMiNick wrote:
And one more question: thread switching is slow? is it more effective realize in main thread thread-like-management: split code parts in minimal logic complete parts and query them?

Thread switching might be slow but it is out of your control anyway.

Multithreading is generally a bad idea when it comes to UI drawing since a lot of poorly documented dark corners of Windows history come into play. That’s why it is generally considered to be a good idea to have a single UI thread in your application, although you can have multiple helper threads which interact with the UI thread.

You’d better describe what you mean by “split code in minimal logic complete parts and query them” in a more detailed manner. Otherwise, I don’t see how it differs from just splitting a program into procedures and calling them, which is not necessarily a thread-like management. Maybe you mean something like fibers but I think you’ll choose to avoid using them.
Post 05 Feb 2018, 09:24
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.