2020-10-12 23:34:22 +08:00
// dear imgui: Renderer Backend for DirectX11
// This needs to be used along with a Platform Backend (e.g. Win32)
2018-02-06 03:34:11 +08:00
// Implemented features:
2019-10-16 17:23:15 +08:00
// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID!
2022-05-03 23:53:47 +08:00
// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
2023-02-07 23:23:49 +08:00
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
2016-03-24 18:00:47 +08:00
2021-06-28 22:52:10 +08:00
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
2021-05-27 19:59:35 +08:00
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
2020-10-14 18:22:53 +08:00
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
// Read online: https://github.com/ocornut/imgui/tree/master/docs
2015-03-09 21:45:23 +08:00
2018-02-17 00:20:18 +08:00
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
2023-01-04 04:06:09 +08:00
// 2023-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
2022-10-11 18:22:29 +08:00
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
2021-06-28 22:52:10 +08:00
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
2021-02-07 19:36:54 +08:00
// 2021-05-19: DirectX11: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
2021-02-18 02:29:07 +08:00
// 2021-02-18: DirectX11: Change blending equation to preserve alpha in output buffer.
2019-08-02 01:58:41 +08:00
// 2019-08-01: DirectX11: Fixed code querying the Geometry Shader state (would generally error with Debug layer enabled).
2019-07-22 10:26:13 +08:00
// 2019-07-21: DirectX11: Backup, clear and restore Geometry Shader is any is bound when calling ImGui_ImplDX10_RenderDrawData. Clearing Hull/Domain/Compute shaders without backup/restore.
2019-05-29 22:29:17 +08:00
// 2019-05-29: DirectX11: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
2019-05-01 04:28:29 +08:00
// 2019-04-30: DirectX11: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
2018-12-04 00:47:10 +08:00
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
2018-12-01 01:18:15 +08:00
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
2018-08-01 18:23:59 +08:00
// 2018-08-01: DirectX11: Querying for IDXGIFactory instead of IDXGIFactory1 to increase compatibility.
2018-07-13 17:25:54 +08:00
// 2018-07-13: DirectX11: Fixed unreleased resources in Init and Shutdown functions.
2018-06-09 01:37:33 +08:00
// 2018-06-08: Misc: Extracted imgui_impl_dx11.cpp/.h away from the old combined DX11+Win32 example.
// 2018-06-08: DirectX11: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
2018-02-17 02:18:16 +08:00
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
2018-02-17 00:20:18 +08:00
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
// 2016-05-07: DirectX11: Disabling depth-write.
2018-01-29 21:38:46 +08:00
# include "imgui.h"
2015-03-09 21:45:23 +08:00
# include "imgui_impl_dx11.h"
// DirectX
2018-04-19 23:23:43 +08:00
# include <stdio.h>
2015-03-09 21:45:23 +08:00
# include <d3d11.h>
# include <d3dcompiler.h>
2018-12-04 00:47:10 +08:00
# ifdef _MSC_VER
# pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
# endif
2015-03-09 21:45:23 +08:00
2021-06-28 22:52:10 +08:00
// DirectX11 data
struct ImGui_ImplDX11_Data
{
ID3D11Device * pd3dDevice ;
ID3D11DeviceContext * pd3dDeviceContext ;
IDXGIFactory * pFactory ;
ID3D11Buffer * pVB ;
ID3D11Buffer * pIB ;
ID3D11VertexShader * pVertexShader ;
ID3D11InputLayout * pInputLayout ;
ID3D11Buffer * pVertexConstantBuffer ;
ID3D11PixelShader * pPixelShader ;
ID3D11SamplerState * pFontSampler ;
ID3D11ShaderResourceView * pFontTextureView ;
ID3D11RasterizerState * pRasterizerState ;
ID3D11BlendState * pBlendState ;
ID3D11DepthStencilState * pDepthStencilState ;
int VertexBufferSize ;
int IndexBufferSize ;
2022-03-13 14:15:32 +08:00
ImGui_ImplDX11_Data ( ) { memset ( ( void * ) this , 0 , sizeof ( * this ) ) ; VertexBufferSize = 5000 ; IndexBufferSize = 10000 ; }
2021-06-28 22:52:10 +08:00
} ;
2022-06-11 18:52:27 +08:00
struct VERTEX_CONSTANT_BUFFER_DX11
2015-03-09 21:45:23 +08:00
{
2018-06-09 01:37:33 +08:00
float mvp [ 4 ] [ 4 ] ;
2015-03-09 21:45:23 +08:00
} ;
2021-06-30 21:22:15 +08:00
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
static ImGui_ImplDX11_Data * ImGui_ImplDX11_GetBackendData ( )
{
2022-10-11 18:22:29 +08:00
return ImGui : : GetCurrentContext ( ) ? ( ImGui_ImplDX11_Data * ) ImGui : : GetIO ( ) . BackendRendererUserData : nullptr ;
2021-06-30 21:22:15 +08:00
}
2018-02-28 06:26:51 +08:00
// Forward Declarations
static void ImGui_ImplDX11_InitPlatformInterface ( ) ;
static void ImGui_ImplDX11_ShutdownPlatformInterface ( ) ;
2021-06-28 22:52:10 +08:00
// Functions
2019-05-01 04:15:59 +08:00
static void ImGui_ImplDX11_SetupRenderState ( ImDrawData * draw_data , ID3D11DeviceContext * ctx )
{
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2019-05-01 04:15:59 +08:00
// Setup viewport
D3D11_VIEWPORT vp ;
memset ( & vp , 0 , sizeof ( D3D11_VIEWPORT ) ) ;
vp . Width = draw_data - > DisplaySize . x ;
vp . Height = draw_data - > DisplaySize . y ;
vp . MinDepth = 0.0f ;
vp . MaxDepth = 1.0f ;
vp . TopLeftX = vp . TopLeftY = 0 ;
ctx - > RSSetViewports ( 1 , & vp ) ;
// Setup shader and vertex buffers
unsigned int stride = sizeof ( ImDrawVert ) ;
unsigned int offset = 0 ;
2021-06-28 22:52:10 +08:00
ctx - > IASetInputLayout ( bd - > pInputLayout ) ;
ctx - > IASetVertexBuffers ( 0 , 1 , & bd - > pVB , & stride , & offset ) ;
ctx - > IASetIndexBuffer ( bd - > pIB , sizeof ( ImDrawIdx ) = = 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT , 0 ) ;
2019-05-01 04:15:59 +08:00
ctx - > IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
2022-10-11 18:22:29 +08:00
ctx - > VSSetShader ( bd - > pVertexShader , nullptr , 0 ) ;
2021-06-28 22:52:10 +08:00
ctx - > VSSetConstantBuffers ( 0 , 1 , & bd - > pVertexConstantBuffer ) ;
2022-10-11 18:22:29 +08:00
ctx - > PSSetShader ( bd - > pPixelShader , nullptr , 0 ) ;
2021-06-28 22:52:10 +08:00
ctx - > PSSetSamplers ( 0 , 1 , & bd - > pFontSampler ) ;
2022-10-11 18:22:29 +08:00
ctx - > GSSetShader ( nullptr , nullptr , 0 ) ;
ctx - > HSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
ctx - > DSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
ctx - > CSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
2019-05-01 04:15:59 +08:00
// Setup blend state
const float blend_factor [ 4 ] = { 0.f , 0.f , 0.f , 0.f } ;
2021-06-28 22:52:10 +08:00
ctx - > OMSetBlendState ( bd - > pBlendState , blend_factor , 0xffffffff ) ;
ctx - > OMSetDepthStencilState ( bd - > pDepthStencilState , 0 ) ;
ctx - > RSSetState ( bd - > pRasterizerState ) ;
2019-05-01 04:15:59 +08:00
}
2018-02-17 02:18:16 +08:00
// Render function
void ImGui_ImplDX11_RenderDrawData ( ImDrawData * draw_data )
2015-03-09 21:45:23 +08:00
{
2019-04-16 00:47:36 +08:00
// Avoid rendering when minimized
if ( draw_data - > DisplaySize . x < = 0.0f | | draw_data - > DisplaySize . y < = 0.0f )
return ;
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
ID3D11DeviceContext * ctx = bd - > pd3dDeviceContext ;
2016-04-03 18:48:38 +08:00
2015-08-14 13:13:20 +08:00
// Create and grow vertex/index buffers if needed
2021-06-28 22:52:10 +08:00
if ( ! bd - > pVB | | bd - > VertexBufferSize < draw_data - > TotalVtxCount )
2015-08-14 13:13:20 +08:00
{
2022-10-11 18:22:29 +08:00
if ( bd - > pVB ) { bd - > pVB - > Release ( ) ; bd - > pVB = nullptr ; }
2021-06-28 22:52:10 +08:00
bd - > VertexBufferSize = draw_data - > TotalVtxCount + 5000 ;
2015-08-14 13:13:20 +08:00
D3D11_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D11_BUFFER_DESC ) ) ;
desc . Usage = D3D11_USAGE_DYNAMIC ;
2021-06-28 22:52:10 +08:00
desc . ByteWidth = bd - > VertexBufferSize * sizeof ( ImDrawVert ) ;
2015-08-14 13:13:20 +08:00
desc . BindFlags = D3D11_BIND_VERTEX_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
2022-10-11 18:22:29 +08:00
if ( bd - > pd3dDevice - > CreateBuffer ( & desc , nullptr , & bd - > pVB ) < 0 )
2015-08-14 13:13:20 +08:00
return ;
}
2021-06-28 22:52:10 +08:00
if ( ! bd - > pIB | | bd - > IndexBufferSize < draw_data - > TotalIdxCount )
2015-08-14 13:13:20 +08:00
{
2022-10-11 18:22:29 +08:00
if ( bd - > pIB ) { bd - > pIB - > Release ( ) ; bd - > pIB = nullptr ; }
2021-06-28 22:52:10 +08:00
bd - > IndexBufferSize = draw_data - > TotalIdxCount + 10000 ;
2016-04-03 19:02:04 +08:00
D3D11_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D11_BUFFER_DESC ) ) ;
desc . Usage = D3D11_USAGE_DYNAMIC ;
2021-06-28 22:52:10 +08:00
desc . ByteWidth = bd - > IndexBufferSize * sizeof ( ImDrawIdx ) ;
2016-04-03 19:02:04 +08:00
desc . BindFlags = D3D11_BIND_INDEX_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
2022-10-11 18:22:29 +08:00
if ( bd - > pd3dDevice - > CreateBuffer ( & desc , nullptr , & bd - > pIB ) < 0 )
2015-08-14 13:13:20 +08:00
return ;
}
2019-03-29 23:18:26 +08:00
// Upload vertex/index data into a single contiguous GPU buffer
2015-04-10 04:05:35 +08:00
D3D11_MAPPED_SUBRESOURCE vtx_resource , idx_resource ;
2021-06-28 22:52:10 +08:00
if ( ctx - > Map ( bd - > pVB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & vtx_resource ) ! = S_OK )
2015-03-09 21:45:23 +08:00
return ;
2021-06-28 22:52:10 +08:00
if ( ctx - > Map ( bd - > pIB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & idx_resource ) ! = S_OK )
2015-04-10 04:05:35 +08:00
return ;
2015-04-10 04:22:06 +08:00
ImDrawVert * vtx_dst = ( ImDrawVert * ) vtx_resource . pData ;
2015-04-10 04:05:35 +08:00
ImDrawIdx * idx_dst = ( ImDrawIdx * ) idx_resource . pData ;
2015-07-08 10:17:07 +08:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
2015-03-09 21:45:23 +08:00
{
2015-07-08 10:17:07 +08:00
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
2016-09-04 01:24:57 +08:00
memcpy ( vtx_dst , cmd_list - > VtxBuffer . Data , cmd_list - > VtxBuffer . Size * sizeof ( ImDrawVert ) ) ;
memcpy ( idx_dst , cmd_list - > IdxBuffer . Data , cmd_list - > IdxBuffer . Size * sizeof ( ImDrawIdx ) ) ;
vtx_dst + = cmd_list - > VtxBuffer . Size ;
idx_dst + = cmd_list - > IdxBuffer . Size ;
2015-03-09 21:45:23 +08:00
}
2021-06-28 22:52:10 +08:00
ctx - > Unmap ( bd - > pVB , 0 ) ;
ctx - > Unmap ( bd - > pIB , 0 ) ;
2015-03-09 21:45:23 +08:00
// Setup orthographic projection matrix into our constant buffer
2019-06-06 22:16:18 +08:00
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
2015-03-09 21:45:23 +08:00
{
2016-04-03 18:48:38 +08:00
D3D11_MAPPED_SUBRESOURCE mapped_resource ;
2021-06-28 22:52:10 +08:00
if ( ctx - > Map ( bd - > pVertexConstantBuffer , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & mapped_resource ) ! = S_OK )
2015-03-09 21:45:23 +08:00
return ;
2022-06-11 18:52:27 +08:00
VERTEX_CONSTANT_BUFFER_DX11 * constant_buffer = ( VERTEX_CONSTANT_BUFFER_DX11 * ) mapped_resource . pData ;
2018-03-13 06:15:40 +08:00
float L = draw_data - > DisplayPos . x ;
float R = draw_data - > DisplayPos . x + draw_data - > DisplaySize . x ;
float T = draw_data - > DisplayPos . y ;
float B = draw_data - > DisplayPos . y + draw_data - > DisplaySize . y ;
2016-04-03 18:48:38 +08:00
float mvp [ 4 ] [ 4 ] =
2015-03-09 21:45:23 +08:00
{
2015-12-04 02:32:01 +08:00
{ 2.0f / ( R - L ) , 0.0f , 0.0f , 0.0f } ,
{ 0.0f , 2.0f / ( T - B ) , 0.0f , 0.0f } ,
2015-03-09 21:45:23 +08:00
{ 0.0f , 0.0f , 0.5f , 0.0f } ,
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.5f , 1.0f } ,
} ;
2016-04-03 18:48:38 +08:00
memcpy ( & constant_buffer - > mvp , mvp , sizeof ( mvp ) ) ;
2021-06-28 22:52:10 +08:00
ctx - > Unmap ( bd - > pVertexConstantBuffer , 0 ) ;
2015-03-09 21:45:23 +08:00
}
2016-04-03 18:43:17 +08:00
// Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
struct BACKUP_DX11_STATE
{
UINT ScissorRectsCount , ViewportsCount ;
D3D11_RECT ScissorRects [ D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE ] ;
D3D11_VIEWPORT Viewports [ D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE ] ;
ID3D11RasterizerState * RS ;
ID3D11BlendState * BlendState ;
FLOAT BlendFactor [ 4 ] ;
UINT SampleMask ;
2016-05-08 02:11:14 +08:00
UINT StencilRef ;
ID3D11DepthStencilState * DepthStencilState ;
2016-04-03 18:43:17 +08:00
ID3D11ShaderResourceView * PSShaderResource ;
ID3D11SamplerState * PSSampler ;
ID3D11PixelShader * PS ;
ID3D11VertexShader * VS ;
2019-07-22 10:26:13 +08:00
ID3D11GeometryShader * GS ;
UINT PSInstancesCount , VSInstancesCount , GSInstancesCount ;
ID3D11ClassInstance * PSInstances [ 256 ] , * VSInstances [ 256 ] , * GSInstances [ 256 ] ; // 256 is max according to PSSetShader documentation
2016-04-03 18:43:17 +08:00
D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology ;
ID3D11Buffer * IndexBuffer , * VertexBuffer , * VSConstantBuffer ;
UINT IndexBufferOffset , VertexBufferStride , VertexBufferOffset ;
DXGI_FORMAT IndexBufferFormat ;
ID3D11InputLayout * InputLayout ;
} ;
2021-04-15 19:29:42 +08:00
BACKUP_DX11_STATE old = { } ;
2016-04-03 18:43:17 +08:00
old . ScissorRectsCount = old . ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE ;
ctx - > RSGetScissorRects ( & old . ScissorRectsCount , old . ScissorRects ) ;
ctx - > RSGetViewports ( & old . ViewportsCount , old . Viewports ) ;
ctx - > RSGetState ( & old . RS ) ;
ctx - > OMGetBlendState ( & old . BlendState , old . BlendFactor , & old . SampleMask ) ;
2016-05-08 02:11:14 +08:00
ctx - > OMGetDepthStencilState ( & old . DepthStencilState , & old . StencilRef ) ;
2016-04-03 18:43:17 +08:00
ctx - > PSGetShaderResources ( 0 , 1 , & old . PSShaderResource ) ;
ctx - > PSGetSamplers ( 0 , 1 , & old . PSSampler ) ;
2019-08-02 01:58:41 +08:00
old . PSInstancesCount = old . VSInstancesCount = old . GSInstancesCount = 256 ;
2016-04-03 18:43:17 +08:00
ctx - > PSGetShader ( & old . PS , old . PSInstances , & old . PSInstancesCount ) ;
ctx - > VSGetShader ( & old . VS , old . VSInstances , & old . VSInstancesCount ) ;
ctx - > VSGetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ;
2019-07-22 10:26:13 +08:00
ctx - > GSGetShader ( & old . GS , old . GSInstances , & old . GSInstancesCount ) ;
2016-04-03 18:43:17 +08:00
ctx - > IAGetPrimitiveTopology ( & old . PrimitiveTopology ) ;
ctx - > IAGetIndexBuffer ( & old . IndexBuffer , & old . IndexBufferFormat , & old . IndexBufferOffset ) ;
ctx - > IAGetVertexBuffers ( 0 , 1 , & old . VertexBuffer , & old . VertexBufferStride , & old . VertexBufferOffset ) ;
ctx - > IAGetInputLayout ( & old . InputLayout ) ;
2016-04-03 10:08:27 +08:00
2019-05-01 04:15:59 +08:00
// Setup desired DX state
ImGui_ImplDX11_SetupRenderState ( draw_data , ctx ) ;
2015-03-09 21:45:23 +08:00
// Render command lists
2019-05-29 21:53:36 +08:00
// (Because we merged all buffers into a single one, we maintain our own offset into them)
int global_idx_offset = 0 ;
int global_vtx_offset = 0 ;
2019-02-12 01:38:07 +08:00
ImVec2 clip_off = draw_data - > DisplayPos ;
2015-07-08 10:17:07 +08:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
2015-03-09 21:45:23 +08:00
{
2015-07-08 10:17:07 +08:00
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
2016-09-04 01:24:57 +08:00
for ( int cmd_i = 0 ; cmd_i < cmd_list - > CmdBuffer . Size ; cmd_i + + )
2015-03-09 21:45:23 +08:00
{
2015-07-08 10:17:07 +08:00
const ImDrawCmd * pcmd = & cmd_list - > CmdBuffer [ cmd_i ] ;
2022-10-11 18:22:29 +08:00
if ( pcmd - > UserCallback ! = nullptr )
2015-03-09 23:26:58 +08:00
{
2019-05-01 04:15:59 +08:00
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if ( pcmd - > UserCallback = = ImDrawCallback_ResetRenderState )
ImGui_ImplDX11_SetupRenderState ( draw_data , ctx ) ;
else
pcmd - > UserCallback ( cmd_list , pcmd ) ;
2015-03-09 23:26:58 +08:00
}
else
{
2021-08-24 23:03:52 +08:00
// Project scissor/clipping rectangles into framebuffer space
ImVec2 clip_min ( pcmd - > ClipRect . x - clip_off . x , pcmd - > ClipRect . y - clip_off . y ) ;
ImVec2 clip_max ( pcmd - > ClipRect . z - clip_off . x , pcmd - > ClipRect . w - clip_off . y ) ;
2021-12-01 04:48:29 +08:00
if ( clip_max . x < = clip_min . x | | clip_max . y < = clip_min . y )
2021-08-24 23:03:52 +08:00
continue ;
2018-02-19 04:09:28 +08:00
// Apply scissor/clipping rectangle
2021-08-24 23:03:52 +08:00
const D3D11_RECT r = { ( LONG ) clip_min . x , ( LONG ) clip_min . y , ( LONG ) clip_max . x , ( LONG ) clip_max . y } ;
2016-04-03 18:48:38 +08:00
ctx - > RSSetScissorRects ( 1 , & r ) ;
2018-02-19 04:09:28 +08:00
// Bind texture, Draw
2021-02-07 19:36:54 +08:00
ID3D11ShaderResourceView * texture_srv = ( ID3D11ShaderResourceView * ) pcmd - > GetTexID ( ) ;
2018-08-09 23:49:48 +08:00
ctx - > PSSetShaderResources ( 0 , 1 , & texture_srv ) ;
2019-05-29 21:53:36 +08:00
ctx - > DrawIndexed ( pcmd - > ElemCount , pcmd - > IdxOffset + global_idx_offset , pcmd - > VtxOffset + global_vtx_offset ) ;
2015-03-09 23:26:58 +08:00
}
2015-03-09 21:45:23 +08:00
}
2019-05-29 21:53:36 +08:00
global_idx_offset + = cmd_list - > IdxBuffer . Size ;
global_vtx_offset + = cmd_list - > VtxBuffer . Size ;
2015-03-09 21:45:23 +08:00
}
2016-04-03 18:43:17 +08:00
// Restore modified DX state
ctx - > RSSetScissorRects ( old . ScissorRectsCount , old . ScissorRects ) ;
ctx - > RSSetViewports ( old . ViewportsCount , old . Viewports ) ;
ctx - > RSSetState ( old . RS ) ; if ( old . RS ) old . RS - > Release ( ) ;
ctx - > OMSetBlendState ( old . BlendState , old . BlendFactor , old . SampleMask ) ; if ( old . BlendState ) old . BlendState - > Release ( ) ;
2016-05-08 02:11:14 +08:00
ctx - > OMSetDepthStencilState ( old . DepthStencilState , old . StencilRef ) ; if ( old . DepthStencilState ) old . DepthStencilState - > Release ( ) ;
2016-04-03 18:43:17 +08:00
ctx - > PSSetShaderResources ( 0 , 1 , & old . PSShaderResource ) ; if ( old . PSShaderResource ) old . PSShaderResource - > Release ( ) ;
ctx - > PSSetSamplers ( 0 , 1 , & old . PSSampler ) ; if ( old . PSSampler ) old . PSSampler - > Release ( ) ;
ctx - > PSSetShader ( old . PS , old . PSInstances , old . PSInstancesCount ) ; if ( old . PS ) old . PS - > Release ( ) ;
for ( UINT i = 0 ; i < old . PSInstancesCount ; i + + ) if ( old . PSInstances [ i ] ) old . PSInstances [ i ] - > Release ( ) ;
ctx - > VSSetShader ( old . VS , old . VSInstances , old . VSInstancesCount ) ; if ( old . VS ) old . VS - > Release ( ) ;
ctx - > VSSetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ; if ( old . VSConstantBuffer ) old . VSConstantBuffer - > Release ( ) ;
2019-07-22 10:26:13 +08:00
ctx - > GSSetShader ( old . GS , old . GSInstances , old . GSInstancesCount ) ; if ( old . GS ) old . GS - > Release ( ) ;
2016-04-03 18:43:17 +08:00
for ( UINT i = 0 ; i < old . VSInstancesCount ; i + + ) if ( old . VSInstances [ i ] ) old . VSInstances [ i ] - > Release ( ) ;
ctx - > IASetPrimitiveTopology ( old . PrimitiveTopology ) ;
ctx - > IASetIndexBuffer ( old . IndexBuffer , old . IndexBufferFormat , old . IndexBufferOffset ) ; if ( old . IndexBuffer ) old . IndexBuffer - > Release ( ) ;
ctx - > IASetVertexBuffers ( 0 , 1 , & old . VertexBuffer , & old . VertexBufferStride , & old . VertexBufferOffset ) ; if ( old . VertexBuffer ) old . VertexBuffer - > Release ( ) ;
ctx - > IASetInputLayout ( old . InputLayout ) ; if ( old . InputLayout ) old . InputLayout - > Release ( ) ;
2015-03-09 21:45:23 +08:00
}
2015-03-09 22:55:46 +08:00
static void ImGui_ImplDX11_CreateFontsTexture ( )
2015-03-09 21:45:23 +08:00
{
2015-11-29 22:54:05 +08:00
// Build texture atlas
2015-03-09 21:45:23 +08:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2015-03-09 21:45:23 +08:00
unsigned char * pixels ;
int width , height ;
io . Fonts - > GetTexDataAsRGBA32 ( & pixels , & width , & height ) ;
2015-11-29 22:54:05 +08:00
// Upload texture to graphics system
2015-03-09 22:51:10 +08:00
{
2016-04-03 19:02:04 +08:00
D3D11_TEXTURE2D_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . Width = width ;
desc . Height = height ;
desc . MipLevels = 1 ;
desc . ArraySize = 1 ;
desc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
desc . SampleDesc . Count = 1 ;
desc . Usage = D3D11_USAGE_DEFAULT ;
desc . BindFlags = D3D11_BIND_SHADER_RESOURCE ;
desc . CPUAccessFlags = 0 ;
2015-03-09 22:51:10 +08:00
2022-10-11 18:22:29 +08:00
ID3D11Texture2D * pTexture = nullptr ;
2015-03-09 22:51:10 +08:00
D3D11_SUBRESOURCE_DATA subResource ;
subResource . pSysMem = pixels ;
2016-04-03 19:02:04 +08:00
subResource . SysMemPitch = desc . Width * 4 ;
2015-03-09 22:51:10 +08:00
subResource . SysMemSlicePitch = 0 ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateTexture2D ( & desc , & subResource , & pTexture ) ;
2022-10-11 18:22:29 +08:00
IM_ASSERT ( pTexture ! = nullptr ) ;
2015-03-09 22:51:10 +08:00
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc ;
ZeroMemory ( & srvDesc , sizeof ( srvDesc ) ) ;
srvDesc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
srvDesc . ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D ;
2016-04-03 19:02:04 +08:00
srvDesc . Texture2D . MipLevels = desc . MipLevels ;
2015-03-09 22:51:10 +08:00
srvDesc . Texture2D . MostDetailedMip = 0 ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateShaderResourceView ( pTexture , & srvDesc , & bd - > pFontTextureView ) ;
2015-03-09 22:51:10 +08:00
pTexture - > Release ( ) ;
}
2015-03-09 21:45:23 +08:00
// Store our identifier
2021-06-28 22:52:10 +08:00
io . Fonts - > SetTexID ( ( ImTextureID ) bd - > pFontTextureView ) ;
2015-03-09 21:45:23 +08:00
// Create texture sampler
2022-04-07 20:28:08 +08:00
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
2015-03-09 22:51:10 +08:00
{
2016-04-03 19:02:04 +08:00
D3D11_SAMPLER_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR ;
desc . AddressU = D3D11_TEXTURE_ADDRESS_WRAP ;
desc . AddressV = D3D11_TEXTURE_ADDRESS_WRAP ;
desc . AddressW = D3D11_TEXTURE_ADDRESS_WRAP ;
desc . MipLODBias = 0.f ;
desc . ComparisonFunc = D3D11_COMPARISON_ALWAYS ;
desc . MinLOD = 0.f ;
desc . MaxLOD = 0.f ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateSamplerState ( & desc , & bd - > pFontSampler ) ;
2015-03-09 22:51:10 +08:00
}
2015-03-09 21:45:23 +08:00
}
2015-03-09 22:51:10 +08:00
bool ImGui_ImplDX11_CreateDeviceObjects ( )
2015-03-09 21:45:23 +08:00
{
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
if ( ! bd - > pd3dDevice )
2015-03-09 22:51:10 +08:00
return false ;
2021-06-28 22:52:10 +08:00
if ( bd - > pFontSampler )
2015-03-09 22:51:10 +08:00
ImGui_ImplDX11_InvalidateDeviceObjects ( ) ;
2016-05-08 17:49:21 +08:00
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
2019-01-21 00:56:17 +08:00
// If you would like to use this DX11 sample code but remove this dependency you can:
2016-09-12 04:02:35 +08:00
// 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
2019-01-21 00:56:17 +08:00
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
2016-05-08 17:49:21 +08:00
// See https://github.com/ocornut/imgui/pull/638 for sources and details.
2015-03-09 21:45:23 +08:00
// Create the vertex shader
{
2016-03-26 22:43:45 +08:00
static const char * vertexShader =
2015-09-01 17:47:36 +08:00
" cbuffer vertexBuffer : register(b0) \
2015-03-09 21:45:23 +08:00
{ \
2020-05-04 17:32:58 +08:00
float4x4 ProjectionMatrix ; \
2015-03-09 21:45:23 +08:00
} ; \
struct VS_INPUT \
{ \
2020-05-04 17:32:58 +08:00
float2 pos : POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
2015-03-09 21:45:23 +08:00
} ; \
\
struct PS_INPUT \
{ \
2020-05-04 17:32:58 +08:00
float4 pos : SV_POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
2015-03-09 21:45:23 +08:00
} ; \
\
PS_INPUT main ( VS_INPUT input ) \
{ \
2020-05-04 17:32:58 +08:00
PS_INPUT output ; \
output . pos = mul ( ProjectionMatrix , float4 ( input . pos . xy , 0.f , 1.f ) ) ; \
output . col = input . col ; \
output . uv = input . uv ; \
return output ; \
2015-03-09 21:45:23 +08:00
} " ;
2020-04-29 03:53:46 +08:00
ID3DBlob * vertexShaderBlob ;
2022-10-11 18:22:29 +08:00
if ( FAILED ( D3DCompile ( vertexShader , strlen ( vertexShader ) , nullptr , nullptr , nullptr , " main " , " vs_4_0 " , 0 , 0 , & vertexShaderBlob , nullptr ) ) )
2020-04-29 03:53:46 +08:00
return false ; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
2022-10-11 18:22:29 +08:00
if ( bd - > pd3dDevice - > CreateVertexShader ( vertexShaderBlob - > GetBufferPointer ( ) , vertexShaderBlob - > GetBufferSize ( ) , nullptr , & bd - > pVertexShader ) ! = S_OK )
2020-04-29 03:53:46 +08:00
{
vertexShaderBlob - > Release ( ) ;
2015-03-09 21:45:23 +08:00
return false ;
2020-04-29 03:53:46 +08:00
}
2015-03-09 21:45:23 +08:00
// Create the input layout
2019-01-21 00:56:17 +08:00
D3D11_INPUT_ELEMENT_DESC local_layout [ ] =
2018-02-17 06:11:43 +08:00
{
2020-05-04 17:32:58 +08:00
{ " POSITION " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( UINT ) IM_OFFSETOF ( ImDrawVert , pos ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " TEXCOORD " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( UINT ) IM_OFFSETOF ( ImDrawVert , uv ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " COLOR " , 0 , DXGI_FORMAT_R8G8B8A8_UNORM , 0 , ( UINT ) IM_OFFSETOF ( ImDrawVert , col ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
2015-03-09 21:45:23 +08:00
} ;
2021-06-28 22:52:10 +08:00
if ( bd - > pd3dDevice - > CreateInputLayout ( local_layout , 3 , vertexShaderBlob - > GetBufferPointer ( ) , vertexShaderBlob - > GetBufferSize ( ) , & bd - > pInputLayout ) ! = S_OK )
2020-04-29 03:53:46 +08:00
{
vertexShaderBlob - > Release ( ) ;
2015-03-09 21:45:23 +08:00
return false ;
2020-04-29 03:53:46 +08:00
}
vertexShaderBlob - > Release ( ) ;
2015-03-09 21:45:23 +08:00
// Create the constant buffer
{
2016-04-03 19:02:04 +08:00
D3D11_BUFFER_DESC desc ;
2022-06-11 18:52:27 +08:00
desc . ByteWidth = sizeof ( VERTEX_CONSTANT_BUFFER_DX11 ) ;
2016-04-03 19:02:04 +08:00
desc . Usage = D3D11_USAGE_DYNAMIC ;
desc . BindFlags = D3D11_BIND_CONSTANT_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
2022-10-11 18:22:29 +08:00
bd - > pd3dDevice - > CreateBuffer ( & desc , nullptr , & bd - > pVertexConstantBuffer ) ;
2015-03-09 21:45:23 +08:00
}
}
// Create the pixel shader
{
2015-12-04 02:32:01 +08:00
static const char * pixelShader =
2015-03-09 21:45:23 +08:00
" struct PS_INPUT \
{ \
float4 pos : SV_POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
sampler sampler0 ; \
Texture2D texture0 ; \
\
float4 main ( PS_INPUT input ) : SV_Target \
{ \
float4 out_col = input . col * texture0 . Sample ( sampler0 , input . uv ) ; \
return out_col ; \
} " ;
2020-04-29 03:53:46 +08:00
ID3DBlob * pixelShaderBlob ;
2022-10-11 18:22:29 +08:00
if ( FAILED ( D3DCompile ( pixelShader , strlen ( pixelShader ) , nullptr , nullptr , nullptr , " main " , " ps_4_0 " , 0 , 0 , & pixelShaderBlob , nullptr ) ) )
2020-04-29 03:53:46 +08:00
return false ; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
2022-10-11 18:22:29 +08:00
if ( bd - > pd3dDevice - > CreatePixelShader ( pixelShaderBlob - > GetBufferPointer ( ) , pixelShaderBlob - > GetBufferSize ( ) , nullptr , & bd - > pPixelShader ) ! = S_OK )
2020-04-29 03:53:46 +08:00
{
pixelShaderBlob - > Release ( ) ;
2015-03-09 21:45:23 +08:00
return false ;
2020-04-29 03:53:46 +08:00
}
pixelShaderBlob - > Release ( ) ;
2015-03-09 21:45:23 +08:00
}
// Create the blending setup
{
D3D11_BLEND_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . AlphaToCoverageEnable = false ;
desc . RenderTarget [ 0 ] . BlendEnable = true ;
desc . RenderTarget [ 0 ] . SrcBlend = D3D11_BLEND_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . DestBlend = D3D11_BLEND_INV_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . BlendOp = D3D11_BLEND_OP_ADD ;
2021-02-18 02:29:07 +08:00
desc . RenderTarget [ 0 ] . SrcBlendAlpha = D3D11_BLEND_ONE ;
desc . RenderTarget [ 0 ] . DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA ;
2015-03-09 21:45:23 +08:00
desc . RenderTarget [ 0 ] . BlendOpAlpha = D3D11_BLEND_OP_ADD ;
desc . RenderTarget [ 0 ] . RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateBlendState ( & desc , & bd - > pBlendState ) ;
2015-05-19 04:38:17 +08:00
}
// Create the rasterizer state
{
D3D11_RASTERIZER_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . FillMode = D3D11_FILL_SOLID ;
desc . CullMode = D3D11_CULL_NONE ;
desc . ScissorEnable = true ;
desc . DepthClipEnable = true ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateRasterizerState ( & desc , & bd - > pRasterizerState ) ;
2015-03-09 21:45:23 +08:00
}
2016-05-08 02:53:55 +08:00
// Create depth-stencil State
2016-05-08 01:42:48 +08:00
{
D3D11_DEPTH_STENCIL_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
2016-05-08 02:53:55 +08:00
desc . DepthEnable = false ;
2016-05-08 01:42:48 +08:00
desc . DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL ;
desc . DepthFunc = D3D11_COMPARISON_ALWAYS ;
2016-05-08 02:11:14 +08:00
desc . StencilEnable = false ;
2016-05-08 02:53:55 +08:00
desc . FrontFace . StencilFailOp = desc . FrontFace . StencilDepthFailOp = desc . FrontFace . StencilPassOp = D3D11_STENCIL_OP_KEEP ;
desc . FrontFace . StencilFunc = D3D11_COMPARISON_ALWAYS ;
desc . BackFace = desc . FrontFace ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > CreateDepthStencilState ( & desc , & bd - > pDepthStencilState ) ;
2016-05-08 01:42:48 +08:00
}
2015-03-09 22:55:46 +08:00
ImGui_ImplDX11_CreateFontsTexture ( ) ;
2015-03-09 22:51:10 +08:00
2015-03-09 21:45:23 +08:00
return true ;
}
2015-03-09 22:51:10 +08:00
void ImGui_ImplDX11_InvalidateDeviceObjects ( )
{
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
if ( ! bd - > pd3dDevice )
2015-03-09 22:51:10 +08:00
return ;
2022-10-11 18:22:29 +08:00
if ( bd - > pFontSampler ) { bd - > pFontSampler - > Release ( ) ; bd - > pFontSampler = nullptr ; }
2022-10-11 21:59:23 +08:00
if ( bd - > pFontTextureView ) { bd - > pFontTextureView - > Release ( ) ; bd - > pFontTextureView = nullptr ; ImGui : : GetIO ( ) . Fonts - > SetTexID ( 0 ) ; } // We copied data->pFontTextureView to io.Fonts->TexID so let's clear that as well.
2022-10-11 18:22:29 +08:00
if ( bd - > pIB ) { bd - > pIB - > Release ( ) ; bd - > pIB = nullptr ; }
if ( bd - > pVB ) { bd - > pVB - > Release ( ) ; bd - > pVB = nullptr ; }
if ( bd - > pBlendState ) { bd - > pBlendState - > Release ( ) ; bd - > pBlendState = nullptr ; }
if ( bd - > pDepthStencilState ) { bd - > pDepthStencilState - > Release ( ) ; bd - > pDepthStencilState = nullptr ; }
if ( bd - > pRasterizerState ) { bd - > pRasterizerState - > Release ( ) ; bd - > pRasterizerState = nullptr ; }
if ( bd - > pPixelShader ) { bd - > pPixelShader - > Release ( ) ; bd - > pPixelShader = nullptr ; }
if ( bd - > pVertexConstantBuffer ) { bd - > pVertexConstantBuffer - > Release ( ) ; bd - > pVertexConstantBuffer = nullptr ; }
if ( bd - > pInputLayout ) { bd - > pInputLayout - > Release ( ) ; bd - > pInputLayout = nullptr ; }
if ( bd - > pVertexShader ) { bd - > pVertexShader - > Release ( ) ; bd - > pVertexShader = nullptr ; }
2015-03-09 22:51:10 +08:00
}
2018-02-17 06:04:17 +08:00
bool ImGui_ImplDX11_Init ( ID3D11Device * device , ID3D11DeviceContext * device_context )
2015-03-09 21:45:23 +08:00
{
2018-12-01 01:18:15 +08:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2022-10-11 18:22:29 +08:00
IM_ASSERT ( io . BackendRendererUserData = = nullptr & & " Already initialized a renderer backend! " ) ;
2021-06-28 22:52:10 +08:00
// Setup backend capabilities flags
2021-06-30 21:22:15 +08:00
ImGui_ImplDX11_Data * bd = IM_NEW ( ImGui_ImplDX11_Data ) ( ) ;
2021-06-28 22:52:10 +08:00
io . BackendRendererUserData = ( void * ) bd ;
2018-12-01 01:18:15 +08:00
io . BackendRendererName = " imgui_impl_dx11 " ;
2019-05-29 22:29:17 +08:00
io . BackendFlags | = ImGuiBackendFlags_RendererHasVtxOffset ; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
2019-06-06 22:16:18 +08:00
io . BackendFlags | = ImGuiBackendFlags_RendererHasViewports ; // We can create multi-viewports on the Renderer side (optional)
2018-12-01 01:18:15 +08:00
2018-02-28 06:26:51 +08:00
// Get factory from device
2022-10-11 18:22:29 +08:00
IDXGIDevice * pDXGIDevice = nullptr ;
IDXGIAdapter * pDXGIAdapter = nullptr ;
IDXGIFactory * pFactory = nullptr ;
2018-02-28 06:26:51 +08:00
2018-07-13 17:25:54 +08:00
if ( device - > QueryInterface ( IID_PPV_ARGS ( & pDXGIDevice ) ) = = S_OK )
if ( pDXGIDevice - > GetParent ( IID_PPV_ARGS ( & pDXGIAdapter ) ) = = S_OK )
if ( pDXGIAdapter - > GetParent ( IID_PPV_ARGS ( & pFactory ) ) = = S_OK )
{
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice = device ;
bd - > pd3dDeviceContext = device_context ;
bd - > pFactory = pFactory ;
2018-07-13 17:25:54 +08:00
}
if ( pDXGIDevice ) pDXGIDevice - > Release ( ) ;
if ( pDXGIAdapter ) pDXGIAdapter - > Release ( ) ;
2021-06-28 22:52:10 +08:00
bd - > pd3dDevice - > AddRef ( ) ;
bd - > pd3dDeviceContext - > AddRef ( ) ;
2018-03-21 05:14:34 +08:00
2018-04-11 01:21:52 +08:00
if ( io . ConfigFlags & ImGuiConfigFlags_ViewportsEnable )
2018-02-28 06:26:51 +08:00
ImGui_ImplDX11_InitPlatformInterface ( ) ;
2018-06-10 21:04:09 +08:00
2015-03-09 21:45:23 +08:00
return true ;
}
void ImGui_ImplDX11_Shutdown ( )
{
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2022-10-11 18:22:29 +08:00
IM_ASSERT ( bd ! = nullptr & & " No renderer backend to shutdown, or already shutdown? " ) ;
2021-09-21 00:43:05 +08:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2021-06-28 22:52:10 +08:00
2018-02-28 06:26:51 +08:00
ImGui_ImplDX11_ShutdownPlatformInterface ( ) ;
2015-03-09 22:51:10 +08:00
ImGui_ImplDX11_InvalidateDeviceObjects ( ) ;
2021-06-30 21:22:15 +08:00
if ( bd - > pFactory ) { bd - > pFactory - > Release ( ) ; }
if ( bd - > pd3dDevice ) { bd - > pd3dDevice - > Release ( ) ; }
if ( bd - > pd3dDeviceContext ) { bd - > pd3dDeviceContext - > Release ( ) ; }
2022-10-11 18:22:29 +08:00
io . BackendRendererName = nullptr ;
io . BackendRendererUserData = nullptr ;
2023-04-19 22:40:18 +08:00
io . BackendFlags & = ~ ( ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports ) ;
2021-06-30 21:22:15 +08:00
IM_DELETE ( bd ) ;
2015-03-09 21:45:23 +08:00
}
void ImGui_ImplDX11_NewFrame ( )
{
2021-06-28 22:52:10 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2022-10-11 18:22:29 +08:00
IM_ASSERT ( bd ! = nullptr & & " Did you call ImGui_ImplDX11_Init()? " ) ;
2021-06-29 23:53:41 +08:00
2021-06-28 22:52:10 +08:00
if ( ! bd - > pFontSampler )
2015-03-09 22:51:10 +08:00
ImGui_ImplDX11_CreateDeviceObjects ( ) ;
2015-03-09 21:45:23 +08:00
}
2018-02-28 06:26:51 +08:00
2018-03-19 01:44:57 +08:00
//--------------------------------------------------------------------------------------------------------
2018-04-24 23:09:50 +08:00
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
2020-10-14 20:43:45 +08:00
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
2018-04-24 23:09:50 +08:00
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
2018-03-19 01:44:57 +08:00
//--------------------------------------------------------------------------------------------------------
2018-02-28 06:26:51 +08:00
2023-04-11 22:19:59 +08:00
// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
2021-06-29 21:34:54 +08:00
struct ImGui_ImplDX11_ViewportData
2018-02-28 06:26:51 +08:00
{
2021-06-29 21:34:54 +08:00
IDXGISwapChain * SwapChain ;
ID3D11RenderTargetView * RTView ;
2018-02-28 06:26:51 +08:00
2022-10-11 21:59:23 +08:00
ImGui_ImplDX11_ViewportData ( ) { SwapChain = nullptr ; RTView = nullptr ; }
~ ImGui_ImplDX11_ViewportData ( ) { IM_ASSERT ( SwapChain = = nullptr & & RTView = = nullptr ) ; }
2018-02-28 06:26:51 +08:00
} ;
2018-03-19 01:44:57 +08:00
static void ImGui_ImplDX11_CreateWindow ( ImGuiViewport * viewport )
2018-02-28 06:26:51 +08:00
{
2021-06-29 20:53:25 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2021-06-29 21:34:54 +08:00
ImGui_ImplDX11_ViewportData * vd = IM_NEW ( ImGui_ImplDX11_ViewportData ) ( ) ;
viewport - > RendererUserData = vd ;
2018-02-28 06:26:51 +08:00
2019-06-19 05:35:48 +08:00
// PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL_Window*).
2023-04-11 22:19:59 +08:00
// Some backends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the HWND.
2019-11-04 23:33:57 +08:00
HWND hwnd = viewport - > PlatformHandleRaw ? ( HWND ) viewport - > PlatformHandleRaw : ( HWND ) viewport - > PlatformHandle ;
2018-02-28 06:26:51 +08:00
IM_ASSERT ( hwnd ! = 0 ) ;
// Create swap chain
DXGI_SWAP_CHAIN_DESC sd ;
ZeroMemory ( & sd , sizeof ( sd ) ) ;
sd . BufferDesc . Width = ( UINT ) viewport - > Size . x ;
sd . BufferDesc . Height = ( UINT ) viewport - > Size . y ;
sd . BufferDesc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
sd . SampleDesc . Count = 1 ;
sd . SampleDesc . Quality = 0 ;
sd . BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
sd . BufferCount = 1 ;
sd . OutputWindow = hwnd ;
sd . Windowed = TRUE ;
sd . SwapEffect = DXGI_SWAP_EFFECT_DISCARD ;
sd . Flags = 0 ;
2022-10-11 21:59:23 +08:00
IM_ASSERT ( vd - > SwapChain = = nullptr & & vd - > RTView = = nullptr ) ;
2021-06-29 21:34:54 +08:00
bd - > pFactory - > CreateSwapChain ( bd - > pd3dDevice , & sd , & vd - > SwapChain ) ;
2018-02-28 06:26:51 +08:00
// Create the render target
2021-06-29 21:34:54 +08:00
if ( vd - > SwapChain )
2018-02-28 06:26:51 +08:00
{
ID3D11Texture2D * pBackBuffer ;
2021-06-29 21:34:54 +08:00
vd - > SwapChain - > GetBuffer ( 0 , IID_PPV_ARGS ( & pBackBuffer ) ) ;
2022-10-11 21:59:23 +08:00
bd - > pd3dDevice - > CreateRenderTargetView ( pBackBuffer , nullptr , & vd - > RTView ) ;
2018-02-28 06:26:51 +08:00
pBackBuffer - > Release ( ) ;
}
}
2018-03-19 01:44:57 +08:00
static void ImGui_ImplDX11_DestroyWindow ( ImGuiViewport * viewport )
2018-02-28 06:26:51 +08:00
{
2022-10-11 21:59:23 +08:00
// The main viewport (owned by the application) will always have RendererUserData == nullptr since we didn't create the data for it.
2021-06-29 21:34:54 +08:00
if ( ImGui_ImplDX11_ViewportData * vd = ( ImGui_ImplDX11_ViewportData * ) viewport - > RendererUserData )
2018-02-28 06:26:51 +08:00
{
2021-06-29 21:34:54 +08:00
if ( vd - > SwapChain )
vd - > SwapChain - > Release ( ) ;
2022-10-11 21:59:23 +08:00
vd - > SwapChain = nullptr ;
2021-06-29 21:34:54 +08:00
if ( vd - > RTView )
vd - > RTView - > Release ( ) ;
2022-10-11 21:59:23 +08:00
vd - > RTView = nullptr ;
2021-06-29 21:34:54 +08:00
IM_DELETE ( vd ) ;
2018-02-28 06:26:51 +08:00
}
2022-10-11 21:59:23 +08:00
viewport - > RendererUserData = nullptr ;
2018-02-28 06:26:51 +08:00
}
2018-03-19 01:44:57 +08:00
static void ImGui_ImplDX11_SetWindowSize ( ImGuiViewport * viewport , ImVec2 size )
2018-02-28 06:26:51 +08:00
{
2021-06-29 20:53:25 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2021-06-29 21:34:54 +08:00
ImGui_ImplDX11_ViewportData * vd = ( ImGui_ImplDX11_ViewportData * ) viewport - > RendererUserData ;
if ( vd - > RTView )
2018-02-28 06:26:51 +08:00
{
2021-06-29 21:34:54 +08:00
vd - > RTView - > Release ( ) ;
2022-10-11 21:59:23 +08:00
vd - > RTView = nullptr ;
2018-02-28 06:26:51 +08:00
}
2021-06-29 21:34:54 +08:00
if ( vd - > SwapChain )
2018-02-28 06:26:51 +08:00
{
2022-10-11 21:59:23 +08:00
ID3D11Texture2D * pBackBuffer = nullptr ;
2021-06-29 21:34:54 +08:00
vd - > SwapChain - > ResizeBuffers ( 0 , ( UINT ) size . x , ( UINT ) size . y , DXGI_FORMAT_UNKNOWN , 0 ) ;
vd - > SwapChain - > GetBuffer ( 0 , IID_PPV_ARGS ( & pBackBuffer ) ) ;
2022-10-11 21:59:23 +08:00
if ( pBackBuffer = = nullptr ) { fprintf ( stderr , " ImGui_ImplDX11_SetWindowSize() failed creating buffers. \n " ) ; return ; }
bd - > pd3dDevice - > CreateRenderTargetView ( pBackBuffer , nullptr , & vd - > RTView ) ;
2018-02-28 06:26:51 +08:00
pBackBuffer - > Release ( ) ;
}
}
2018-03-19 22:20:47 +08:00
static void ImGui_ImplDX11_RenderWindow ( ImGuiViewport * viewport , void * )
2018-02-28 06:26:51 +08:00
{
2021-06-29 20:53:25 +08:00
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
2021-06-29 21:34:54 +08:00
ImGui_ImplDX11_ViewportData * vd = ( ImGui_ImplDX11_ViewportData * ) viewport - > RendererUserData ;
2018-03-16 00:52:53 +08:00
ImVec4 clear_color = ImVec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ;
2022-10-11 21:59:23 +08:00
bd - > pd3dDeviceContext - > OMSetRenderTargets ( 1 , & vd - > RTView , nullptr ) ;
2018-03-16 00:52:53 +08:00
if ( ! ( viewport - > Flags & ImGuiViewportFlags_NoRendererClear ) )
2021-06-29 21:34:54 +08:00
bd - > pd3dDeviceContext - > ClearRenderTargetView ( vd - > RTView , ( float * ) & clear_color ) ;
2018-03-19 01:44:57 +08:00
ImGui_ImplDX11_RenderDrawData ( viewport - > DrawData ) ;
2018-02-28 06:26:51 +08:00
}
2018-03-19 22:20:47 +08:00
static void ImGui_ImplDX11_SwapBuffers ( ImGuiViewport * viewport , void * )
2018-02-28 06:26:51 +08:00
{
2021-06-29 21:34:54 +08:00
ImGui_ImplDX11_ViewportData * vd = ( ImGui_ImplDX11_ViewportData * ) viewport - > RendererUserData ;
vd - > SwapChain - > Present ( 0 , 0 ) ; // Present without vsync
2018-02-28 06:26:51 +08:00
}
2018-03-19 01:44:57 +08:00
static void ImGui_ImplDX11_InitPlatformInterface ( )
2018-02-28 06:26:51 +08:00
{
2018-03-19 01:44:57 +08:00
ImGuiPlatformIO & platform_io = ImGui : : GetPlatformIO ( ) ;
platform_io . Renderer_CreateWindow = ImGui_ImplDX11_CreateWindow ;
platform_io . Renderer_DestroyWindow = ImGui_ImplDX11_DestroyWindow ;
platform_io . Renderer_SetWindowSize = ImGui_ImplDX11_SetWindowSize ;
platform_io . Renderer_RenderWindow = ImGui_ImplDX11_RenderWindow ;
platform_io . Renderer_SwapBuffers = ImGui_ImplDX11_SwapBuffers ;
2018-02-28 06:26:51 +08:00
}
2018-03-19 01:44:57 +08:00
static void ImGui_ImplDX11_ShutdownPlatformInterface ( )
2018-02-28 06:26:51 +08:00
{
2020-03-07 00:53:09 +08:00
ImGui : : DestroyPlatformWindows ( ) ;
2018-02-28 06:26:51 +08:00
}