News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Seeking friendly collaborator

Started by mikorians, October 19, 2012, 03:29:02 AM

Previous topic - Next topic

mikorians

I think what we need to reach common ground with FreeBasic would be an example of Dx7 or Dx8 usage.  Dx9 is really Dx10 in FB.
Hutch advised that older versions run faster on older machines.
Besides, it has viable vb6 dll examples...

Other esoteric features my engine uses...
ForceFeedback
DirectShow - for intermission scenes - like avi, mp3.  Works well enough in DX8.
Use of material alpha channel for object translucency.

TouEnMasm

Quote
I think what we need to reach common ground with FreeBasic would be an example of Dx7 or Dx8 usage.  Dx9 is really Dx10 in FB.
Dx10 is for WIN7.
i have the dx 9.C and he is in use with most of the game.
Have you tested the freebasic dx sample of wich I speak.
if just a crash satisfy you ....
Fa is a musical note to play with CL

mikorians

Yeah...  Compiles, but cannot run it "DLL missing".  Downloaded dll.  Run program. "DLL is not for your version of windows"
But look in the include\win folder.  D3D.bi

TouEnMasm

Quote
"DLL is not for your version of windows"
You need the directx sdk,x version
If  it say you, d3dx9d.dll is missing ,made this:
in windows\system32 directorie search (dir d3dx9d*.dll) for d3dx9d*.dll
You wil find some:
Quote
02/06/2010  14:23         2 261 336 d3dx9d.dll         ;don't exist
02/06/2010  14:23         3 795 800 d3dx9d_33.dll
02/06/2010  14:23         2 261 336 D3dx9d_43.dll
then:
copy d3dx9d_nn.dll d3dx9d.dll

And all is good.
Fa is a musical note to play with CL

mikorians

Sigh...  Older DX version would be better...
[Update] Yeah.  Above dx9 sample's no good because the library file refers to dx10.
I copied the relevant files you suggested, and if just says I need a newer version of windows, contact my vendor.

MichaelW

I have not had much time to look at any of this but it looks like the FreeBASIC 0.24.0 distribution also includes the D3DX8 components. And given the target DLL, it's possible to create your own import library, with the DLL name tailored to whatever you need. The difficult part is the header files.
Well Microsoft, here's another nice mess you've gotten us into.

mikorians

That would be good since 8 is what I'm most familiar with.   :greenclp:
I looked at 7, but all I could find in my archives were some examples, but not the all-important help files or sdk.exe   :redface:
Dx7 seems to use a different method to do texture transformations than 8.  Hm...

MichaelW

I found a DX8.1 VB SDK on one of my hard drives so I installed it and VB6 on my Windows XP SP3 system. The system already had a more recent version of DX installed, but I wanted the SDK for the documentation and sample code. Using MASM and Link I created a DLL that contains an assembly version of one of the functions in the VB D3Dframework utility module, and created a VB6 project to test and time it against the VB function. I made no special effort to optimize the assembly code, so it may be nowhere near the fastest possible. I'm not sure that this is all correct, but it appears to work as expected and I'm out of time to work on it. For the MASM test app I used the import library, but for the VB project only the DLL was required. After trying the project and System32 directories, I determined that the DLL had to be in the VB98 directory. Hopefully I included everything necessary in the attachment.

I used 1000000 loops timing the code so I could get reasonable repeatability on my 3GHz P4 Northwood. For slower systems you may need to reduce the loop count.

Well Microsoft, here's another nice mess you've gotten us into.

mikorians

Thank you for going through all of the trouble for this old antique.  I haven't yet heard much from the FreeBasic site, though I was wishing to.
It's good to hear from you.
So calling a dll from VB6...  would seem quite worth it --- same function, but much faster.
Granted, it's just a color function, but it is interesting.
How shall we proceed now?
Still dazed...
If you need anything from me, let me know.  I had all the old Dx8 stuff...

[Update]
I looked at the .asm of what you just did.  You're kinder than I thought.  You didn't call the API, you rewrote the function completely.
We'll still need to access the vertex buffer and rendering API (HW accelerator functions) of course.
Probably not too much to be gained there, but with Asm, who can say for sure?
It seems to trim the fat in places one doesn't expect.
On my machine we got 1.81 secs and .059 or something...  So 30x faster.       It read 1.6  .059 when compiled, 27x.
So as you can see on my system, compiled or interpreted, little difference.

[Update 2]
Got a taste on my friends computer (only slightly newer) of something a bit faster.
He had a NVidia 8800gts and a Core2Extreme 2.93mhz (X6800) with XP Pro SP3 on it, and my program runs about 10x faster with no optimization at all...   Wow.
Wish I could use it.  There are cards that are 10x faster than his too.  But we'd all be just throwing money at a problem that should be solved first before hardware IMHO...  Since money's real tight with my bills.

MichaelW

The attachment here compares the assembly procedure in the DLL to the VB function source as compiled by FreeBASIC. I'm not sure how well (or if) the cycle count macro will work on your older system. Assuming it will work, you will need to reduce the loop count to something that will run in a reasonable time, and you may need to reduce the priority to keep it from crashing Windows.
Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Celeron M:
  0 cycles
62 cycles
23 cycles


P.S.: No timings for the second function?

Function D3DCOLORVALUETOLONG(ByRef cv As D3DCOLORVALUE) As Long
function D3DCOLORVALUETOLONG_( byref cv as D3DCOLORVALUE ) as integer

mikorians

Nope.  No crash.  Ran Test.exe--
0
102
42

FB took >twice as long as raw Asm.
And I haven't had much help with the folks on the forum at freebasic regarding DirectX.  They're like, obsessed with OpenGL-
Which I wouldn't personally choose.  I know it's cross-platform, but I tried it many years ago and didn't like it.  Picked standard DirectX for me.  :t
Fixed Test.bas to reflect _ function also...

99
102
42

Simple benchmark acknowledged.
So, remembering the former test...   Raw Asm was...   2.36x faster than FreeBasic and 30x faster than the DirectX DLL on the D3DColorValueToLong function on my machine.  Whoop.  Just noticed you compiled the raw asm in FreeBasic, not Masm32.  Same same, mm?

Which raw DirectX8 graphics functions can we not improve?  Why did someone not make a nice, generic optimized library a long time ago?  Even more modern XP users can still benefit from this technology (just a different DX9/10 label set, yes?)  Let us continue with this function set.

Enjoying this sweet meditation...  Ah...

MichaelW

QuoteJust noticed you compiled the raw asm in FreeBasic, not Masm32. Same same, mm?

The FreeBASIC app above calls the function in the DLL that was built with MASM. The .asm file included in the attachment is the FreeBASIC compiler output (using the command line fbc -w pedantic -v -C -R  -s console test.bas) , that is assembled with as (aka GAS) to produce an object module, that is then linked with ld (the GNU linker), using a long and complex command line, to produce an EXE.

But I can port the MASM code to a naked function, with only a few changes, and while as may not encode the instructions exactly as MASM would, the assembled code will be functionally equivalent to the code that MASM produces.


''===================================================================================
#include "counter.bas"
''===================================================================================
''
'' The newer cycle count macros are available here:
''
''    http://www.freebasic.net/forum/viewtopic.php?f=7&t=20003
''
''===================================================================================

Type D3DCOLORVALUE
    r As Single
    g As Single
    b As Single
    a As Single
End Type

extern "Windows-MS"
Declare Function D3DCOLORVALUETOLONGASM Lib "testdll" (ByRef cv As D3DCOLORVALUE) As Long
end extern

Function D3DCOLORVALUETOLONG(ByRef cv As D3DCOLORVALUE) As Long

    Dim r As Long
    Dim g As Long
    Dim b As Long
    Dim a As Long
    Dim c As Long

    r = cv.r * 255
    g = cv.g * 255
    b = cv.b * 255
    a = cv.a * 255

    If a > 127 Then
        a = a - 128
        c = a * 2 ^ 24 Or &H80000000
        c = c Or r * 2 ^ 16
        c = c Or g * 2 ^ 8
        c = c Or b
    Else
        c = a * 2 ^ 24
        c = c Or r * 2 ^ 16
        c = c Or g * 2 ^ 8
        c = c Or b
    End If

    D3DCOLORVALUETOLONG = c

End Function

function D3DCOLORVALUETOLONG_( byref cv as D3DCOLORVALUE ) as integer

    dim as integer r,g,b,a,c

    r = cv.r * 255
    g = cv.g * 255
    b = cv.b * 255
    a = cv.a * 255

    if a > 127 then
        a -= 128
        c = a * 2 ^ 24 or &H80000000
        c or= r * 2 ^ 16
        c or= g * 2 ^ 8
        c or= b
    else
        c = a * 2 ^ 24
        c or= r * 2 ^ 16
        c or= g * 2 ^ 8
        c or= b
    end if

    return c

End Function

''----------------------------------------------------
'' This is a naked function version of the MASM code.
''----------------------------------------------------

function D3DCOLORVALUETOLONG_NAKED naked( byref cv as D3DCOLORVALUE ) as integer
    asm

        push ebx
        push edi

        mov eax, [esp+12]

        sub esp, 16

        .section .data
            f255: .float 255
        .section .text

        fld DWORD PTR f255

        R = 0
        G = 4
        B = 8
        A = 12

        fld DWORD PTR [eax+R]
        fmul st, st(1)
        fistp DWORD PTR [esp]
        fld DWORD PTR [eax+G]
        fmul st, st(1)
        fistp DWORD PTR [esp+4]
        fld DWORD PTR [eax+B]
        fmul st, st(1)
        fistp DWORD PTR [esp+8]
        fld DWORD PTR [eax+A]
        fmul st, st(1)
        fistp DWORD PTR [esp+12]
        fstp st

        mov ebx, [esp]
        mov ecx, [esp+4]
        mov edx, [esp+8]
        mov edi, [esp+12]

        add esp, 16

        cmp edi, 127
        jna 0f

        sub edi, 128
        shl edi, 24
        or edi, 0x80000000
        mov eax, edi
        shl ebx, 16
        or eax, ebx
        shl ecx, 8
        or eax, ecx
        or eax, edx

        pop edi
        pop ebx
        ret 4

    0:

        shl edi, 24
        mov eax, edi
        shl ebx, 16
        or eax, ebx
        shl ecx, 8
        or eax, ecx
        or eax, edx

        pop edi
        pop ebx
        ret 4

    end asm

End Function

''===================================================================================

dim as D3DCOLORVALUE cv
dim as integer r

cv.r = 0.123
cv.g = 0.234
cv.b = 0.345
cv.a = 0.456

r = D3DCOLORVALUETOLONG( cv )
print hex(r)
r = D3DCOLORVALUETOLONG_( cv )
print hex(r)
r = D3DCOLORVALUETOLONG_NAKED( cv )
print hex(r)
r = D3DCOLORVALUETOLONGASM( cv )
print hex(r)
print

SetProcessAffinityMask( GetCurrentProcess(), 1)

sleep 5000

for i as integer = 1 to 4

    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
    counter_end()
    print counter_cycles;" cycles"

    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
        r = D3DCOLORVALUETOLONG( cv )
    counter_end()
    print counter_cycles;" cycles"

    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
        r = D3DCOLORVALUETOLONG_NAKED( cv )
    counter_end()
    print counter_cycles;" cycles"

    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
        r = D3DCOLORVALUETOLONGASM( cv )
    counter_end()
    print counter_cycles;" cycles"

next

sleep


Running on my P3:

741F3C58
741F3C58
741F3C58
741F3C58

0 cycles
78 cycles
31 cycles
30 cycles
0 cycles
78 cycles
31 cycles
30 cycles
0 cycles
78 cycles
31 cycles
30 cycles
0 cycles
78 cycles
31 cycles
30 cycles


And MASM leads by 1 full cycle.

Well Microsoft, here's another nice mess you've gotten us into.

mikorians


MichaelW

Quote from: jj2007 on October 24, 2012, 04:32:22 PM
P.S.: No timings for the second function?

Function D3DCOLORVALUETOLONG(ByRef cv As D3DCOLORVALUE) As Long
function D3DCOLORVALUETOLONG_( byref cv as D3DCOLORVALUE ) as integer

I didn't bother timing it because there should be no significant difference in the compiled code. I was just trying to show the more compact syntax that FreeBASIC allows, compared to VB.
Well Microsoft, here's another nice mess you've gotten us into.