2018-02-22 06:05:17 +08:00
// ImGui Renderer for: DirectX11
// This needs to be used along with a Platform Binding (e.g. Win32)
2018-02-06 03:34:11 +08:00
// Implemented features:
// [X] User texture binding. Use 'ID3D11ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
2016-03-24 18:00:47 +08:00
2015-11-29 19:19:30 +08:00
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
2015-10-13 23:53:43 +08:00
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
2015-11-29 19:19:30 +08:00
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
2015-03-09 21:45:23 +08:00
// https://github.com/ocornut/imgui
2018-02-17 00:20:18 +08:00
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
2018-03-01 01:35:16 +08:00
// 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiRendererInterface.
2018-02-20 23:18:02 +08:00
// 2018-XX-XX: DirectX11: Offset projection matrix and clipping rectangle by io.DisplayPos (which will be non-zero for multi-viewport applications).
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
# include <d3d11.h>
# include <d3dcompiler.h>
2018-02-20 20:55:09 +08:00
// DirectX data
2015-03-09 21:45:23 +08:00
static ID3D11Device * g_pd3dDevice = NULL ;
static ID3D11DeviceContext * g_pd3dDeviceContext = NULL ;
2018-02-28 06:26:51 +08:00
static IDXGIFactory1 * g_pFactory = NULL ;
2015-03-09 21:45:23 +08:00
static ID3D11Buffer * g_pVB = NULL ;
2015-04-10 04:05:35 +08:00
static ID3D11Buffer * g_pIB = NULL ;
2015-03-09 21:45:23 +08:00
static ID3D10Blob * g_pVertexShaderBlob = NULL ;
static ID3D11VertexShader * g_pVertexShader = NULL ;
static ID3D11InputLayout * g_pInputLayout = NULL ;
static ID3D11Buffer * g_pVertexConstantBuffer = NULL ;
static ID3D10Blob * g_pPixelShaderBlob = NULL ;
static ID3D11PixelShader * g_pPixelShader = NULL ;
static ID3D11SamplerState * g_pFontSampler = NULL ;
2015-03-09 22:51:10 +08:00
static ID3D11ShaderResourceView * g_pFontTextureView = NULL ;
2015-05-19 04:38:17 +08:00
static ID3D11RasterizerState * g_pRasterizerState = NULL ;
static ID3D11BlendState * g_pBlendState = NULL ;
2016-05-08 02:11:14 +08:00
static ID3D11DepthStencilState * g_pDepthStencilState = NULL ;
2015-08-14 13:13:20 +08:00
static int g_VertexBufferSize = 5000 , g_IndexBufferSize = 10000 ;
2015-03-09 21:45:23 +08:00
struct VERTEX_CONSTANT_BUFFER
{
float mvp [ 4 ] [ 4 ] ;
} ;
2018-02-28 06:26:51 +08:00
// Forward Declarations
static void ImGui_ImplDX11_InitPlatformInterface ( ) ;
static void ImGui_ImplDX11_ShutdownPlatformInterface ( ) ;
2018-02-17 02:18:16 +08:00
// Render function
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplDX11_RenderDrawData ( ImDrawData * draw_data )
2015-03-09 21:45:23 +08:00
{
2016-04-03 18:48:38 +08:00
ID3D11DeviceContext * ctx = g_pd3dDeviceContext ;
2015-08-14 13:13:20 +08:00
// Create and grow vertex/index buffers if needed
if ( ! g_pVB | | g_VertexBufferSize < draw_data - > TotalVtxCount )
{
if ( g_pVB ) { g_pVB - > Release ( ) ; g_pVB = NULL ; }
g_VertexBufferSize = draw_data - > TotalVtxCount + 5000 ;
D3D11_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D11_BUFFER_DESC ) ) ;
desc . Usage = D3D11_USAGE_DYNAMIC ;
desc . ByteWidth = g_VertexBufferSize * sizeof ( ImDrawVert ) ;
desc . BindFlags = D3D11_BIND_VERTEX_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
if ( g_pd3dDevice - > CreateBuffer ( & desc , NULL , & g_pVB ) < 0 )
return ;
}
if ( ! g_pIB | | g_IndexBufferSize < draw_data - > TotalIdxCount )
{
if ( g_pIB ) { g_pIB - > Release ( ) ; g_pIB = NULL ; }
g_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 ;
desc . ByteWidth = g_IndexBufferSize * sizeof ( ImDrawIdx ) ;
desc . BindFlags = D3D11_BIND_INDEX_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
if ( g_pd3dDevice - > CreateBuffer ( & desc , NULL , & g_pIB ) < 0 )
2015-08-14 13:13:20 +08:00
return ;
}
2015-03-09 21:45:23 +08:00
// Copy and convert all vertices into a single contiguous buffer
2015-04-10 04:05:35 +08:00
D3D11_MAPPED_SUBRESOURCE vtx_resource , idx_resource ;
2016-04-03 18:48:38 +08:00
if ( ctx - > Map ( g_pVB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & vtx_resource ) ! = S_OK )
2015-03-09 21:45:23 +08:00
return ;
2016-04-03 18:48:38 +08:00
if ( ctx - > Map ( g_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
}
2016-04-03 18:48:38 +08:00
ctx - > Unmap ( g_pVB , 0 ) ;
ctx - > Unmap ( g_pIB , 0 ) ;
2015-03-09 21:45:23 +08:00
// Setup orthographic projection matrix into our constant buffer
2018-02-19 04:09:28 +08:00
// Our visible imgui space lies from io.DisplayPos (top left) to io.DisplayPos+io.DisplaySize (bottom right). io.DisplayPos is typically (0,0) for single viewport applications.
ImGuiIO & io = ImGui : : GetIO ( ) ;
2015-03-09 21:45:23 +08:00
{
2016-04-03 18:48:38 +08:00
D3D11_MAPPED_SUBRESOURCE mapped_resource ;
if ( ctx - > Map ( g_pVertexConstantBuffer , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & mapped_resource ) ! = S_OK )
2015-03-09 21:45:23 +08:00
return ;
2016-04-03 18:48:38 +08:00
VERTEX_CONSTANT_BUFFER * constant_buffer = ( VERTEX_CONSTANT_BUFFER * ) mapped_resource . pData ;
2018-02-19 04:09:28 +08:00
float L = io . DisplayPos . x ;
float R = io . DisplayPos . x + io . DisplaySize . x ;
float T = io . DisplayPos . y ;
float B = io . DisplayPos . y + io . 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 ) ) ;
ctx - > Unmap ( g_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 ;
UINT PSInstancesCount , VSInstancesCount ;
ID3D11ClassInstance * PSInstances [ 256 ] , * VSInstances [ 256 ] ; // 256 is max according to PSSetShader documentation
D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology ;
ID3D11Buffer * IndexBuffer , * VertexBuffer , * VSConstantBuffer ;
UINT IndexBufferOffset , VertexBufferStride , VertexBufferOffset ;
DXGI_FORMAT IndexBufferFormat ;
ID3D11InputLayout * InputLayout ;
} ;
BACKUP_DX11_STATE old ;
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 ) ;
old . PSInstancesCount = old . VSInstancesCount = 256 ;
ctx - > PSGetShader ( & old . PS , old . PSInstances , & old . PSInstancesCount ) ;
ctx - > VSGetShader ( & old . VS , old . VSInstances , & old . VSInstancesCount ) ;
ctx - > VSGetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ;
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
2015-03-09 21:45:23 +08:00
// Setup viewport
2016-04-03 18:48:38 +08:00
D3D11_VIEWPORT vp ;
memset ( & vp , 0 , sizeof ( D3D11_VIEWPORT ) ) ;
2018-02-19 04:09:28 +08:00
vp . Width = io . DisplaySize . x ;
vp . Height = io . DisplaySize . y ;
2016-04-03 18:48:38 +08:00
vp . MinDepth = 0.0f ;
vp . MaxDepth = 1.0f ;
2018-02-17 06:11:43 +08:00
vp . TopLeftX = vp . TopLeftY = 0 ;
2016-04-03 18:48:38 +08:00
ctx - > RSSetViewports ( 1 , & vp ) ;
2015-03-09 21:45:23 +08:00
// Bind shader and vertex buffers
2015-04-10 04:14:52 +08:00
unsigned int stride = sizeof ( ImDrawVert ) ;
2015-03-09 21:45:23 +08:00
unsigned int offset = 0 ;
2016-04-03 18:48:38 +08:00
ctx - > IASetInputLayout ( g_pInputLayout ) ;
ctx - > IASetVertexBuffers ( 0 , 1 , & g_pVB , & stride , & offset ) ;
ctx - > IASetIndexBuffer ( g_pIB , sizeof ( ImDrawIdx ) = = 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT , 0 ) ;
ctx - > IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
ctx - > VSSetShader ( g_pVertexShader , NULL , 0 ) ;
ctx - > VSSetConstantBuffers ( 0 , 1 , & g_pVertexConstantBuffer ) ;
ctx - > PSSetShader ( g_pPixelShader , NULL , 0 ) ;
ctx - > PSSetSamplers ( 0 , 1 , & g_pFontSampler ) ;
2015-03-09 21:45:23 +08:00
// Setup render state
2016-04-03 18:48:38 +08:00
const float blend_factor [ 4 ] = { 0.f , 0.f , 0.f , 0.f } ;
ctx - > OMSetBlendState ( g_pBlendState , blend_factor , 0xffffffff ) ;
2016-05-08 02:11:14 +08:00
ctx - > OMSetDepthStencilState ( g_pDepthStencilState , 0 ) ;
2016-04-03 18:48:38 +08:00
ctx - > RSSetState ( g_pRasterizerState ) ;
2015-03-09 21:45:23 +08:00
// Render command lists
int vtx_offset = 0 ;
2015-04-10 04:05:35 +08:00
int idx_offset = 0 ;
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 ] ;
if ( pcmd - > UserCallback )
2015-03-09 23:26:58 +08:00
{
2018-02-19 04:09:28 +08:00
// User callback (registered via ImDrawList::AddCallback)
2015-07-08 10:17:07 +08:00
pcmd - > UserCallback ( cmd_list , pcmd ) ;
2015-03-09 23:26:58 +08:00
}
else
{
2018-02-19 04:09:28 +08:00
// Apply scissor/clipping rectangle
const ImVec4 clip_rect = ImVec4 ( pcmd - > ClipRect . x - io . DisplayPos . x , pcmd - > ClipRect . y - io . DisplayPos . y , pcmd - > ClipRect . z - io . DisplayPos . x , pcmd - > ClipRect . w - io . DisplayPos . y ) ;
const D3D11_RECT clip_rect_dx = { ( LONG ) clip_rect . x , ( LONG ) clip_rect . y , ( LONG ) clip_rect . z , ( LONG ) clip_rect . w } ;
ctx - > RSSetScissorRects ( 1 , & clip_rect_dx ) ;
// Bind texture, Draw
2016-04-03 18:48:38 +08:00
ctx - > PSSetShaderResources ( 0 , 1 , ( ID3D11ShaderResourceView * * ) & pcmd - > TextureId ) ;
ctx - > DrawIndexed ( pcmd - > ElemCount , idx_offset , vtx_offset ) ;
2015-03-09 23:26:58 +08:00
}
2015-07-08 10:17:07 +08:00
idx_offset + = pcmd - > ElemCount ;
2015-03-09 21:45:23 +08:00
}
2016-09-04 01:24:57 +08:00
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 ( ) ;
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 ( ) ;
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
ID3D11Texture2D * pTexture = NULL ;
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 ;
2016-04-03 19:02:04 +08:00
g_pd3dDevice - > CreateTexture2D ( & desc , & subResource , & pTexture ) ;
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 ;
g_pd3dDevice - > CreateShaderResourceView ( pTexture , & srvDesc , & g_pFontTextureView ) ;
pTexture - > Release ( ) ;
}
2015-03-09 21:45:23 +08:00
// Store our identifier
2015-03-09 22:51:10 +08:00
io . Fonts - > TexID = ( void * ) g_pFontTextureView ;
2015-03-09 21:45:23 +08:00
// Create texture sampler
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 ;
g_pd3dDevice - > CreateSamplerState ( & desc , & g_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
{
2015-03-09 22:51:10 +08:00
if ( ! g_pd3dDevice )
return false ;
2015-08-14 13:13:20 +08:00
if ( g_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)
// 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]
2016-05-08 17:49:21 +08:00
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
// 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
{ \
float4x4 ProjectionMatrix ; \
} ; \
struct VS_INPUT \
{ \
float2 pos : POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
\
struct PS_INPUT \
{ \
float4 pos : SV_POSITION ; \
float4 col : COLOR0 ; \
float2 uv : TEXCOORD0 ; \
} ; \
\
PS_INPUT main ( VS_INPUT input ) \
{ \
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-12-01 02:08:30 +08:00
D3DCompile ( vertexShader , strlen ( vertexShader ) , NULL , NULL , NULL , " main " , " vs_4_0 " , 0 , 0 , & g_pVertexShaderBlob , NULL ) ;
2015-03-09 21:45:23 +08:00
if ( g_pVertexShaderBlob = = NULL ) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
return false ;
if ( g_pd3dDevice - > CreateVertexShader ( ( DWORD * ) g_pVertexShaderBlob - > GetBufferPointer ( ) , g_pVertexShaderBlob - > GetBufferSize ( ) , NULL , & g_pVertexShader ) ! = S_OK )
return false ;
// Create the input layout
2018-02-17 06:11:43 +08:00
D3D11_INPUT_ELEMENT_DESC local_layout [ ] =
{
2015-10-12 07:42:22 +08:00
{ " POSITION " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > pos ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " TEXCOORD " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > uv ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
{ " COLOR " , 0 , DXGI_FORMAT_R8G8B8A8_UNORM , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > col ) , D3D11_INPUT_PER_VERTEX_DATA , 0 } ,
2015-03-09 21:45:23 +08:00
} ;
2016-04-03 19:02:04 +08:00
if ( g_pd3dDevice - > CreateInputLayout ( local_layout , 3 , g_pVertexShaderBlob - > GetBufferPointer ( ) , g_pVertexShaderBlob - > GetBufferSize ( ) , & g_pInputLayout ) ! = S_OK )
2015-03-09 21:45:23 +08:00
return false ;
// Create the constant buffer
{
2016-04-03 19:02:04 +08:00
D3D11_BUFFER_DESC desc ;
desc . ByteWidth = sizeof ( VERTEX_CONSTANT_BUFFER ) ;
desc . Usage = D3D11_USAGE_DYNAMIC ;
desc . BindFlags = D3D11_BIND_CONSTANT_BUFFER ;
desc . CPUAccessFlags = D3D11_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
g_pd3dDevice - > CreateBuffer ( & desc , NULL , & g_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 ; \
} " ;
2015-12-01 02:08:30 +08:00
D3DCompile ( pixelShader , strlen ( pixelShader ) , NULL , NULL , NULL , " main " , " ps_4_0 " , 0 , 0 , & g_pPixelShaderBlob , NULL ) ;
2015-03-09 21:45:23 +08:00
if ( g_pPixelShaderBlob = = NULL ) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
return false ;
if ( g_pd3dDevice - > CreatePixelShader ( ( DWORD * ) g_pPixelShaderBlob - > GetBufferPointer ( ) , g_pPixelShaderBlob - > GetBufferSize ( ) , NULL , & g_pPixelShader ) ! = S_OK )
return false ;
}
// 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 ;
desc . RenderTarget [ 0 ] . SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA ;
desc . RenderTarget [ 0 ] . DestBlendAlpha = D3D11_BLEND_ZERO ;
desc . RenderTarget [ 0 ] . BlendOpAlpha = D3D11_BLEND_OP_ADD ;
desc . RenderTarget [ 0 ] . RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;
2015-05-19 04:38:17 +08:00
g_pd3dDevice - > CreateBlendState ( & desc , & g_pBlendState ) ;
}
// 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 ;
g_pd3dDevice - > CreateRasterizerState ( & desc , & g_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 ;
2016-05-08 02:11:14 +08:00
g_pd3dDevice - > CreateDepthStencilState ( & desc , & g_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 ( )
{
if ( ! g_pd3dDevice )
return ;
if ( g_pFontSampler ) { g_pFontSampler - > Release ( ) ; g_pFontSampler = NULL ; }
2017-06-02 17:28:48 +08:00
if ( g_pFontTextureView ) { g_pFontTextureView - > Release ( ) ; g_pFontTextureView = NULL ; ImGui : : GetIO ( ) . Fonts - > TexID = NULL ; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
2015-04-10 04:05:35 +08:00
if ( g_pIB ) { g_pIB - > Release ( ) ; g_pIB = NULL ; }
2015-03-09 22:51:10 +08:00
if ( g_pVB ) { g_pVB - > Release ( ) ; g_pVB = NULL ; }
2015-05-19 04:38:17 +08:00
if ( g_pBlendState ) { g_pBlendState - > Release ( ) ; g_pBlendState = NULL ; }
2016-05-08 02:11:14 +08:00
if ( g_pDepthStencilState ) { g_pDepthStencilState - > Release ( ) ; g_pDepthStencilState = NULL ; }
2015-05-19 04:38:17 +08:00
if ( g_pRasterizerState ) { g_pRasterizerState - > Release ( ) ; g_pRasterizerState = NULL ; }
2015-03-09 22:51:10 +08:00
if ( g_pPixelShader ) { g_pPixelShader - > Release ( ) ; g_pPixelShader = NULL ; }
if ( g_pPixelShaderBlob ) { g_pPixelShaderBlob - > Release ( ) ; g_pPixelShaderBlob = NULL ; }
if ( g_pVertexConstantBuffer ) { g_pVertexConstantBuffer - > Release ( ) ; g_pVertexConstantBuffer = NULL ; }
if ( g_pInputLayout ) { g_pInputLayout - > Release ( ) ; g_pInputLayout = NULL ; }
if ( g_pVertexShader ) { g_pVertexShader - > Release ( ) ; g_pVertexShader = NULL ; }
if ( g_pVertexShaderBlob ) { g_pVertexShaderBlob - > Release ( ) ; g_pVertexShaderBlob = NULL ; }
}
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-02-28 06:26:51 +08:00
// Get factory from device
IDXGIDevice * pDXGIDevice = NULL ;
IDXGIAdapter * pDXGIAdapter = NULL ;
IDXGIFactory1 * pFactory = NULL ;
if ( device - > QueryInterface ( IID_PPV_ARGS ( & pDXGIDevice ) ) ! = S_OK )
return false ;
if ( pDXGIDevice - > GetParent ( IID_PPV_ARGS ( & pDXGIAdapter ) ) ! = S_OK )
return false ;
if ( pDXGIAdapter - > GetParent ( IID_PPV_ARGS ( & pFactory ) ) ! = S_OK )
return false ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
2015-03-09 21:45:23 +08:00
g_pd3dDevice = device ;
g_pd3dDeviceContext = device_context ;
2018-02-28 06:26:51 +08:00
g_pFactory = pFactory ;
if ( io . ConfigFlags & ImGuiConfigFlags_MultiViewports )
ImGui_ImplDX11_InitPlatformInterface ( ) ;
2015-03-09 21:45:23 +08:00
return true ;
}
void ImGui_ImplDX11_Shutdown ( )
{
2018-02-28 06:26:51 +08:00
ImGui_ImplDX11_ShutdownPlatformInterface ( ) ;
2015-03-09 22:51:10 +08:00
ImGui_ImplDX11_InvalidateDeviceObjects ( ) ;
g_pd3dDevice = NULL ;
g_pd3dDeviceContext = NULL ;
2015-03-09 21:45:23 +08:00
}
void ImGui_ImplDX11_NewFrame ( )
{
2016-01-11 18:19:38 +08:00
if ( ! g_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
// --------------------------------------------------------------------------------------------------------
// Platform Windows
// --------------------------------------------------------------------------------------------------------
# include "imgui_internal.h" // ImGuiViewport
struct ImGuiPlatformDataDx11
{
IDXGISwapChain * SwapChain ;
ID3D11RenderTargetView * RTView ;
ImGuiPlatformDataDx11 ( ) { SwapChain = NULL ; RTView = NULL ; }
~ ImGuiPlatformDataDx11 ( ) { IM_ASSERT ( SwapChain = = NULL & & RTView = = NULL ) ; }
} ;
static void ImGui_ImplDX11_CreateViewport ( ImGuiViewport * viewport )
{
ImGuiPlatformDataDx11 * data = IM_NEW ( ImGuiPlatformDataDx11 ) ( ) ;
viewport - > RendererUserData = data ;
// FIXME-PLATFORM
HWND hwnd = ( HWND ) viewport - > PlatformHandle ;
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 ;
IM_ASSERT ( data - > SwapChain = = NULL & & data - > RTView = = NULL ) ;
g_pFactory - > CreateSwapChain ( g_pd3dDevice , & sd , & data - > SwapChain ) ;
// Create the render target
if ( data - > SwapChain )
{
ID3D11Texture2D * pBackBuffer ;
data - > SwapChain - > GetBuffer ( 0 , IID_PPV_ARGS ( & pBackBuffer ) ) ;
g_pd3dDevice - > CreateRenderTargetView ( pBackBuffer , NULL , & data - > RTView ) ;
pBackBuffer - > Release ( ) ;
}
}
static void ImGui_ImplDX11_DestroyViewport ( ImGuiViewport * viewport )
{
if ( ImGuiPlatformDataDx11 * data = ( ImGuiPlatformDataDx11 * ) viewport - > RendererUserData )
{
if ( data - > SwapChain )
data - > SwapChain - > Release ( ) ;
data - > SwapChain = NULL ;
if ( data - > RTView )
data - > RTView - > Release ( ) ;
data - > RTView = NULL ;
IM_DELETE ( data ) ;
}
viewport - > RendererUserData = NULL ;
}
static void ImGui_ImplDX11_ResizeViewport ( ImGuiViewport * viewport , int w , int h )
{
ImGuiPlatformDataDx11 * data = ( ImGuiPlatformDataDx11 * ) viewport - > RendererUserData ;
if ( data - > RTView )
{
data - > RTView - > Release ( ) ;
data - > RTView = NULL ;
}
if ( data - > SwapChain )
{
ID3D11Texture2D * pBackBuffer = NULL ;
data - > SwapChain - > ResizeBuffers ( 0 , w , h , DXGI_FORMAT_UNKNOWN , 0 ) ;
data - > SwapChain - > GetBuffer ( 0 , IID_PPV_ARGS ( & pBackBuffer ) ) ;
g_pd3dDevice - > CreateRenderTargetView ( pBackBuffer , NULL , & data - > RTView ) ;
pBackBuffer - > Release ( ) ;
}
}
static void ImGui_ImplDX11_RenderViewport ( ImGuiViewport * viewport )
{
ImGuiPlatformDataDx11 * data = ( ImGuiPlatformDataDx11 * ) viewport - > RendererUserData ;
ImVec4 clear_color = ImGui : : GetStyle ( ) . Colors [ ImGuiCol_WindowBg ] ; // FIXME-PLATFORM
clear_color . w = 1.0f ;
g_pd3dDeviceContext - > OMSetRenderTargets ( 1 , & data - > RTView , NULL ) ;
g_pd3dDeviceContext - > ClearRenderTargetView ( data - > RTView , ( float * ) & clear_color ) ;
ImGui_ImplDX11_RenderDrawData ( & viewport - > DrawData ) ;
}
static void ImGui_ImplDX11_SwapBuffers ( ImGuiViewport * viewport )
{
ImGuiPlatformDataDx11 * data = ( ImGuiPlatformDataDx11 * ) viewport - > RendererUserData ;
data - > SwapChain - > Present ( 0 , 0 ) ; // Present without vsync
}
void ImGui_ImplDX11_InitPlatformInterface ( )
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . RendererInterface . CreateViewport = ImGui_ImplDX11_CreateViewport ;
io . RendererInterface . DestroyViewport = ImGui_ImplDX11_DestroyViewport ;
io . RendererInterface . ResizeViewport = ImGui_ImplDX11_ResizeViewport ;
io . RendererInterface . RenderViewport = ImGui_ImplDX11_RenderViewport ;
io . RendererInterface . SwapBuffers = ImGui_ImplDX11_SwapBuffers ;
}
void ImGui_ImplDX11_ShutdownPlatformInterface ( )
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
memset ( & io . RendererInterface , 0 , sizeof ( io . RendererInterface ) ) ;
}