OpenTTD Source 20261112-master-gbcbec5e63f
VideoDriver_Win32D3D11 Class Reference

The D3D11 video driver for windows. More...

Inheritance diagram for VideoDriver_Win32D3D11:
VideoDriver_Win32Base VideoDriver Driver

Public Member Functions

 VideoDriver_Win32D3D11 ()
 Constructor for the D3D11 video driver.
std::optional< std::string_view > Start (const StringList &param) override
 Start this driver.
void Stop () override
 Stop this driver.
bool ChangeResolution (int w, int h) override
 Change the resolution of the window.
bool ToggleFullscreen (bool fullscreen) override
 Change the full screen setting.
bool AfterBlitterChange () override
 Callback invoked after the blitter was changed.
bool HasEfficient8Bpp () const override
 Has this video driver an efficient code path for palette animated 8-bpp sprites?
bool UseSystemCursor () override
 Get whether the mouse cursor is drawn by the video driver.
std::string_view GetName () const override
 Get the name of this driver.
std::string_view GetInfoString () const override
 Get some information about the selected driver/backend to be shown to the user.
Public Member Functions inherited from VideoDriver_Win32Base
 VideoDriver_Win32Base (bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
 Create the video driver.
void MakeDirty (int left, int top, int width, int height) override
 Mark a particular area dirty.
void MainLoop () override
 Perform the actual drawing.
void ClaimMousePointer () override
 Claim the exclusive rights for the mouse pointer.
void EditBoxLostFocus () override
 An edit box lost the input focus.
std::vector< int > GetListOfMonitorRefreshRates () override
 Get a list of refresh rates of each available monitor.
Public Member Functions inherited from VideoDriver
 VideoDriver (bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
 Create the video driver.
virtual void ToggleVsync (bool vsync)
 Change the vsync setting.
virtual void PopulateSystemSprites ()
 Populate all sprites in cache.
virtual void ClearSystemSprites ()
 Clear all cached sprites.
virtual bool HasGUI () const
 Whether the driver has a graphical user interface with the end user.
constexpr bool SupportsAnimationBuffer () const
 Does this video driver support a separate animation buffer in addition to the colour buffer?
constexpr uint8_t * GetAnimBuffer () const
 Get a pointer to the animation buffer of the video back-end.
constexpr int GetAnimBufferPitch () const
 Get the animation buffer pitch in uint8_t elements per row.
virtual void EditBoxGainedFocus ()
 An edit box gained the input focus.
void QueueOnMainThread (std::function< void()> &&func)
 Queue a function to be called on the main thread with game state lock held and video buffer locked.
void GameLoopPause ()
 Pause the game-loop for a bit, releasing the game-state lock.
virtual void SetScreensaverInhibited (bool inhibited)
 Prevents the system from going to sleep.
Public Member Functions inherited from Driver
virtual ~Driver ()=default
 Ensure the destructor of the sub classes are called as well.

Protected Member Functions

bool MinimiseRestoreWithToggleFullscreen () const override
 Whether minimising and restoring fullscreen windows uses ToggleFullscreen.
uint8_t GetFullscreenBpp () override
 Colour depth to use for fullscreen display modes.
void Paint () override
 Paint the window.
bool AllocateBackingStore (int w, int h, bool force=false) override
 (Re-)create the backing store.
void * GetVideoPointer () override
 Get a pointer to the video buffer.
void ReleaseVideoPointer () override
 Hand video buffer back to the painting backend.
void PaletteChanged (HWND) override
 Palette of the window has changed.
std::string_view CreateSwapchain ()
 Creates a DXGI swap chain for the current window.
void DestroySwapchain ()
 Destroys the DXGI swap chain and releases all associated resources.
Protected Member Functions inherited from VideoDriver_Win32Base
Dimension GetScreenSize () const override
 Get the resolution of the main screen.
void InputLoop () override
 Handle input logic, is CTRL pressed, should we fast-forward, etc.
bool LockVideoBuffer () override
 Make sure the video buffer is ready for drawing.
void UnlockVideoBuffer () override
 Unlock a previously locked video buffer.
void CheckPaletteAnim () override
 Process any pending palette animation.
bool PollEvent () override
 Process a single system event.
std::optional< std::string_view > Initialize ()
bool MakeWindow (bool full_screen, bool resize=true)
 Instantiate a new window.
void ClientSizeChanged (int w, int h, bool force=false)
 Indicate to the driver the client-size might have changed.
Protected Member Functions inherited from VideoDriver
void UpdateAutoResolution ()
 Apply resolution auto-detection and clamp to sensible defaults.
void StartGameThread ()
 Start the loop for game-tick.
void StopGameThread ()
 Stop the loop for the game-tick.
void Tick ()
 Give the video-driver a tick.
void SleepTillNextTick ()
 Sleep till the next tick is about to happen.
std::chrono::steady_clock::duration GetGameInterval ()
std::chrono::steady_clock::duration GetDrawInterval ()
void DrainCommandQueue ()
 Execute all queued commands.

Protected Attributes

ComPtr< IDXGISwapChain > swap_chain
 DXGI swap chain.
ComPtr< ID3D11RenderTargetView > rendertarget
 D3D11 render target view for the back buffer.
bool dxgi_force_resize_buffer = false
 Flag indicating whether to force resize the swap chain buffer.
bool dxgi_fullscreen = false
 Flag indicating whether we're in fullscreen mode.
UINT dxgi_flags = 0
 DXGI swap chain flags.
std::string driver_info
 Information string about selected driver.
Protected Attributes inherited from VideoDriver_Win32Base
HWND main_wnd = nullptr
 Handle to system window.
bool fullscreen = false
 Whether to use (true) fullscreen mode.
bool has_focus = false
 Does our window have system focus?
Rect dirty_rect
 Region of the screen that needs redrawing.
int width = 0
 Width in pixels of our display surface.
int height = 0
 Height in pixels of our display surface.
int width_org = 0
 Original monitor resolution width, before we changed it.
int height_org = 0
 Original monitor resolution height, before we changed it.
bool buffer_locked = false
 Video buffer was locked by the main thread.
Palette local_palette
 Current palette to use for drawing.
Protected Attributes inherited from VideoDriver
const uint ALLOWED_DRIFT = 5
 How many times videodriver can miss deadlines without it being overly compensated.
std::chrono::steady_clock::time_point next_game_tick
std::chrono::steady_clock::time_point next_draw_tick
std::thread game_thread
std::mutex game_state_mutex
std::mutex game_thread_wait_mutex
uint8_t * anim_buffer = nullptr
 The animation buffer if supported/used.
int anim_pitch = 0
 Pitch of the animation buffer if supported/used.
const bool uses_hardware_acceleration
 Whether hardware acceleration is used or not.
const bool supports_animation_buffer
 Whether this driver supports hardware accelerated animation.
bool fast_forward_key_pressed = false
 The fast-forward key is being pressed.
bool fast_forward_via_key = false
 The fast-forward was enabled by key press.
bool is_game_threaded = true
 Whether the game is allowed to use threads; cleared in the driver implementation constructors if required.

Private Member Functions

std::string_view InitD3D ()
 Check for the needed D3D11 functionality and allocate all resources.
std::string_view Resize (int w, int h)
 Resizes the D3D11 textures to the given width and height.

Private Attributes

ComPtr< ID3D11Device > device
 D3D11 device.
ComPtr< ID3D11DeviceContext > device_ctx
 D3D11 device context.
ComPtr< ID3D11VertexShader > vertex_shader
 Vertex shader for sprite rendering.
ComPtr< ID3D11PixelShader > direct_shader
 Pixel shader for direct rendering.
ComPtr< ID3D11PixelShader > palette_shader
 Pixel shader for palette animation.
ComPtr< ID3D11PixelShader > rgb_mask_blend_shader
 Pixel shader for RGB mask blending.
ComPtr< ID3D11PixelShader > sprite_blend_shader
 Pixel shader for sprite blending.
ComPtr< ID3D11Buffer > constant_buffer
 Constant buffer.
ComPtr< ID3D11Texture2D > vid_texture
 Video texture for the back buffer.
ComPtr< ID3D11Texture2D > vid_texture_staging
 Staging texture for video texture.
ComPtr< ID3D11ShaderResourceView > vid_texture_srv
 Shader resource view for video texture.
ComPtr< ID3D11Texture2D > anim_texture
 Animation texture for palette animation.
ComPtr< ID3D11Texture2D > anim_texture_staging
 Staging texture for animation texture.
ComPtr< ID3D11ShaderResourceView > anim_texture_srv
 Shader resource view for animation texture.
ComPtr< ID3D11Texture1D > pal_texture
 Palette texture for palette animation.
ComPtr< ID3D11ShaderResourceView > pal_texture_srv
 Shader resource view for palette texture.
ComPtr< ID3D11SamplerState > texture_sampler
 Sampler state for texture sampling.

Additional Inherited Members

Public Types inherited from Driver
enum class  Type : uint8_t {
  Begin = 0 , Music = 0 , Sound , Video ,
  End
}
 The type of driver. More...
Static Public Member Functions inherited from VideoDriver
static VideoDriver * GetInstance ()
 Get the currently active instance of the video driver.
static std::string GetCaption ()
 Get the caption to use for the game's title bar.
Static Protected Member Functions inherited from VideoDriver
static void GameThreadThunk (VideoDriver *drv)

Detailed Description

The D3D11 video driver for windows.

Definition at line 120 of file d3d11_v.cpp.

Constructor & Destructor Documentation

◆ VideoDriver_Win32D3D11()

VideoDriver_Win32D3D11::VideoDriver_Win32D3D11 ( )
inline

Constructor for the D3D11 video driver.

Definition at line 125 of file d3d11_v.cpp.

References driver_info, GetName(), and VideoDriver_Win32Base::VideoDriver_Win32Base().

Member Function Documentation

◆ AfterBlitterChange()

bool VideoDriver_Win32D3D11::AfterBlitterChange ( )
overridevirtual

Callback invoked after the blitter was changed.

Returns
True if no error.

Reimplemented from VideoDriver.

Definition at line 602 of file d3d11_v.cpp.

References VideoDriver_Win32Base::ClientSizeChanged(), BlitterFactory::GetCurrentBlitter(), VideoDriver_Win32Base::height, and VideoDriver_Win32Base::width.

◆ AllocateBackingStore()

bool VideoDriver_Win32D3D11::AllocateBackingStore ( int w,
int h,
bool force = false )
overrideprotectedvirtual

(Re-)create the backing store.

Parameters
wThe width of the window.
hThe height of the window.
forceWhether to force full reallocation, instead of not reallocating when size did not change.
Returns
Whether the backing store was (re-)created.

Implements VideoDriver_Win32Base.

Definition at line 609 of file d3d11_v.cpp.

References CreateSwapchain(), Critical, Debug, device, VideoDriver_Win32Base::dirty_rect, Driver, dxgi_flags, dxgi_force_resize_buffer, GetVideoPointer(), VideoDriver_Win32Base::height, ReleaseVideoPointer(), rendertarget, Resize(), swap_chain, and VideoDriver_Win32Base::width.

Referenced by Paint().

◆ ChangeResolution()

bool VideoDriver_Win32D3D11::ChangeResolution ( int w,
int h )
overridevirtual

Change the resolution of the window.

Parameters
wThe new width.
hThe new height.
Returns
True if the change succeeded.

Reimplemented from VideoDriver_Win32Base.

Definition at line 576 of file d3d11_v.cpp.

References _window_maximize, VideoDriver_Win32Base::main_wnd, and swap_chain.

◆ CreateSwapchain()

std::string_view VideoDriver_Win32D3D11::CreateSwapchain ( )
protected

Creates a DXGI swap chain for the current window.

Returns
Error string or nullptr if successful.

Definition at line 492 of file d3d11_v.cpp.

References _video_vsync, device, dxgi_flags, dxgi_fullscreen, and VideoDriver_Win32Base::main_wnd.

Referenced by AllocateBackingStore().

◆ DestroySwapchain()

void VideoDriver_Win32D3D11::DestroySwapchain ( )
protected

Destroys the DXGI swap chain and releases all associated resources.

Definition at line 569 of file d3d11_v.cpp.

References dxgi_fullscreen, rendertarget, and swap_chain.

Referenced by Stop().

◆ GetFullscreenBpp()

uint8_t VideoDriver_Win32D3D11::GetFullscreenBpp ( )
inlineoverrideprotectedvirtual

Colour depth to use for fullscreen display modes.

Returns
The colour depth in bits per pixel.

Reimplemented from VideoDriver_Win32Base.

Definition at line 174 of file d3d11_v.cpp.

◆ GetInfoString()

std::string_view VideoDriver_Win32D3D11::GetInfoString ( ) const
inlineoverridevirtual

Get some information about the selected driver/backend to be shown to the user.

Returns
The information.

Reimplemented from VideoDriver.

Definition at line 154 of file d3d11_v.cpp.

References driver_info.

◆ GetName()

std::string_view VideoDriver_Win32D3D11::GetName ( ) const
inlineoverridevirtual

Get the name of this driver.

Returns
The name of the driver.

Implements Driver.

Definition at line 149 of file d3d11_v.cpp.

Referenced by Start(), and VideoDriver_Win32D3D11().

◆ GetVideoPointer()

void * VideoDriver_Win32D3D11::GetVideoPointer ( )
overrideprotectedvirtual

Get a pointer to the video buffer.

Returns
The pointer.

Implements VideoDriver_Win32Base.

Definition at line 660 of file d3d11_v.cpp.

References VideoDriver::anim_buffer, VideoDriver::anim_pitch, anim_texture_staging, device_ctx, BlitterFactory::GetCurrentBlitter(), Blitter::GetScreenDepth(), and vid_texture_staging.

Referenced by AllocateBackingStore().

◆ HasEfficient8Bpp()

bool VideoDriver_Win32D3D11::HasEfficient8Bpp ( ) const
inlineoverridevirtual

Has this video driver an efficient code path for palette animated 8-bpp sprites?

Returns
True if the driver has an efficient code path for 8-bpp.

Reimplemented from VideoDriver.

Definition at line 139 of file d3d11_v.cpp.

◆ InitD3D()

std::string_view VideoDriver_Win32D3D11::InitD3D ( )
private

Check for the needed D3D11 functionality and allocate all resources.

Returns
Error string or nullptr if successful.

Definition at line 246 of file d3d11_v.cpp.

References _D3D11CreateDevice, _frag_shader_direct_hlsl, _frag_shader_palette_hlsl, _frag_shader_rgb_mask_blend_hlsl, _frag_shader_sprite_blend_hlsl, _vertex_shader_sprite_hlsl, constant_buffer, CreatePixelShader(), CreateVertexShader(), device, device_ctx, pal_texture, and texture_sampler.

Referenced by Start().

◆ MinimiseRestoreWithToggleFullscreen()

bool VideoDriver_Win32D3D11::MinimiseRestoreWithToggleFullscreen ( ) const
inlineoverrideprotectedvirtual

Whether minimising and restoring fullscreen windows uses ToggleFullscreen.

Returns
True if ToggleFullscreen should be used.

Reimplemented from VideoDriver_Win32Base.

Definition at line 169 of file d3d11_v.cpp.

◆ Paint()

◆ PaletteChanged()

void VideoDriver_Win32D3D11::PaletteChanged ( HWND hWnd)
inlineoverrideprotectedvirtual

Palette of the window has changed.

Parameters
hWndThe window handle of the changed window.

Implements VideoDriver_Win32Base.

Definition at line 184 of file d3d11_v.cpp.

◆ ReleaseVideoPointer()

void VideoDriver_Win32D3D11::ReleaseVideoPointer ( )
overrideprotectedvirtual

Hand video buffer back to the painting backend.

Reimplemented from VideoDriver_Win32Base.

Definition at line 678 of file d3d11_v.cpp.

References VideoDriver::anim_buffer, VideoDriver::anim_pitch, anim_texture, anim_texture_staging, device_ctx, VideoDriver_Win32Base::dirty_rect, IsEmptyRect(), vid_texture, and vid_texture_staging.

Referenced by AllocateBackingStore(), and Start().

◆ Resize()

std::string_view VideoDriver_Win32D3D11::Resize ( int w,
int h )
private

Resizes the D3D11 textures to the given width and height.

Parameters
wThe new width.
hThe new height.
Returns
Error string or nullptr if successful.

Definition at line 338 of file d3d11_v.cpp.

References anim_texture, anim_texture_staging, device, BlitterFactory::GetCurrentBlitter(), Blitter::GetScreenDepth(), vid_texture, and vid_texture_staging.

Referenced by AllocateBackingStore().

◆ Start()

std::optional< std::string_view > VideoDriver_Win32D3D11::Start ( const StringList & parm)
overridevirtual

◆ Stop()

void VideoDriver_Win32D3D11::Stop ( )
overridevirtual

Stop this driver.

Reimplemented from VideoDriver_Win32Base.

Definition at line 479 of file d3d11_v.cpp.

References _D3D11CreateDevice, _D3DCompile, DestroySwapchain(), and VideoDriver_Win32Base::Stop().

◆ ToggleFullscreen()

bool VideoDriver_Win32D3D11::ToggleFullscreen ( bool fullscreen)
overridevirtual

Change the full screen setting.

Parameters
fullscreenThe new setting.
Returns
True if the change succeeded.

Reimplemented from VideoDriver_Win32Base.

Definition at line 588 of file d3d11_v.cpp.

References dxgi_force_resize_buffer, dxgi_fullscreen, VideoDriver_Win32Base::fullscreen, InvalidateWindowClassesData(), and swap_chain.

Referenced by Start().

◆ UseSystemCursor()

bool VideoDriver_Win32D3D11::UseSystemCursor ( )
inlineoverridevirtual

Get whether the mouse cursor is drawn by the video driver.

Returns
True if cursor drawing is done by the video driver.

Reimplemented from VideoDriver.

Definition at line 144 of file d3d11_v.cpp.

Field Documentation

◆ anim_texture

ComPtr<ID3D11Texture2D> VideoDriver_Win32D3D11::anim_texture
private

Animation texture for palette animation.

Definition at line 207 of file d3d11_v.cpp.

Referenced by ReleaseVideoPointer(), and Resize().

◆ anim_texture_srv

ComPtr<ID3D11ShaderResourceView> VideoDriver_Win32D3D11::anim_texture_srv
private

Shader resource view for animation texture.

Definition at line 209 of file d3d11_v.cpp.

Referenced by Paint().

◆ anim_texture_staging

ComPtr<ID3D11Texture2D> VideoDriver_Win32D3D11::anim_texture_staging
private

Staging texture for animation texture.

Definition at line 208 of file d3d11_v.cpp.

Referenced by GetVideoPointer(), ReleaseVideoPointer(), and Resize().

◆ constant_buffer

ComPtr<ID3D11Buffer> VideoDriver_Win32D3D11::constant_buffer
private

Constant buffer.

Definition at line 201 of file d3d11_v.cpp.

Referenced by InitD3D(), and Paint().

◆ device

ComPtr<ID3D11Device> VideoDriver_Win32D3D11::device
private

D3D11 device.

Definition at line 192 of file d3d11_v.cpp.

Referenced by AllocateBackingStore(), CreateSwapchain(), InitD3D(), Resize(), and Start().

◆ device_ctx

ComPtr<ID3D11DeviceContext> VideoDriver_Win32D3D11::device_ctx
private

D3D11 device context.

Definition at line 193 of file d3d11_v.cpp.

Referenced by GetVideoPointer(), InitD3D(), Paint(), and ReleaseVideoPointer().

◆ direct_shader

ComPtr<ID3D11PixelShader> VideoDriver_Win32D3D11::direct_shader
private

Pixel shader for direct rendering.

Definition at line 196 of file d3d11_v.cpp.

Referenced by Paint().

◆ driver_info

std::string VideoDriver_Win32D3D11::driver_info
protected

Information string about selected driver.

Definition at line 167 of file d3d11_v.cpp.

Referenced by GetInfoString(), Start(), and VideoDriver_Win32D3D11().

◆ dxgi_flags

UINT VideoDriver_Win32D3D11::dxgi_flags = 0
protected

DXGI swap chain flags.

Definition at line 165 of file d3d11_v.cpp.

Referenced by AllocateBackingStore(), CreateSwapchain(), and Paint().

◆ dxgi_force_resize_buffer

bool VideoDriver_Win32D3D11::dxgi_force_resize_buffer = false
protected

Flag indicating whether to force resize the swap chain buffer.

Definition at line 163 of file d3d11_v.cpp.

Referenced by AllocateBackingStore(), Paint(), and ToggleFullscreen().

◆ dxgi_fullscreen

bool VideoDriver_Win32D3D11::dxgi_fullscreen = false
protected

Flag indicating whether we're in fullscreen mode.

Definition at line 164 of file d3d11_v.cpp.

Referenced by CreateSwapchain(), DestroySwapchain(), Paint(), Start(), and ToggleFullscreen().

◆ pal_texture

ComPtr<ID3D11Texture1D> VideoDriver_Win32D3D11::pal_texture
private

Palette texture for palette animation.

Definition at line 211 of file d3d11_v.cpp.

Referenced by InitD3D(), and Paint().

◆ pal_texture_srv

ComPtr<ID3D11ShaderResourceView> VideoDriver_Win32D3D11::pal_texture_srv
private

Shader resource view for palette texture.

Definition at line 212 of file d3d11_v.cpp.

Referenced by Paint().

◆ palette_shader

ComPtr<ID3D11PixelShader> VideoDriver_Win32D3D11::palette_shader
private

Pixel shader for palette animation.

Definition at line 197 of file d3d11_v.cpp.

Referenced by Paint().

◆ rendertarget

ComPtr<ID3D11RenderTargetView> VideoDriver_Win32D3D11::rendertarget
protected

D3D11 render target view for the back buffer.

Definition at line 161 of file d3d11_v.cpp.

Referenced by AllocateBackingStore(), DestroySwapchain(), and Paint().

◆ rgb_mask_blend_shader

ComPtr<ID3D11PixelShader> VideoDriver_Win32D3D11::rgb_mask_blend_shader
private

Pixel shader for RGB mask blending.

Definition at line 198 of file d3d11_v.cpp.

Referenced by Paint().

◆ sprite_blend_shader

ComPtr<ID3D11PixelShader> VideoDriver_Win32D3D11::sprite_blend_shader
private

Pixel shader for sprite blending.

Definition at line 199 of file d3d11_v.cpp.

◆ swap_chain

ComPtr<IDXGISwapChain> VideoDriver_Win32D3D11::swap_chain
protected

DXGI swap chain.

Definition at line 160 of file d3d11_v.cpp.

Referenced by AllocateBackingStore(), ChangeResolution(), DestroySwapchain(), Paint(), and ToggleFullscreen().

◆ texture_sampler

ComPtr<ID3D11SamplerState> VideoDriver_Win32D3D11::texture_sampler
private

Sampler state for texture sampling.

Definition at line 214 of file d3d11_v.cpp.

Referenced by InitD3D(), and Paint().

◆ vertex_shader

ComPtr<ID3D11VertexShader> VideoDriver_Win32D3D11::vertex_shader
private

Vertex shader for sprite rendering.

Definition at line 195 of file d3d11_v.cpp.

Referenced by Paint().

◆ vid_texture

ComPtr<ID3D11Texture2D> VideoDriver_Win32D3D11::vid_texture
private

Video texture for the back buffer.

Definition at line 203 of file d3d11_v.cpp.

Referenced by ReleaseVideoPointer(), and Resize().

◆ vid_texture_srv

ComPtr<ID3D11ShaderResourceView> VideoDriver_Win32D3D11::vid_texture_srv
private

Shader resource view for video texture.

Definition at line 205 of file d3d11_v.cpp.

Referenced by Paint().

◆ vid_texture_staging

ComPtr<ID3D11Texture2D> VideoDriver_Win32D3D11::vid_texture_staging
private

Staging texture for video texture.

Definition at line 204 of file d3d11_v.cpp.

Referenced by GetVideoPointer(), ReleaseVideoPointer(), and Resize().


The documentation for this class was generated from the following file: