没有设备窗口的DirectX9

时间:2017-01-13 10:33:42

标签: c++ handle directx-9

我一直认为在创建DirectX9设备之前需要先创建一个窗口。实际上,这就是我如何理解CreateDevice方法的official documentation

  

hFocusWindow [in]类型:HWND(...)对于窗口模式,仅当pPresentationParameters的hDeviceWindow成员设置为有效的非NULL值时,此参数才可以为NULL。

现在,我只是尝试了一下,似乎下面的一段代码对我有效(例如,我得到一个有效的设备指针,我可以用它来做渲染目标渲染,即使我没有&#39 ; t随时提供任何窗口句柄):

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <chrono>
#include <thread>

#if defined(_MSC_VER)
    #pragma warning(disable : 4005)
#endif

#include <d3d9.h>
#include <d3dx9.h>
#include <DxErr.h>

int (WINAPIV * __vsnprintf)(char *, size_t, const char*, va_list) = _vsnprintf;

#define DEBUG_MSG(msg) std::cout << msg << std::endl;
#define ERROR_MSG(msg) std::cout << "[ERROR] " << msg << std::endl;

#define THROW_MSG(msg) { \
    std::ostringstream os; \
    os.precision(9); \
    os << std::fixed << "[FATAL] " << msg << " (at " << __FILE__ <<":"<<__LINE__<<")"; \
    DEBUG_MSG(os.str()); \
    throw std::runtime_error(os.str()); \
}

#define CHECK(cond,msg) if(!(cond)) { THROW_MSG(msg); return; }
#define CHECK_RET(cond,ret,msg) if(!(cond)) { THROW_MSG(msg); return ret; }
#define CHECK_RESULT(val,msg) { HRESULT hr = (val); if(FAILED(hr)) { THROW_MSG(msg << ", err=" << DXGetErrorString(hr) << ", desc=" << DXGetErrorDescription(hr)); return; } }
#define CHECK_RESULT_RET(val,ret,msg) { HRESULT hr = (val); if(FAILED(hr)) { THROW_MSG(msg << ", err=" << DXGetErrorString(hr) << ", desc=" << DXGetErrorDescription(hr)); return ret; } }

#define SAFERELEASE(x) if(x) { x->Release(); x = NULL; }

IDirect3D9Ex* d3dEx = nullptr;
IDirect3DDevice9* deviceEx = nullptr;

IDirect3DSurface9* renderSurface1 = nullptr;
HANDLE                     renderSurfaceHandle1 = nullptr;
IDirect3DSurface9* renderSurface2 = nullptr;
HANDLE                     renderSurfaceHandle2 = nullptr;

bool testCycle() {
  CHECK_RET(d3dEx==nullptr, false,"Invalid D3D context.");
  CHECK_RET(deviceEx==nullptr, false,"Invalid D3D device.");

  // Create dedicated device:
  DEBUG_MSG("Creating Direct3D9Ex context");
  CHECK_RESULT_RET(Direct3DCreate9Ex(D3D_SDK_VERSION, (IDirect3D9Ex **)&d3dEx), 
    false, "Cannot create Direct3D9Ex context" );

  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp, sizeof(d3dpp));
  d3dpp.Windowed = TRUE;
  d3dpp.BackBufferCount = 1;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferWidth = 200; 
  d3dpp.BackBufferHeight = 200; 
  d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  d3dpp.hDeviceWindow = NULL;
  d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

  DEBUG_MSG("Creating Device9Ex.");
  CHECK_RESULT_RET(d3dEx->CreateDevice(0, D3DDEVTYPE_HAL, NULL, 
    D3DCREATE_MULTITHREADED | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, 
    &d3dpp, &deviceEx), false, "Cannot create Device9Ex.");

  DEBUG_MSG("Setting lighting render state");
  CHECK_RESULT_RET(deviceEx->SetRenderState( D3DRS_LIGHTING, FALSE ), 
    false, "Cannot set lighting render state.");

  int width = 512;
  int height = 256;

  DEBUG_MSG("Creating SDI render surfaces of size "<<width<<"x"<<height);

  CHECK_RESULT_RET(deviceEx->CreateRenderTarget(width, height,
                                          D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0,
                                          TRUE, &renderSurface1, &renderSurfaceHandle1),
               false, "Cannot create Render surface 1 for SDIOutput");

  CHECK_RESULT_RET(deviceEx->CreateRenderTarget(width, height,
                                          D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0,
                                          TRUE, &renderSurface2, &renderSurfaceHandle2),
               false, "Cannot create Render surface 2 for SDIOutput");

  CHECK_RET(renderSurfaceHandle1 != nullptr, false, "Invalid shared handle for surface 1");
  CHECK_RET(renderSurfaceHandle2 != nullptr, false, "Invalid shared handle for surface 2");

  DEBUG_MSG("Initial render of SDI surface 1")
  CHECK_RESULT_RET(deviceEx->SetRenderTarget(0, renderSurface1), false, "Cannot set render target 1");
  CHECK_RESULT_RET(deviceEx->BeginScene(),false, "Cannot begin scene 1");
  CHECK_RESULT_RET(deviceEx->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,0,0), 1.0f, 0), false, "Cannot clear scene 1");
  CHECK_RESULT_RET(deviceEx->EndScene(), false, "Cannot end scene 1");

  DEBUG_MSG("Initial render of SDI surface 2")
  CHECK_RESULT_RET(deviceEx->SetRenderTarget(0, renderSurface2), false, "Cannot set render target 2");
  CHECK_RESULT_RET(deviceEx->BeginScene(), false, "Cannot begin scene 2");
  CHECK_RESULT_RET(deviceEx->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0), false, "Cannot clear scene 2");
  CHECK_RESULT_RET(deviceEx->EndScene(), false, "Cannot end scene 2");

  DEBUG_MSG("Test cycle performed successfully.")
  return true;
}

void releaseResources()
{
  DEBUG_MSG("Releasing resources.");

  SAFERELEASE(renderSurface1);
  SAFERELEASE(renderSurface2);
  renderSurfaceHandle1 = nullptr;
  renderSurfaceHandle2 = nullptr;

  SAFERELEASE(deviceEx);
  SAFERELEASE(d3dEx);
}

#ifdef WINDOWS_APP
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
#else
int main(int argc, char *argv[]) {
#endif

  testCycle();
  releaseResources();

  DEBUG_MSG("Exiting.");
  return 0;
}

所以,任何人都开始对此进行解释,文档是否不正确或者我错过了一点? : - )

2 个答案:

答案 0 :(得分:0)

文档告诉您必须执行的操作,但没有明确说明违反规则时可能发生的情况。

它似乎对您有用,但您是否在多显示器设置上烦恼测试?或者在运行另一个DirectX应用程序时?这只是两个随机的变化。

答案 1 :(得分:0)

文档已经告诉你发生了什么:

MSDN

  

对于窗口模式应用程序,此句柄将是Present的默认目标窗口。如果此句柄为NULL,则将执行焦点窗口。

因此,当您拨打Present时,它正在使用任何关注的窗口。

请注意,Direct3D 10或更高版本通过DXGI修复了这种歧义。只有创建交换链才需要窗口句柄,并且可以独立于交换链创建Direct3D设备(除非您使用的是稍微笨重的辅助函数D3D11CreateDeviceAndSwapChain,它同时执行两个操作)。请参阅Anatomy of Direct3D 11 Create Device