BBC BASIC for Windows
Programming >> Graphics and Games >> DirectX9 tutorials for BB4W
http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1263621303

DirectX9 tutorials for BB4W
Post by Michael Hutton on Jan 16th, 2010, 04:55am

I have uploaded a first 10 example programs for DirectX9. They are commented .BBC programs, although the comments get more sparse as the examples progress. So just load it into your IDE and run/read through.

I have uploaded them as a .zip archive to:

http://tech.groups.yahoo.com/group/bb4w/files/Graphics/DIRECTX9/Tutorials/Directx9%20Lessons.zip

They are based on this excellent website :

http://www.directxtutorial.com/ and I will loosely follow

http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B.aspx

The idea is to give a fully working BB4W version of the program listed in the tutorial. My comments are BB4W orientated and I would recommend people read the original tutorial first.

You will need three other files for all the examples to work:

First, create a folder called "DirectX9" in your @lib$ folder and put:

http://tech.groups.yahoo.com/group/bb4w/files/Graphics/DIRECTX9/Tutorials/Libraries/D3D9Definitions.bbc

and

http://tech.groups.yahoo.com/group/bb4w/files/Graphics/DIRECTX9/Tutorials/Libraries/D3D9LIB.bbc

and

http://tech.groups.yahoo.com/group/bb4w/files/Libraries/D3DX8BBC.dll

in it.

Now, with the last file (D3DX8BBC.dll) RENAME IT TO D3DX9BBC.dll

Michael.
Re: DirectX9 tutorials for BB4W
Post by Colin on Jan 27th, 2010, 2:44pm

Michael

In going through your tutorials lessons 7 through 9 only display a blank black window on my computer, lessons 1 through 6 and lesson 10 work correctly. Anything I'm doing wrong?

Colin
Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jan 27th, 2010, 11:43pm

Hi Colin,


I suspect that the functions from the D3DX9DLL are not being loaded but could you report back?

** Now, with the last file (D3DX8BBC.dll) RENAME IT TO D3DX9BBC.dll ** (and make sure it is in the @lib$/Directx9 folder

Thanks for the report back. I will update and include some more error reporting to check that all the functions are installed properly.

Have you any other comments or critisisms...? I do find I will get something working this end and not think of little things to make life easier for other users. So if you have any suggestions I would value them.

Michael
Re: DirectX9 tutorials for BB4W
Post by admin on Jan 28th, 2010, 08:41am

Quote:
Now, with the last file (D3DX8BBC.dll) RENAME IT TO D3DX9BBC.dll

No no no no no! Don't do that - D3DX8BBC.DLL is not compatible with DirectX9.

Richard.

Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jan 28th, 2010, 11:15am

I know, I know. I've just hit CreateTextureFromFileA% where you have to pass the device pointer as the first parameter. The maths functions work but obviously not these functions. I am now dedicating my time to creating the real D3DX9BBC.DLL...

Michael
Re: DirectX9 tutorials for BB4W
Post by Torro on Jul 24th, 2015, 1:17pm

What is the situation with using directx9 via the BBClibs?

With directx12 on horizon is that going to be quite different?
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 24th, 2015, 2:59pm

on Jul 24th, 2015, 1:17pm, Torro wrote:
What is the situation with using directx9 via the BBClibs?

The existing D3DLIB library uses DirectX8 rather than DirectX9. There's a particularly important reason for that, which involves the Direct 3D Extensions 'D3DX'. Microsoft permits the distribution of the relevant DirectX8 DLL with applications (supplied as standard with BB4W as D3DX8BBC.DLL) but they do not allow the distribution of the D3DX9 DLL on its own. This makes it almost impossible to support DirectX9 legally from BB4W without shipping a massive MS installer at the same time.

To date, each new version of DirectX has been backwards-compatible with earlier versions, so DirectX8 code (and therefore programs using D3DLIB) still run fine on the latest versions of Windows. If this situation changes, and DirectX8 support is withdrawn by Microsoft, then something would need to be done to maintain D3DLIB functionality.

Of course you can use DirectX9 from BB4W, so long as you avoid the D3DX extensions library entirely, or rely on it already being installed on the target PC (tricky, because there are many versions), or distribute it 'illegally'. Before Michael Hutton 'jumped ship' to PureBasic he wrote a number of DirectX9 applications, proving that it's possible within those restrictions (see earlier posts in this thread).

So, I don't currently have any plans to update D3DLIB but I would be interested to know if there is a demand for this. I haven't investigated whether DirectX versions later than 9 resolve the DLL issue, or make it even more problematic.

Edit: Checking what files are on my PC - d3dx9_32.dll, d3dx10_42.dll and d3dx11_43.dll - those pesky version numbers strongly suggest that the problem is not eased in DirectX 10 or 11. sad

Richard.
Re: DirectX9 tutorials for BB4W
Post by Torro on Jul 25th, 2015, 05:50am

Thanks for that.

After reading this it might not be a big deal

http://www.anandtech.com/show/1549/2


Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 25th, 2015, 07:12am

on Jul 25th, 2015, 05:50am, Torro wrote:
After reading this it might not be a big dea

Arguably, the biggest disadvantage of working with DirectX8 rather than DirectX9 is the lack of documentation (other than that supplied with BB4W). Much of what used to be available online seems to have disappeared (I've not tried searching the web archive).

Unfortunately it appears that Michael Hutton has deleted his BB4W DirectX9 tutorial, and its support files, (referred to earlier in this thread) from the Yahoo group. sad

Richard.
Re: DirectX9 tutorials for BB4W
Post by Torro on Jul 25th, 2015, 10:30am

They were based on http://www.directxtutorial.com/default.aspx

but crucially it requires your DLL wrappers to support it . Are those Dlls opensource?

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 25th, 2015, 4:14pm

on Jul 25th, 2015, 10:30am, Torro wrote:
They were based on http://www.directxtutorial.com/default.aspx
but crucially it requires your DLL wrappers to support it

See my announcement in the Libraries section.

Richard.
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 26th, 2015, 9:54pm

on Jul 25th, 2015, 10:30am, Torro wrote:
They were based on http://www.directxtutorial.com/default.aspx

Those tutorials look quite interesting (particularly the DirectX 9 one) although much of the earlier material - what they describe as Direct3D Basics - is covered in the BB4W documentation or the various example programs.

It's the more advanced stuff - like meshes, sprites and HLSL programming - that could be of particular relevance to us, but unfortunately that's only accessible by paying $50 for 'premium membership'!

Richard.




Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 27th, 2015, 5:49pm

on Jul 25th, 2015, 4:14pm, g4bau wrote:
See my announcement in the Libraries section.

Unlike DirectX 8, for which documentation was hard to find, there are many web sites with tutorials and other information on DirectX 9, and most importantly MSDN itself still covers Direct3D 9 in detail.

Specifically, when using the new BB4W libraries, the value returned from FN_init3d() is a pointer to an IDirect3DDevice9 interface. Would it be helpful for me to produce an appropriate BB4W structure declaration for this interface?

Is anybody going to take the bait and write some 3D code?

Richard.
Re: DirectX9 tutorials for BB4W
Post by dfeugey on Jul 28th, 2015, 06:02am

I will. But when, it's another question grin
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 28th, 2015, 08:53am

on Jul 28th, 2015, 06:02am, dfeugey wrote:
I will. But when, it's another question grin

You didn't answer the question about whether a Direct3DDevice9 structure declaration would be useful. It's not a trivial thing to generate (there are an awful lot of methods to include) so I'd like to know that the effort wouldn't be wasted.

Richard.
Re: DirectX9 tutorials for BB4W
Post by DDRM on Jul 28th, 2015, 09:22am

Hi Richard,

Hmm, maybe. I can't work out exactly what I'm trying to do.

I've had a play in the past with directly writing a vertex buffer, and generating "worlds" and mazes in real time, to move around. As part of that, you provided me with some codes, which includes the magic lines:
Code:
      SYS!(!D%+92),D%,N%*L%,0,V%,0,^B% TO R%:REM CreateVertexBuffer
      IF R% THEN=0
      SYS!(!B%+44),B%,0,N%*L%,^P%,0:REM pVB::Lock
 

This now fails with D3D9LIB, presumably because the offsets have changed, as for the specular reflection example you gave earlier? Changing the 92 to 104, as in the library, means it doesn't crash (but still doesn't render, so there's still something else wrong...). I note that the "magic numbers" for the lock and unlock are the same. I'll have a play with a simpler object sometime.

Do these SYS calls address the methods of the device? Will your proposal to provide a structure declaration allow us to use these methods without knowing the "magic numbers"? If not, where can we find them?

On what I believe to be a similar point (which may make you laugh...), is the issue with things like the teapot demo that you can't use "GetProcAddress" unless you know the precise name of the D3D9 DLL, which may vary between machines? I suspect I'm confusing the functions of the DLL called directly by D3D(9)LIB, and the one referred to as D3DX8BBC.DLL - or are they actually the same?

D
Re: DirectX9 tutorials for BB4W
Post by dfeugey on Jul 28th, 2015, 09:55am

on Jul 28th, 2015, 08:53am, g4bau wrote:
You didn't answer the question about whether a Direct3DDevice9 structure declaration would be useful. It's not a trivial thing to generate (there are an awful lot of methods to include) so I'd like to know that the effort wouldn't be wasted.
Richard.


Difficult question. It would be, if I use it. But since it's not yet the case, I would say that's it's not a necessity... for me.
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 28th, 2015, 10:07am

on Jul 28th, 2015, 09:22am, DDRM wrote:
This now fails with D3D9LIB, presumably because the offsets have changed

As you will appreciate there are two different aspects to adapting code from DirectX 8 to DirectX 9:
  1. Although many of the methods in D3D9 work identically to, and have the same parameters as, their equivalents in D3D8, in almost every case their ordinal values (positions in the vTable) change. So it is necessary to discover the new value and change the offset.

  2. In a few cases the methods do not have an identical interface, for example the number of parameters may have changed (you should not be surprised by this, because after all D3D9 is functionally different from D3D8). In those cases you must check the documentation of the function to find out what the new parameters are.
Quote:
Changing the 92 to 104, as in the library, means it doesn't crash (but still doesn't render, so there's still something else wrong...)

That method (IDirect3DDevice9::CreateVertexBuffer) is one whose parameters have changed, as you might have noticed by comparing the equivalent calls in D3DLIB and D3D9LIB. To be specific, it has acquired an extra, sixth, parameter (which you must set to zero):

https://msdn.microsoft.com/en-us/library/windows/desktop/bb174364.aspx

This is presumably why your code didn't work, since you do not appear to have added that parameter.

Quote:
Will your proposal to provide a structure declaration allow us to use these methods without knowing the "magic numbers"?

That is the whole point. This wiki article (8 years old) gives the details - I rather took it for granted that anybody attempting to call COM methods from BB4W would be familiar with it:

http://bb4w.wikispaces.com/Calling+object+methods+using+structures

Quote:
you can't use "GetProcAddress" unless you know the precise name of the D3D9 DLL, which may vary between machines?
(I'm assuming you mean D3DX9 rather than D3D9 there).

It's not just the name, but whether any D3DX9... DLL is present at all! Windows comes with DirectX pre-installed, so (these days) you can be certain that D3D8.DLL and D3D9.DLL will be available. But the extensions library is not part of the standard installation, and will be present only if you happen to have installed another application which needs it.

A correspondent recently sent me this useful link, which gives both the background and some solutions to the D3DX issue:

http://blogs.msdn.com/b/chuckw/archive/2013/08/21/living-without-d3dx.aspx

Richard.

Re: DirectX9 tutorials for BB4W
Post by DDRM on Jul 28th, 2015, 12:44pm

Ah, some pennies beginning to drop...

You are right, I had missed the extra parameter, and it works now...

I understand the difference between D3D9 and D3DX9...

I realise that the renderer is a COM device with methods, and the relevance of the wiki page. I see that there are lots of methods, and understand why you can't implement only the most useful ones.

I still don't understand why the index for CreateVertex is 104, since it is the 15th method listed (plus 3 inherited), but I can live with that... I guess there's a header or something. More worryingly I see SetRenderState is 80 further down the list, but only 124 bytes later: I'd have expected it to be a multiple of 4, or at least 2... Suggests I have no idea what's going on after all...

I've had some fun playing with the teapot program, making it draw "cylinders" that look like cones (only in d3d8, though)

Summary: your proposed structure would be useful to me, but there probably won't be any useful output for anyone else, since my understanding is obviously limited, and I'd quite understand if you felt one happy punter didn't justify the effort it will take you.

What output were you hoping for when you "laid the bait"?

D
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 28th, 2015, 3:44pm

on Jul 28th, 2015, 12:44pm, DDRM wrote:
I still don't understand why the index for CreateVertex is 104, since it is the 15th method listed (plus 3 inherited)

You have to be careful which 'list' you use. Sometimes you will find the methods listed in alphabetical order, rather than in 'vTable' order, which is obviously of no use in determining the offset. As that method has an offset of 104 it is definitely ordinal 26 (104/4) not ordinal 18 (or perhaps you meant 17, depending in whether you were counting from 0 or 1) that you state.

Quote:
I guess there's a header or something.

Nope, the ordinals start at zero (QueryInterface).

Quote:
More worryingly I see SetRenderState is 80 further down the list, but only 124 bytes later: I'd have expected it to be a multiple of 4, or at least 2

Well, in fact, 124 is a multiple of 4 (it's 4*31)! Back to school for you....

Quote:
Summary: your proposed structure would be useful to me, but there probably won't be any useful output for anyone else, since my understanding is obviously limited

Defeatist! tongue

Quote:
What output were you hoping for when you "laid the bait"?

Over the years the absence of support for DirectX 9 (or, more accurately, the lack of support for a version of DirectX with readily-available documentation) has been cited as a reason for so little uptake beyond what can be achieved using D3DLIB alone. I suppose I am hoping that with D3D9LIB - and some solutions to the D3DX issue - we may see some more exciting 3D applications.

Richard.

Re: DirectX9 tutorials for BB4W
Post by DDRM on Jul 28th, 2015, 4:03pm

Hi Richard,

List: yes, obviously the order is important - but both lists I found on MSDN gave them in alphabetical order, so I assumed that must be the vTable order, since I couldn't find a separate mention of the latter, which probably reflects my lack of familiarity with MSDN... That would explain it if they are different. If they are different, then your header (or at least a pointer to the vTable list) will DEFINITELY be useful!

124/4: OK, sloppily expressed! I meant I expected the gap to be 80 x 4, or maybe 80 x 2, since it was 80 places later in the alphabetical list. Of course, if the list isn't alphabetical, it could easily occur 31 places later... grin

[defeatist] Maybe. Realist? wink

Exciting 3D applications? Well, at least I enjoy playing with it - and actually, it's not all that hard when I concentrate...

Best wishes,

D
Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 28th, 2015, 7:36pm

Hullo all,

I haven't read the topic in its entirety but I still get notified to updates to the thread. I will dig out these old files and see if they are of any use. Of note, the helper file contain a mass of structure definitions etc for DirectX9 so code like

Code:
SYS!(!pDevice%+228), pDevice%, 29, 1 : REM Enable specular
 


can be avoided completely. There is a lot of helpful stuff within the library which makes it a lot simpler to get a program up and running.

Can you give me a bit of time and I will see what I turn up. I will try and test them to see if they work in v6.00. I expect they will *but* will a structure variant now be a different size? I assume so, but I can't even remember if I used any or not. It has been a long time.

Give me a few days and I'll see what I have got left over.

Michael
Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 28th, 2015, 9:10pm

I've dug around and found this - I'll have to use two separate posts...

Code:
      REM *******************************************************************
      REM
      REM Introduction :
      REM
      REM *******************************************************************
      REM
      REM These tutorials are based on http://www.directxtutorial.com/ which
      REM is an excellent site to learn about DirectX programming. These are
      REM very in depth tutorials and I won't repeat what is said there,
      REM rather I will show you the 'minimal' BB4W program which will
      REM achieve the same effect as the tutorial. You can then go on to
      REM modify them at your hearts desire!
      REM
      REM We will use DirectX9. Why? Well, DirectX8 is now officially
      REM 'unsupported' although DirectX9 and 10 still maintain backward
      REM compatibility. There is no BB4W library for DirectX9 and so it will
      REM be useful to learn to create the functions we need from the ground
      REM up. You will find that a lot of your programs are based around
      REM the same shell.
      REM
      REM DirectX uses COM (Component Object Model), if you are not familiar
      REM with COM then a quick read of :
      REM
      REM http://bb4w.wikispaces.com/Component+Object+Model+programming
      REM
      REM which will help with the basics. I will not try to explain it here.
      REM
      REM I have decided to use structures to call the different methods in
      REM each interface see :
      REM
      REM http://bb4w.wikispaces.com/Calling+object+methods+using+structures
      REM
      REM Although it can seem tedious to type out the structures first time,
      REM it does lead to more readable code than the more obfuscating
      REM indirection method. This is especially useful when you come back to
      REM the code after a long time.
      REM
      REM For instance reading :
      REM
      REM             SYS IDirect3DDevice9.BeginScene%, IDirect3DDevice9%
      REM
      REM is a lot easier to reference than
      REM
      REM             SYS !(!D%+164), D%
      REM
      REM There are some caveats to using structures. The first is that the
      REM original memory allocated for the structure is wasted but this is
      REM not really a big problem. Second, we have to use a tricky line of
      REM code to redirect the structure to point to the interfaces despatch
      REM table (we *must* remember to do this!) and third we could argue
      REM that they are slightly slower to call than using indirection. All
      REM in all, I think it is worthwhile doing for the readability of the
      REM code.
      REM
      REM The only other thing to mention is the FNf() function. DirectX uses
      REM 4 byte floating point values rather than BB4W's 5 or 8 byte float
      REM (*FLOAT 40 and *FLOAT 64 respectively). Whenever you see a value
      REM such as 1.0f or 0.5f mentioned in the tutorials we need to use
      REM FNf(1.0) or FNf(0.5) instead, otherwise we will get some very funny
      REM results. See :
      REM
      REM http://bb4w.wikispaces.com/Passing+floating-point+values+to+DLLs
      REM
      REM For further information.

      REM *******************************************************************
      REM
      REM  Programming DirectX9
      REM  Lesson 1
      REM
      REM *******************************************************************

      REM Remember, this lesson shows you a lot of code which will later
      REM be moved into a library. You could skip to lesson three which
      REM shows you exactly the same program but with all the reusable
      REM code put in a library.

      REM Well, we should choose a screen mode.
      REM MODE 8 is a good start. 640 x 512 pixels.
      REM we'll also turn the cursor off.
      REM Notice initialisation of the window is done for you by BB4W!

      MODE 8
      OFF


      REM We should always make sure that we have a cleanup procedure.
      REM This enables us to free up all the resources we create with DirectX
      REM Remember, any object 'created' by you will stay there until you
      REM free it. To prevent all your memory eventually being used up you
      REM must free it.
      ON ERROR PROC_Error   : PROC_Cleanup : END
      ON CLOSE PROC_Cleanup : QUIT

      REM First we need to load in the D3D9.DLL. This 'Dynamic Linked
      REM Library' contains all the goods to use DirectX. If you haven't,
      REM install the latest DirectX SDK it would be a good reference
      REM Otherwise, make sure you have DirectX9 installed.
      SYS "LoadLibrary", "D3D9.DLL" TO D3D9DLL%


      REM It is good practice to check the result of any Windows API call we
      REM make just in case things rely on them later, but we won't always
      REM do this.
      IF D3D9DLL% = FALSE THEN
        m$ = "Failed to load D3D9.DLL. " +CHR$13
        m$+= "Please ensure you have DirectX9 installed."
        ERROR 100, m$
      ENDIF

      REM The first function we need is 'Direct3DCreate9' so we will query
      REM our dll loaded into memory and get its address.
      SYS"GetProcAddress", D3D9DLL%, "Direct3DCreate9" TO Direct3DCreate9%

      REM Again, check to see if we have found the address
      IF Direct3DCreate9% = FALSE THEN
        ERROR 100, "Could not locate Direct3DCreate9 function."
      ENDIF



      REM Now we will 'create' the DirectX9 'device'. This is an 'object'
      REM which contains 'interfaces' and/or 'methods'. We can call these
      REM methods to control DirectX.
      REM
      REM At first we will define constants etc as we need them, but later
      REM we can move all definitions to a separate initialisation routine,
      REM and later still, to a separate library.



      REM First get the Direct3D9 interface pointer :
      D3D_SDK_VERSION = 32 : REM DirectX 9c
      SYS Direct3DCreate9%, D3D_SDK_VERSION TO IDirect3D9%
      IF IDirect3D9% = FALSE THEN
        ERROR 100, "Failed to Create Direct3D9 interface."
      ENDIF


      REM Now we need to define our first interface. We do this by defining
      REM the following structure and then redirecting it to point to the
      REM interface's methods.
      DIM IDirect3D9{                        \
      \QueryInterface%,                      \
      \AddRef%,                              \
      \Release%,                             \
      \RegisterSoftwareDevice%,              \
      \GetAdapterCount%,                     \
      \GetAdapterIdentifier%,                \
      \GetAdapterModeCount%,                 \
      \EnumAdapterModes%,                    \
      \GetAdapterDisplayMode%,               \
      \CheckDeviceType%,                     \
      \CheckDeviceFormat%,                   \
      \CheckDeviceMultiSampleType%,          \
      \CheckDepthStencilMatch%,              \
      \CheckDeviceFormatConversion%,         \
      \GetDeviceCaps%,                       \
      \GetAdapterMonitor%,                   \
      \CreateDevice%                        }

      REM We have created the structure, but unfortunately if we were to call
      REM any of the methods with it we would fail dramatically. With the
      REM next line we redirect our structure to point to the IDirect3D9
      REM despatch table.
      !(^IDirect3D9{}+4) = !IDirect3D9%

      REM Sorry about that, but it is essential we do this if we want to use
      REM structure members as methods/functions!

      REM Next we define the PRESENT_PARAMETERS{} which we need to 'create
      REM the device' and set some of its members.
      DIM D3DPRESENT_PARAMETERS{              \
      \BackBufferWidth%,                      \
      \BackBufferHeight%,                     \
      \BackBufferFormat%,                     \
      \BackBufferCount%,                      \
      \MultiSampleType%,                      \
      \MultiSampleQuality%,                   \
      \SwapEffect%,                           \
      \hDeviceWindow%,                        \
      \Windowed%,                             \
      \EnableAutoDepthStencil%,               \
      \AutoDepthStencilFormat%,               \
      \Flags%,                                \
      \FullScreen_RefreshRateInHz%,           \
      \PresentationInterval%                 }

      REM Define some constants
      REM Take note this is NOT BB4W's TRUE which is -1!
      _TRUE                                 = 1
      D3DSWAPEFFECT_DISCARD                 = 1

      REM Set some D3DPRESENT_PARAMETERS{} members
      D3DPRESENT_PARAMETERS.Windowed%       = _TRUE
      D3DPRESENT_PARAMETERS.SwapEffect%     = D3DSWAPEFFECT_DISCARD
      D3DPRESENT_PARAMETERS.hDeviceWindow%  = @hwnd%

      REM And now we Create the Device
      D3DADAPTER_DEFAULT                    = 0
      D3DDEVTYPE_HAL                        = 1
      D3DCREATE_SOFTWARE_VERTEXPROCESSING   = &20

      SYS IDirect3D9.CreateDevice%, IDirect3D9%, D3DADAPTER_DEFAULT, \
      \ D3DDEVTYPE_HAL, @hwnd%, D3DCREATE_SOFTWARE_VERTEXPROCESSING, \
      \ D3DPRESENT_PARAMETERS{}, ^IDirect3DDevice9% TO R%
      IF R%<>0 OR IDirect3DDevice9% = FALSE THEN
        ERROR 100, "Failed to Create IDirect3DDevice9."
      ENDIF

      REM Notice the ^ (address of) symbol with ^IDirect3DDevice9%. We will
      REM come across this a lot when we want to receive a value back from
      REM the SYS call which is not returned in the result.

      REM A note of the SYS call format:
      REM You may have noticed the slightly different SYS call format when
      REM calling an method via an object's interface compared to a simple
      REM Windows API function. You must put the interface pointer as the
      REM second parameter ie after calling the method :
      REM
      REM SYS IDirect3D9.CreateDevice%
      REM
      REM We add the interface pointer
      REM
      REM SYS IDirect3D9.CreateDevice%, IDirect3D9%
      REM
   

Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 28th, 2015, 9:11pm

Add this to the first bit...

Code:
      REM and then the parameters we are passing. See the call we made above.

      REM In fact this Device is an Interface, and so if we want we can
      REM define another structure and redirect it to this Interface's
      REM despatch table.
      DIM IDirect3DDevice9{                   \
      \QueryInterface%,                       \
      \AddRef%,                               \
      \Release%,                              \
      \TestCooperativeLevel%,                 \
      \GetAvailableTextureMem%,               \
      \EvictManagedResources%,                \
      \GetDirect3D%,                          \
      \GetDeviceCaps%,                        \
      \GetDisplayMode%,                       \
      \GetCreationParameters%,                \
      \SetCursorProperties%,                  \
      \SetCursorPosition%,                    \
      \ShowCursor%,                           \
      \CreateAdditionalSwapChain%,            \
      \GetSwapChain%,                         \
      \GetNumberOfSwapChains%,                \
      \Reset%,                                \
      \Present%,                              \
      \GetBackBuffer%,                        \
      \GetRasterStatus%,                      \
      \SetDialogBoxMode%,                     \
      \SetGammaRamp%,                         \
      \GetGammaRamp%,                         \
      \CreateTexture%,                        \
      \CreateVolumeTexture%,                  \
      \CreateCubeTexture%,                    \
      \CreateVertexBuffer%,                   \
      \CreateIndexBuffer%,                    \
      \CreateRenderTarget%,                   \
      \CreateDepthStencilSurface%,            \
      \UpdateSurface%,                        \
      \UpdateTexture%,                        \
      \GetRenderTargetData%,                  \
      \GetFrontBufferData%,                   \
      \StretchRect%,                          \
      \ColorFill%,                            \
      \CreateOffscreenPlainSurface%,          \
      \SetRenderTarget%,                      \
      \GetRenderTarget%,                      \
      \SetDepthStencilSurface%,               \
      \GetDepthStencilSurface%,               \
      \BeginScene%,                           \
      \EndScene%,                             \
      \Clear%,                                \
      \SetTransform%,                         \
      \GetTransform%,                         \
      \MultiplyTransform%,                    \
      \SetViewport%,                          \
      \GetViewport%,                          \
      \SetMaterial%,                          \
      \GetMaterial%,                          \
      \SetLight%,                             \
      \GetLight%,                             \
      \LightEnable%,                          \
      \GetLightEnable%,                       \
      \SetClipPlane%,                         \
      \GetClipPlane%,                         \
      \SetRenderState%,                       \
      \GetRenderState%,                       \
      \CreateStateBlock%,                     \
      \BeginStateBlock%,                      \
      \EndStateBlock%,                        \
      \SetClipStatus%,                        \
      \GetClipStatus%,                        \
      \GetTexture%,                           \
      \SetTexture%,                           \
      \GetTextureStageState%,                 \
      \SetTextureStageState%,                 \
      \GetSamplerState%,                      \
      \SetSamplerState%,                      \
      \ValidateDevice%,                       \
      \SetPaletteEntries%,                    \
      \GetPaletteEntries%,                    \
      \SetCurrentTexturePalette%,             \
      \GetCurrentTexturePalette%,             \
      \SetScissorRect%,                       \
      \GetScissorRect%,                       \
      \SetSoftwareVertexProcessing%,          \
      \GetSoftwareVertexProcessing%,          \
      \SetNPatchMode%,                        \
      \GetNPatchMode%,                        \
      \DrawPrimitive%,                        \
      \DrawIndexedPrimitive%,                 \
      \DrawPrimitiveUP%,                      \
      \DrawIndexedPrimitiveUP%,               \
      \ProcessVertices%,                      \
      \CreateVertexDeclaration%,              \
      \SetVertexDeclaration%,                 \
      \GetVertexDeclaration%,                 \
      \SetFVF%,                               \
      \GetFVF%,                               \
      \CreateVertexShader%,                   \
      \SetVertexShader%,                      \
      \GetVertexShader%,                      \
      \SetVertexShaderConstantF%,             \
      \GetVertexShaderConstantF%,             \
      \SetVertexShaderConstantI%,             \
      \GetVertexShaderConstantI%,             \
      \SetVertexShaderConstantB%,             \
      \GetVertexShaderConstantB%,             \
      \SetStreamSource%,                      \
      \GetStreamSource%,                      \
      \SetStreamSourceFreq%,                  \
      \GetStreamSourceFreq%,                  \
      \SetIndices%,                           \
      \GetIndices%,                           \
      \CreatePixelShader%,                    \
      \SetPixelShader%,                       \
      \GetPixelShader%,                       \
      \SetPixelShaderConstantF%,              \
      \GetPixelShaderConstantF%,              \
      \SetPixelShaderConstantI%,              \
      \GetPixelShaderConstantI%,              \
      \SetPixelShaderConstantB%,              \
      \GetPixelShaderConstantB%,              \
      \DrawRectPatch%,                        \
      \DrawTriPatch%,                         \
      \DeletePatch%,                          \
      \CreateQuery%                          }


      REM Phew! We will put all this stuff in a library so we don't have to
      REM type it out every time.


      REM Now, remember to redirect our structure we just typed out to the
      REM interface's despatch table.
      !(^IDirect3DDevice9{}+4) = !IDirect3DDevice9%


      REM Now we can do something with this device.
      REM For this tutorial we shall just change the background colour by
      REM cycling though some different RGB values 1000 times.

      D3DCLEAR_TARGET = 1

      FOR I%=1 TO 1000
  
        R% = 128 + 127 * SIN(TIME/50)
        G% = 128 + 127 * SIN(TIME/70)
        B% = 128 + 127 * SIN(TIME/90)
  
        REM Now we OR these values together to create an D3DCOLOR value
  
        C% = &FF000000 OR (R%<<16) OR (G%<<8) OR B%
  
        REM We could check the return values of these calls,
        REM but we won't bother here because we are in a tight
        REM loop and we want to get on with things. If we had
        REM a problem we could check the return values to aid
        REM our debugging process.
  
        SYS IDirect3DDevice9.Clear%, IDirect3DDevice9%, 0, 0, \
        \ D3DCLEAR_TARGET, C%, FNf(1.0), 0
  
        SYS IDirect3DDevice9.BeginScene%, IDirect3DDevice9%
  
  
        REM We will do lots of other things here!
  
  
        SYS IDirect3DDevice9.EndScene%, IDirect3DDevice9%
        SYS IDirect3DDevice9.Present%, IDirect3DDevice9%, 0,0,0,0
  
        PRINT TAB(0,0)"Frame : "; I%, " Colour : ";~C%
  
      NEXT

      PRINT "Finished."

      PROC_Cleanup

      END

      REM A cleanup routine.
      DEF PROC_Cleanup
      REM Release IDirect3DDevice9
      IDirect3DDevice9% += 0 : IF IDirect3DDevice9% THEN
        SYS IDirect3DDevice9.Release%, IDirect3DDevice9%
      ENDIF
      REM Release IDirect3D9
      IDirect3D9% += 0 : IF IDirect3D9% THEN
        SYS IDirect3D9.Release%, IDirect3D9%
      ENDIF
      REM Free the dll
      D3D9DLL% += 0 : IF D3D9DLL% THEN SYS "FreeLibrary", D3D9DLL%
      ENDPROC

      DEF PROC_Error
      SYS "MessageBox", @hwnd%, REPORT$, "Error : ", 0
      ENDPROC

      REM Convert to 32-bit float
      DEF FNf(A#)
      LOCAL A%,P%
      PRIVATE F%
      IF F%=0 THEN
        DIM P%10
        [OPT 2
        .F%
        mov esi,[ebp+2]:mov edi,[ebp+7]
        fld qword [esi]:fstp dword [edi]
        ret
        ]
      ENDIF
      A# *= 1.0#
      CALL F%,A#,A%
      =A%
 


Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 28th, 2015, 9:13pm

....and it works in v6.00 trial!

grin

Um, Richard, I need an ungrade.exe I am afraid... new computer again..

Michael

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 28th, 2015, 9:34pm

on Jul 28th, 2015, 4:03pm, DDRM wrote:
If they are different, then your header (or at least a pointer to the vTable list) will DEFINITELY be useful!

OK, here it is. I've checked a few methods chosen at random to see if they have the expected offsets, and they do - which is all good!

Code:
      DIM IDirect3DDevice9{QueryInterface%,AddRef%,Release%,TestCooperativeLevel%, \
      \ GetAvailableTextureMem%,EvictManagedResources%,GetDirect3D%,GetDeviceCaps%, \
      \ GetDisplayMode%,GetCreationParameters%,SetCursorProperties%,SetCursorPosition%, \
      \ ShowCursor%,CreateAdditionalSwapChain%,GetSwapChain%,GetNumberOfSwapChains%, \
      \ Reset%,Present%,GetBackBuffer%,GetRasterStatus%,SetDialogBoxMode%,SetGammaRamp%, \
      \ GetGammaRamp%,CreateTexture%,CreateVolumeTexture%,CreateCubeTexture%,CreateVertexBuffer%, \
      \ CreateIndexBuffer%,CreateRenderTarget%,CreateDepthStencilSurface%,UpdateSurface%, \
      \ UpdateTexture%,GetRenderTargetData%,GetFrontBufferData%,StretchRect%,ColorFill%, \
      \ CreateOffscreenPlainSurface%,SetRenderTarget%,GetRenderTarget%,SetDepthStencilSurface%, \
      \ GetDepthStencilSurface%,BeginScene%,EndScene%,Clear%,SetTransform%,GetTransform%, \
      \ MultiplyTransform%,SetViewport%,GetViewport%,SetMaterial%,GetMaterial%,SetLight%, \
      \ GetLight%,LightEnable%,GetLightEnable%,SetClipPlane%,GetClipPlane%,SetRenderState%, \
      \ GetRenderState%,CreateStateBlock%,BeginStateBlock%,EndStateBlock%,SetClipStatus%, \
      \ GetClipStatus%,GetTexture%,SetTexture%,GetTextureStageState%,SetTextureStageState%, \
      \ GetSamplerState%,SetSamplerState%,ValidateDevice%,SetPaletteEntries%,GetPaletteEntries%, \
      \ SetCurrentTexturePalette%,GetCurrentTexturePalette%,SetScissorRect%,GetScissorRect%, \
      \ SetSoftwareVertexProcessing%,GetSoftwareVertexProcessing%,SetNPatchMode%, \
      \ GetNPatchMode%,DrawPrimitive%,DrawIndexedPrimitive%,DrawPrimitiveUP%,DrawIndexedPrimitiveUP%, \
      \ ProcessVertices%,CreateVertexDeclaration%,SetVertexDeclaration%,GetVertexDeclaration%, \
      \ SetFVF%,GetFVF%,CreateVertexShader%,SetVertexShader%,GetVertexShader%,SetVertexShaderConstantF%, \
      \ GetVertexShaderConstantF%,SetVertexShaderConstantI%,GetVertexShaderConstantI%, \
      \ SetVertexShaderConstantB%,GetVertexShaderConstantB%,SetStreamSource%,GetStreamSource%, \
      \ SetStreamSourceFreq%,GetStreamSourceFreq%,SetIndices%,GetIndices%,CreatePixelShader%, \
      \ SetPixelShader%,GetPixelShader%,SetPixelShaderConstantF%,GetPixelShaderConstantF%, \
      \ SetPixelShaderConstantI%,GetPixelShaderConstantI%,SetPixelShaderConstantB%, \
      \ GetPixelShaderConstantB%,DrawRectPatch%,DrawTriPatch%,DeletePatch%,CreateQuery%} 

If you don't want to clog up your memory with this declaration it's an ideal application for the CALL statement. Just add a RETURN as the last line of the file and then use:

Code:
      CALL @lib$+"IDirect3DDevice9" 

(or whatever name you have saved the file as).

Richard.


Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 29th, 2015, 08:18am

^^ I've done all the hard work with all those declarations. They are in a library ready for use. I am presuming people would like them? I'll have to upload them.

Michael

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 29th, 2015, 10:02am

on Jul 29th, 2015, 08:18am, Michael Hutton wrote:
I've done all the hard work

Hard work? I wrote a little program to scan D3D9.H and create the BB4W structure declarations automatically, so there theoretically shouldn't be any errors! I've uploaded the full file D3D9DECL.BBC to the Yahoo group here:

https://groups.yahoo.com/neo/groups/bb4w/files/Graphics/

It can also be downloaded from this direct link.

I wonder if it would be helpful to incorporate some other declarations, such as IID values and maybe constants. Any thoughts?

Richard.


Re: DirectX9 tutorials for BB4W
Post by Torro on Jul 29th, 2015, 11:52am

cool so this can be done for directx12?
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 29th, 2015, 12:40pm

on Jul 29th, 2015, 11:52am, Torro wrote:
cool so this can be done for directx12?

I haven't looked into it. Generally speaking I'm not very interested in technologies that work only on recent versions of Windows. Because BB4W runs fine on Windows XP (and earlier) - and indeed that's still what my main development PC has - I try to ensure that libraries do too. Also, the 'user demographic' probably suggests that the average BBC BASIC programmer doesn't own a Windows 8 PC!

But since BB4W can do (virtually) anything any other language can - if sometimes rather more slowly! - I assume that interfacing with DirectX 12 should be entirely possible.

Richard.
Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 29th, 2015, 8:46pm

on Jul 29th, 2015, 10:02am, g4bau wrote:
Hard work? I wrote a little program to scan D3D9.H and create the BB4W structure declarations automatically, so there theoretically shouldn't be any errors! I've uploaded the full file D3D9DECL.BBC to the Yahoo group here:

... snip ...

I wonder if it would be helpful to incorporate some other declarations, such as IID values and maybe constants. Any thoughts?

Richard.



"some hard work" - I note that you said it wouldn't be trivial a few posts ago. What has changed?

I think I did the same thing a few years ago. My definitions file has a lot of the constants defined. I think also there were some functions as well, so not so good for a CALL. I do have to install BB4W on this computer before I can update you with any more of what I did those years ago.

I'll try to update.
Michael

Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 29th, 2015, 8:50pm

on Jul 29th, 2015, 11:52am, Torro wrote:
cool so this can be done for directx12?


Yes, it can. The caveat being that setting up DirectX10/11/12 is different from 9 and below. I only have a 'starter file', basically getting a device going but in theory it is vastly more powerful. What is a major difference is the use of HLSL or 'effect' '.fx' files to control the shaders which make access to DirectCompute and the higher level shaders easier. It is a bit of a stepping stone though and not lightly undertaken.

Michael

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 29th, 2015, 9:45pm

on Jul 29th, 2015, 8:46pm, Michael Hutton wrote:
I think I did the same thing a few years ago.

The thing is, we are trying to make a fresh start. What you did that long time ago was impressive in many ways, but your programs never worked on my (or several other people's) PCs because of their reliance on D3DX9, with all that entailed. Perhaps understandably - since the programs ran perfectly on your own machine - you never seemed particularly committed to resolving that issue.

As you are probably aware, the way the new D3D9LIB (and the original D3DLIB) are made independent of D3DX is by coding equivalent functions directly in BASIC (made easier and faster by using BB4W's array operators and arithmetic). Whilst that only provides 'simple' solutions for the maths functions in D3DX, it shows one way towards the ultimate goal of writing more complex 3D programs that will work on any modern PC.

So, from my perspective, I don't want to go back to where DirectX9 support in BB4W was when you jumped ship to PureBasic (and deleted all your Direct3D utilities and tutorials from Yahoo). I would rather start with D3D9LIB, which for the first time has made it possible to run simple Direct3D9 programs in BB4W on my own PC. Hopefully we can develop that approach further, perhaps with guidance from the 'Living without D3DX' blog site to which I linked.

Richard.
Re: DirectX9 tutorials for BB4W
Post by Michael Hutton on Jul 29th, 2015, 10:04pm

on Jul 29th, 2015, 9:45pm, g4bau wrote:
- you never seemed particularly committed to resolving that issue.


I'm really not going to reinvent the wheel by recoding DirectX9 or whatever for BB4W! There is a vast resource out there and all one needs to do is download DirectX9 or whatever version they want. I did add some functions which are missing from the dll which were defined in inline files but really, the idea of recoding DirectX9 to BB4W functions is rather ludicrous, even more so when there are multiple updates of DirectX by a team of professional coders!

If you manage to do it. Well done, but I'm not even going to bother. I have way too many other things to do. I have uploaded the tutorials I made. If they are there for people to look at and change at their will. Good luck with the 'fresh start'!

..and yes, I did delete the lessons when I 'jumped ship'(!) but I understand you had 'ran away' to Liberty Basic also... 'pot and kettle' I am afraid RTR.

Before casting stones..... oh, but it all starts again. lipsrsealed

Michael


Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 29th, 2015, 11:06pm

on Jul 29th, 2015, 10:04pm, Michael Hutton wrote:
There is a vast resource out there and all one needs to do is download DirectX9 or whatever version they want.

DirectX (8, 9 and typically later versions) comes pre-installed with all recent versions of Windows, it is not necessary to download anything. However the Direct3D extensions (D3DX), which are what are under discussion here, are neither pre-installed with Windows nor (legally at least - post D3DX8) downloadable from anywhere as a DLL.

The only legitimate way to make a D3DX DLL available to an end-user is to ship a massive Microsoft installer with your application, which makes no sense for a BB4W app.

Quote:
the idea of recoding DirectX9 to BB4W functions is rather ludicrous, even more so when there are multiple updates of DirectX by a team of professional coders!

You are muddling DirectX with the Direct 3D Extensions (D3DX). It is perfectly sensible to consider alternatives to D3DX, indeed the blog page I linked to previously explains why D3DX is deprecated (scroll down to Background at the end of the article). The solutions may involve BASIC code (which contrary to your claim is perfectly reasonable for the maths functions at least), other DLLs or open-source tools.

Quote:
..and yes, I did delete the lessons when I 'jumped ship'(!) but I understand you had 'ran away' to Liberty Basic also... 'pot and kettle' I am afraid RTR.

At no point did my 'absence' from the BB4W scene result in any of the support materials being deleted. After all I continued to market BB4W throughout that period so was both morally and legally obliged to provide ongoing support.

Richard.
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 30th, 2015, 09:29am

on Jul 29th, 2015, 11:52am, Torro wrote:
cool so this can be done for directx12?

Further to Michael's reply, see this page (it's referring to migrating from Direct3D 9 to Direct3D 11, but the issues are no doubt similar):

https://msdn.microsoft.com/en-us/library/windows/desktop/ff476190.aspx#direct3d_9_to_direct3d_11

Although it removes the dependency on D3DX, a D3DCompiler DLL (which is not installed with Windows earlier than 8.1) is still required. In the comments at the bottom of this page the issue of whether it is legal to distribute that DLL is discussed (with not a very clear answer):

http://blogs.msdn.com/b/chuckw/archive/2012/05/07/hlsl-fxc-and-d3dcompile.aspx

However it states there that "The latest version of the D3DCompiler (#47) is now included with the Windows 8.1 OS" so we are off the hook for that OS version at least (I don't know about Windows 10).

As it also states in that article, if the only reason you need a D3DX (or D3DCompiler) DLL on the target machine is to compile the shader(s), then a good solution is to pre-compile them to binary shader files and (legitimately) ship those with your application.

Richard.
Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 30th, 2015, 12:39pm

I can confirm that the Direct3D examples supplied with BB4W (TEAPOT.BBC, PYRAMID.BBC, WORLD.BBC), and LIGHTING.BBC from the website - oh, and BBCOWL too - all work perfectly in Windows 10 (64-bit, although that shouldn't matter). This is as expected, but it's nice to know that Direct3D 8 remains supported on the newest (and last, if we are to believe Microsoft) Windows platform.

Richard.

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 30th, 2015, 2:58pm

on Jul 30th, 2015, 2:29pm, Torro wrote:
You would not believe how many banks Ive seen still running XP for mission critical apps because they are tied to ie61

Nothing particularly wrong with that, so long as they pay Microsoft enough to keep security updates coming (and if that's cheaper/safer than updating the banks' systems everybody is happy!).

Richard.

Re: DirectX9 tutorials for BB4W
Post by rtr2 on Jul 30th, 2015, 3:04pm

on Jul 30th, 2015, 2:25pm, Torro wrote:
Thats the point certain Commercial games only worked with the latest Windows you couldnt run them or install say dx10 on Xp . Windows 10 has Windows 10 only games . Same with browsers.

I strive to ensure all my BBC BASIC programs run on every version of Windows from 2000, through XP, Vista, 7, 8 and 8.1, to Windows 10. If that means not using the very latest technology, the impact on the appearance or performance of the final program is usually insignificant.

Richard.