RenderingDevice::RenderingDevice(IDirect3DDevice9Ex *device, int renderWidth, int renderHeight) : mDevice(device), mRenderWidth(renderWidth), mRenderHeight(renderHeight), mShaders(*this), mTextures(*this, 128 * 1024 * 1024) { Expects(!renderingDevice); renderingDevice = this; mCamera.SetScreenWidth((float)renderWidth, (float)renderHeight); // TODO: color bullshit is not yet done (tig_d3d_init_handleformat et al) // Get the device caps for real this time. ReadCaps(); // Get the currently attached backbuffer if (D3DLOG(mDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer)) != D3D_OK) { throw TempleException("Unable to retrieve the back buffer"); } if (D3DLOG(mDevice->GetDepthStencilSurface(&mBackBufferDepth)) != D3D_OK) { throw TempleException("Unable to retrieve depth/stencil surface from device"); } memset(&mBackBufferDesc, 0, sizeof(mBackBufferDesc)); if (D3DLOG(mBackBuffer->GetDesc(&mBackBufferDesc)) != D3D_OK) { throw TempleException("Unable to retrieve back buffer description"); } // Create surfaces for the scene D3DLOG(mDevice->CreateRenderTarget( mRenderWidth, mRenderHeight, mBackBufferDesc.Format, D3DMULTISAMPLE_16_SAMPLES, 0, FALSE, &mSceneSurface, nullptr)); D3DLOG(mDevice->CreateDepthStencilSurface( mRenderWidth, mRenderHeight, D3DFMT_D16, D3DMULTISAMPLE_16_SAMPLES, 0, TRUE, &mSceneDepthSurface, nullptr)); for (auto &listener : mResourcesListeners) { listener->CreateResources(*this); } mResourcesCreated = true; }
RenderingDevice::RenderingDevice(HWND windowHandle, int renderWidth, int renderHeight, bool antiAliasing) : mWindowHandle(windowHandle), mRenderWidth(renderWidth), mRenderHeight(renderHeight), mShaders(*this), mTextures(*this, 128 * 1024 * 1024), mAntiAliasing(antiAliasing) { Expects(!renderingDevice); renderingDevice = this; HRESULT status; status = D3DLOG(Direct3DCreate9Ex(D3D_SDK_VERSION, &mDirect3d9)); if (status != D3D_OK) { throw TempleException("Unable to create Direct3D9Ex interface."); } // At this point we only do a GetDisplayMode to check the resolution. We could also do this elsewhere D3DDISPLAYMODE displayMode; status = D3DLOG(mDirect3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)); if (status != D3D_OK) { throw TempleException("Unable to query display mode for primary adapter."); } D3DMULTISAMPLE_TYPE aaTypes[] = { D3DMULTISAMPLE_2_SAMPLES, D3DMULTISAMPLE_3_SAMPLES, D3DMULTISAMPLE_4_SAMPLES, D3DMULTISAMPLE_5_SAMPLES, D3DMULTISAMPLE_6_SAMPLES, D3DMULTISAMPLE_7_SAMPLES, D3DMULTISAMPLE_8_SAMPLES, D3DMULTISAMPLE_9_SAMPLES, D3DMULTISAMPLE_10_SAMPLES, D3DMULTISAMPLE_11_SAMPLES, D3DMULTISAMPLE_12_SAMPLES, D3DMULTISAMPLE_13_SAMPLES, D3DMULTISAMPLE_14_SAMPLES, D3DMULTISAMPLE_15_SAMPLES, D3DMULTISAMPLE_16_SAMPLES }; for (auto type : aaTypes) { status = mDirect3d9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, TRUE, type, nullptr); if (status == D3D_OK) { logger->trace("AA method {} is available", type); mSupportedAaSamples.push_back(type); } } // We need at least 1024x768 if (displayMode.Width < 1024 || displayMode.Height < 768) { throw TempleException("You need at least a display resolution of 1024x768."); } CreatePresentParams(); status = D3DLOG(mDirect3d9->CreateDeviceEx( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mWindowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &mPresentParams, nullptr, &mDevice)); if (status != D3D_OK) { throw TempleException("Unable to create Direct3D9 device!"); } // TODO: color bullshit is not yet done (tig_d3d_init_handleformat et al) // Get the device caps for real this time. ReadCaps(); // Get the currently attached backbuffer if (D3DLOG(mDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer)) != D3D_OK) { throw TempleException("Unable to retrieve the back buffer"); } if (D3DLOG(mDevice->GetDepthStencilSurface(&mBackBufferDepth)) != D3D_OK) { throw TempleException("Unable to retrieve depth/stencil surface from device"); } memset(&mBackBufferDesc, 0, sizeof(mBackBufferDesc)); if (D3DLOG(mBackBuffer->GetDesc(&mBackBufferDesc)) != D3D_OK) { throw TempleException("Unable to retrieve back buffer description"); } SetRenderSize(renderWidth, renderHeight); for (auto &listener : mResourcesListeners) { listener->CreateResources(*this); } mResourcesCreated = true; }
void Graphics::InitializeDirect3d() { HRESULT d3dresult; /* Set some global flags. */ if (config.useDirect3d9Ex) { logger->info("Using Direct3D9Ex mode"); d3dresult = D3DLOG(Direct3DCreate9Ex(D3D_SDK_VERSION, &mDirect3d9)); if (d3dresult != D3D_OK) { throw TempleException("Unable to create Direct3D9 interface."); } } else { logger->info("Using standard Direct3D9 mode"); mDirect3d9 = static_cast<IDirect3D9Ex*>(Direct3DCreate9(D3D_SDK_VERSION)); if (!mDirect3d9) { throw TempleException("Unable to create Direct3D9 interface."); } } /** START OF OLD WINDOWED INIT */ // At this point we only do a GetDisplayMode to check the resolution. We could also do this elsewhere D3DDISPLAYMODE displayMode; d3dresult = D3DLOG(mDirect3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)); if (d3dresult != D3D_OK) { throw TempleException("Unable to query display mode for primary adapter."); } // We need at least 1024x768 if (displayMode.Width < 1024 || displayMode.Height < 768) { throw TempleException("You need at least a display resolution of 1024x768."); } auto presentParams = CreatePresentParams(); // presentParams.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES; // presentParams.MultiSampleQuality = 0; // Nvidia drivers seriously barf on D3d9ex if we use software vertex processing here, as ToEE specifies. // I think we are safe with hardware vertex processing, since HW T&L has been standard for more than 10 years. if (config.useDirect3d9Ex) { logger->info("Creating Direct3D9Ex device."); d3dresult = D3DLOG(mDirect3d9->CreateDeviceEx( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mMainWindow.GetHwnd(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &presentParams, nullptr, &mDevice)); } else { logger->info("Creating Direct3D9 device."); d3dresult = D3DLOG(mDirect3d9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, mMainWindow.GetHwnd(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &presentParams, reinterpret_cast<IDirect3DDevice9**>(&mDevice))); } if (d3dresult != D3D_OK) { throw TempleException("Unable to create Direct3D9 device!"); } // TODO: color bullshit is not yet done (tig_d3d_init_handleformat et al) // Get the device caps for real this time. ReadCaps(); SetDefaultRenderStates(); CreateResources(); }