@ -81,7 +81,7 @@ static ImGui_ImplDX11_Data* ImGui_ImplDX11_GetBackendData()
}
// Functions
static void ImGui_ImplDX11_SetupRenderState ( ImDrawData * draw_data , ID3D11DeviceContext * ctx )
static void ImGui_ImplDX11_SetupRenderState ( ImDrawData * draw_data , ID3D11DeviceContext * device_ ctx)
{
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
@ -93,29 +93,29 @@ static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceC
vp . MinDepth = 0.0f ;
vp . MaxDepth = 1.0f ;
vp . TopLeftX = vp . TopLeftY = 0 ;
ctx - > RSSetViewports ( 1 , & vp ) ;
device_ ctx- > RSSetViewports ( 1 , & vp ) ;
// Setup shader and vertex buffers
unsigned int stride = sizeof ( ImDrawVert ) ;
unsigned int offset = 0 ;
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 ) ;
ctx - > IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
ctx - > VSSetShader ( bd - > pVertexShader , nullptr , 0 ) ;
ctx - > VSSetConstantBuffers ( 0 , 1 , & bd - > pVertexConstantBuffer ) ;
ctx - > PSSetShader ( bd - > pPixelShader , nullptr , 0 ) ;
ctx - > PSSetSamplers ( 0 , 1 , & bd - > pFontSampler ) ;
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..
device_ ctx- > IASetInputLayout ( bd - > pInputLayout ) ;
device_ ctx- > IASetVertexBuffers ( 0 , 1 , & bd - > pVB , & stride , & offset ) ;
device_ ctx- > IASetIndexBuffer ( bd - > pIB , sizeof ( ImDrawIdx ) = = 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT , 0 ) ;
device_ ctx- > IASetPrimitiveTopology ( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
device_ ctx- > VSSetShader ( bd - > pVertexShader , nullptr , 0 ) ;
device_ ctx- > VSSetConstantBuffers ( 0 , 1 , & bd - > pVertexConstantBuffer ) ;
device_ ctx- > PSSetShader ( bd - > pPixelShader , nullptr , 0 ) ;
device_ ctx- > PSSetSamplers ( 0 , 1 , & bd - > pFontSampler ) ;
device_ ctx- > GSSetShader ( nullptr , nullptr , 0 ) ;
device_ ctx- > HSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
device_ ctx- > DSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
device_ ctx- > CSSetShader ( nullptr , nullptr , 0 ) ; // In theory we should backup and restore this as well.. very infrequently used..
// Setup blend state
const float blend_factor [ 4 ] = { 0.f , 0.f , 0.f , 0.f } ;
ctx - > OMSetBlendState ( bd - > pBlendState , blend_factor , 0xffffffff ) ;
ctx - > OMSetDepthStencilState ( bd - > pDepthStencilState , 0 ) ;
ctx - > RSSetState ( bd - > pRasterizerState ) ;
device_ ctx- > OMSetBlendState ( bd - > pBlendState , blend_factor , 0xffffffff ) ;
device_ ctx- > OMSetDepthStencilState ( bd - > pDepthStencilState , 0 ) ;
device_ ctx- > RSSetState ( bd - > pRasterizerState ) ;
}
// Render function
@ -126,7 +126,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
return ;
ImGui_ImplDX11_Data * bd = ImGui_ImplDX11_GetBackendData ( ) ;
ID3D11DeviceContext * ctx = bd - > pd3dDeviceContext ;
ID3D11DeviceContext * device = bd - > pd3dDeviceContext ;
// Create and grow vertex/index buffers if needed
if ( ! bd - > pVB | | bd - > VertexBufferSize < draw_data - > TotalVtxCount )
@ -159,28 +159,28 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
// Upload vertex/index data into a single contiguous GPU buffer
D3D11_MAPPED_SUBRESOURCE vtx_resource , idx_resource ;
if ( ctx - > Map ( bd - > pVB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & vtx_resource ) ! = S_OK )
if ( device - > Map ( bd - > pVB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & vtx_resource ) ! = S_OK )
return ;
if ( ctx - > Map ( bd - > pIB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & idx_resource ) ! = S_OK )
if ( device - > Map ( bd - > pIB , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & idx_resource ) ! = S_OK )
return ;
ImDrawVert * vtx_dst = ( ImDrawVert * ) vtx_resource . pData ;
ImDrawIdx * idx_dst = ( ImDrawIdx * ) idx_resource . pData ;
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cm d_list = draw_data - > CmdLists [ n ] ;
memcpy ( vtx_dst , cm d_list- > VtxBuffer . Data , cm d_list- > VtxBuffer . Size * sizeof ( ImDrawVert ) ) ;
memcpy ( idx_dst , cm d_list- > IdxBuffer . Data , cm d_list- > IdxBuffer . Size * sizeof ( ImDrawIdx ) ) ;
vtx_dst + = cm d_list- > VtxBuffer . Size ;
idx_dst + = cm d_list- > IdxBuffer . Size ;
const ImDrawList * draw _list = draw_data - > CmdLists [ n ] ;
memcpy ( vtx_dst , draw _list - > VtxBuffer . Data , draw _list - > VtxBuffer . Size * sizeof ( ImDrawVert ) ) ;
memcpy ( idx_dst , draw _list - > IdxBuffer . Data , draw _list - > IdxBuffer . Size * sizeof ( ImDrawIdx ) ) ;
vtx_dst + = draw _list - > VtxBuffer . Size ;
idx_dst + = draw _list - > IdxBuffer . Size ;
}
ctx - > Unmap ( bd - > pVB , 0 ) ;
ctx - > Unmap ( bd - > pIB , 0 ) ;
device - > Unmap ( bd - > pVB , 0 ) ;
device - > Unmap ( bd - > pIB , 0 ) ;
// Setup orthographic projection matrix into our constant buffer
// 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.
{
D3D11_MAPPED_SUBRESOURCE mapped_resource ;
if ( ctx - > Map ( bd - > pVertexConstantBuffer , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & mapped_resource ) ! = S_OK )
if ( device - > Map ( bd - > pVertexConstantBuffer , 0 , D3D11_MAP_WRITE_DISCARD , 0 , & mapped_resource ) ! = S_OK )
return ;
VERTEX_CONSTANT_BUFFER_DX11 * constant_buffer = ( VERTEX_CONSTANT_BUFFER_DX11 * ) mapped_resource . pData ;
float L = draw_data - > DisplayPos . x ;
@ -195,7 +195,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.5f , 1.0f } ,
} ;
memcpy ( & constant_buffer - > mvp , mvp , sizeof ( mvp ) ) ;
ctx - > Unmap ( bd - > pVertexConstantBuffer , 0 ) ;
device - > Unmap ( bd - > pVertexConstantBuffer , 0 ) ;
}
// Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
@ -225,26 +225,26 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
} ;
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 ) ;
ctx - > OMGetDepthStencilState ( & old . DepthStencilState , & old . StencilRef ) ;
ctx - > PSGetShaderResources ( 0 , 1 , & old . PSShaderResource ) ;
ctx - > PSGetSamplers ( 0 , 1 , & old . PSSampler ) ;
device - > RSGetScissorRects ( & old . ScissorRectsCount , old . ScissorRects ) ;
device - > RSGetViewports ( & old . ViewportsCount , old . Viewports ) ;
device - > RSGetState ( & old . RS ) ;
device - > OMGetBlendState ( & old . BlendState , old . BlendFactor , & old . SampleMask ) ;
device - > OMGetDepthStencilState ( & old . DepthStencilState , & old . StencilRef ) ;
device - > PSGetShaderResources ( 0 , 1 , & old . PSShaderResource ) ;
device - > PSGetSamplers ( 0 , 1 , & old . PSSampler ) ;
old . PSInstancesCount = old . VSInstancesCount = old . GSInstancesCount = 256 ;
ctx - > PSGetShader ( & old . PS , old . PSInstances , & old . PSInstancesCount ) ;
ctx - > VSGetShader ( & old . VS , old . VSInstances , & old . VSInstancesCount ) ;
ctx - > VSGetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ;
ctx - > GSGetShader ( & old . GS , old . GSInstances , & old . GSInstancesCount ) ;
device - > PSGetShader ( & old . PS , old . PSInstances , & old . PSInstancesCount ) ;
device - > VSGetShader ( & old . VS , old . VSInstances , & old . VSInstancesCount ) ;
device - > VSGetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ;
device - > GSGetShader ( & old . GS , old . GSInstances , & old . GSInstancesCount ) ;
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 ) ;
device - > IAGetPrimitiveTopology ( & old . PrimitiveTopology ) ;
device - > IAGetIndexBuffer ( & old . IndexBuffer , & old . IndexBufferFormat , & old . IndexBufferOffset ) ;
device - > IAGetVertexBuffers ( 0 , 1 , & old . VertexBuffer , & old . VertexBufferStride , & old . VertexBufferOffset ) ;
device - > IAGetInputLayout ( & old . InputLayout ) ;
// Setup desired DX state
ImGui_ImplDX11_SetupRenderState ( draw_data , ctx ) ;
ImGui_ImplDX11_SetupRenderState ( draw_data , device ) ;
// Render command lists
// (Because we merged all buffers into a single one, we maintain our own offset into them)
@ -253,18 +253,18 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
ImVec2 clip_off = draw_data - > DisplayPos ;
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cm d_list = draw_data - > CmdLists [ n ] ;
for ( int cmd_i = 0 ; cmd_i < cm d_list- > CmdBuffer . Size ; cmd_i + + )
const ImDrawList * draw _list = draw_data - > CmdLists [ n ] ;
for ( int cmd_i = 0 ; cmd_i < draw _list - > CmdBuffer . Size ; cmd_i + + )
{
const ImDrawCmd * pcmd = & cm d_list- > CmdBuffer [ cmd_i ] ;
const ImDrawCmd * pcmd = & draw _list - > CmdBuffer [ cmd_i ] ;
if ( pcmd - > UserCallback ! = nullptr )
{
// 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 ) ;
ImGui_ImplDX11_SetupRenderState ( draw_data , device ) ;
else
pcmd - > UserCallback ( cm d_list, pcmd ) ;
pcmd - > UserCallback ( draw _list , pcmd ) ;
}
else
{
@ -276,36 +276,36 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
// Apply scissor/clipping rectangle
const D3D11_RECT r = { ( LONG ) clip_min . x , ( LONG ) clip_min . y , ( LONG ) clip_max . x , ( LONG ) clip_max . y } ;
ctx - > RSSetScissorRects ( 1 , & r ) ;
device - > RSSetScissorRects ( 1 , & r ) ;
// Bind texture, Draw
ID3D11ShaderResourceView * texture_srv = ( ID3D11ShaderResourceView * ) pcmd - > GetTexID ( ) ;
ctx - > PSSetShaderResources ( 0 , 1 , & texture_srv ) ;
ctx - > DrawIndexed ( pcmd - > ElemCount , pcmd - > IdxOffset + global_idx_offset , pcmd - > VtxOffset + global_vtx_offset ) ;
device - > PSSetShaderResources ( 0 , 1 , & texture_srv ) ;
device - > DrawIndexed ( pcmd - > ElemCount , pcmd - > IdxOffset + global_idx_offset , pcmd - > VtxOffset + global_vtx_offset ) ;
}
}
global_idx_offset + = cm d_list- > IdxBuffer . Size ;
global_vtx_offset + = cm d_list- > VtxBuffer . Size ;
global_idx_offset + = draw _list - > IdxBuffer . Size ;
global_vtx_offset + = draw _list - > VtxBuffer . Size ;
}
// 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 ( ) ;
ctx - > OMSetDepthStencilState ( old . DepthStencilState , old . StencilRef ) ; if ( old . DepthStencilState ) old . DepthStencilState - > Release ( ) ;
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 ( ) ;
device - > RSSetScissorRects ( old . ScissorRectsCount , old . ScissorRects ) ;
device - > RSSetViewports ( old . ViewportsCount , old . Viewports ) ;
device - > RSSetState ( old . RS ) ; if ( old . RS ) old . RS - > Release ( ) ;
device - > OMSetBlendState ( old . BlendState , old . BlendFactor , old . SampleMask ) ; if ( old . BlendState ) old . BlendState - > Release ( ) ;
device - > OMSetDepthStencilState ( old . DepthStencilState , old . StencilRef ) ; if ( old . DepthStencilState ) old . DepthStencilState - > Release ( ) ;
device - > PSSetShaderResources ( 0 , 1 , & old . PSShaderResource ) ; if ( old . PSShaderResource ) old . PSShaderResource - > Release ( ) ;
device - > PSSetSamplers ( 0 , 1 , & old . PSSampler ) ; if ( old . PSSampler ) old . PSSampler - > Release ( ) ;
device - > 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 ( ) ;
ctx - > GSSetShader ( old . GS , old . GSInstances , old . GSInstancesCount ) ; if ( old . GS ) old . GS - > Release ( ) ;
device - > VSSetShader ( old . VS , old . VSInstances , old . VSInstancesCount ) ; if ( old . VS ) old . VS - > Release ( ) ;
device - > VSSetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ; if ( old . VSConstantBuffer ) old . VSConstantBuffer - > Release ( ) ;
device - > GSSetShader ( old . GS , old . GSInstances , old . GSInstancesCount ) ; if ( old . GS ) old . GS - > 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 ( ) ;
device - > IASetPrimitiveTopology ( old . PrimitiveTopology ) ;
device - > IASetIndexBuffer ( old . IndexBuffer , old . IndexBufferFormat , old . IndexBufferOffset ) ; if ( old . IndexBuffer ) old . IndexBuffer - > Release ( ) ;
device - > IASetVertexBuffers ( 0 , 1 , & old . VertexBuffer , & old . VertexBufferStride , & old . VertexBufferOffset ) ; if ( old . VertexBuffer ) old . VertexBuffer - > Release ( ) ;
device - > IASetInputLayout ( old . InputLayout ) ; if ( old . InputLayout ) old . InputLayout - > Release ( ) ;
}
static void ImGui_ImplDX11_CreateFontsTexture ( )