Antialiasing troubles - c++

When full-scene antialiasing is enabled, I have some troubles when rendering to texture.
Here's an image of what is hapenning (rendered image has some edges).
http://i.imgur.com/VcFNn.png
When AA is off - nothing of this happening, and all goes ok.
Why might I experience these troubles, and what is the correct way to render to texture while AA is on?
Thanks in advance.

The first thing you should do is to make sure your game is capable to run full scene anti-aliasing (thats is to assume you have a ready graphics card for the job). The way to do that is by doing this:
/*
* The code below assumes that pD3D is a valid pointer
* to a IDirect3D9 interface.
*/
if( SUCCEEDED(pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL , D3DFMT_R8G8B8, FALSE,
D3DMULTISAMPLE_2_SAMPLES, NULL ) ) )
// Full-scene antialiasing is supported. Enable it here.
If the device supports it then all you have to do is set up the parameters for multi-sampling:
/*
* The example below assumes that pD3D is a valid pointer
* to a IDirect3D9 interface, d3dDevice is a pointer to a
* IDirect3DDevice9 interface, and hWnd is a valid handle
* to a window.
*/
D3DPRESENT_PARAMETER d3dPP
ZeroMemory( &d3dPP, sizeof( d3dPP ) );
d3dPP.Windowed = FALSE
d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPP.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &d3dDevice)
Hope this helps!

Related

DirectX9 CreateDevice() returns D3DERR_INVALIDCALL in injected DLL for VMT hooking

I want to modify a DirectX-Application's behavior (namely I want to implement a program similar to the Statman-Application by OrfeasZ [https://github.com/OrfeasZ/Statman/releases] as Onscreen-Info for Hitman 2) by injecting code (as DLL) into it and hooking the DirectX DeviceInterface VMT.
Since there are very limited resources on how to do this for DirectX11-Applications, I first wanted to learn how to do this in DX9 by creating a program that gets the DeviceInterface pointer of any DirectX9-Application. I wrote this code in the DllMain() function of my DLL (which is almost a 1:1 copy/paste of the third answer to this thread Hooking DirectX EndScene from an injected DLL):
HMODULE hDLL = GetModuleHandleA("d3d9");
LPDIRECT3D9(__stdcall*pDirect3DCreate9)(UINT) = (LPDIRECT3D9(__stdcall*)(UINT))GetProcAddress(hDLL, "Direct3DCreate9");
LPDIRECT3D9 pD3D = pDirect3DCreate9(D3D_SDK_VERSION);
D3DDISPLAYMODE d3ddm;
HRESULT hRes = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
//WNDPROC TempWndProc;
WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC, WndProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,TEXT("1"),NULL };
RegisterClassEx(&wc);
HWND hWnd = CreateWindow(TEXT("1"), NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL);
ShowWindow(hWnd, SW_SHOW);
IDirect3DDevice9 * ppReturnedDeviceInterface;
hRes = pD3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &ppReturnedDeviceInterface);
pD3D->Release();
DestroyWindow(hWnd);
if (pD3D == NULL) {
//printf ("WARNING: D3D FAILED");
return false;
}
unsigned long* pInterface = (unsigned long*)*((unsigned long*)ppReturnedDeviceInterface);
When I inject the DLL into a DirectX9-Application (I've tried this with Civilization V and Total War: Shogun 2), it opens a window, so it actually is able to get the Direct3DCreate9 function from the d3d9.dll within the game, but pD3D->CreateDevice() always returns `D3DERR_INVALIDCALL. I don't really get what could be the reason for this, especially since the rest of this program works flawlessly. Does anybody have any idea what is missing/wrong?
D3DERR_INVALIDCALL
The method call is invalid. For example, a method's parameter may not
be a valid pointer.
Based on the error information this issue may caused by invalid parameter of IDirect3D9::CreateDevice method. You need initialize the pointer:
IDirect3DDevice9 *pReturnedDeviceInterface = NULL;
Also check if hWnd is a valid window handle and d3ddm.Format etc.

Different D3D9 devices dont share the same VTable address

Lately I've been messing around with VTables and virtual pointers,
and I've found that every vptr from the same class type (should) point to the same VTable.(They point to the same address).
And I've searched for information about how COM objects work, and I've found that they also share the same VTable.(Objects of the same class ofc )
https://www.codeproject.com/articles/153096/intercepting-calls-to-com-interfaces]
First of all, when you set a method hook, it will work not only for the current instance of the COM object. It’ll work for all objects of the same class (but not for all the classes implementing the interface hooked).
The problem I have is as follows: I created in a test app 3 different d3d9 devices and I checked their pointer to the VTable but they are not the same!
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
if (NULL == (g_pD3D2 = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
D3DPRESENT_PARAMETERS d3dpp2;
ZeroMemory(&d3dpp2, sizeof(d3dpp2));
d3dpp2.Windowed = TRUE;
d3dpp2.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp2.BackBufferFormat = D3DFMT_UNKNOWN;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp2, &g_pd3dDevic)))
{
return E_FAIL;
}
WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC,TempWProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,(LPCWSTR)("1"),NULL };
RegisterClassEx(&wc);
HWND hWndd = CreateWindow((L"1"), NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL);
if (FAILED(g_pD3D2->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_DISABLE_DRIVER_MANAGEMENT,
&d3dpp2, &g_pd3dDevic3)))
{
return E_FAIL;
}
(As a last resort I also created a different window and initialized a new D3D Interface just in case it would make the new d3d9 device point to the same VTable.)
In the debugger I checked the values of the device's pointers and this is what they yield:
g_pd3dDevice(The first one) : __vfptr = 0x00b75d1c { 0x6a976f19 }
g_pd3dDevic(The second one) : __vfptr = 0x0512a75c { 0x6a976f19 }
g_pd3dDevic3(The third one) : __vfptr = 0x0524393c { 0x6a976f19 }
The 3 VTables are the same, but they are in different places. (The three pointers point to the same function)
(And I also checked and the VTable is offseted by 0x2D9C from the object)
So this is what I dont really understand:
Do D3D9 devices always use the same VTable?
Why don't they use the same VTable as COM objects do as pointed by the link I've given?
Is it a problem related to having more than one device in the same HWND?
Is it some compiler optimization- related thingie?
Are they really VTables?
P.S: Sorry for my poor English, and my way of explaining things :C
Thanks a lot for reading this wall of text, seriously, thanks.
Thanks in advance!!!
(And sorry if it is a really dumby question)

Creating a D3D device without HWND input parameter to MSFT CreateDevice() function

Kindly Pardon me if my doubt is silly or foolish. I am totally new to DirectX programming. Just have C++ knowledge (Very basic COM knowledge).
Below code sample is from MSDN Creating D3D device which explains how to create a D3D device from scratch.
MyDoubt is :
Here the function "pD3D->CreateDeviceEx()" takes in a parameter
HWND hwnd. What if I am trying to create a D3D device from a
commadline C++ win32 app where I need to use some of the functions in D3D device's interfaces. How do I get the HWND field. In this case
how do I create D3D device. PLease explain in detail.
HRESULT InitD3D9Ex( /* IN */ HWND hWnd, /* OUT */ IDirect3DDevice9Ex ** ppD3DDevice )
{
HRESULT hr = E_FAIL;
IDirect3D9Ex * pD3D = NULL;
IDirect3DDevice9Ex * pDevice = NULL;
if(ppD3DDevice == NULL)
{
return hr;
}
// Create the D3D object, which is needed to create the D3DDevice.
if(FAILED(hr = Direct3DCreate9Ex( D3D_SDK_VERSION, &pD3D )))
{
*ppD3DDevice = NULL;
return hr;
}
// Set up the structure used to create the D3DDevice.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the Direct3D device.
if( FAILED( hr = pD3D->CreateDeviceEx( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, NULL, &pDevice ) ) )
{
*ppD3DDevice = NULL;
return hr;
}
// Device state would normally be set here
*ppD3DDevice = pDevice;
return hr;
}
In Windows all the visual things are controlled by window handles. You cannot create the D3D "device" and attach it to "nothing". You must associate the "D3D device" with some window (your own one or a desktop).
Your console window is created by the system and you do not control its creation flags, so even if you use the GetConsoleWindow function, you cannot use this HWND in Direct3D device creation functions (this might have changed with the introduction of Aero).
You cannot avoid creating getting yet another window handle in your console application. Use the RegisterWindowClass and CreateWindow functions to create a new window or find the handle to your desktop (I doubt you would want that).

D3D device in vgasave

Is it possible to create device/directx application when pc uses vgasave mode?
This is my init function:
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = TRUE; // program windowed, not fullscreen
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
However when i call later
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
Program crashes, reporting about unhandled violation. Or maybe it isn't about vga, just I'm making something wrong?
CreateDevice returns D3DERR_NOTAVAILABLE
You've filled out way too few parameters in the D3DPRESENT_PARAMETERS. You've alomst certainly got bad settings for the back buffer and that kind of thing. If CreateDevice returns D3DERR_NOTAVAILABLE, then the d3ddev pointer is NULL, resulting in the access violation when you try to clear the back buffer- since there is no device.
D3DDeviceParameters.Windowed = true;
D3DDeviceParameters.BackBufferHeight = 0;
D3DDeviceParameters.BackBufferWidth = 0;
D3DDeviceParameters.BackBufferCount = 1;
D3DDeviceParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DDeviceParameters.MultiSampleQuality = 0;
D3DDeviceParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
D3DDeviceParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DDeviceParameters.hDeviceWindow = OS->GetHWND();
D3DDeviceParameters.EnableAutoDepthStencil = true;
D3DDeviceParameters.AutoDepthStencilFormat = D3DFMT_D24S8;
D3DDeviceParameters.Flags = 0;
D3DDeviceParameters.FullScreen_RefreshRateInHz = 0;
D3DDeviceParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Vsync.
This is my D3DPRESENT_PARAMETERS.

DirectX 9 C++ program crashes and wont re-open

I am just beginning to learn how to program DirectX 9 applications in C++, so I'm still not very good, and I'm messing it around quite a bit.
When I reopen the program after it crashes, the D3D Device fails to create with the result being D3DERR_INVALIDCALL. I am compiling with G++ in MinGW, using the DirectX August 2009 SDK. I'm guessing it's because I didnt release all the Devices and Textures etc when it crashes, and it still thinks I'm using them. It re-opens on a restart. Would someone be able to point me in the right direction as to how to sortof "reset" DirectX?
Here is what I am using to create the device:
D3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS D3DPP;
ZeroMemory(&D3DPP, sizeof(D3DPP));
D3DPP.Windowed = TRUE;
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPP.hDeviceWindow = hWnd;
D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPP.BackBufferWidth = WIDTH;
D3DPP.BackBufferHeight = HEIGHT;
D3DDevice = 0; //Null pointer to make sure I can check it
HRESULT hr = D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDevice);
if (!D3DDevice) {
MessageBox(NULL,"Device could not be created","error",0);
switch (hr) {
case D3DERR_DEVICELOST:
MessageBox(NULL,"1","error",0);
break;
case D3DERR_INVALIDCALL:
MessageBox(NULL,"2","error",0);
break;
case D3DERR_NOTAVAILABLE:
MessageBox(NULL,"3","error",0);
break;
case D3DERR_OUTOFVIDEOMEMORY:
MessageBox(NULL,"4","error",0);
break;
};
return 1;
};
return 0;
Well I have to ask. Do you have a valid hWnd before you make the call?
Make sure it's valid and good things might happen. :-)
It seems something in the MsgProc was causing the window to fail for the device, and my program to crash. Thanks anyway.