2018-09-13 16:44:08 +02:00
// dear imgui: Renderer for DirectX10
2018-06-08 19:37:33 +02:00
// This needs to be used along with a Platform Binding (e.g. Win32)
2018-02-05 20:34:11 +01:00
// Implemented features:
2018-06-11 12:33:51 +02:00
// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
2016-03-24 11:00:47 +01:00
2015-12-03 19:32:01 +01:00
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
2018-07-04 19:06:28 +02:00
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
2015-12-03 13:18:35 +01:00
// https://github.com/ocornut/imgui
2018-02-16 17:20:18 +01:00
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
2018-11-30 18:18:15 +01:00
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
2018-07-13 11:25:54 +02:00
// 2018-07-13: DirectX10: Fixed unreleased resources in Init and Shutdown functions.
2018-06-08 19:37:33 +02:00
// 2018-06-08: Misc: Extracted imgui_impl_dx10.cpp/.h away from the old combined DX10+Win32 example.
// 2018-06-08: DirectX10: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
2018-04-09 13:13:06 +02:00
// 2018-04-09: Misc: Fixed erroneous call to io.Fonts->ClearInputData() + ClearTexData() that was left in DX10 example but removed in 1.47 (Nov 2015) on other back-ends.
2018-02-16 19:18:16 +01:00
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX10_RenderDrawData() in the .h file so you can call it yourself.
2018-02-16 17:20:18 +01: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: DirectX10: Disabling depth-write.
2015-12-03 13:18:35 +01:00
# include "imgui.h"
# include "imgui_impl_dx10.h"
// DirectX
2018-06-08 19:37:33 +02:00
# include <stdio.h>
2015-12-03 13:18:35 +01:00
# include <d3d10_1.h>
# include <d3d10.h>
# include <d3dcompiler.h>
2018-02-20 13:55:09 +01:00
// DirectX data
2015-12-03 13:18:35 +01:00
static ID3D10Device * g_pd3dDevice = NULL ;
2018-06-08 19:37:33 +02:00
static IDXGIFactory * g_pFactory = NULL ;
2015-12-03 13:18:35 +01:00
static ID3D10Buffer * g_pVB = NULL ;
static ID3D10Buffer * g_pIB = NULL ;
2018-08-01 12:23:59 +02:00
static ID3D10Blob * g_pVertexShaderBlob = NULL ;
2015-12-03 13:18:35 +01:00
static ID3D10VertexShader * g_pVertexShader = NULL ;
static ID3D10InputLayout * g_pInputLayout = NULL ;
static ID3D10Buffer * g_pVertexConstantBuffer = NULL ;
2018-08-01 12:23:59 +02:00
static ID3D10Blob * g_pPixelShaderBlob = NULL ;
2015-12-03 13:18:35 +01:00
static ID3D10PixelShader * g_pPixelShader = NULL ;
static ID3D10SamplerState * g_pFontSampler = NULL ;
static ID3D10ShaderResourceView * g_pFontTextureView = NULL ;
static ID3D10RasterizerState * g_pRasterizerState = NULL ;
static ID3D10BlendState * g_pBlendState = NULL ;
2016-05-07 20:57:38 +02:00
static ID3D10DepthStencilState * g_pDepthStencilState = NULL ;
2015-12-03 13:18:35 +01:00
static int g_VertexBufferSize = 5000 , g_IndexBufferSize = 10000 ;
struct VERTEX_CONSTANT_BUFFER
{
2018-06-08 19:37:33 +02:00
float mvp [ 4 ] [ 4 ] ;
2015-12-03 13:18:35 +01:00
} ;
2018-02-16 19:18:16 +01: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_ImplDX10_RenderDrawData ( ImDrawData * draw_data )
2015-12-03 13:18:35 +01:00
{
2016-04-03 12:59:56 +02:00
ID3D10Device * ctx = g_pd3dDevice ;
2015-12-03 13:18:35 +01: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 ;
D3D10_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D10_BUFFER_DESC ) ) ;
desc . Usage = D3D10_USAGE_DYNAMIC ;
desc . ByteWidth = g_VertexBufferSize * sizeof ( ImDrawVert ) ;
desc . BindFlags = D3D10_BIND_VERTEX_BUFFER ;
desc . CPUAccessFlags = D3D10_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
2016-04-03 12:59:56 +02:00
if ( ctx - > CreateBuffer ( & desc , NULL , & g_pVB ) < 0 )
2015-12-03 13:18:35 +01:00
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 12:59:56 +02:00
D3D10_BUFFER_DESC desc ;
memset ( & desc , 0 , sizeof ( D3D10_BUFFER_DESC ) ) ;
desc . Usage = D3D10_USAGE_DYNAMIC ;
desc . ByteWidth = g_IndexBufferSize * sizeof ( ImDrawIdx ) ;
desc . BindFlags = D3D10_BIND_INDEX_BUFFER ;
desc . CPUAccessFlags = D3D10_CPU_ACCESS_WRITE ;
if ( ctx - > CreateBuffer ( & desc , NULL , & g_pIB ) < 0 )
2015-12-03 13:18:35 +01:00
return ;
}
2015-12-03 19:32:01 +01:00
// Copy and convert all vertices into a single contiguous buffer
ImDrawVert * vtx_dst = NULL ;
ImDrawIdx * idx_dst = NULL ;
g_pVB - > Map ( D3D10_MAP_WRITE_DISCARD , 0 , ( void * * ) & vtx_dst ) ;
g_pIB - > Map ( D3D10_MAP_WRITE_DISCARD , 0 , ( void * * ) & idx_dst ) ;
2015-12-03 13:18:35 +01:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
2016-09-03 19:24:57 +02: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-12-03 13:18:35 +01:00
}
g_pVB - > Unmap ( ) ;
g_pIB - > Unmap ( ) ;
// Setup orthographic projection matrix into our constant buffer
2018-06-08 19:37:33 +02:00
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
2015-12-03 13:18:35 +01:00
{
2016-04-03 12:59:56 +02:00
void * mapped_resource ;
if ( g_pVertexConstantBuffer - > Map ( D3D10_MAP_WRITE_DISCARD , 0 , & mapped_resource ) ! = S_OK )
2015-12-03 19:32:01 +01:00
return ;
2016-04-03 12:59:56 +02:00
VERTEX_CONSTANT_BUFFER * constant_buffer = ( VERTEX_CONSTANT_BUFFER * ) mapped_resource ;
2018-06-08 19:37:33 +02: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 ;
float mvp [ 4 ] [ 4 ] =
2015-12-03 13:18:35 +01:00
{
2015-12-03 19:32:01 +01:00
{ 2.0f / ( R - L ) , 0.0f , 0.0f , 0.0f } ,
{ 0.0f , 2.0f / ( T - B ) , 0.0f , 0.0f } ,
2015-12-03 13:18:35 +01:00
{ 0.0f , 0.0f , 0.5f , 0.0f } ,
2015-12-03 19:32:01 +01:00
{ ( R + L ) / ( L - R ) , ( T + B ) / ( B - T ) , 0.5f , 1.0f } ,
2015-12-03 13:18:35 +01:00
} ;
2016-04-03 12:59:56 +02:00
memcpy ( & constant_buffer - > mvp , mvp , sizeof ( mvp ) ) ;
2015-12-03 13:18:35 +01:00
g_pVertexConstantBuffer - > Unmap ( ) ;
}
2016-04-03 12:59:56 +02: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_DX10_STATE
2015-12-03 13:18:35 +01:00
{
2016-04-03 12:59:56 +02:00
UINT ScissorRectsCount , ViewportsCount ;
D3D10_RECT ScissorRects [ D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE ] ;
D3D10_VIEWPORT Viewports [ D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE ] ;
ID3D10RasterizerState * RS ;
ID3D10BlendState * BlendState ;
FLOAT BlendFactor [ 4 ] ;
UINT SampleMask ;
2016-05-07 20:57:38 +02:00
UINT StencilRef ;
ID3D10DepthStencilState * DepthStencilState ;
2016-04-03 12:59:56 +02:00
ID3D10ShaderResourceView * PSShaderResource ;
ID3D10SamplerState * PSSampler ;
ID3D10PixelShader * PS ;
ID3D10VertexShader * VS ;
D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology ;
ID3D10Buffer * IndexBuffer , * VertexBuffer , * VSConstantBuffer ;
UINT IndexBufferOffset , VertexBufferStride , VertexBufferOffset ;
DXGI_FORMAT IndexBufferFormat ;
ID3D10InputLayout * InputLayout ;
} ;
BACKUP_DX10_STATE old ;
old . ScissorRectsCount = old . ViewportsCount = D3D10_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-07 20:57:38 +02:00
ctx - > OMGetDepthStencilState ( & old . DepthStencilState , & old . StencilRef ) ;
2016-04-03 12:59:56 +02:00
ctx - > PSGetShaderResources ( 0 , 1 , & old . PSShaderResource ) ;
ctx - > PSGetSamplers ( 0 , 1 , & old . PSSampler ) ;
ctx - > PSGetShader ( & old . PS ) ;
ctx - > VSGetShader ( & old . VS ) ;
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 ) ;
// Setup viewport
D3D10_VIEWPORT vp ;
memset ( & vp , 0 , sizeof ( D3D10_VIEWPORT ) ) ;
2018-06-08 19:37:33 +02:00
vp . Width = ( UINT ) draw_data - > DisplaySize . x ;
vp . Height = ( UINT ) draw_data - > DisplaySize . y ;
2016-04-03 12:59:56 +02:00
vp . MinDepth = 0.0f ;
vp . MaxDepth = 1.0f ;
vp . TopLeftX = vp . TopLeftY = 0 ;
ctx - > RSSetViewports ( 1 , & vp ) ;
2015-12-03 13:18:35 +01:00
// Bind shader and vertex buffers
unsigned int stride = sizeof ( ImDrawVert ) ;
unsigned int offset = 0 ;
2016-04-03 12:59:56 +02: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 ( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ;
ctx - > VSSetShader ( g_pVertexShader ) ;
ctx - > VSSetConstantBuffers ( 0 , 1 , & g_pVertexConstantBuffer ) ;
ctx - > PSSetShader ( g_pPixelShader ) ;
ctx - > PSSetSamplers ( 0 , 1 , & g_pFontSampler ) ;
2015-12-03 13:18:35 +01:00
// Setup render state
2016-04-03 12:59:56 +02:00
const float blend_factor [ 4 ] = { 0.f , 0.f , 0.f , 0.f } ;
ctx - > OMSetBlendState ( g_pBlendState , blend_factor , 0xffffffff ) ;
2016-05-07 20:57:38 +02:00
ctx - > OMSetDepthStencilState ( g_pDepthStencilState , 0 ) ;
2016-04-03 12:59:56 +02:00
ctx - > RSSetState ( g_pRasterizerState ) ;
2015-12-03 13:18:35 +01:00
// Render command lists
int vtx_offset = 0 ;
int idx_offset = 0 ;
2018-06-08 19:37:33 +02:00
ImVec2 pos = draw_data - > DisplayPos ;
2015-12-03 13:18:35 +01:00
for ( int n = 0 ; n < draw_data - > CmdListsCount ; n + + )
{
const ImDrawList * cmd_list = draw_data - > CmdLists [ n ] ;
2016-09-03 19:24:57 +02:00
for ( int cmd_i = 0 ; cmd_i < cmd_list - > CmdBuffer . Size ; cmd_i + + )
2015-12-03 13:18:35 +01:00
{
const ImDrawCmd * pcmd = & cmd_list - > CmdBuffer [ cmd_i ] ;
if ( pcmd - > UserCallback )
{
2018-06-08 19:37:33 +02:00
// User callback (registered via ImDrawList::AddCallback)
2015-12-03 13:18:35 +01:00
pcmd - > UserCallback ( cmd_list , pcmd ) ;
}
else
{
2018-06-08 19:37:33 +02:00
// Apply scissor/clipping rectangle
const D3D10_RECT r = { ( LONG ) ( pcmd - > ClipRect . x - pos . x ) , ( LONG ) ( pcmd - > ClipRect . y - pos . y ) , ( LONG ) ( pcmd - > ClipRect . z - pos . x ) , ( LONG ) ( pcmd - > ClipRect . w - pos . y ) } ;
2016-04-03 12:59:56 +02:00
ctx - > RSSetScissorRects ( 1 , & r ) ;
2018-06-08 19:37:33 +02:00
// Bind texture, Draw
2018-08-09 17:49:48 +02:00
ID3D10ShaderResourceView * texture_srv = ( ID3D10ShaderResourceView * ) pcmd - > TextureId ;
ctx - > PSSetShaderResources ( 0 , 1 , & texture_srv ) ;
2016-04-03 12:59:56 +02:00
ctx - > DrawIndexed ( pcmd - > ElemCount , idx_offset , vtx_offset ) ;
2015-12-03 13:18:35 +01:00
}
idx_offset + = pcmd - > ElemCount ;
}
2016-09-03 19:24:57 +02:00
vtx_offset + = cmd_list - > VtxBuffer . Size ;
2015-12-03 13:18:35 +01:00
}
2016-04-03 12:59:56 +02: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-07 20:57:38 +02:00
ctx - > OMSetDepthStencilState ( old . DepthStencilState , old . StencilRef ) ; if ( old . DepthStencilState ) old . DepthStencilState - > Release ( ) ;
2016-04-03 12:59:56 +02: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 ) ; if ( old . PS ) old . PS - > Release ( ) ;
ctx - > VSSetShader ( old . VS ) ; if ( old . VS ) old . VS - > Release ( ) ;
ctx - > VSSetConstantBuffers ( 0 , 1 , & old . VSConstantBuffer ) ; if ( old . VSConstantBuffer ) old . VSConstantBuffer - > 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-12-03 13:18:35 +01:00
}
static void ImGui_ImplDX10_CreateFontsTexture ( )
{
2018-06-08 19:37:33 +02:00
// Build texture atlas
2015-12-03 13:18:35 +01:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
unsigned char * pixels ;
int width , height ;
io . Fonts - > GetTexDataAsRGBA32 ( & pixels , & width , & height ) ;
2018-06-08 19:37:33 +02:00
// Upload texture to graphics system
2015-12-03 13:18:35 +01:00
{
2016-04-03 13:02:04 +02:00
D3D10_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 = D3D10_USAGE_DEFAULT ;
desc . BindFlags = D3D10_BIND_SHADER_RESOURCE ;
desc . CPUAccessFlags = 0 ;
2015-12-03 13:18:35 +01:00
ID3D10Texture2D * pTexture = NULL ;
D3D10_SUBRESOURCE_DATA subResource ;
subResource . pSysMem = pixels ;
2016-04-03 13:02:04 +02:00
subResource . SysMemPitch = desc . Width * 4 ;
2015-12-03 13:18:35 +01:00
subResource . SysMemSlicePitch = 0 ;
2016-04-03 13:02:04 +02:00
g_pd3dDevice - > CreateTexture2D ( & desc , & subResource , & pTexture ) ;
2015-12-03 13:18:35 +01:00
// Create texture view
2016-04-03 13:02:04 +02:00
D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc ;
ZeroMemory ( & srv_desc , sizeof ( srv_desc ) ) ;
srv_desc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
srv_desc . ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D ;
srv_desc . Texture2D . MipLevels = desc . MipLevels ;
srv_desc . Texture2D . MostDetailedMip = 0 ;
g_pd3dDevice - > CreateShaderResourceView ( pTexture , & srv_desc , & g_pFontTextureView ) ;
2015-12-03 13:18:35 +01:00
pTexture - > Release ( ) ;
}
// Store our identifier
2018-08-09 17:49:48 +02:00
io . Fonts - > TexID = ( ImTextureID ) g_pFontTextureView ;
2015-12-03 13:18:35 +01:00
// Create texture sampler
{
2016-04-03 13:02:04 +02:00
D3D10_SAMPLER_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR ;
desc . AddressU = D3D10_TEXTURE_ADDRESS_WRAP ;
desc . AddressV = D3D10_TEXTURE_ADDRESS_WRAP ;
desc . AddressW = D3D10_TEXTURE_ADDRESS_WRAP ;
desc . MipLODBias = 0.f ;
desc . ComparisonFunc = D3D10_COMPARISON_ALWAYS ;
desc . MinLOD = 0.f ;
desc . MaxLOD = 0.f ;
g_pd3dDevice - > CreateSamplerState ( & desc , & g_pFontSampler ) ;
2015-12-03 13:18:35 +01:00
}
}
bool ImGui_ImplDX10_CreateDeviceObjects ( )
{
if ( ! g_pd3dDevice )
return false ;
if ( g_pFontSampler )
ImGui_ImplDX10_InvalidateDeviceObjects ( ) ;
2016-05-08 11:49:21 +02: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)
2018-06-08 19:37:33 +02:00
// If you would like to use this DX10 sample code but remove this dependency you can:
2016-09-11 22:02:35 +02: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 11:49:21 +02: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-12-03 13:18:35 +01:00
// Create the vertex shader
{
static const char * vertexShader =
" cbuffer vertexBuffer : register(b0) \
{ \
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 ; \
} " ;
D3DCompile ( vertexShader , strlen ( vertexShader ) , NULL , NULL , NULL , " main " , " vs_4_0 " , 0 , 0 , & g_pVertexShaderBlob , NULL ) ;
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 ( ) , & g_pVertexShader ) ! = S_OK )
return false ;
// Create the input layout
2016-04-03 13:02:04 +02:00
D3D10_INPUT_ELEMENT_DESC local_layout [ ] =
{
2015-12-03 13:18:35 +01:00
{ " POSITION " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > pos ) , D3D10_INPUT_PER_VERTEX_DATA , 0 } ,
{ " TEXCOORD " , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > uv ) , D3D10_INPUT_PER_VERTEX_DATA , 0 } ,
{ " COLOR " , 0 , DXGI_FORMAT_R8G8B8A8_UNORM , 0 , ( size_t ) ( & ( ( ImDrawVert * ) 0 ) - > col ) , D3D10_INPUT_PER_VERTEX_DATA , 0 } ,
} ;
2016-04-03 13:02:04 +02:00
if ( g_pd3dDevice - > CreateInputLayout ( local_layout , 3 , g_pVertexShaderBlob - > GetBufferPointer ( ) , g_pVertexShaderBlob - > GetBufferSize ( ) , & g_pInputLayout ) ! = S_OK )
2015-12-03 13:18:35 +01:00
return false ;
// Create the constant buffer
{
2016-04-03 13:02:04 +02:00
D3D10_BUFFER_DESC desc ;
desc . ByteWidth = sizeof ( VERTEX_CONSTANT_BUFFER ) ;
desc . Usage = D3D10_USAGE_DYNAMIC ;
desc . BindFlags = D3D10_BIND_CONSTANT_BUFFER ;
desc . CPUAccessFlags = D3D10_CPU_ACCESS_WRITE ;
desc . MiscFlags = 0 ;
g_pd3dDevice - > CreateBuffer ( & desc , NULL , & g_pVertexConstantBuffer ) ;
2015-12-03 13:18:35 +01:00
}
}
// Create the pixel shader
{
static const char * pixelShader =
" 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 ; \
} " ;
D3DCompile ( pixelShader , strlen ( pixelShader ) , NULL , NULL , NULL , " main " , " ps_4_0 " , 0 , 0 , & g_pPixelShaderBlob , NULL ) ;
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 ( ) , & g_pPixelShader ) ! = S_OK )
return false ;
}
// Create the blending setup
{
D3D10_BLEND_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . AlphaToCoverageEnable = false ;
desc . BlendEnable [ 0 ] = true ;
desc . SrcBlend = D3D10_BLEND_SRC_ALPHA ;
desc . DestBlend = D3D10_BLEND_INV_SRC_ALPHA ;
desc . BlendOp = D3D10_BLEND_OP_ADD ;
desc . SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA ;
desc . DestBlendAlpha = D3D10_BLEND_ZERO ;
desc . BlendOpAlpha = D3D10_BLEND_OP_ADD ;
desc . RenderTargetWriteMask [ 0 ] = D3D10_COLOR_WRITE_ENABLE_ALL ;
g_pd3dDevice - > CreateBlendState ( & desc , & g_pBlendState ) ;
}
// Create the rasterizer state
{
D3D10_RASTERIZER_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . FillMode = D3D10_FILL_SOLID ;
desc . CullMode = D3D10_CULL_NONE ;
desc . ScissorEnable = true ;
desc . DepthClipEnable = true ;
g_pd3dDevice - > CreateRasterizerState ( & desc , & g_pRasterizerState ) ;
}
2016-05-07 20:57:38 +02:00
// Create depth-stencil State
{
D3D10_DEPTH_STENCIL_DESC desc ;
ZeroMemory ( & desc , sizeof ( desc ) ) ;
desc . DepthEnable = false ;
desc . DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL ;
desc . DepthFunc = D3D10_COMPARISON_ALWAYS ;
desc . StencilEnable = false ;
desc . FrontFace . StencilFailOp = desc . FrontFace . StencilDepthFailOp = desc . FrontFace . StencilPassOp = D3D10_STENCIL_OP_KEEP ;
desc . FrontFace . StencilFunc = D3D10_COMPARISON_ALWAYS ;
desc . BackFace = desc . FrontFace ;
g_pd3dDevice - > CreateDepthStencilState ( & desc , & g_pDepthStencilState ) ;
}
2015-12-03 13:18:35 +01:00
ImGui_ImplDX10_CreateFontsTexture ( ) ;
return true ;
}
void ImGui_ImplDX10_InvalidateDeviceObjects ( )
{
if ( ! g_pd3dDevice )
return ;
if ( g_pFontSampler ) { g_pFontSampler - > Release ( ) ; g_pFontSampler = NULL ; }
2017-06-02 11:28:48 +02: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-12-03 13:18:35 +01:00
if ( g_pIB ) { g_pIB - > Release ( ) ; g_pIB = NULL ; }
if ( g_pVB ) { g_pVB - > Release ( ) ; g_pVB = NULL ; }
if ( g_pBlendState ) { g_pBlendState - > Release ( ) ; g_pBlendState = NULL ; }
2016-05-07 20:57:38 +02:00
if ( g_pDepthStencilState ) { g_pDepthStencilState - > Release ( ) ; g_pDepthStencilState = NULL ; }
2015-12-03 13:18:35 +01:00
if ( g_pRasterizerState ) { g_pRasterizerState - > Release ( ) ; g_pRasterizerState = NULL ; }
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-06-08 19:37:33 +02:00
bool ImGui_ImplDX10_Init ( ID3D10Device * device )
2015-12-03 13:18:35 +01:00
{
2018-11-30 18:18:15 +01:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . BackendRendererName = " imgui_impl_dx10 " ;
2018-06-08 19:37:33 +02:00
// Get factory from device
IDXGIDevice * pDXGIDevice = NULL ;
IDXGIAdapter * pDXGIAdapter = NULL ;
IDXGIFactory * pFactory = NULL ;
2015-12-03 13:18:35 +01:00
2018-07-13 11:25:54 +02: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 )
{
g_pd3dDevice = device ;
g_pFactory = pFactory ;
}
if ( pDXGIDevice ) pDXGIDevice - > Release ( ) ;
if ( pDXGIAdapter ) pDXGIAdapter - > Release ( ) ;
2015-12-03 13:18:35 +01:00
return true ;
}
void ImGui_ImplDX10_Shutdown ( )
{
ImGui_ImplDX10_InvalidateDeviceObjects ( ) ;
2018-07-13 11:25:54 +02:00
if ( g_pFactory ) { g_pFactory - > Release ( ) ; g_pFactory = NULL ; }
2015-12-03 13:18:35 +01:00
g_pd3dDevice = NULL ;
}
void ImGui_ImplDX10_NewFrame ( )
{
2016-01-11 11:29:03 +01:00
if ( ! g_pFontSampler )
2015-12-03 13:18:35 +01:00
ImGui_ImplDX10_CreateDeviceObjects ( ) ;
}