Author Topic: Directx 9 last shot (at least for now)  (Read 21490 times)

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Directx 9 last shot (at least for now)
« Reply #45 on: April 19, 2018, 08:53:07 PM »
Since you are not using macros you might be able to save a few bytes and cycles across repeated calls

Now you make me curious, José! How exactly can you save bytes, or gain speed, as compared to a coinvoke macro?

aw27

  • Guest
Re: Directx 9 last shot (at least for now)
« Reply #46 on: April 19, 2018, 09:28:22 PM »
You save two instructions on a second call:

1st call
push    0                   ; push the parameters
push    FLT4(1.0)
push    BackgroundColor
push    D3DCLEAR_TARGET
push    NULL
push    0
mov     edi,g_pD3DDevice    ; load the com interface
push    edi                 ; push it
mov     ebx,[edi]           ; get the pointer from the interface pointer
call    dword ptr[ebx+IDirect3DDevice9_Clear] ; call the com interface method

2nd call
push    0                   ; push the parameters
push    FLT4(1.0)
push    BackgroundColor
push    D3DCLEAR_TARGET
push    NULL
push    0
; NOT NEEDED NOW mov     edi,g_pD3DDevice    ; load the com interface
push    edi                 ; push it
;NOT NEEDED NOW mov     ebx,[edi]           ; get the pointer from the interface pointer
call    dword ptr[ebx+IDirect3DDevice9_Clear] ; call the com interface method

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Directx 9 last shot (at least for now)
« Reply #47 on: April 19, 2018, 09:43:40 PM »
OK, I see. I've tested another example with my current CoInvoke macro:
Code: [Select]
pInterface equ esi
INT 3
CoInvoke pInterface, IWebBrowserVtbl.put_MenuBar, VARIANT_TRUE ; false = no menu
nops 4

This translates to:
Code: [Select]
int3
push 0FFFF                  ; VARIANT_TRUE
push esi                    ; pInterface
mov eax, [esi]
call near [eax+0C4]         ; method
nop

So, when using the interface a second time, one could save the mov eax, [esi] step and use once mov ebx, [esi] instead. That saves two bytes, but you need a non-volatile register like ebx, so two bytes for push+pop. Which means you can save two bytes each for the third, fourth, ... use of the interface. In short: You are right, one can save some bytes by coding this stuff by hand :t

Siekmanski

  • Member
  • *****
  • Posts: 2725
Re: Directx 9 last shot (at least for now)
« Reply #48 on: April 19, 2018, 09:51:40 PM »
This technique is commonly used in the Demo scene to save bytes when coding size limited executables.
Group everything that's possible and use one pointer for the same interface.  8)
Creative coders use backward thinking techniques as a strategy.

Siekmanski

  • Member
  • *****
  • Posts: 2725
Re: Directx 9 last shot (at least for now)
« Reply #49 on: April 19, 2018, 10:07:03 PM »
Not every COM model is the same.
ASIO audio COM interface wants the pointer from the interface pointer in ECX

Code: [Select]
    mov ecx,pASIO_Device
    mov ecx,[ecx]
    mov eax,[ecx]
    call dword ptr[eax+Function]
Creative coders use backward thinking techniques as a strategy.

felipe

  • Member
  • *****
  • Posts: 1381
Re: Directx 9 last shot (at least for now)
« Reply #50 on: April 20, 2018, 01:38:17 AM »
I guess i understand what you say aw27, but my question still remains in this form:

;1st call
mov     edi,g_pD3DDevice    ; load the com interface
push    edi                 ; push it here because is not a parameter for this method.

push    0                   ; push the parameters
push    FLT4(1.0)
push    BackgroundColor
push    D3DCLEAR_TARGET
push    NULL
push    0

mov     ebx,[edi]           ; get the pointer from the interface pointer
call    dword ptr[ebx+IDirect3DDevice9_Clear] ; call the com interface method

;2nd call
; NOT NEEDED NOW mov     edi,g_pD3DDevice    ; load the com interface
;push    edi                 ; No need to push it here, if you don't trash the register.

push    0                   ; push the parameters
push    FLT4(1.0)
push    BackgroundColor
push    D3DCLEAR_TARGET
push    NULL
push    0

;NOT NEEDED NOW mov     ebx,[edi]           ; get the pointer from the interface pointer
call    dword ptr[ebx+IDirect3DDevice9_Clear] ; call the com interface method

Isn't this right? What i'm missing of the call to the methods here?

aw27

  • Guest
Re: Directx 9 last shot (at least for now)
« Reply #51 on: April 20, 2018, 01:51:56 AM »
Quote
push it here because is not a parameter for this method.

No, no, you are not pushing it to preserve edi. You can do that elsewhere, in the beginning of the function for example, or don't preserve at all if you know what you are doing because it is a standalone ASM application not called from anywhere.

It is indeed a parameter for the method as you can see from the http://masm32.com/board/index.php?topic=7078.msg76110#msg76110
mov     eax,g_pD3DDevice    ; load the com interface
push    eax

I replaced eax with edi because I know that the call to the interface method will not corrupt edi since it has the obligation to preserve non-volatile registers.




felipe

  • Member
  • *****
  • Posts: 1381
Re: Directx 9 last shot (at least for now)
« Reply #52 on: April 20, 2018, 02:14:24 AM »
It's a little confusing this  documentation from the directx 9 sdk (this method call as an example):

Quote
IDirect3DDevice9::BeginScene Method
Begins a scene.

Syntax
HRESULT BeginScene();

Parameters
This method has no parameters.
Return Value
HRESULT

And siekmanski code even confirm the no parameters for this method call:
Quote
Code: [Select]
; no parameters
    mov     eax,g_pD3DDevice    ; load the com interface
    push    eax                 ; push it
    mov     eax,[eax]           ; get the pointer from the interface pointer
    call    dword ptr[eax+IDirect3DDevice9_BeginScene] ; call the com interface method

And in this sdk documentation i haven't seen the pointer to the interface as a parameter yet in any method call. It's something only know to assembly language programmers  :biggrin:? I can't find the explanation of this in that link either. Thanks for all the help, seriously.  :greenclp:

felipe

  • Member
  • *****
  • Posts: 1381
Re: Directx 9 last shot (at least for now)
« Reply #53 on: April 20, 2018, 02:26:25 AM »
Is something related to the headers file from the sdk? I'm not well versed in that idiom, but i have found lines like this (probably for every method):
Quote
#define IDirect3D9_CreateDevice(p,a,b,c,d,e,f) (p)->lpVtbl->CreateDevice(p,a,b,c,d,e,f)
So that p is the pointer defined implicitly?

aw27

  • Guest
Re: Directx 9 last shot (at least for now)
« Reply #54 on: April 20, 2018, 02:55:01 AM »
You need pass a "THIS" pointer, so you need as a minimum (according to my example, 2nd call):
push    edi                 ; push it
call    dword ptr[ebx+IDirect3DDevice9_BeginScene] ; call the com interface method

Have a look here and see if it makes some light. If not, keep trying this is not easy at all.


jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Directx 9 last shot (at least for now)
« Reply #55 on: April 20, 2018, 03:48:37 AM »
Ernie explains lots of things. If you have less time, try Mysteries of COM 8)

daydreamer

  • Member
  • *****
  • Posts: 2399
  • my kind of REAL10 Blonde
Re: Directx 9 last shot (at least for now)
« Reply #56 on: April 20, 2018, 04:07:31 AM »
I have seen Hitchhikers 1k demos in old forum, so to be able to save size I could make a minimum .inc file like that, so I could be able to post some real d3d9-11 game, so instead of having large lib files included in 512kb I have place for textures instead
but what about you initialize dx adresses to your .data section with most used first and least used last, the 20 most used


my none asm creations
http://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

felipe

  • Member
  • *****
  • Posts: 1381
Re: Directx 9 last shot (at least for now)
« Reply #57 on: April 20, 2018, 04:25:24 AM »
 :greenclp: Now this comment:
Quote
; get the pointer from the interface pointer
has finally perfectly sense to me.  :bgrin:

Now one might ask the developers of COM why CoCreateInstance needs the address of a variable to deliver pInterface, instead of simply returning it in eax. The answer will be "it's by design" or "why do something simple if there is a more complicated way?". Redmond speaking... 8)

I was asking me the same from the article above. Well now, thanks to the genius programmers from the best forum in the world, it really doesn't matter. :greensml:

Siekmanski

  • Member
  • *****
  • Posts: 2725
Re: Directx 9 last shot (at least for now)
« Reply #58 on: April 20, 2018, 04:52:26 AM »
I should have commented it like this,

    mov     eax,g_pD3DDevice    ; load the com interface
    push    eax                          ; push it
    mov     eax,[eax]                 ; get the vtable pointer from the interface pointer
    call      dword ptr[eax+IDirect3DDevice9_BeginScene]      ; call the com interface method function.
Creative coders use backward thinking techniques as a strategy.

Siekmanski

  • Member
  • *****
  • Posts: 2725
Re: Directx 9 last shot (at least for now)
« Reply #59 on: April 20, 2018, 05:18:08 AM »
Hi daydreamer,
You have to set up the screen first and it delivers you the interface address in return.
Setting up the video device, same story etc.
No need to use CoCreateInstance in DX.
For the function Direct3DCreate9 there is no GUID available to get the interface address with CoCreateInstance.

Quote
I have seen Hitchhikers 1k demos in old forum, so to be able to save size I could make a minimum .inc file like that, so I could be able to post some real d3d9-11 game, so instead of having large lib files included in 512kb I have place for textures instead
but what about you initialize dx adresses to your .data section with most used first and least used last, the 20 most used

We don't use static libs anymore.
Do you trust Microsoft the addresses will be the same in next operating systems?
Creative coders use backward thinking techniques as a strategy.