diff options
| author | lbavoil <[email protected]> | 2016-03-25 13:01:54 +0100 |
|---|---|---|
| committer | lbavoil <[email protected]> | 2016-03-25 13:01:54 +0100 |
| commit | 99174e4e5fb4b7079da80b35a6dfd68f3fd56a1c (patch) | |
| tree | fbcd4260d6c953d569a887505336a1c3f202e10f /samples/D3D12/external/imgui/examples | |
| download | hbaoplus-99174e4e5fb4b7079da80b35a6dfd68f3fd56a1c.tar.xz hbaoplus-99174e4e5fb4b7079da80b35a6dfd68f3fd56a1c.zip | |
GFSDK_HBAO+_distro_r3.0_cl20573789
Diffstat (limited to 'samples/D3D12/external/imgui/examples')
5 files changed, 1272 insertions, 0 deletions
diff --git a/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj b/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj new file mode 100644 index 0000000..d3dcd0e --- /dev/null +++ b/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj @@ -0,0 +1,156 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{9F316E83-5AE5-4939-A723-305A94F48005}</ProjectGuid> + <RootNamespace>directx11_example</RootNamespace> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <OutDir>$(ProjectDir)$(Configuration)\</OutDir> + <IntDir>$(ProjectDir)$(Configuration)\</IntDir> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <OutDir>$(ProjectDir)$(Configuration)\</OutDir> + <IntDir>$(ProjectDir)$(Configuration)\</IntDir> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <OutDir>$(ProjectDir)$(Configuration)\</OutDir> + <IntDir>$(ProjectDir)$(Configuration)\</IntDir> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <OutDir>$(ProjectDir)$(Configuration)\</OutDir> + <IntDir>$(ProjectDir)$(Configuration)\</IntDir> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <WarningLevel>Level4</WarningLevel> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..;%(AdditionalIncludeDirectories);$(DXSDK_DIR)Include;</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <AdditionalDependencies>d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalLibraryDirectories>$(DXSDK_DIR)/Lib/x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <WarningLevel>Level4</WarningLevel> + <Optimization>Disabled</Optimization> + <AdditionalIncludeDirectories>..\..;%(AdditionalIncludeDirectories);$(DXSDK_DIR)Include;</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <AdditionalDependencies>d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalLibraryDirectories>$(DXSDK_DIR)/Lib/x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <WarningLevel>Level4</WarningLevel> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <AdditionalIncludeDirectories>..\..;%(AdditionalIncludeDirectories);$(DXSDK_DIR)Include;</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <AdditionalDependencies>d3d11.lib;d3dcompiler.lib;dxgi.lib;imm32.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalLibraryDirectories>$(DXSDK_DIR)/Lib/x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <WarningLevel>Level4</WarningLevel> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <AdditionalIncludeDirectories>..\..;%(AdditionalIncludeDirectories);$(DXSDK_DIR)Include;</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <GenerateDebugInformation>true</GenerateDebugInformation> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <AdditionalDependencies>d3d11.lib;d3dcompiler.lib;dxgi.lib;imm32.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalLibraryDirectories>$(DXSDK_DIR)/Lib/x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClInclude Include="..\..\imconfig.h" /> + <ClInclude Include="..\..\imgui.h" /> + <ClInclude Include="..\..\imgui_internal.h" /> + <ClInclude Include="imgui_impl_dx11.h" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\imgui.cpp" /> + <ClCompile Include="..\..\imgui_demo.cpp" /> + <ClCompile Include="..\..\imgui_draw.cpp" /> + <ClCompile Include="imgui_impl_dx11.cpp" /> + <ClCompile Include="main.cpp" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj.filters b/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj.filters new file mode 100644 index 0000000..ac905dd --- /dev/null +++ b/samples/D3D12/external/imgui/examples/directx12_example/directx12_example.vcxproj.filters @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="imgui"> + <UniqueIdentifier>{0587d7a3-f2ce-4d56-b84f-a0005d3bfce6}</UniqueIdentifier> + </Filter> + <Filter Include="sources"> + <UniqueIdentifier>{08e36723-ce4f-4cff-9662-c40801cf1acf}</UniqueIdentifier> + </Filter> + </ItemGroup> + <ItemGroup> + <ClInclude Include="..\..\imconfig.h"> + <Filter>imgui</Filter> + </ClInclude> + <ClInclude Include="..\..\imgui.h"> + <Filter>imgui</Filter> + </ClInclude> + <ClInclude Include="imgui_impl_dx11.h"> + <Filter>sources</Filter> + </ClInclude> + <ClInclude Include="..\..\imgui_internal.h"> + <Filter>imgui</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\imgui.cpp"> + <Filter>imgui</Filter> + </ClCompile> + <ClCompile Include="main.cpp"> + <Filter>sources</Filter> + </ClCompile> + <ClCompile Include="imgui_impl_dx11.cpp"> + <Filter>sources</Filter> + </ClCompile> + <ClCompile Include="..\..\imgui_demo.cpp"> + <Filter>imgui</Filter> + </ClCompile> + <ClCompile Include="..\..\imgui_draw.cpp"> + <Filter>imgui</Filter> + </ClCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.cpp b/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.cpp new file mode 100644 index 0000000..5947cd4 --- /dev/null +++ b/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.cpp @@ -0,0 +1,826 @@ +// ImGui Win32 + DirectX11 binding +// https://github.com/ocornut/imgui + +#include <imgui.h> +#include "imgui_impl_dx12.h" + +// DirectX +#include <d3d12.h> +#include <d3dx12.h> + +#include <d3dcompiler.h> +#define DIRECTINPUT_VERSION 0x0800 +#include <dinput.h> + +#define ALIGNED_SIZE(size, align) ((size + (align - 1)) & ~(align - 1)) + +// Data +static INT64 g_Time = 0; +static INT64 g_TicksPerSecond = 0; + +static HWND g_hWnd = 0; +static ID3D12Device* g_pd3dDevice = NULL; +static ID3D12GraphicsCommandList* g_pd3dCmdList = NULL; +static ID3D12CommandQueue* g_pd3dCmdQueue = NULL; +static ID3D12Resource* g_pFontTextureUploadHeap = NULL; + +enum DescHeapHandleLayout +{ + eVertexCB = 0, + eFontTexture = 1, +}; + +struct GpuFence +{ + ID3D12Fence* pFence = NULL; + HANDLE hFenceEvent = 0; + UINT64 FenceValue = 0; + + void Init() + { + if (FAILED(g_pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&pFence)))) return; + pFence->SetName(L"ImguiFence"); + FenceValue = 0; + hFenceEvent = CreateEventEx(nullptr, FALSE, FALSE, EVENT_ALL_ACCESS); + } + + void Release() + { + if (pFence) pFence->Release(); + pFence = NULL; + FenceValue = 0; + CloseHandle(hFenceEvent); + } + + void IncrFence() + { + FenceValue++; + } + + void WaitForFence() + { + // Schedule a Signal command in the queue. + if (FAILED(g_pd3dCmdQueue->Signal(pFence, FenceValue))) return; + + if (FAILED(pFence->SetEventOnCompletion(FenceValue, hFenceEvent))) return; + + WaitForSingleObjectEx(hFenceEvent, INFINITE, FALSE); + } +}; + +static GpuFence g_GpuFence; + +struct DescHeapInfo +{ + ID3D12DescriptorHeap* DescHeap; + UINT BaseOffset; + UINT IncrSize; + + D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(UINT Index) + { + D3D12_CPU_DESCRIPTOR_HANDLE CpuHandle = DescHeap->GetCPUDescriptorHandleForHeapStart(); + CpuHandle.ptr += IncrSize * (BaseOffset + Index); + return CpuHandle; + } + + D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(UINT Index) + { + D3D12_GPU_DESCRIPTOR_HANDLE GpuHandle = DescHeap->GetGPUDescriptorHandleForHeapStart(); + GpuHandle.ptr += IncrSize * (BaseOffset + Index); + return GpuHandle; + } +}; + +DescHeapInfo g_DescHeapCbvSrvUav; + +static ID3D12Resource* g_pVB = NULL; +static ID3D12Resource* g_pIB = NULL; +static D3D12_VERTEX_BUFFER_VIEW g_VBView = {}; +static D3D12_INDEX_BUFFER_VIEW g_IBView = {}; +static ID3D10Blob* g_pVertexShader = NULL; +static ID3D12Resource* g_pVertexConstantBuffer = NULL; +static ID3D10Blob* g_pPixelShader = NULL; +static ID3D12RootSignature* g_pRootSignature = NULL; +static ID3D12PipelineState* g_pPSO = NULL; +static ID3D12Resource* g_pFontTexture = NULL; +static D3D12_STATIC_SAMPLER_DESC g_FontSampler; +static D3D12_CPU_DESCRIPTOR_HANDLE g_cbCpuHandle; + +static int VERTEX_BUFFER_SIZE = 20000; // TODO: Make buffers smaller and grow dynamically as needed. +static int INDEX_BUFFER_SIZE = 40000; // TODO: Make buffers smaller and grow dynamically as needed. + +struct VERTEX_CONSTANT_BUFFER +{ + float mvp[4][4]; +}; + +// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) +// If text or lines are blurry when integrating ImGui in your engine: +// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) +static void ImGui_ImplDX12_RenderDrawLists(ImDrawData* draw_data) +{ + ImDrawVert* vtx_dst = NULL; + ImDrawIdx* idx_dst = NULL; + + // Copy and convert all vertices into a single contiguous buffer + if (g_pVB->Map(0, nullptr, reinterpret_cast<void**>(&vtx_dst)) != S_OK) + return; + if (g_pIB->Map(0, nullptr, reinterpret_cast<void**>(&idx_dst)) != S_OK) + return; + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert)); + memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx)); + vtx_dst += cmd_list->VtxBuffer.size(); + idx_dst += cmd_list->IdxBuffer.size(); + } + g_pVB->Unmap(0, nullptr); + g_pIB->Unmap(0, nullptr); + + // Setup orthographic projection matrix into our constant buffer + { + VERTEX_CONSTANT_BUFFER* pConstantBuffer = NULL; + g_pVertexConstantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pConstantBuffer)); + + const float L = 0.0f; + const float R = ImGui::GetIO().DisplaySize.x; + const float B = ImGui::GetIO().DisplaySize.y; + const float T = 0.0f; + const float mvp[4][4] = + { + { 2.0f/(R-L), 0.0f, 0.0f, 0.0f}, + { 0.0f, 2.0f/(T-B), 0.0f, 0.0f,}, + { 0.0f, 0.0f, 0.5f, 0.0f }, + { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, + }; + memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp)); + g_pVertexConstantBuffer->Unmap(0, nullptr); + } + + // Setup viewport + { + D3D12_VIEWPORT vp; + memset(&vp, 0, sizeof(D3D12_VIEWPORT)); + vp.Width = ImGui::GetIO().DisplaySize.x; + vp.Height = ImGui::GetIO().DisplaySize.y; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = 0; + vp.TopLeftY = 0; + g_pd3dCmdList->RSSetViewports(1, &vp); + } + + ID3D12DescriptorHeap* descHeaps[] = { g_DescHeapCbvSrvUav.DescHeap }; + g_pd3dCmdList->SetDescriptorHeaps(ARRAYSIZE(descHeaps), descHeaps); + + // Bind shader and vertex buffers + g_pd3dCmdList->SetGraphicsRootSignature(g_pRootSignature); + g_pd3dCmdList->SetPipelineState(g_pPSO); + g_pd3dCmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + g_pd3dCmdList->IASetVertexBuffers(0, 1, &g_VBView); + g_pd3dCmdList->IASetIndexBuffer(&g_IBView); + + g_pd3dCmdList->SetGraphicsRootConstantBufferView(0, g_pVertexConstantBuffer->GetGPUVirtualAddress()); + //g_pd3dCmdList->SetGraphicsRootDescriptorTable(0, g_DescHeapCbvSrvUav.GetGpuHandle(eCB)); + + // Setup render state + const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f }; + g_pd3dCmdList->OMSetBlendFactor(blendFactor); + + // Render command lists + int vtx_offset = 0; + int idx_offset = 0; + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + if (pcmd->UserCallback) + { + pcmd->UserCallback(cmd_list, pcmd); + } + else + { + const D3D12_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w }; + D3D12_GPU_DESCRIPTOR_HANDLE* FontTextureGpuHandle = (D3D12_GPU_DESCRIPTOR_HANDLE*)pcmd->TextureId; + g_pd3dCmdList->SetGraphicsRootDescriptorTable(1, *FontTextureGpuHandle); + g_pd3dCmdList->RSSetScissorRects(1, &r); + g_pd3dCmdList->DrawIndexedInstanced(pcmd->ElemCount, 1, idx_offset, vtx_offset, 0); + } + idx_offset += pcmd->ElemCount; + } + vtx_offset += cmd_list->VtxBuffer.size(); + } + + g_GpuFence.IncrFence(); +} + +LRESULT ImGui_ImplDX12_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam) +{ + ImGuiIO& io = ImGui::GetIO(); + switch (msg) + { + case WM_LBUTTONDOWN: + io.MouseDown[0] = true; + return true; + case WM_LBUTTONUP: + io.MouseDown[0] = false; + return true; + case WM_RBUTTONDOWN: + io.MouseDown[1] = true; + return true; + case WM_RBUTTONUP: + io.MouseDown[1] = false; + return true; + case WM_MOUSEWHEEL: + io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f; + return true; + case WM_MOUSEMOVE: + io.MousePos.x = (signed short)(lParam); + io.MousePos.y = (signed short)(lParam >> 16); + return true; + case WM_KEYDOWN: + if (wParam < 256) + io.KeysDown[wParam] = 1; + return true; + case WM_KEYUP: + if (wParam < 256) + io.KeysDown[wParam] = 0; + return true; + case WM_CHAR: + // You can also use ToAscii()+GetKeyboardState() to retrieve characters. + if (wParam > 0 && wParam < 0x10000) + io.AddInputCharacter((unsigned short)wParam); + return true; + } + return 0; +} + +void SetResourceBarrier(ID3D12GraphicsCommandList* commandList, + ID3D12Resource* res, + D3D12_RESOURCE_STATES before, + D3D12_RESOURCE_STATES after) +{ + D3D12_RESOURCE_BARRIER desc = {}; + desc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + desc.Transition.pResource = res; + desc.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + desc.Transition.StateBefore = before; + desc.Transition.StateAfter = after; + desc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; + commandList->ResourceBarrier(1, &desc); +} + +static void ImGui_ImplDX12_CreateFontsTexture() +{ + ImGuiIO& io = ImGui::GetIO(); + + ImFont* font0 = io.Fonts->AddFontDefault(); + //io.Fonts->AddFontFromFileTTF("DroidSans.ttf", 18); + + // Build + unsigned char* pixels; + int width, height; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + // Create DX12 texture + { + D3D12_RESOURCE_DESC Desc = {}; + Desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + Desc.Width = width; + Desc.Height = height; + Desc.MipLevels = 1; + Desc.DepthOrArraySize = 1; + Desc.SampleDesc.Count = 1; + Desc.SampleDesc.Quality = 0; + Desc.Flags = D3D12_RESOURCE_FLAG_NONE; + Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + Desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + Desc.Alignment = 0; + + D3D12_HEAP_PROPERTIES texHeapProp = { + D3D12_HEAP_TYPE_DEFAULT, + D3D12_CPU_PAGE_PROPERTY_UNKNOWN, + D3D12_MEMORY_POOL_UNKNOWN, + 1, 1 + }; + + if (g_pFontTexture) g_pFontTexture->Release(); + + if (g_pd3dDevice->CreateCommittedResource( + &texHeapProp, + D3D12_HEAP_FLAG_NONE, + &Desc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&g_pFontTexture)) != S_OK) + { + assert(0); + return; + } + + g_pFontTexture->SetName(L"ImGuiFontTexture"); + + // Initialize texture + { + // Create the texture. + const UINT64 uploadBufferSize = GetRequiredIntermediateSize(g_pFontTexture, 0, 1); + + // Create the GPU upload buffer. + if (g_pd3dDevice->CreateCommittedResource( + &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), + D3D12_HEAP_FLAG_NONE, + &CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize), + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&g_pFontTextureUploadHeap)) != S_OK) + { + assert(0); + return; + } + g_pFontTextureUploadHeap->SetName(L"Imgui_TextureUploadHeap"); + // Copy data to the intermediate upload heap and then schedule a copy + // from the upload heap to the Texture2D. + D3D12_SUBRESOURCE_DATA textureData = {}; + textureData.pData = pixels; + textureData.RowPitch = width * 4; + textureData.SlicePitch = textureData.RowPitch * height; + + SetResourceBarrier(g_pd3dCmdList, g_pFontTexture, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_COPY_DEST); + + UpdateSubresources(g_pd3dCmdList, g_pFontTexture, g_pFontTextureUploadHeap, 0, 0, 1, &textureData); + + SetResourceBarrier(g_pd3dCmdList, g_pFontTexture, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); + } + + // SRV + D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; + SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + SRVDesc.Texture2D.MipLevels = 1; + SRVDesc.Texture2D.MostDetailedMip = 0; // No MIP + SRVDesc.Texture2D.PlaneSlice = 0; + SRVDesc.Texture2D.ResourceMinLODClamp = 0.0f; + + D3D12_CPU_DESCRIPTOR_HANDLE CpuHandle = g_DescHeapCbvSrvUav.GetCpuHandle(eFontTexture); + g_pd3dDevice->CreateShaderResourceView(g_pFontTexture, &SRVDesc, CpuHandle); + + // Store our identifier + D3D12_GPU_DESCRIPTOR_HANDLE* pGpuHandle = new D3D12_GPU_DESCRIPTOR_HANDLE; + (*pGpuHandle) = g_DescHeapCbvSrvUav.GetGpuHandle(eFontTexture); + io.Fonts->TexID = (void *)pGpuHandle; + + //D3D12_SAMPLER_DESC samplerDesc; + //samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; + //samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + //samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + //samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + //samplerDesc.MinLOD = -FLT_MAX; + //samplerDesc.MaxLOD = FLT_MAX; + //samplerDesc.MipLODBias = 0; + //samplerDesc.MaxAnisotropy = 0; + //samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; + //mDev->CreateSampler(&samplerDesc, mDescHeapSampler->GetCPUDescriptorHandleForHeapStart()); + } + + // Cleanup (don't clear the input data if you want to append new fonts later) + io.Fonts->ClearInputData(); + io.Fonts->ClearTexData(); +} + +bool ImGui_ImplDX12_CreateDeviceObjects() +{ + if (!g_pd3dDevice) + return false; + if (g_pVB) + ImGui_ImplDX12_InvalidateDeviceObjects(); + + D3D12_HEAP_PROPERTIES uploadHeapProp = { + D3D12_HEAP_TYPE_UPLOAD, + D3D12_CPU_PAGE_PROPERTY_UNKNOWN, + D3D12_MEMORY_POOL_UNKNOWN, + 1, 1 + }; + + ID3D10Blob* pVertexShaderBlob = NULL; + ID3D10Blob* pPixelShaderBlob = NULL; + + // Create the vertex shader + { + static const char* vertexShader = + "cbuffer vertexBuffer : register(b0) \ + {\ + float4x4 ProjectionMatrix; \ + };\ + struct VS_INPUT\ + {\ + float2 pos : POSITION;\ + float2 uv : TEXCOORD0;\ + float4 col : COLOR0;\ + };\ + \ + 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_5_0", 0, 0, &pVertexShaderBlob, NULL); + if (pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! + { + assert(0); + return false; + } + + // Create the constant buffer + { + D3D12_RESOURCE_DESC cbResourceDesc = { + D3D12_RESOURCE_DIMENSION_BUFFER, + 0, + D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT, + 1, 1, 1, + DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE + }; + + if (g_pd3dDevice->CreateCommittedResource( + &uploadHeapProp, + D3D12_HEAP_FLAG_NONE, + &cbResourceDesc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&g_pVertexConstantBuffer)) != S_OK) + { + assert(0); + return false; + } + + g_pVertexConstantBuffer->SetName(L"ImguiVertexCB"); + + D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {}; + cbvDesc.BufferLocation = g_pVertexConstantBuffer->GetGPUVirtualAddress(); + cbvDesc.SizeInBytes = D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT; + + D3D12_CPU_DESCRIPTOR_HANDLE g_cbCpuHandle = g_DescHeapCbvSrvUav.GetCpuHandle(eVertexCB); + g_pd3dDevice->CreateConstantBufferView(&cbvDesc, g_cbCpuHandle); + } + } + + // 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_5_0", 0, 0, &pPixelShaderBlob, NULL); + if (pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! + { + assert(0); + return false; + } + } + + // Create the blending setup + D3D12_BLEND_DESC localBlendState; + { + ZeroMemory(&localBlendState, sizeof(localBlendState)); + localBlendState.IndependentBlendEnable = false; + localBlendState.AlphaToCoverageEnable = false; + localBlendState.RenderTarget[0].BlendEnable = true; + localBlendState.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA; + localBlendState.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA; + localBlendState.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD; + localBlendState.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA; + localBlendState.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO; + localBlendState.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD; + localBlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + } + + // Create the rasterizer state + D3D12_RASTERIZER_DESC localRasterizeState; + { + ZeroMemory(&localRasterizeState, sizeof(localRasterizeState)); + localRasterizeState.FillMode = D3D12_FILL_MODE_SOLID; + localRasterizeState.CullMode = D3D12_CULL_MODE_NONE; + //localRasterizeState.ScissorEnable = true; + localRasterizeState.DepthClipEnable = true; + } + + // Create the vertex buffer + { + UINT VBSizeInByte = VERTEX_BUFFER_SIZE * sizeof(ImDrawVert); + D3D12_RESOURCE_DESC vbResourceDesc = { + D3D12_RESOURCE_DIMENSION_BUFFER, + 0, + VBSizeInByte, 1, 1, 1, + DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE + }; + + if (FAILED(g_pd3dDevice->CreateCommittedResource( + &uploadHeapProp, + D3D12_HEAP_FLAG_NONE, + &vbResourceDesc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&g_pVB)))) + { + assert(0); + return false; + } + + g_VBView.BufferLocation = g_pVB->GetGPUVirtualAddress(); + g_VBView.StrideInBytes = sizeof(ImDrawVert); + g_VBView.SizeInBytes = VBSizeInByte; + } + + // Create the index buffer + { + UINT IBSizeInByte = INDEX_BUFFER_SIZE * sizeof(ImDrawIdx);; + D3D12_RESOURCE_DESC ibResourceDesc = { + D3D12_RESOURCE_DIMENSION_BUFFER, + 0, + IBSizeInByte, 1, 1, 1, + DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE + }; + + if (FAILED(g_pd3dDevice->CreateCommittedResource( + &uploadHeapProp, + D3D12_HEAP_FLAG_NONE, + &ibResourceDesc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&g_pIB)))) + { + assert(0); + return false; + } + + g_IBView.BufferLocation = g_pIB->GetGPUVirtualAddress(); + g_IBView.Format = DXGI_FORMAT_R16_UINT; + g_IBView.SizeInBytes = IBSizeInByte; + } + + D3D12_INPUT_ELEMENT_DESC localLayout[] = { + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 8 + 8, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, + }; + + { +#if (1) + D3D12_DESCRIPTOR_RANGE range1[1]; + range1[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV; + range1[0].NumDescriptors = 1; + range1[0].BaseShaderRegister = 0; + range1[0].RegisterSpace = 0; + range1[0].OffsetInDescriptorsFromTableStart = 0; + + D3D12_DESCRIPTOR_RANGE range2[1]; + range2[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; + range2[0].NumDescriptors = 1; + range2[0].BaseShaderRegister = 0; + range2[0].RegisterSpace = 0; + range2[0].OffsetInDescriptorsFromTableStart = 0; + + D3D12_ROOT_PARAMETER rootParam[2]; + // Init as constants + rootParam[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; + rootParam[0].Descriptor.RegisterSpace = 0; + rootParam[0].Descriptor.ShaderRegister = 0; + rootParam[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; + + rootParam[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + rootParam[1].DescriptorTable.NumDescriptorRanges = 1; + rootParam[1].DescriptorTable.pDescriptorRanges = range2; + rootParam[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + + //rootParam[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + //rootParam[0].DescriptorTable.NumDescriptorRanges = 1; + //rootParam[0].DescriptorTable.pDescriptorRanges = range1; + //rootParam[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; + + //rootParam[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + //rootParam[1].DescriptorTable.NumDescriptorRanges = 1; + //rootParam[1].DescriptorTable.pDescriptorRanges = range2; + //rootParam[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; +#else + CD3DX12_DESCRIPTOR_RANGE descRange1[1]; + descRange1[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0); // b0 + CD3DX12_DESCRIPTOR_RANGE descRange2[1]; + descRange2[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0); // srv0 + + CD3DX12_ROOT_PARAMETER rootParam[2]; + rootParam[0].InitAsDescriptorTable(ARRAYSIZE(descRange1), descRange1, D3D12_SHADER_VISIBILITY_VERTEX); + rootParam[1].InitAsDescriptorTable(ARRAYSIZE(descRange2), descRange2, D3D12_SHADER_VISIBILITY_PIXEL); +#endif + + D3D12_STATIC_SAMPLER_DESC samplerDesc; + { + ZeroMemory(&samplerDesc, sizeof(samplerDesc)); + samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; + samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + samplerDesc.MipLODBias = 0.f; + samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; + samplerDesc.MinLOD = 0.f; + samplerDesc.MaxLOD = 0.f; + } + + ID3D10Blob *sig, *info; + D3D12_ROOT_SIGNATURE_DESC rootSigDesc = {}; + rootSigDesc.NumParameters = sizeof(rootParam)/sizeof(rootParam[0]); + rootSigDesc.pParameters = rootParam; + rootSigDesc.NumStaticSamplers = 1; + rootSigDesc.pStaticSamplers = &samplerDesc; + rootSigDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; + + if (FAILED(D3D12SerializeRootSignature(&rootSigDesc, D3D_ROOT_SIGNATURE_VERSION_1, &sig, &info))) + { + assert(0); + return false; + } + + g_pd3dDevice->CreateRootSignature(0, sig->GetBufferPointer(), sig->GetBufferSize(), IID_PPV_ARGS(&g_pRootSignature)); + g_pRootSignature->SetName(L"ImguiRS"); + sig->Release(); + } + + D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {}; + psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + psoDesc.InputLayout.NumElements = 3; + psoDesc.InputLayout.pInputElementDescs = localLayout; + psoDesc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED; + psoDesc.pRootSignature = g_pRootSignature; + psoDesc.VS.pShaderBytecode = pVertexShaderBlob->GetBufferPointer(); + psoDesc.VS.BytecodeLength = pVertexShaderBlob->GetBufferSize(); + psoDesc.PS.pShaderBytecode = pPixelShaderBlob->GetBufferPointer(); + psoDesc.PS.BytecodeLength = pPixelShaderBlob->GetBufferSize(); + psoDesc.RasterizerState = localRasterizeState; + psoDesc.BlendState = localBlendState; + psoDesc.DepthStencilState.DepthEnable = false; + psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL; + psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + psoDesc.DepthStencilState.StencilEnable = false; + psoDesc.SampleMask = UINT_MAX; + psoDesc.NumRenderTargets = 1; + psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + psoDesc.DSVFormat = DXGI_FORMAT_UNKNOWN; + psoDesc.SampleDesc.Count = 1; + if (FAILED(g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pPSO)))) + { + assert(0); + return false; + } + g_pPSO->SetName(L"ImGuiPso"); + pVertexShaderBlob->Release(); + pPixelShaderBlob->Release(); + + ImGui_ImplDX12_CreateFontsTexture(); + + return true; +} + +void ImGui_ImplDX12_InvalidateDeviceObjects() +{ + if (!g_pd3dDevice) + return; + + if (g_pFontTexture) + { + g_pFontTexture->Release(); + g_pFontTexture = NULL; + D3D12_GPU_DESCRIPTOR_HANDLE* pGpuHandle = (D3D12_GPU_DESCRIPTOR_HANDLE*)ImGui::GetIO().Fonts->TexID; + delete pGpuHandle; + ImGui::GetIO().Fonts->TexID = 0; + } + + if (g_pIB) { g_pIB->Release(); g_pIB = NULL; } + if (g_pVB) { g_pVB->Release(); g_pVB = NULL; } + if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; } + if (g_pPSO) { g_pPSO->Release(); g_pPSO = NULL; } + if (g_pRootSignature) { g_pRootSignature->Release(); g_pRootSignature = NULL; } +} + +bool ImGui_ImplDX12_Init(void* hwnd, ID3D12Device* device, ID3D12CommandQueue* pCmdQueue, ID3D12GraphicsCommandList* pCmdList, ID3D12DescriptorHeap* descHeap, unsigned int descHeapBaseOffset) +{ + g_hWnd = (HWND)hwnd; + g_pd3dDevice = device; + g_pd3dCmdList = pCmdList; + g_pd3dCmdQueue = pCmdQueue; + + if (!descHeap) + return false; + + g_GpuFence.Init(); + + g_DescHeapCbvSrvUav.DescHeap = descHeap; + g_DescHeapCbvSrvUav.BaseOffset = descHeapBaseOffset; + g_DescHeapCbvSrvUav.IncrSize = g_pd3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + + if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond)) + return false; + if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time)) + return false; + + ImGuiIO& io = ImGui::GetIO(); + io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime. + io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT; + io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT; + io.KeyMap[ImGuiKey_UpArrow] = VK_UP; + io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN; + io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR; + io.KeyMap[ImGuiKey_PageDown] = VK_NEXT; + io.KeyMap[ImGuiKey_Home] = VK_HOME; + io.KeyMap[ImGuiKey_End] = VK_END; + io.KeyMap[ImGuiKey_Delete] = VK_DELETE; + io.KeyMap[ImGuiKey_Backspace] = VK_BACK; + io.KeyMap[ImGuiKey_Enter] = VK_RETURN; + io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE; + io.KeyMap[ImGuiKey_A] = 'A'; + io.KeyMap[ImGuiKey_C] = 'C'; + io.KeyMap[ImGuiKey_V] = 'V'; + io.KeyMap[ImGuiKey_X] = 'X'; + io.KeyMap[ImGuiKey_Y] = 'Y'; + io.KeyMap[ImGuiKey_Z] = 'Z'; + + io.RenderDrawListsFn = ImGui_ImplDX12_RenderDrawLists; + io.ImeWindowHandle = g_hWnd; + + return true; +} + +void ImGui_ImplDX12_Shutdown() +{ + // Command list should be closed before this method + g_GpuFence.WaitForFence(); + g_GpuFence.Release(); + + if (g_pFontTextureUploadHeap) g_pFontTextureUploadHeap->Release(); + g_pFontTextureUploadHeap = NULL; + if (g_pFontTexture) g_pFontTexture->Release(); + g_pFontTexture = NULL; + + ImGui_ImplDX12_InvalidateDeviceObjects(); + ImGui::Shutdown(); + g_pd3dDevice = NULL; + g_pd3dCmdList = NULL; + g_hWnd = (HWND)0; +} + +void ImGui_ImplDX12_NewFrame() +{ + if (!g_pVB) + ImGui_ImplDX12_CreateDeviceObjects(); + + ImGuiIO& io = ImGui::GetIO(); + + // Setup display size (every frame to accommodate for window resizing) + RECT rect; + GetClientRect(g_hWnd, &rect); + io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); + + // Setup time step + INT64 current_time; + QueryPerformanceCounter((LARGE_INTEGER *)¤t_time); + io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond; + g_Time = current_time; + + // Read keyboard modifiers inputs + io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0; + io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0; + io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0; + // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events + // io.MousePos : filled by WM_MOUSEMOVE events + // io.MouseDown : filled by WM_*BUTTON* events + // io.MouseWheel : filled by WM_MOUSEWHEEL events + + // Hide OS mouse cursor if ImGui is drawing it + SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW)); + + // Start the frame + ImGui::NewFrame(); +} diff --git a/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.h b/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.h new file mode 100644 index 0000000..0555607 --- /dev/null +++ b/samples/D3D12/external/imgui/examples/directx12_example/imgui_impl_dx12.h @@ -0,0 +1,23 @@ +// ImGui Win32 + DirectX12 binding +// https://github.com/ocornut/imgui + +struct ID3D12Device; +struct ID3D12DeviceContext; +struct ID3D12DescriptorHeap; +struct ID3D12GraphicsCommandList; +struct ID3D12CommandQueue; + +bool ImGui_ImplDX12_Init(void* hwnd, ID3D12Device* device, ID3D12CommandQueue* pCmdQueue, ID3D12GraphicsCommandList* pCmdList, ID3D12DescriptorHeap* descHeap, unsigned int descHeapBaseOffset); +void ImGui_ImplDX12_Shutdown(); +void ImGui_ImplDX12_NewFrame(); + +// Use if you want to reset your rendering device without losing ImGui state. +void ImGui_ImplDX12_InvalidateDeviceObjects(); +bool ImGui_ImplDX12_CreateDeviceObjects(); + +// Handler for Win32 messages, update mouse/keyboard data. +// You may or not need this for your implementation, but it can serve as reference for handling inputs. +// Commented out to avoid dragging dependencies on <windows.h> types. You can copy the extern declaration in your code. +/* +extern LRESULT ImGui_ImplDX12_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); +*/ diff --git a/samples/D3D12/external/imgui/examples/directx12_example/main.cpp b/samples/D3D12/external/imgui/examples/directx12_example/main.cpp new file mode 100644 index 0000000..7ac1fd1 --- /dev/null +++ b/samples/D3D12/external/imgui/examples/directx12_example/main.cpp @@ -0,0 +1,225 @@ +// ImGui - standalone example application for DirectX 11 + +#include <imgui.h> +#include "imgui_impl_dx11.h" +#include <d3d11.h> +#include <d3dcompiler.h> +#define DIRECTINPUT_VERSION 0x0800 +#include <dinput.h> + +// Data +static ID3D11Device* g_pd3dDevice = NULL; +static ID3D11DeviceContext* g_pd3dDeviceContext = NULL; +static IDXGISwapChain* g_pSwapChain = NULL; +static ID3D11RenderTargetView* g_mainRenderTargetView = NULL; + +void CreateRenderTarget() +{ + DXGI_SWAP_CHAIN_DESC sd; + g_pSwapChain->GetDesc(&sd); + + // Create the render target + ID3D11Texture2D* pBackBuffer; + D3D11_RENDER_TARGET_VIEW_DESC render_target_view_desc; + ZeroMemory(&render_target_view_desc, sizeof(render_target_view_desc)); + render_target_view_desc.Format = sd.BufferDesc.Format; + render_target_view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; + g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); + g_pd3dDevice->CreateRenderTargetView(pBackBuffer, &render_target_view_desc, &g_mainRenderTargetView); + g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL); + pBackBuffer->Release(); +} + +void CleanupRenderTarget() +{ + if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; } +} + +HRESULT CreateDeviceD3D(HWND hWnd) +{ + // Setup swap chain + DXGI_SWAP_CHAIN_DESC sd; + { + ZeroMemory(&sd, sizeof(sd)); + sd.BufferCount = 2; + sd.BufferDesc.Width = 0; + sd.BufferDesc.Height = 0; + sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + sd.BufferDesc.RefreshRate.Numerator = 60; + sd.BufferDesc.RefreshRate.Denominator = 1; + sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.OutputWindow = hWnd; + sd.SampleDesc.Count = 1; + sd.SampleDesc.Quality = 0; + sd.Windowed = TRUE; + sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + } + + UINT createDeviceFlags = 0; +#ifdef _DEBUG + createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + D3D_FEATURE_LEVEL featureLevel; + const D3D_FEATURE_LEVEL featureLevelArray[1] = { D3D_FEATURE_LEVEL_11_0, }; + if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK) + return E_FAIL; + + // Setup rasterizer + { + D3D11_RASTERIZER_DESC RSDesc; + memset(&RSDesc, 0, sizeof(D3D11_RASTERIZER_DESC)); + RSDesc.FillMode = D3D11_FILL_SOLID; + RSDesc.CullMode = D3D11_CULL_NONE; + RSDesc.FrontCounterClockwise = FALSE; + RSDesc.DepthBias = 0; + RSDesc.SlopeScaledDepthBias = 0.0f; + RSDesc.DepthBiasClamp = 0; + RSDesc.DepthClipEnable = TRUE; + RSDesc.ScissorEnable = TRUE; + RSDesc.AntialiasedLineEnable = FALSE; + RSDesc.MultisampleEnable = (sd.SampleDesc.Count > 1) ? TRUE : FALSE; + + ID3D11RasterizerState* pRState = NULL; + g_pd3dDevice->CreateRasterizerState(&RSDesc, &pRState); + g_pd3dDeviceContext->RSSetState(pRState); + pRState->Release(); + } + + CreateRenderTarget(); + + return S_OK; +} + +void CleanupDeviceD3D() +{ + CleanupRenderTarget(); + if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; } + if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; } + if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; } +} + +extern LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); +LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + if (ImGui_ImplDX11_WndProcHandler(hWnd, msg, wParam, lParam)) + return true; + + switch (msg) + { + case WM_SIZE: + if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED) + { + ImGui_ImplDX11_InvalidateDeviceObjects(); + CleanupRenderTarget(); + g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0); + CreateRenderTarget(); + ImGui_ImplDX11_CreateDeviceObjects(); + } + return 0; + case WM_SYSCOMMAND: + if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu + return 0; + break; + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hWnd, msg, wParam, lParam); +} + +int main(int, char**) +{ + // Create application window + WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, L"ImGui Example", NULL }; + RegisterClassEx(&wc); + HWND hwnd = CreateWindow(L"ImGui Example", L"ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL); + + // Initialize Direct3D + if (CreateDeviceD3D(hwnd) < 0) + { + CleanupDeviceD3D(); + UnregisterClass(L"ImGui Example", wc.hInstance); + return 1; + } + + // Show the window + ShowWindow(hwnd, SW_SHOWDEFAULT); + UpdateWindow(hwnd); + + // Setup ImGui binding + ImGui_ImplDX11_Init(hwnd, g_pd3dDevice, g_pd3dDeviceContext); + + // Load Fonts + // (see extra_fonts/README.txt for more details) + //ImGuiIO& io = ImGui::GetIO(); + //io.Fonts->AddFontDefault(); + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f); + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f); + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); + //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); + + // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons) + //ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; + //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f); + //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges); + + bool show_test_window = true; + bool show_another_window = false; + ImVec4 clear_col = ImColor(114, 144, 154); + + // Main loop + MSG msg; + ZeroMemory(&msg, sizeof(msg)); + while (msg.message != WM_QUIT) + { + if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + continue; + } + ImGui_ImplDX11_NewFrame(); + + // 1. Show a simple window + // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" + { + static float f = 0.0f; + ImGui::Text("Hello, world!"); + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); + ImGui::ColorEdit3("clear color", (float*)&clear_col); + if (ImGui::Button("Test Window")) show_test_window ^= 1; + if (ImGui::Button("Another Window")) show_another_window ^= 1; + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + } + + // 2. Show another simple window, this time using an explicit Begin/End pair + if (show_another_window) + { + ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver); + ImGui::Begin("Another Window", &show_another_window); + ImGui::Text("Hello"); + ImGui::End(); + } + + // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() + if (show_test_window) + { + ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly! + ImGui::ShowTestWindow(&show_test_window); + } + + // Rendering + g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&clear_col); + ImGui::Render(); + g_pSwapChain->Present(0, 0); + } + + ImGui_ImplDX11_Shutdown(); + CleanupDeviceD3D(); + UnregisterClass(L"ImGui Example", wc.hInstance); + + return 0; +} |